From 50106f548d9e02d168b03bb96e9cfc1dd56d298e Mon Sep 17 00:00:00 2001 From: root Date: Sat, 12 Jul 2025 21:33:39 +0800 Subject: [PATCH] fix --- src/pages/TemplateManagePageV2.tsx | 2 -- src/services/TemplateServiceV2.ts | 39 +++++++++++++++++------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/pages/TemplateManagePageV2.tsx b/src/pages/TemplateManagePageV2.tsx index d6a29a2..ffc06bb 100644 --- a/src/pages/TemplateManagePageV2.tsx +++ b/src/pages/TemplateManagePageV2.tsx @@ -7,11 +7,9 @@ import { Grid, List, Upload, - Filter, Tag, BarChart3, RefreshCw, - Download, Eye, Trash2 } from 'lucide-react' diff --git a/src/services/TemplateServiceV2.ts b/src/services/TemplateServiceV2.ts index 9fe3331..245ecdb 100644 --- a/src/services/TemplateServiceV2.ts +++ b/src/services/TemplateServiceV2.ts @@ -1,4 +1,5 @@ import { invoke } from '@tauri-apps/api/core' +import { useAuthStore } from '../stores/useAuthStore' // 统一的响应格式 export interface TemplateResponse { @@ -137,13 +138,17 @@ export interface TemplateDetail { } export class TemplateServiceV2 { - private static currentUserId: string = 'default' - /** - * 设置当前用户ID + * 获取当前用户ID */ - static setUserId(userId: string) { - this.currentUserId = userId + private static getCurrentUserId(): string { + const authState = useAuthStore.getState() + + if (!authState.isAuthenticated || !authState.user?.id) { + throw new Error('用户未登录,请先登录后再进行操作') + } + + return authState.user.id } /** @@ -153,7 +158,7 @@ export class TemplateServiceV2 { try { const request: BatchImportRequest = { source_folder: sourceFolder, - user_id: userId || this.currentUserId, + user_id: userId || this.getCurrentUserId(), verbose: false, json_output: true } @@ -177,7 +182,7 @@ export class TemplateServiceV2 { static async getTemplates(includeCloud: boolean = true, limit: number = 100, userId?: string): Promise { try { const request: TemplateListRequest = { - user_id: userId || this.currentUserId, + user_id: userId || this.getCurrentUserId(), include_cloud: includeCloud, limit: limit, verbose: false, @@ -204,7 +209,7 @@ export class TemplateServiceV2 { try { const request: TemplateGetRequest = { template_id: templateId, - user_id: userId || this.currentUserId, + user_id: userId || this.getCurrentUserId(), verbose: false, json_output: true } @@ -250,7 +255,7 @@ export class TemplateServiceV2 { try { const request: TemplateDeleteRequest = { template_id: templateId, - user_id: userId || this.currentUserId, + user_id: userId || this.getCurrentUserId(), verbose: false, json_output: true } @@ -280,7 +285,7 @@ export class TemplateServiceV2 { try { const request: TemplateSearchRequest = { query: query, - user_id: userId || this.currentUserId, + user_id: userId || this.getCurrentUserId(), include_cloud: includeCloud, limit: limit, verbose: false, @@ -305,8 +310,8 @@ export class TemplateServiceV2 { */ static async getTemplateStats(userId?: string): Promise { try { - const response = await invoke('get_template_stats_cli', { - user_id: userId || this.currentUserId, + const response = await invoke('get_template_stats_cli', { + user_id: userId || this.getCurrentUserId(), verbose: false }) @@ -326,8 +331,8 @@ export class TemplateServiceV2 { */ static async getPopularTags(limit: number = 20, userId?: string): Promise { try { - const response = await invoke('get_popular_tags_cli', { - user_id: userId || this.currentUserId, + const response = await invoke('get_popular_tags_cli', { + user_id: userId || this.getCurrentUserId(), limit: limit, verbose: false }) @@ -353,9 +358,9 @@ export class TemplateServiceV2 { userId?: string ): Promise { try { - const response = await invoke('get_templates_by_tag_cli', { + const response = await invoke('get_templates_by_tag_cli', { tag: tag, - user_id: userId || this.currentUserId, + user_id: userId || this.getCurrentUserId(), include_cloud: includeCloud, limit: limit, verbose: false @@ -430,6 +435,6 @@ export class TemplateServiceV2 { * 检查模板是否属于当前用户 */ static isUserTemplate(template: TemplateInfo, userId?: string): boolean { - return template.user_id === (userId || this.currentUserId) + return template.user_id === (userId || this.getCurrentUserId()) } }