From 2911a5436f74b3b2bc8bb1af9c616e8c99d21185 Mon Sep 17 00:00:00 2001 From: sophie Date: Tue, 1 Sep 2026 11:41:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=AA=E4=BA=BA?= =?UTF-8?q?=E4=B8=AD=E5=BF=83=E6=97=A0=E6=B3=95=E6=9F=A5=E7=9C=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/features/user/api/user.api.ts | 45 ---- .../user/components/UserPasswordDialog.vue | 59 ------ .../user/composables/useUserProfile.ts | 193 ------------------ .../user/composables/user-profile.context.ts | 31 --- frontend/src/features/user/index.ts | 9 - .../features/user/types/user-profile.types.ts | 22 -- .../features/user/views/UserProfilePage.vue | 6 +- 7 files changed, 4 insertions(+), 361 deletions(-) delete mode 100644 frontend/src/features/user/api/user.api.ts delete mode 100644 frontend/src/features/user/components/UserPasswordDialog.vue delete mode 100644 frontend/src/features/user/composables/useUserProfile.ts delete mode 100644 frontend/src/features/user/composables/user-profile.context.ts delete mode 100644 frontend/src/features/user/index.ts delete mode 100644 frontend/src/features/user/types/user-profile.types.ts diff --git a/frontend/src/features/user/api/user.api.ts b/frontend/src/features/user/api/user.api.ts deleted file mode 100644 index 4d08b990..00000000 --- a/frontend/src/features/user/api/user.api.ts +++ /dev/null @@ -1,45 +0,0 @@ -/** - * features/user/api/user.api.ts - * 用途:封装当前登录用户的资料与其可访问 Agent 查询请求。 - * 依赖:shared/utils/auth、shared/api/client。 - * 逻辑:Chat 和用户页面通过本模块读取当前用户资源,不与认证登录流程耦合。 - */ -import { requestJson } from '@/shared/api/client'; -import { getAuthHeaders } from '@/shared/utils/auth'; -import type { UserProfileForm } from '../types/user-profile.types'; - -const auth = () => ({ headers: getAuthHeaders() }); - -/** 获取当前登录用户的资料及个人设置。 */ -export const getUserProfile = () => requestJson>('/api/user/profile', auth()); - -/** 读取个人中心编辑表单所需的完整资料。 */ -export const getEditableUserProfile = () => - requestJson>('/api/user/profile', auth()); - -/** 保存当前用户允许自助编辑的资料与偏好。 */ -export const updateUserProfile = (profile: Partial) => - requestJson('/api/user/profile', { - method: 'PUT', - headers: { 'Content-Type': 'application/json', ...getAuthHeaders() }, - body: JSON.stringify(profile), - }); - -/** 校验当前密码后修改 Casdoor 与 HXOPS 的统一密码。 */ -export const changeUserPassword = (currentPassword: string, newPassword: string) => - requestJson('/api/user/change-password', { - method: 'POST', - headers: { 'Content-Type': 'application/json', ...getAuthHeaders() }, - body: JSON.stringify({ current_password: currentPassword, new_password: newPassword }), - }); - -/** 获取当前用户有权访问的单个 Agent。 */ -export const getUserAgent = >(agentId: number) => - requestJson(`/api/user/agents/${agentId}`, auth()); - -/** 获取当前用户有权访问的 Agent 列表。 */ -export const listUserAgents = () => requestJson('/api/agents', auth()); - -/** 获取当前用户有权查看的 Agent 使用指南。 */ -export const listAgentGuides = (agentId: number) => - requestJson(`/api/agents/${agentId}/guides`, auth()); diff --git a/frontend/src/features/user/components/UserPasswordDialog.vue b/frontend/src/features/user/components/UserPasswordDialog.vue deleted file mode 100644 index 7b8ca82a..00000000 --- a/frontend/src/features/user/components/UserPasswordDialog.vue +++ /dev/null @@ -1,59 +0,0 @@ - - - diff --git a/frontend/src/features/user/composables/useUserProfile.ts b/frontend/src/features/user/composables/useUserProfile.ts deleted file mode 100644 index 7d3dd652..00000000 --- a/frontend/src/features/user/composables/useUserProfile.ts +++ /dev/null @@ -1,193 +0,0 @@ -/** - * features/user/composables/useUserProfile.ts - * 用途:编排个人资料读取/编辑、主题偏好与统一密码修改。 - * 依赖:user.api、主题/消息/i18n composable、Casdoor 错误映射。 - * 逻辑:UserProfilePage 和 UserPasswordDialog 消费本 composable;Workspace 仅提供它以维持布局上下文。 - */ -import { computed, ref } from 'vue'; -import type { ThemePreference } from '@/shared/composables/useTheme'; -import { resolveCasdoorErrorMessage } from '@/shared/utils/casdoor'; -import { BusinessError } from '@/shared/api/client'; -import { changeUserPassword, getEditableUserProfile, updateUserProfile } from '../api/user.api'; -import { CHANGE_PASSWORD_ENABLED, type UserProfileForm } from '../types/user-profile.types'; -import type { ComputedRef, WritableComputedRef } from 'vue'; -import type { ComposerTranslation } from 'vue-i18n'; -import type { useMessage } from '@/shared/composables/useMessage'; - -interface UserProfileDeps { - t: ComposerTranslation; - locale: WritableComputedRef; - msg: ReturnType; - setThemePreference: (preference: ThemePreference) => void; - appliedTheme: ComputedRef; -} - -/** - * 管理当前用户资料、偏好设置和密码修改状态。 - * 由工作台在初始化时创建,使资料页面与改密弹窗共享同一份状态和请求动作。 - */ -export function useUserProfile(deps: UserProfileDeps) { - const { t, locale, msg, setThemePreference, appliedTheme } = deps; - - const isProfileLoading = ref(false); - const isProfileSaving = ref(false); - const isChangingPassword = ref(false); - const isProfileEditing = ref(false); - const showPasswordModal = ref(false); - const profileUser = ref({ - id: 0, - username: '', - real_name: '', - email: '', - phone_number: '', - department: '', - role: '', - employee_id: '', - language: 'zh-CN', - theme_preference: 'system' as ThemePreference, - }); - const originalProfile = ref(null); - const passwordForm = ref({ current: '', new: '', confirm: '' }); - - const themeOptions: Array<{ value: ThemePreference; label: () => string }> = [ - { value: 'light', label: () => t('theme.light') || 'Light' }, - { value: 'dark', label: () => t('theme.dark') || 'Dark' }, - { value: 'system', label: () => t('theme.system') || 'Follow System' }, - ]; - - const hasProfileUnsavedChanges = computed(() => { - if (!originalProfile.value) return false; - return ( - profileUser.value.real_name !== originalProfile.value.real_name || - profileUser.value.phone_number !== originalProfile.value.phone_number || - profileUser.value.language !== originalProfile.value.language || - profileUser.value.theme_preference !== originalProfile.value.theme_preference || - profileUser.value.email !== originalProfile.value.email || - profileUser.value.employee_id !== originalProfile.value.employee_id || - profileUser.value.department !== originalProfile.value.department - ); - }); - - const captureOriginalProfile = () => { - originalProfile.value = { ...profileUser.value }; - }; - - const handleChangePasswordClick = () => { - if (!CHANGE_PASSWORD_ENABLED) { - msg.warning(t('profile.changePasswordContactAdmin')); - return; - } - showPasswordModal.value = true; - }; - - const fetchProfile = async () => { - isProfileLoading.value = true; - try { - const data = await getEditableUserProfile(); - if (!data) { - msg.error(t('workspace.profile.msg.loadFailed')); - return; - } - profileUser.value = { ...profileUser.value, ...data }; - if (data.language) locale.value = data.language; - if (data.theme_preference) { - setThemePreference(data.theme_preference); - } else { - profileUser.value.theme_preference = appliedTheme.value; - } - captureOriginalProfile(); - isProfileEditing.value = false; - } catch (e) { - console.error(e); - msg.error(t('workspace.profile.msg.loadFailed')); - } finally { - isProfileLoading.value = false; - } - }; - - const startProfileEditing = () => { - isProfileEditing.value = true; - }; - - const cancelProfileEditing = () => { - if (originalProfile.value) { - profileUser.value = { ...originalProfile.value }; - locale.value = profileUser.value.language; - setThemePreference(profileUser.value.theme_preference); - } - isProfileEditing.value = false; - }; - - const saveProfile = async () => { - if (!hasProfileUnsavedChanges.value) { - isProfileEditing.value = false; - return; - } - const payload = { - real_name: profileUser.value.real_name, - phone_number: profileUser.value.phone_number, - language: profileUser.value.language, - theme_preference: profileUser.value.theme_preference, - email: profileUser.value.email, - employee_id: profileUser.value.employee_id, - department: profileUser.value.department, - }; - locale.value = profileUser.value.language; - setThemePreference(profileUser.value.theme_preference); - try { - isProfileSaving.value = true; - await updateUserProfile(payload); - msg.success(t('workspace.profile.msg.saved')); - captureOriginalProfile(); - isProfileEditing.value = false; - } catch (e) { - console.error(e); - msg.error((e as Error)?.message || t('workspace.profile.msg.saveFailed')); - } finally { - isProfileSaving.value = false; - } - }; - - const handleChangePassword = async () => { - if (isChangingPassword.value) return; - if (passwordForm.value.new !== passwordForm.value.confirm) { - msg.warning(t('profile.passwordMismatch') || 'Passwords do not match'); - return; - } - try { - isChangingPassword.value = true; - await changeUserPassword(passwordForm.value.current, passwordForm.value.new); - msg.success(t('profile.passwordChanged') || 'Password changed successfully'); - passwordForm.value = { current: '', new: '', confirm: '' }; - showPasswordModal.value = false; - } catch (e) { - console.error(e); - msg.error( - resolveCasdoorErrorMessage(e as BusinessError, t) || t('workspace.profile.msg.changePasswordFailed'), - ); - } finally { - isChangingPassword.value = false; - } - }; - - return { - isProfileLoading, - isProfileSaving, - isChangingPassword, - isProfileEditing, - showPasswordModal, - CHANGE_PASSWORD_ENABLED, - handleChangePasswordClick, - profileUser, - originalProfile, - passwordForm, - themeOptions, - hasProfileUnsavedChanges, - captureOriginalProfile, - fetchProfile, - startProfileEditing, - cancelProfileEditing, - saveProfile, - handleChangePassword, - }; -} diff --git a/frontend/src/features/user/composables/user-profile.context.ts b/frontend/src/features/user/composables/user-profile.context.ts deleted file mode 100644 index 7437b186..00000000 --- a/frontend/src/features/user/composables/user-profile.context.ts +++ /dev/null @@ -1,31 +0,0 @@ -/** - * features/user/composables/user-profile.context.ts - * 用途:定义个人资料页面和改密弹窗共享的注入上下文。 - * 依赖:useUserProfile 的返回契约与工作台提供的移动端/登出能力。 - * 逻辑:WorkspaceLayout 提供上下文;user 域组件只依赖此契约,不读取工作台注入 key。 - */ -import type { InjectionKey } from 'vue'; -import type { UserProfileForm } from '../types/user-profile.types'; -import type { ThemePreference } from '@/shared/composables/useTheme'; - -export interface UserProfileContext { - isMobile: boolean; - handleLogout: () => void; - isProfileLoading: boolean; - isProfileSaving: boolean; - isChangingPassword: boolean; - isProfileEditing: boolean; - showPasswordModal: boolean; - CHANGE_PASSWORD_ENABLED: boolean; - profileUser: UserProfileForm; - passwordForm: { current: string; new: string; confirm: string }; - hasProfileUnsavedChanges: boolean; - themeOptions: Array<{ value: ThemePreference; label: () => string }>; - handleChangePasswordClick: () => void; - startProfileEditing: () => void; - cancelProfileEditing: () => void; - saveProfile: () => Promise; - handleChangePassword: () => Promise; -} - -export const USER_PROFILE_KEY: InjectionKey = Symbol('user-profile'); diff --git a/frontend/src/features/user/index.ts b/frontend/src/features/user/index.ts deleted file mode 100644 index 0f7f2315..00000000 --- a/frontend/src/features/user/index.ts +++ /dev/null @@ -1,9 +0,0 @@ -/** - * features/user/index.ts - * 用途:暴露用户域 HTTP 能力,避免其他 feature 直接依赖实现路径。 - * 依赖:user.api。 - * 逻辑:Chat 等使用方从此处或 user.api 导入当前用户资料和授权资源查询。 - */ -export * from './api/user.api'; -export { useUserProfile } from './composables/useUserProfile'; -export type { UserProfileForm } from './types/user-profile.types'; diff --git a/frontend/src/features/user/types/user-profile.types.ts b/frontend/src/features/user/types/user-profile.types.ts deleted file mode 100644 index d7ca9ad9..00000000 --- a/frontend/src/features/user/types/user-profile.types.ts +++ /dev/null @@ -1,22 +0,0 @@ -/** - * features/user/types/user-profile.types.ts - * 用途:定义个人资料编辑与密码修改的用户域类型和功能开关。 - * 依赖:shared/composables/useTheme。 - * 逻辑:useUserProfile、UserProfilePage 和 UserPasswordDialog 使用同一份资料契约。 - */ -import type { ThemePreference } from '@/shared/composables/useTheme'; - -export interface UserProfileForm { - id: number; - username: string; - real_name: string; - email: string; - phone_number: string; - department: string; - role: string; - employee_id: string; - language: string; - theme_preference: ThemePreference; -} - -export const CHANGE_PASSWORD_ENABLED = true; diff --git a/frontend/src/features/user/views/UserProfilePage.vue b/frontend/src/features/user/views/UserProfilePage.vue index 1e0bc3eb..1a5bacfc 100644 --- a/frontend/src/features/user/views/UserProfilePage.vue +++ b/frontend/src/features/user/views/UserProfilePage.vue @@ -6,9 +6,11 @@ import { inject } from 'vue'; import { User, Mail, Building, CreditCard, Globe, Save, Palette, } from 'lucide-vue-next'; -import { USER_PROFILE_KEY } from '../composables/user-profile.context'; +import { WORKSPACE_KEY } from '@/features/workspace/composables/workspace.context'; -const userProfile = inject(USER_PROFILE_KEY)!; +// 个人资料与改密状态由工作台 useWorkspaceProfile 编排,经 WORKSPACE_KEY 注入。 +// 这里复用同一份 ws 上下文(含 profileUser/主题/移动端/登出能力),避免与工作台状态分叉。 +const userProfile = inject(WORKSPACE_KEY)!;