Files
hms/apps/miniprogram/inject-auth.cjs
iven f59e40e6fe fix(mp): inject-auth 清除 _es_ 旧加密值,避免 secureGet 读到过期 token
inject_auth 写入明文 storage 键但不清除 _es_ 前缀的旧加密值,
导致 secureGet 优先读到旧的/过期的加密 token,所有 API 请求 401。
修复:写入前先 removeStorageSync 所有 _es_ 前缀键。
2026-05-21 18:23:46 +08:00

87 lines
2.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 重建后注入明文 token无加密密钥
*/
const automator = require('miniprogram-automator');
const http = require('http');
function getFreshToken() {
return new Promise((resolve, reject) => {
const data = JSON.stringify({ username: 'admin', password: 'Admin@2026' });
const req = http.request({
hostname: 'localhost', port: 3000,
path: '/api/v1/auth/login', method: 'POST',
headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(data) }
}, res => {
let body = '';
res.on('data', d => body += d);
res.on('end', () => {
try { const j = JSON.parse(body); resolve(j.data); } catch (e) { reject(e); }
});
});
req.on('error', reject);
req.write(data);
req.end();
});
}
async function main() {
console.log('1. 获取 token...');
const loginData = await getFreshToken();
console.log(` access: ${loginData.access_token.length} chars`);
console.log('2. 连接 DevTools...');
const mp = await automator.connect({ wsEndpoint: 'ws://localhost:9420' });
console.log('3. 写入 storage (明文模式)...');
const result = await mp.evaluate((at, rt, ud, ur, tid, pid) => {
try {
// 清除 _es_ 前缀旧加密值,避免 secureGet 读到过期 token
['access_token','refresh_token','user_data','user_roles','tenant_id','current_patient_id','current_patient','token_expires_at'].forEach(k => {
wx.removeStorageSync('_es_' + k);
});
wx.setStorageSync('access_token', at);
wx.setStorageSync('refresh_token', rt);
wx.setStorageSync('user_data', ud);
wx.setStorageSync('user_roles', ur);
wx.setStorageSync('tenant_id', tid);
wx.setStorageSync('current_patient_id', pid);
wx.setStorageSync('current_patient', {
id: pid, name: 'TestPatient', gender: 'male',
birth_date: '1990-01-15', status: 'active'
});
const v = wx.getStorageSync('access_token');
return 'ok:' + v.length;
} catch(e) { return 'err:' + e.message; }
},
loginData.access_token,
loginData.refresh_token,
JSON.stringify({
id: loginData.user.id,
username: loginData.user.username,
display_name: loginData.user.display_name,
tenant_id: '019d80da-7a2c-7820-b0a3-3d5266a3a324'
}),
JSON.stringify(['admin']),
'019d80da-7a2c-7820-b0a3-3d5266a3a324',
'019dcd34-bc4d-72c1-8c19-77ce1f4839d6'
);
console.log(` 结果: ${result}`);
console.log('4. reLaunch 首页...');
await mp.reLaunch('/pages/index/index');
await new Promise(r => setTimeout(r, 3000));
const page = await mp.currentPage();
console.log(`5. 当前页面: ${page.path}`);
if (page.path === 'pages/index/index') {
console.log('SUCCESS!');
} else {
console.log('FAILED - redirected to:', page.path);
}
await mp.disconnect();
}
main().catch(e => { console.error(e); process.exit(1); });