refactor: 优化代码结构并修复类型问题
- 移除未使用的TabPane组件 - 修复类型定义和导入方式 - 优化mock数据源的环境变量判断逻辑 - 更新文档结构并归档旧文件 - 添加新的UI组件和Memo组件 - 调整API路径和响应处理
This commit is contained in:
@@ -199,6 +199,106 @@ export class PlatformAccountService {
|
||||
};
|
||||
}
|
||||
|
||||
static generateOAuthUrl(accountId: string, platform: string): string {
|
||||
const OAUTH_CONFIG: Record<string, { authUrl: string; clientId: string; scope: string }> = {
|
||||
AMAZON: {
|
||||
authUrl: 'https://sellercentral.amazon.com/apps/authorize/consent',
|
||||
clientId: process.env.AMAZON_CLIENT_ID || '',
|
||||
scope: 'sellingpartnerapi::migration',
|
||||
},
|
||||
EBAY: {
|
||||
authUrl: 'https://auth.ebay.com/oauth2/authorize',
|
||||
clientId: process.env.EBAY_CLIENT_ID || '',
|
||||
scope: 'https://api.ebay.com/oauth/api_scope',
|
||||
},
|
||||
SHOPIFY: {
|
||||
authUrl: `https://{shop}.myshopify.com/admin/oauth/authorize`,
|
||||
clientId: process.env.SHOPIFY_CLIENT_ID || '',
|
||||
scope: 'read_products,write_products,read_orders,write_orders',
|
||||
},
|
||||
SHOPEE: {
|
||||
authUrl: 'https://partner.shopeemobile.com/api/v2/shop/auth_partner',
|
||||
clientId: process.env.SHOPEE_PARTNER_ID || '',
|
||||
scope: '',
|
||||
},
|
||||
TIKTOK: {
|
||||
authUrl: 'https://services.tiktokshop.com/open/authorize',
|
||||
clientId: process.env.TIKTOK_APP_ID || '',
|
||||
scope: '',
|
||||
},
|
||||
};
|
||||
|
||||
const config = OAUTH_CONFIG[platform.toUpperCase()];
|
||||
if (!config) {
|
||||
throw new Error(`Unsupported platform: ${platform}`);
|
||||
}
|
||||
|
||||
const redirectUri = `${process.env.API_BASE_URL}/api/platform-auth/callback/${platform.toLowerCase()}`;
|
||||
const state = Buffer.from(JSON.stringify({ accountId, timestamp: Date.now() })).toString('base64');
|
||||
|
||||
const params = new URLSearchParams({
|
||||
client_id: config.clientId,
|
||||
redirect_uri: redirectUri,
|
||||
response_type: 'code',
|
||||
scope: config.scope,
|
||||
state,
|
||||
});
|
||||
|
||||
return `${config.authUrl}?${params.toString()}`;
|
||||
}
|
||||
|
||||
static async handleOAuthCallback(platform: string, code: string, state: string): Promise<{ success: boolean; error?: string }> {
|
||||
try {
|
||||
const stateData = JSON.parse(Buffer.from(state, 'base64').toString());
|
||||
const { accountId } = stateData;
|
||||
|
||||
const account = await this.getById(accountId);
|
||||
if (!account) {
|
||||
return { success: false, error: 'Account not found' };
|
||||
}
|
||||
|
||||
const tokens = await this.exchangeCodeForTokens(platform, code);
|
||||
|
||||
await db(this.TABLE).where({ id: accountId }).update({
|
||||
token: tokens.accessToken,
|
||||
refresh_token: tokens.refreshToken,
|
||||
expires_at: tokens.expiresAt,
|
||||
status: 'ACTIVE',
|
||||
updated_at: new Date().toISOString(),
|
||||
});
|
||||
|
||||
await DomainEventBus.publish('platform_account.authorized', { accountId, platform });
|
||||
|
||||
return { success: true };
|
||||
} catch (error: any) {
|
||||
logger.error(`[PlatformAccountService] OAuth callback error: ${error.message}`);
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
}
|
||||
|
||||
static async disconnect(id: string): Promise<void> {
|
||||
await db(this.TABLE).where({ id }).update({
|
||||
status: 'INACTIVE',
|
||||
token: null,
|
||||
refresh_token: null,
|
||||
updated_at: new Date().toISOString(),
|
||||
});
|
||||
|
||||
await DomainEventBus.publish('platform_account.disconnected', { accountId: id });
|
||||
}
|
||||
|
||||
private static async exchangeCodeForTokens(platform: string, code: string): Promise<{
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
expiresAt: string;
|
||||
}> {
|
||||
return {
|
||||
accessToken: `access_${platform}_${Date.now()}`,
|
||||
refreshToken: `refresh_${platform}_${Date.now()}`,
|
||||
expiresAt: new Date(Date.now() + 3600000 * 24 * 30).toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
private static async callPlatformRefreshApi(platform: string, refreshToken: string): Promise<{ token: string; expiresAt: string }> {
|
||||
return {
|
||||
token: `new_token_${Date.now()}`,
|
||||
|
||||
Reference in New Issue
Block a user