105 lines
3.5 KiB
TypeScript
105 lines
3.5 KiB
TypeScript
/**
|
||
* 邀请码验证服务
|
||
*/
|
||
import * as vscode from 'vscode';
|
||
import { verifyInvitationCode, checkInvitationStatus } from './apiClient';
|
||
|
||
/**
|
||
* 邀请码验证服务类
|
||
*/
|
||
export class InvitationService {
|
||
/**
|
||
* 检查用户是否已验证邀请码
|
||
*/
|
||
static async isVerified(context: vscode.ExtensionContext): Promise<boolean> {
|
||
// 【临时】使用本地验证,不调用后端
|
||
const localVerified = context.globalState.get<boolean>('invitationCodeVerified');
|
||
return localVerified || false;
|
||
}
|
||
|
||
/**
|
||
* 验证邀请码
|
||
*/
|
||
static async verifyCode(code: string): Promise<{ success: boolean; message: string }> {
|
||
try {
|
||
console.log('[InvitationService] ========== 开始验证邀请码 ==========');
|
||
console.log('[InvitationService] 邀请码:', code);
|
||
console.log('[InvitationService] 邀请码长度:', code.length);
|
||
|
||
const response = await verifyInvitationCode(code);
|
||
|
||
console.log('[InvitationService] 收到响应:', JSON.stringify(response, null, 2));
|
||
console.log('[InvitationService] 响应代码:', response.code);
|
||
console.log('[InvitationService] 响应消息:', response.msg);
|
||
console.log('[InvitationService] 验证结果:', response.data?.verified);
|
||
|
||
if (response.code === 200 && response.data?.verified) {
|
||
console.log('[InvitationService] ✓ 验证成功');
|
||
return {
|
||
success: true,
|
||
message: response.msg || '验证成功'
|
||
};
|
||
} else {
|
||
console.log('[InvitationService] ✗ 验证失败');
|
||
return {
|
||
success: false,
|
||
message: response.msg || '验证失败'
|
||
};
|
||
}
|
||
} catch (error: any) {
|
||
console.error('[InvitationService] ========== 验证邀请码异常 ==========');
|
||
console.error('[InvitationService] 错误类型:', error.constructor.name);
|
||
console.error('[InvitationService] 错误消息:', error.message);
|
||
console.error('[InvitationService] 错误堆栈:', error.stack);
|
||
return {
|
||
success: false,
|
||
message: error.message || '网络连接失败,请检查网络后重试'
|
||
};
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 保存验证状态到本地
|
||
*/
|
||
static async saveVerificationStatus(
|
||
context: vscode.ExtensionContext,
|
||
code: string,
|
||
verifiedTime?: string
|
||
): Promise<void> {
|
||
await context.globalState.update('invitationCodeVerified', true);
|
||
await context.globalState.update('invitationCode', code);
|
||
await context.globalState.update('invitationVerifiedTime', verifiedTime || new Date().toISOString());
|
||
}
|
||
|
||
/**
|
||
* 清除验证状态(用于退出登录或更换邀请码)
|
||
*/
|
||
static async clearVerificationStatus(context: vscode.ExtensionContext): Promise<void> {
|
||
await context.globalState.update('invitationCodeVerified', undefined);
|
||
await context.globalState.update('invitationCode', undefined);
|
||
await context.globalState.update('invitationVerifiedTime', undefined);
|
||
}
|
||
|
||
/**
|
||
* 显示邀请码输入弹窗
|
||
*/
|
||
static async showInputDialog(): Promise<string | undefined> {
|
||
const code = await vscode.window.showInputBox({
|
||
prompt: '请输入邀请码以继续使用 IC Coder',
|
||
placeHolder: '例如:INVITE2024ABC',
|
||
ignoreFocusOut: true,
|
||
validateInput: (value) => {
|
||
if (!value || value.trim().length === 0) {
|
||
return '邀请码不能为空';
|
||
}
|
||
if (value.trim().length < 6) {
|
||
return '邀请码格式不正确';
|
||
}
|
||
return null;
|
||
}
|
||
});
|
||
|
||
return code?.trim();
|
||
}
|
||
}
|