Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | /** * 用户信息管理服务 * * 负责在 VSCode 扩展中存储和管理用户信息 * 提供与 Web 应用和 MCP 服务器的用户信息同步接口 */ import type { DalidevUserInfo } from '@dalidev/core' import type { ExtensionContext, SecretStorage } from 'vscode' import { clearUserInfo as clearCoreUserInfo, setUserInfo as setCoreUserInfo } from '@dalidev/core' import { logger } from '../utils/logger-adapter' import { loginWorkflowService } from './login-workflow-service' /** * 扩展版本的用户信息接口(包含过期时间等扩展字段) */ export interface ExtensionUserInfo extends DalidevUserInfo { /** 登录时间戳 */ loginTime?: number /** 过期时间戳 */ expireTime?: number /** 最后更新时间 */ lastUpdateTime?: number /** 来源标识 */ source?: 'web' | 'extension' | 'mcp' } /** * 用户信息管理服务类 */ export class UserInfoService { private static instance: UserInfoService private context: ExtensionContext | null = null private secretStorage: SecretStorage | null = null private currentUserInfo: ExtensionUserInfo | null = null // 存储 key private static readonly USER_INFO_SECRET_KEY = 'dalidev.userInfo' private static readonly USER_LOGIN_STATE_KEY = 'dalidev.userLoginState' private constructor() {} static getInstance(): UserInfoService { if (!UserInfoService.instance) { UserInfoService.instance = new UserInfoService() } return UserInfoService.instance } /** * 初始化服务(在扩展激活时调用) */ async initialize(context: ExtensionContext): Promise<void> { this.context = context this.secretStorage = context.secrets // 尝试从存储中恢复用户信息 await this.restoreUserInfoInternal() if (!this.currentUserInfo) { // 登录 await loginWorkflowService.handleWorkflowStart('login') } logger.info('[UserInfoService] 用户信息服务已初始化') } /** * 从存储中恢复用户信息(公开方法,供外部调用) */ async restoreUserInfo(): Promise<void> { await this.restoreUserInfoInternal() } /** * 从存储中恢复用户信息(内部实现) */ private async restoreUserInfoInternal(): Promise<void> { try { if (!this.secretStorage) return const userInfoJson = await this.secretStorage.get(UserInfoService.USER_INFO_SECRET_KEY) if (!userInfoJson) { logger.info('[UserInfoService] 未找到存储的用户信息') return } const userInfo: ExtensionUserInfo = JSON.parse(userInfoJson) // // 检查是否过期 // if (userInfo.expireTime && Date.now() > userInfo.expireTime) { // logger.info('[UserInfoService] 存储的用户信息已过期,清除') // await this.clearUserInfo() // return // } // 恢复用户信息 this.currentUserInfo = userInfo setCoreUserInfo(userInfo) logger.info('[UserInfoService] 用户信息已恢复', { uid: userInfo.uid, uname: userInfo.uname, zhname: userInfo.zhname, source: userInfo.source || 'unknown', }) } catch (error) { logger.error('[UserInfoService] 恢复用户信息失败', error) // 如果恢复失败,清除可能损坏的数据 await this.clearUserInfo() } } /** * 尝试从存储中恢复用户信息(忽略错误) */ private async tryRestoreUserInfo(): Promise<void> { try { await this.restoreUserInfoInternal() } catch (error) { // 忽略恢复错误,不影响服务初始化 logger.warn('[UserInfoService] 恢复用户信息时发生错误(已忽略)', error) } } /** * 设置用户信息 */ async setUserInfo(userInfo: ExtensionUserInfo, source: 'web' | 'extension' | 'mcp' = 'extension'): Promise<void> { try { const now = Date.now() const enhancedUserInfo: ExtensionUserInfo = { ...userInfo, lastUpdateTime: now, loginTime: userInfo.loginTime || now, source, } // 存储到 VSCode 密钥存储 if (this.secretStorage) { console.log(enhancedUserInfo, 'enhancedUserInfo') await this.secretStorage.store( UserInfoService.USER_INFO_SECRET_KEY, JSON.stringify(enhancedUserInfo), ) } // 存储登录状态到普通存储 if (this.context) { await this.context.globalState.update(UserInfoService.USER_LOGIN_STATE_KEY, { isLoggedIn: true, uid: enhancedUserInfo.uid, zhname: enhancedUserInfo.zhname, lastLoginTime: now, source, }) } // 更新当前用户信息 this.currentUserInfo = enhancedUserInfo // 同步到 Core 包 setCoreUserInfo(enhancedUserInfo) logger.info('[UserInfoService] 用户信息已设置', { uid: enhancedUserInfo.uid, uname: enhancedUserInfo.uname, zhname: enhancedUserInfo.zhname, source, }) } catch (error) { logger.error('[UserInfoService] 设置用户信息失败', error) throw error } } /** * 获取当前用户信息 */ getUserInfo(): ExtensionUserInfo | null { return this.currentUserInfo ? { ...this.currentUserInfo } : null } /** * 检查是否已登录 */ isLoggedIn(): boolean { if (!this.currentUserInfo) return false // 检查是否过期 if (this.currentUserInfo.expireTime && Date.now() > this.currentUserInfo.expireTime) { this.clearUserInfo() // 自动清除过期信息 return false } return true } /** * 获取用户显示名称 */ getUserDisplayName(): string { if (!this.currentUserInfo) return '未登录用户' return this.currentUserInfo.zhname || this.currentUserInfo.uname || `用户${this.currentUserInfo.uid}` } /** * 获取用户ID */ getUserId(): number | null { return this.currentUserInfo?.uid || null } /** * 更新用户信息的部分字段 */ async updateUserInfo(updates: Partial<ExtensionUserInfo>): Promise<void> { if (!this.currentUserInfo) { throw new Error('无法更新用户信息:用户未登录') } const updatedUserInfo: ExtensionUserInfo = { ...this.currentUserInfo, ...updates, lastUpdateTime: Date.now(), } await this.setUserInfo(updatedUserInfo, this.currentUserInfo.source || 'extension') } /** * 清除用户信息(用户登出时调用) */ async clearUserInfo(): Promise<void> { try { // 清除密钥存储 if (this.secretStorage) { await this.secretStorage.delete(UserInfoService.USER_INFO_SECRET_KEY) } // 清除登录状态 if (this.context) { await this.context.globalState.update(UserInfoService.USER_LOGIN_STATE_KEY, { isLoggedIn: false, lastLogoutTime: Date.now(), }) } // 清除当前用户信息 this.currentUserInfo = null // 同步到 Core 包 clearCoreUserInfo() logger.info('[UserInfoService] 用户信息已清除') } catch (error) { logger.error('[UserInfoService] 清除用户信息失败', error) throw error } } /** * 获取登录状态信息 */ getLoginState(): any { if (!this.context) return { isLoggedIn: false } return this.context.globalState.get(UserInfoService.USER_LOGIN_STATE_KEY, { isLoggedIn: false }) } /** * 检查用户信息是否即将过期(在过期前30分钟提醒) */ isUserInfoExpiringSoon(): boolean { if (!this.currentUserInfo?.expireTime) return false const thirtyMinutes = 30 * 60 * 1000 return Date.now() > (this.currentUserInfo.expireTime - thirtyMinutes) } /** * 获取用于API请求的用户信息(清理后的格式) */ getUserInfoForAPI(): DalidevUserInfo | null { if (!this.currentUserInfo) return null return { uid: this.currentUserInfo.uid, uname: this.currentUserInfo.uname, zhname: this.currentUserInfo.zhname, emplId: this.currentUserInfo.emplId, schoolId: this.currentUserInfo.schoolId, userType: this.currentUserInfo.userType, company: this.currentUserInfo.company, } } /** * 验证用户信息的完整性 */ validateUserInfo(userInfo: any): userInfo is ExtensionUserInfo { return !!( userInfo && typeof userInfo.uid === 'number' && typeof userInfo.uname === 'string' && typeof userInfo.zhname === 'string' && typeof userInfo.emplId === 'string' ) } /** * 处理来自 Web 应用的用户信息更新 */ async handleUserInfoFromWeb(userInfo: any): Promise<void> { if (!this.validateUserInfo(userInfo)) { throw new Error('无效的用户信息格式') } await this.setUserInfo(userInfo, 'web') logger.info('[UserInfoService] 已接收来自 Web 应用的用户信息更新') } /** * 处理来自 MCP 的用户信息请求 */ async handleUserInfoRequestFromMCP(): Promise<ExtensionUserInfo | null> { const userInfo = this.getUserInfo() if (userInfo) { logger.info('[UserInfoService] 已响应 MCP 的用户信息请求') } else { logger.warn('[UserInfoService] MCP 请求用户信息,但用户未登录') } return userInfo } } // 导出单例实例 export const userInfoService = UserInfoService.getInstance() // 便捷方法导出 export function initializeUserInfoService(context: ExtensionContext) { return userInfoService.initialize(context) } export function setUserInfo(userInfo: ExtensionUserInfo, source?: 'web' | 'extension' | 'mcp') { return userInfoService.setUserInfo(userInfo, source) } export const getUserInfo = () => userInfoService.getUserInfo() export const isLoggedIn = () => userInfoService.isLoggedIn() export const clearUserInfo = () => userInfoService.clearUserInfo() export const getUserDisplayName = () => userInfoService.getUserDisplayName() export const getUserId = () => userInfoService.getUserId() |