Skip to content

Commit 472d9ad

Browse files
author
lhy
committed
初始化插件时添加用户信息更新操作(以及添加超时限制)
1 parent e5b870d commit 472d9ad

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

src/main.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ import { ConfigurationService } from "./configurationService";
1212
import { ErrorHandler } from "./errorHandler";
1313
import { WebviewProvider } from "./webviewProvider";
1414
import { ApiService } from "./apiService";
15+
import { PasswordManager } from "./PasswordManager";
1516

1617
export class PawSQLExtension {
1718
private readonly decorationManager: DecorationManager;
1819
private readonly commandManager: CommandManager;
1920
private readonly sqlCodeLensProvider: SqlCodeLensProvider;
2021
private readonly webviewProvider: WebviewProvider;
2122
private readonly treeProvider: PawSQLTreeProvider;
23+
private readonly passwordManager: PasswordManager;
2224

2325
constructor(private readonly context: vscode.ExtensionContext) {
2426
this.decorationManager = new DecorationManager(context);
@@ -30,6 +32,7 @@ export class PawSQLExtension {
3032
context,
3133
this.sqlCodeLensProvider
3234
);
35+
this.passwordManager = new PasswordManager(context);
3336
}
3437

3538
public async activate(): Promise<void> {
@@ -40,11 +43,56 @@ export class PawSQLExtension {
4043
await this.decorationManager.registerDecorationListeners();
4144
this.registerEventListeners();
4245
this.registerSqlCodeLensProvider();
46+
47+
// 添加用户信息更新逻辑
48+
await this.updateUserCredentials(this.context);
4349
} catch (error) {
4450
ErrorHandler.handle("extension.activation.failed", error);
4551
}
4652
}
4753

54+
private async updateUserCredentials(
55+
context: vscode.ExtensionContext
56+
): Promise<void> {
57+
try {
58+
const config = vscode.workspace.getConfiguration("pawsql");
59+
const email = config.get<string>("email");
60+
const password = await this.passwordManager.getPassword();
61+
const backendUrl = config.get<string>("backendUrl");
62+
63+
if (!email || !password || !backendUrl) {
64+
return;
65+
}
66+
67+
// 设置5秒超时
68+
const timeout = new Promise<null>((_, reject) => {
69+
setTimeout(() => reject(new Error("请求超时")), 5000);
70+
});
71+
72+
const userKeyPromise = ApiService.getUserKey(email, password);
73+
74+
const result = await Promise.race([userKeyPromise, timeout]).catch(
75+
(error) => {
76+
console.error("更新用户凭证失败:", error);
77+
return null;
78+
}
79+
);
80+
81+
if (!result) {
82+
await config.update("frontendUrl", "", true);
83+
await config.update("apiKey", "", true);
84+
return;
85+
}
86+
87+
const { apikey, frontendUrl } = result;
88+
89+
await config.update("frontendUrl", frontendUrl, true);
90+
await config.update("apiKey", apikey, true);
91+
} catch (error) {
92+
console.error("更新用户凭证时发生错误:", error);
93+
}
94+
}
95+
4896
private registerSettingsWebview() {
4997
let disposable = vscode.commands.registerCommand(
5098
"pawsql.openSettings",

0 commit comments

Comments
 (0)