diff --git a/src/config/settings.ts b/src/config/settings.ts index 769ba9c..34bd658 100644 --- a/src/config/settings.ts +++ b/src/config/settings.ts @@ -8,7 +8,7 @@ import * as vscode from "vscode"; type Environment = "dev" | "test" | "prod"; /** 当前环境 - 修改这里切换环境 */ -const CURRENT_ENV: Environment = "test"; +const CURRENT_ENV: Environment = "dev"; /** 服务等级类型 */ export type ServiceTier = "lite" | "syntaxic" | "max" | "auto"; diff --git a/src/services/creditsService.ts b/src/services/creditsService.ts index 475d379..ae5d341 100644 --- a/src/services/creditsService.ts +++ b/src/services/creditsService.ts @@ -100,7 +100,18 @@ export async function fetchBalance(): Promise { return null; } - const token = session.accessToken; + return await fetchBalanceWithToken(session.accessToken); + } catch (error) { + console.error('[CreditsService] 查询余额异常:', error); + return null; + } +} + +/** + * 使用指定 token 查询余额(登录过程中使用) + */ +export async function fetchBalanceWithToken(token: string): Promise { + try { console.log('[CreditsService] 开始查询余额,token 长度:', token.length); // 直接调用 StrangeLoop 的 /api/credit/balance 接口 diff --git a/src/services/icCoderAuthProvider.ts b/src/services/icCoderAuthProvider.ts index 2e40cee..4a34195 100644 --- a/src/services/icCoderAuthProvider.ts +++ b/src/services/icCoderAuthProvider.ts @@ -24,8 +24,23 @@ export class ICCoderAuthenticationProvider private _sessions: vscode.AuthenticationSession[] = []; constructor(private readonly context: vscode.ExtensionContext) { - // 从存储中恢复会话 - this.loadSessions(); + // 从存储中恢复会话(同步执行) + this.loadSessionsSync(); + } + + /** + * 从存储中加载会话(同步版本) + */ + private loadSessionsSync(): void { + const storedSessions = this.context.globalState.get< + vscode.AuthenticationSession[] + >("icCoderSessions", []); + this._sessions = storedSessions; + console.log("[AuthProvider] 同步加载 sessions, 数量:", this._sessions.length); + if (this._sessions.length > 0) { + console.log("[AuthProvider] Session ID:", this._sessions[0].id); + console.log("[AuthProvider] Account:", this._sessions[0].account.label); + } } /** @@ -42,7 +57,9 @@ export class ICCoderAuthenticationProvider * 保存会话到存储 */ private async saveSessions(): Promise { + console.log("[AuthProvider] 保存 sessions, 数量:", this._sessions.length); await this.context.globalState.update("icCoderSessions", this._sessions); + console.log("[AuthProvider] sessions 已保存到 globalState"); } /** @@ -51,6 +68,7 @@ export class ICCoderAuthenticationProvider async getSessions( scopes?: readonly string[] ): Promise { + console.log("[AuthProvider] getSessions 被调用, 当前 sessions 数量:", this._sessions.length); return [...this._sessions]; } diff --git a/src/services/userService.ts b/src/services/userService.ts index e91d641..eb33028 100644 --- a/src/services/userService.ts +++ b/src/services/userService.ts @@ -8,7 +8,7 @@ import { URL } from 'url'; import * as vscode from 'vscode'; import { getStrangeLoopApiUrl, getConfig } from '../config/settings'; import type { UserInfoResponse, MembershipResponse, MultiMembershipVO, MembershipItemVO } from '../types/api'; -import { fetchBalance, getCachedBalance } from './creditsService'; +import { fetchBalanceWithToken, getCachedBalance } from './creditsService'; /** * HTTP 请求选项 @@ -230,7 +230,7 @@ export async function onTokenReceived(token: string): Promise { const [userInfo, membershipInfo, credits] = await Promise.all([ getUserInfo(token), getMembershipInfo(token), - fetchBalance() + fetchBalanceWithToken(token) ]); if (!userInfo) {