194 lines
6.6 KiB
TypeScript
194 lines
6.6 KiB
TypeScript
/**
|
||
* 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<string>;
|
||
msg: ReturnType<typeof useMessage>;
|
||
setThemePreference: (preference: ThemePreference) => void;
|
||
appliedTheme: ComputedRef<ThemePreference>;
|
||
}
|
||
|
||
/**
|
||
* 管理当前用户资料、偏好设置和密码修改状态。
|
||
* 由工作台在初始化时创建,使资料页面与改密弹窗共享同一份状态和请求动作。
|
||
*/
|
||
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<UserProfileForm>({
|
||
id: 0,
|
||
username: '',
|
||
real_name: '',
|
||
email: '',
|
||
phone_number: '',
|
||
department: '',
|
||
role: '',
|
||
employee_id: '',
|
||
language: 'zh-CN',
|
||
theme_preference: 'system' as ThemePreference,
|
||
});
|
||
const originalProfile = ref<UserProfileForm | null>(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,
|
||
};
|
||
}
|