fix: 更新用户信息命令以使用新的获取令牌逻辑,并优化本地用户检查功能

This commit is contained in:
2026-02-21 22:19:30 +08:00
parent c0edd2cbbf
commit a4a5ed0b50
3 changed files with 52 additions and 43 deletions

View File

@@ -7,7 +7,7 @@ const me = new Command('me')
.action(async () => { .action(async () => {
const aq = new AssistantQuery(assistantConfig); const aq = new AssistantQuery(assistantConfig);
await aq.init() await aq.init()
const info = await aq.queryLogin.checkLocalUser() const info = await aq.getToken()
logger.info(info); logger.info(info);
}); });

View File

@@ -257,48 +257,52 @@ export class AssistantApp extends Manager {
* 检查本地用户登录状态,如果未登录且存在 CNB_TOKEN则尝试使用 CNB_TOKEN 登录并更新用户信息 * 检查本地用户登录状态,如果未登录且存在 CNB_TOKEN则尝试使用 CNB_TOKEN 登录并更新用户信息
*/ */
async checkLocalUser() { async checkLocalUser() {
const config = this.config.getConfig(); return checkLocalUser({ assistantApp: this });
const auth = config?.auth; }
let checkCNB = false; }
if (!auth?.username) {
checkCNB = true;
} else { const checkLocalUser = async (opts: { assistantApp: AssistantApp }) => {
let temp = await assistantQuery.queryLogin.getToken() const { assistantApp } = opts;
if (temp) { const config = assistantApp.config.getConfig();
const isExpired = await assistantQuery.queryLogin.checkTokenValid() const auth = config?.auth;
console.log('Token 是否过期', isExpired); let checkCNB = false;
if (!auth?.username) {
checkCNB = true;
// 没有登录过自动检测ci进行登录
// 检测条件1环境变量中存在 CNB_TOKEN
} else {
let temp = await assistantQuery.getToken()
logger.info('[assistant] 当前登录用户', auth.username, 'token有效性检查结果', !!temp);
}
const cnbToken = useKey('CNB_TOKEN');
if (!checkCNB && cnbToken) {
const res = await assistantQuery.query.post({
path: 'user',
key: 'cnb-login',
payload: {
data: {
cnbToken: cnbToken,
}
} }
logger.info('[assistant] 当前登录用户', auth.username, 'token有效性检查结果', !!temp); });
} if (res.code === 200) {
const cnbToken = useKey('CNB_TOKEN'); logger.info('CNB登录成功用户信息已更新');
if (!checkCNB && cnbToken) { const resUser = await assistantQuery.queryLogin.beforeSetLoginUser(res.data)
const res = await assistantQuery.query.post({ if (resUser.code === 200) {
path: 'user', const userInfo = resUser.data;
key: 'cnb-login', auth.username = userInfo.username;
payload: { auth.share = 'protected'
data: { const app = config?.app || {};
cnbToken: cnbToken, if (!app?.id) {
} app.id = 'dev-cnb'
}
});
if (res.code === 200) {
logger.info('CNB登录成功用户信息已更新');
const resUser = await assistantQuery.queryLogin.beforeSetLoginUser(res.data)
if (resUser.code === 200) {
const userInfo = resUser.data;
auth.username = userInfo.username;
auth.share = 'protected'
const app = config?.app || {};
if (!app?.id) {
app.id = 'dev-cnb'
}
this.config.setConfig({
auth,
app
});
} else {
console.error('CNB登录失败无法获取用户信息', resUser);
} }
assistantApp.config.setConfig({
auth,
app
});
} else {
console.error('CNB登录失败无法获取用户信息', resUser);
} }
} }
} }

View File

@@ -35,7 +35,12 @@ export class AssistantQuery {
get(body: any, options?: DataOpts) { get(body: any, options?: DataOpts) {
return this.query.get(body, options); return this.query.get(body, options);
} }
getToken() { async getToken() {
return this.queryLogin.getToken(); const token = await this.queryLogin.getToken();
if (!token) return '';
const isExpired = await this.queryLogin.checkTokenValid()
console.log('Token 是否过期', isExpired, token);
console.log('info', this.queryLogin.cacheStore.cacheData)
return token;
} }
} }