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 | /** * 登录工作流服务 * * 负责检查用户登录状态并在需要时触发登录流程 * 提供统一的登录状态检查和登录页面打开功能 */ import { getPluginServerPort } from '@dalidev/core' import { commands, env, Uri, window, workspace } from 'vscode' import { logger } from '../utils/logger-adapter' import { userInfoService } from './user-info-service' export interface LoginWorkflowOptions { /** 是否在检查到未登录时自动弹出登录提示 */ autoPrompt?: boolean /** 自定义登录页面URL */ loginUrl?: string /** 登录成功后的回调URL */ callbackUrl?: string /** 操作描述,用于提示信息 */ actionDescription?: string } /** * 登录工作流服务类 */ export class LoginWorkflowService { private static instance: LoginWorkflowService private defaultLoginUrl: string = 'http://localhost:5137/' private isCheckingLogin: boolean = false private lastCheckTime: number = 0 private checkCooldown: number = 5 * 1000 // 5秒检查冷却 private constructor() { const actualPort = getPluginServerPort(workspace.workspaceFolders?.[0]?.uri.fsPath) if (__IS_DEV__) { this.defaultLoginUrl = `http://localhost:5137/#/?actualPort=${actualPort}` } else { this.defaultLoginUrl = `https://ued.zuoyebang.cc/documents/dalidev/#/?actualPort=${actualPort}` } } static getInstance(): LoginWorkflowService { if (!LoginWorkflowService.instance) { LoginWorkflowService.instance = new LoginWorkflowService() } return LoginWorkflowService.instance } /** * 设置默认登录页面 URL */ setDefaultLoginUrl(url: string): void { this.defaultLoginUrl = url logger.info('[LoginWorkflow] 默认登录页面设置为:', url) } /** * 检查用户是否已登录 */ async isUserLoggedIn(): Promise<boolean> { return userInfoService.isLoggedIn() } /** * 获取用户显示信息 */ async getUserDisplayInfo(): Promise<string> { if (await this.isUserLoggedIn()) { return userInfoService.getUserDisplayName() } return '未登录' } /** * 确保用户已登录,如果未登录则提示登录 */ async ensureUserLoggedIn(options: LoginWorkflowOptions = {}): Promise<boolean> { const { autoPrompt = true, actionDescription = '执行此操作', loginUrl = this.defaultLoginUrl, } = options // 检查冷却时间,避免频繁检查 const now = Date.now() if (this.isCheckingLogin || (now - this.lastCheckTime) < this.checkCooldown) { return userInfoService.isLoggedIn() } this.isCheckingLogin = true this.lastCheckTime = now try { const isLoggedIn = await this.isUserLoggedIn() if (isLoggedIn) { const userDisplay = await this.getUserDisplayInfo() logger.info('[LoginWorkflow] 用户已登录:', userDisplay) return true } // 用户未登录 logger.warn('[LoginWorkflow] 检测到用户未登录') if (!autoPrompt) { return false } // 显示登录提示 const result = await this.showLoginPrompt(actionDescription, loginUrl) return result } finally { this.isCheckingLogin = false } } /** * 显示登录提示对话框 */ private async showLoginPrompt(actionDescription: string, loginUrl: string): Promise<boolean> { const message = `需要登录才能${actionDescription}。请先在 DaliDev Web 应用中登录。` const choice = await window.showWarningMessage( message, { modal: false, detail: '登录后您的用户信息将自动同步到 VSCode 扩展和 MCP 服务器中,用于埋点数据收集。', }, '打开登录页面', '稍后登录', '检查登录状态', ) switch (choice) { case '打开登录页面': await this.openLoginPage(loginUrl) // 等待一段时间后再次检查登录状态 await this.waitAndCheckLogin() return userInfoService.isLoggedIn() case '检查登录状态': // 强制刷新用户信息 try { await userInfoService.restoreUserInfo() const isLoggedIn = userInfoService.isLoggedIn() if (isLoggedIn) { const userDisplay = userInfoService.getUserDisplayName() window.showInformationMessage(`登录状态检查完成:已登录为 ${userDisplay}`) return true } else { window.showWarningMessage('登录状态检查完成:仍未登录') return false } } catch (error) { logger.error('[LoginWorkflow] 检查登录状态失败', error) window.showErrorMessage('检查登录状态失败,请稍后重试') return false } case '稍后登录': default: return false } } /** * 打开登录页面 */ async openLoginPage(loginUrl: string = this.defaultLoginUrl): Promise<void> { try { await env.openExternal(Uri.parse(loginUrl)) logger.info('[LoginWorkflow] 已打开登录页面:', loginUrl) // 显示提示信息 window.showInformationMessage( '已在浏览器中打开 DaliDev 登录页面。登录完成后,用户信息将自动同步。', '了解更多', ).then((choice) => { if (choice === '了解更多') { window.showInformationMessage( '用户信息同步说明:\n\n' + '1. 在 Web 应用中登录后,用户信息会自动传递到 VSCode 扩展\n' + '2. 扩展会安全存储您的用户信息(用户ID、姓名、员工编号等)\n' + '3. MCP 服务器会从扩展获取用户信息用于埋点数据收集\n' + '4. 所有埋点数据都会包含您的用户信息,便于数据分析和问题追踪\n\n' + '您可以随时使用 "检查登录状态" 命令查看当前登录状态。', ) } }) } catch (error) { logger.error('[LoginWorkflow] 打开登录页面失败', error) window.showErrorMessage('无法打开登录页面,请手动访问 DaliDev Web 应用进行登录') } } /** * 等待并检查登录状态 */ private async waitAndCheckLogin(): Promise<void> { // 显示等待提示 await window.withProgress( { location: { viewId: 'explorer' }, title: '等待登录完成...', cancellable: true, }, async (progress, token) => { const maxWaitTime = 60 * 1000 // 最多等待60秒 const checkInterval = 2000 // 每2秒检查一次 const totalSteps = maxWaitTime / checkInterval let currentStep = 0 while (currentStep < totalSteps && !token.isCancellationRequested) { progress.report({ increment: 100 / totalSteps, message: `正在检查登录状态... (${Math.round(currentStep * checkInterval / 1000)}/${Math.round(maxWaitTime / 1000)}秒)`, }) // 检查登录状态 try { const isLoggedIn = await this.isUserLoggedIn() if (isLoggedIn) { const userDisplay = await this.getUserDisplayInfo() window.showInformationMessage(`登录成功!欢迎 ${userDisplay}`) return } } catch (error) { logger.warn('[LoginWorkflow] 检查登录状态时出错', error) } // 等待一段时间 await new Promise(resolve => setTimeout(resolve, checkInterval)) currentStep++ } if (token.isCancellationRequested) { window.showInformationMessage('登录状态检查已取消') } else { window.showWarningMessage('等待登录超时,请手动检查登录状态') } }, ) } /** * 处理工作流开始前的登录检查 */ async handleWorkflowStart(workflowName: string): Promise<boolean> { const isLoggedIn = await this.ensureUserLoggedIn({ autoPrompt: true, actionDescription: `执行 ${workflowName}`, }) if (isLoggedIn) { logger.info('[LoginWorkflow] 工作流登录检查通过:', workflowName) } else { logger.warn('[LoginWorkflow] 工作流登录检查失败:', workflowName) } return isLoggedIn } /** * 注册登录相关的 VSCode 命令 */ registerCommands(): void { // 检查登录状态命令 commands.registerCommand('dalidev.checkLoginStatus', async () => { try { const isLoggedIn = await this.isUserLoggedIn() const userDisplay = await this.getUserDisplayInfo() const loginState = userInfoService.getLoginState() const statusMessage = isLoggedIn ? `✅ 已登录为:${userDisplay}` : '❌ 未登录' const detailMessage = isLoggedIn ? `用户信息:${JSON.stringify(loginState, null, 2)}` : '请在 DaliDev Web 应用中登录,用户信息将自动同步。' await window.showInformationMessage(statusMessage, { detail: detailMessage }) } catch (error) { logger.error('[LoginWorkflow] 检查登录状态命令失败', error) window.showErrorMessage('检查登录状态失败') } }) // 打开登录页面命令 commands.registerCommand('dalidev.openLogin', async () => { await this.openLoginPage() }) // 强制刷新用户信息命令 commands.registerCommand('dalidev.refreshUserInfo', async () => { try { await userInfoService.restoreUserInfo() const isLoggedIn = userInfoService.isLoggedIn() if (isLoggedIn) { const userDisplay = userInfoService.getUserDisplayName() window.showInformationMessage(`用户信息刷新成功:${userDisplay}`) } else { window.showWarningMessage('用户信息刷新完成,但检测到未登录状态') } } catch (error) { logger.error('[LoginWorkflow] 刷新用户信息命令失败', error) window.showErrorMessage('刷新用户信息失败') } }) logger.info('[LoginWorkflow] 登录相关命令已注册') } } // 导出单例实例 export const loginWorkflowService = LoginWorkflowService.getInstance() // 便捷方法导出 export function ensureUserLoggedIn(options?: LoginWorkflowOptions) { return loginWorkflowService.ensureUserLoggedIn(options) } export const isUserLoggedIn = () => loginWorkflowService.isUserLoggedIn() export function handleWorkflowStart(workflowName: string) { return loginWorkflowService.handleWorkflowStart(workflowName) } export function openLoginPage(loginUrl?: string) { return loginWorkflowService.openLoginPage(loginUrl) } export const registerLoginCommands = () => loginWorkflowService.registerCommands() |