Merge branch 'dev' into runny827
This commit is contained in:
commit
22ab8dc1ea
7 files changed
+4
-361
No files matched your search
@@ -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<Record<string, unknown>>('/api/user/profile', auth());
|
||||
|
||||
/** 读取个人中心编辑表单所需的完整资料。 */
|
||||
export const getEditableUserProfile = () =>
|
||||
requestJson<Partial<UserProfileForm>>('/api/user/profile', auth());
|
||||
|
||||
/** 保存当前用户允许自助编辑的资料与偏好。 */
|
||||
export const updateUserProfile = (profile: Partial<UserProfileForm>) =>
|
||||
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 = <T = Record<string, unknown>>(agentId: number) =>
|
||||
requestJson<T>(`/api/user/agents/${agentId}`, auth());
|
||||
|
||||
/** 获取当前用户有权访问的 Agent 列表。 */
|
||||
export const listUserAgents = <T = unknown>() => requestJson<T[]>('/api/agents', auth());
|
||||
|
||||
/** 获取当前用户有权查看的 Agent 使用指南。 */
|
||||
export const listAgentGuides = (agentId: number) =>
|
||||
requestJson<unknown[]>(`/api/agents/${agentId}/guides`, auth());
|
||||
@@ -1,59 +0,0 @@
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* features/user/components/UserPasswordDialog.vue
|
||||
* 用途:呈现并提交当前用户的密码修改表单。
|
||||
* 依赖:user-profile.context 提供的资料状态和操作方法。
|
||||
* 逻辑:由工作台布局常驻渲染,用户资料页打开弹窗后在此完成密码修改。
|
||||
*/
|
||||
import { inject } from 'vue';
|
||||
import { X } from 'lucide-vue-next';
|
||||
import { USER_PROFILE_KEY } from '../composables/user-profile.context';
|
||||
|
||||
const userProfile = inject(USER_PROFILE_KEY)!;
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
v-if="userProfile.CHANGE_PASSWORD_ENABLED && userProfile.showPasswordModal"
|
||||
class="dialog-overlay z-50 items-center"
|
||||
>
|
||||
<div class="dialog-panel max-w-md">
|
||||
<div class="dialog-header">
|
||||
<h3 class="section-title">{{ $t('profile.changePassword') || 'Change Password' }}</h3>
|
||||
<button
|
||||
type="button"
|
||||
class="btn-icon-ds"
|
||||
:aria-label="$t('admin.close')"
|
||||
:disabled="userProfile.isChangingPassword"
|
||||
@click="userProfile.showPasswordModal = false"
|
||||
>
|
||||
<X class="w-5 h-5" />
|
||||
</button>
|
||||
</div>
|
||||
<form @submit.prevent="userProfile.handleChangePassword">
|
||||
<div class="dialog-body">
|
||||
<div>
|
||||
<label class="label-field">{{ $t('profile.currentPassword') || 'Current Password' }}</label>
|
||||
<input v-model="userProfile.passwordForm.current" type="password" class="input-field mt-1" :disabled="userProfile.isChangingPassword" required />
|
||||
</div>
|
||||
<div>
|
||||
<label class="label-field">{{ $t('profile.newPassword') || 'New Password' }}</label>
|
||||
<input v-model="userProfile.passwordForm.new" type="password" class="input-field mt-1" :disabled="userProfile.isChangingPassword" required />
|
||||
</div>
|
||||
<div>
|
||||
<label class="label-field">{{ $t('profile.confirmPassword') || 'Confirm Password' }}</label>
|
||||
<input v-model="userProfile.passwordForm.confirm" type="password" class="input-field mt-1" :disabled="userProfile.isChangingPassword" required />
|
||||
</div>
|
||||
</div>
|
||||
<div class="dialog-footer">
|
||||
<button type="button" :disabled="userProfile.isChangingPassword" @click="userProfile.showPasswordModal = false" class="btn-secondary flex-1 disabled:opacity-50 disabled:cursor-not-allowed">
|
||||
{{ $t('profile.cancel') || 'Cancel' }}
|
||||
</button>
|
||||
<button type="submit" :disabled="userProfile.isChangingPassword" class="btn-primary flex-1 disabled:opacity-50 disabled:cursor-not-allowed">
|
||||
{{ userProfile.isChangingPassword ? $t('profile.changingPassword') : ($t('profile.changePassword') || 'Update') }}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
@@ -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<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,
|
||||
};
|
||||
}
|
||||
@@ -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<void>;
|
||||
handleChangePassword: () => Promise<void>;
|
||||
}
|
||||
|
||||
export const USER_PROFILE_KEY: InjectionKey<UserProfileContext> = Symbol('user-profile');
|
||||
@@ -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';
|
||||
@@ -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;
|
||||
@@ -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)!;
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
Reference in new issue
Block a user