mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 04:29:20 +08:00
feat: 个人中心两栏布局整合资料与改密
This commit is contained in:
@@ -113,8 +113,6 @@ const UserIcon = createIconifyIcon('lucide:user');
|
|||||||
const MemoryIcon = createIconifyIcon('lucide:brain');
|
const MemoryIcon = createIconifyIcon('lucide:brain');
|
||||||
// 意见反馈图标
|
// 意见反馈图标
|
||||||
const FeedbackIcon = createIconifyIcon('lucide:message-square-plus');
|
const FeedbackIcon = createIconifyIcon('lucide:message-square-plus');
|
||||||
// 锁图标
|
|
||||||
const LockIcon = createIconifyIcon('lucide:lock');
|
|
||||||
|
|
||||||
/** 是否具备智能助手聊天权限(用于「我的记忆」入口;含超管) */
|
/** 是否具备智能助手聊天权限(用于「我的记忆」入口;含超管) */
|
||||||
const canUseAgentChat = computed(() => hasPermission('agent:chat'));
|
const canUseAgentChat = computed(() => hasPermission('agent:chat'));
|
||||||
@@ -128,13 +126,13 @@ const companyCount = computed(() => {
|
|||||||
const menus = computed(() => {
|
const menus = computed(() => {
|
||||||
const menuItems = [];
|
const menuItems = [];
|
||||||
|
|
||||||
// 个人信息
|
// 个人中心
|
||||||
menuItems.push({
|
menuItems.push({
|
||||||
handler: () => {
|
handler: () => {
|
||||||
router.push('/system/profile');
|
router.push('/system/profile');
|
||||||
},
|
},
|
||||||
icon: UserIcon,
|
icon: UserIcon,
|
||||||
text: '个人信息',
|
text: '个人中心',
|
||||||
});
|
});
|
||||||
// 我的记忆(有助手聊天权才显示)
|
// 我的记忆(有助手聊天权才显示)
|
||||||
if (canUseAgentChat.value) {
|
if (canUseAgentChat.value) {
|
||||||
@@ -154,14 +152,6 @@ const menus = computed(() => {
|
|||||||
icon: FeedbackIcon,
|
icon: FeedbackIcon,
|
||||||
text: '意见反馈',
|
text: '意见反馈',
|
||||||
});
|
});
|
||||||
// 修改密码
|
|
||||||
menuItems.push({
|
|
||||||
handler: () => {
|
|
||||||
router.push('/system/change-password');
|
|
||||||
},
|
|
||||||
icon: LockIcon,
|
|
||||||
text: '修改密码',
|
|
||||||
});
|
|
||||||
|
|
||||||
// 可切换公司数大于 1 时显示切换公司
|
// 可切换公司数大于 1 时显示切换公司
|
||||||
if (companyCount.value > 1) {
|
if (companyCount.value > 1) {
|
||||||
|
|||||||
@@ -1,16 +1,13 @@
|
|||||||
import type { RouteRecordRaw } from 'vue-router';
|
import type { RouteRecordRaw } from 'vue-router';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改密码静态路由
|
* 修改密码:并入个人中心,保留旧路径兼容跳转
|
||||||
*/
|
*/
|
||||||
const staticRoutes: RouteRecordRaw[] = [
|
const staticRoutes: RouteRecordRaw[] = [
|
||||||
{
|
{
|
||||||
name: 'ChangePassword',
|
name: 'ChangePassword',
|
||||||
path: '/system/change-password',
|
path: '/system/change-password',
|
||||||
component: () =>
|
redirect: '/system/profile?tab=password',
|
||||||
import(
|
|
||||||
'#/views/sys/profile/modules/ChangePasswordModal.vue'
|
|
||||||
),
|
|
||||||
meta: {
|
meta: {
|
||||||
title: '修改密码',
|
title: '修改密码',
|
||||||
hideInMenu: true,
|
hideInMenu: true,
|
||||||
|
|||||||
@@ -5,14 +5,16 @@ import type { RouteRecordRaw } from 'vue-router';
|
|||||||
*/
|
*/
|
||||||
const staticRoutes: RouteRecordRaw[] = [
|
const staticRoutes: RouteRecordRaw[] = [
|
||||||
{
|
{
|
||||||
name: 'PersonalProfile',
|
name: 'PersonalCenter',
|
||||||
path: '/system/profile',
|
path: '/system/profile',
|
||||||
component: () => import('#/views/sys/profile/index.vue'),
|
component: () => import('#/views/sys/profile/index.vue'),
|
||||||
meta: {
|
meta: {
|
||||||
icon: 'lucide:user',
|
icon: 'lucide:user',
|
||||||
title: '个人信息',
|
title: '个人中心',
|
||||||
hideInMenu: true,
|
hideInMenu: true,
|
||||||
ignoreAccess: true,
|
ignoreAccess: true,
|
||||||
|
// 页内 ?tab= 不另开系统标签
|
||||||
|
fullPathKey: false,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -1,279 +1,87 @@
|
|||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import type { UserDTO, UserVO } from '#/api/sys/user/types';
|
import type { MenuProps } from 'ant-design-vue';
|
||||||
|
|
||||||
import { computed, reactive, ref } from 'vue';
|
import { computed, onMounted, ref } from 'vue';
|
||||||
import { useRouter } from 'vue-router';
|
import { useRoute, useRouter } from 'vue-router';
|
||||||
|
|
||||||
import { Page } from '@vben/common-ui';
|
import { Page } from '@vben/common-ui';
|
||||||
|
import { createIconifyIcon } from '@vben/icons';
|
||||||
|
|
||||||
import { Button, Form, Input, message, Spin, Upload } from 'ant-design-vue';
|
import { Menu } from 'ant-design-vue';
|
||||||
|
|
||||||
import { getUserInfoApi, updateUserInfoApi } from '#/api/sys/user';
|
import ChangePasswordCard from './modules/ChangePasswordCard.vue';
|
||||||
import { FileCategory } from '#/constants/file-category';
|
import ProfileInfoCard from './modules/ProfileInfoCard.vue';
|
||||||
import { uploadFile } from '#/utils/upload-file';
|
|
||||||
|
|
||||||
defineOptions({ name: 'PersonalProfile' });
|
defineOptions({ name: 'PersonalCenter' });
|
||||||
|
|
||||||
|
type TabKey = 'password' | 'profile';
|
||||||
|
|
||||||
|
const route = useRoute();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
const loading = ref(false);
|
const ProfileIcon = createIconifyIcon('lucide:user');
|
||||||
const submitting = ref(false);
|
const LockIcon = createIconifyIcon('lucide:lock');
|
||||||
const userInfo = ref<UserVO.UserInfoVO | null>(null);
|
|
||||||
const avatarLoading = ref(false);
|
|
||||||
|
|
||||||
// 待上传的头像文件(用户选择后先缓存,保存时再上传)
|
const activeTab = ref<TabKey>('profile');
|
||||||
const pendingAvatarFile = ref<File | null>(null);
|
const selectedKeys = computed(() => [activeTab.value]);
|
||||||
|
|
||||||
// 计算属性:头像显示优先级:本地预览 > 已保存的头像 > 初始头像
|
/**
|
||||||
const avatarSrc = computed(() => {
|
* 页内切换只改本地状态,不要改 query。
|
||||||
if (pendingAvatarFile.value) {
|
* tabbar 用 fullPath 做标签 key,带 ?tab= 会开出两个系统标签。
|
||||||
return URL.createObjectURL(pendingAvatarFile.value);
|
*/
|
||||||
|
const onMenuClick: MenuProps['onClick'] = ({ key }) => {
|
||||||
|
activeTab.value = key === 'password' ? 'password' : 'profile';
|
||||||
|
};
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
// 兼容 /system/change-password → ?tab=password:读一次后清掉 query
|
||||||
|
if (route.query.tab === 'password') {
|
||||||
|
activeTab.value = 'password';
|
||||||
|
void router.replace({ path: route.path, query: {} });
|
||||||
}
|
}
|
||||||
if (userInfo.value?.avatar) {
|
|
||||||
return userInfo.value.avatar;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// 表单数据(与更新接口 DTO 一致,头像 ID 用 undefined 表示未变更/未上传)
|
|
||||||
const formData = reactive<UserDTO.UpdateUserInfoDTO>({
|
|
||||||
username: '',
|
|
||||||
realName: '',
|
|
||||||
phone: '',
|
|
||||||
remark: '',
|
|
||||||
});
|
|
||||||
|
|
||||||
const formRef = ref<any>(null);
|
|
||||||
|
|
||||||
async function fetchUserInfo() {
|
|
||||||
try {
|
|
||||||
loading.value = true;
|
|
||||||
const data = await getUserInfoApi();
|
|
||||||
userInfo.value = data;
|
|
||||||
formData.username = data.username || '';
|
|
||||||
formData.realName = data.realName || '';
|
|
||||||
formData.phone = data.phone || '';
|
|
||||||
formData.remark = data.remark || '';
|
|
||||||
// 重置待上传的头像
|
|
||||||
pendingAvatarFile.value = null;
|
|
||||||
} catch (error) {
|
|
||||||
console.error('获取用户信息失败:', error);
|
|
||||||
message.error('获取用户信息失败');
|
|
||||||
} finally {
|
|
||||||
loading.value = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleAvatarChange({ file }: { file: any }) {
|
|
||||||
// 获取实际文件对象
|
|
||||||
const actualFile = file.originFileObj || file;
|
|
||||||
if (!actualFile) return;
|
|
||||||
|
|
||||||
if (!actualFile.type.startsWith('image/')) {
|
|
||||||
message.error('只能上传图片文件');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (actualFile.size > 2 * 1024 * 1024) {
|
|
||||||
message.error('图片大小不能超过 2MB');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 缓存头像文件,预览由 computed avatarSrc 自动处理
|
|
||||||
pendingAvatarFile.value = actualFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleSubmit() {
|
|
||||||
try {
|
|
||||||
await formRef.value?.validate();
|
|
||||||
submitting.value = true;
|
|
||||||
|
|
||||||
// 如果有待上传的头像,先上传
|
|
||||||
if (pendingAvatarFile.value) {
|
|
||||||
avatarLoading.value = true;
|
|
||||||
try {
|
|
||||||
const fileInfo = await uploadFile(pendingAvatarFile.value, {
|
|
||||||
category: FileCategory.AVATAR,
|
|
||||||
errorMessage: '头像上传失败',
|
|
||||||
});
|
|
||||||
formData.avatarImageId = fileInfo.id;
|
|
||||||
} finally {
|
|
||||||
avatarLoading.value = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await updateUserInfoApi(formData);
|
|
||||||
message.success('保存成功');
|
|
||||||
formData.avatarImageId = undefined;
|
|
||||||
await fetchUserInfo();
|
|
||||||
} catch (error: any) {
|
|
||||||
if (error?.errorFields) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
console.error('保存失败:', error);
|
|
||||||
message.error('保存失败');
|
|
||||||
} finally {
|
|
||||||
submitting.value = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleReset() {
|
|
||||||
if (userInfo.value) {
|
|
||||||
formData.username = userInfo.value.username || '';
|
|
||||||
formData.realName = userInfo.value.realName || '';
|
|
||||||
formData.phone = userInfo.value.phone || '';
|
|
||||||
formData.remark = userInfo.value.remark || '';
|
|
||||||
}
|
|
||||||
formData.avatarImageId = undefined;
|
|
||||||
formRef.value?.resetFields();
|
|
||||||
pendingAvatarFile.value = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
fetchUserInfo();
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<Page
|
<Page auto-content-height content-class="flex min-h-0 flex-col">
|
||||||
title="个人信息"
|
<div
|
||||||
auto-content-height
|
class="flex min-h-0 flex-1 overflow-hidden rounded-2xl border border-slate-200/80 bg-white shadow-sm dark:border-[hsl(240deg_3.7%_22%)] dark:bg-[hsl(222.34deg_10.43%_12.27%)]"
|
||||||
class="bg-[#f8fafc] dark:bg-[hsl(222.34deg_10.43%_12.27%)]"
|
>
|
||||||
>
|
<aside
|
||||||
<div class="mx-auto max-w-2xl">
|
class="flex w-52 shrink-0 flex-col border-r border-slate-100 bg-slate-50/60 dark:border-[hsl(240deg_3.7%_22%)] dark:bg-[hsl(240deg_3.7%_15.9%/30%)]"
|
||||||
<Spin :spinning="loading">
|
>
|
||||||
<div
|
<div
|
||||||
class="overflow-hidden rounded-2xl border border-slate-200/80 bg-white shadow-sm dark:border-[hsl(240deg_3.7%_22%)] dark:bg-[hsl(222.34deg_10.43%_12.27%)]"
|
class="border-b border-slate-100 px-4 py-4 text-sm font-medium text-slate-600 dark:border-[hsl(240deg_3.7%_22%)] dark:text-[hsl(240deg_5%_64.9%)]"
|
||||||
>
|
>
|
||||||
<!-- 头部 -->
|
设置
|
||||||
<div class="border-b border-slate-100 bg-slate-50/50 px-8 py-6 dark:border-[hsl(240deg_3.7%_22%)] dark:bg-[hsl(240deg_3.7%_15.9%/30%)]">
|
|
||||||
<div class="flex items-start justify-between gap-3">
|
|
||||||
<div>
|
|
||||||
<h2 class="text-xl font-bold text-slate-800 dark:text-[hsl(0deg_0%_95%)]">
|
|
||||||
个人信息
|
|
||||||
</h2>
|
|
||||||
<p class="mt-1 text-sm text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]">
|
|
||||||
编辑您的个人信息和头像
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
<Button type="link" class="!px-0" @click="router.push('/feedback')">
|
|
||||||
意见反馈
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- 表单 -->
|
|
||||||
<div class="px-8 py-6">
|
|
||||||
<Form
|
|
||||||
ref="formRef"
|
|
||||||
:model="formData"
|
|
||||||
layout="vertical"
|
|
||||||
class="space-y-6"
|
|
||||||
>
|
|
||||||
<!-- 头像 -->
|
|
||||||
<div class="flex items-center">
|
|
||||||
<div class="mr-6">
|
|
||||||
<Upload
|
|
||||||
@change="handleAvatarChange"
|
|
||||||
:show-upload-list="false"
|
|
||||||
accept="image/*"
|
|
||||||
>
|
|
||||||
<div class="relative cursor-pointer">
|
|
||||||
<Spin
|
|
||||||
v-if="avatarLoading"
|
|
||||||
class="absolute inset-0 z-10 flex items-center justify-center bg-white/80"
|
|
||||||
/>
|
|
||||||
<img
|
|
||||||
v-if="avatarSrc"
|
|
||||||
:src="avatarSrc"
|
|
||||||
alt="头像"
|
|
||||||
class="h-24 w-24 rounded-full object-cover"
|
|
||||||
/>
|
|
||||||
<div
|
|
||||||
v-else
|
|
||||||
class="flex h-24 w-24 items-center justify-center rounded-full bg-slate-200 text-2xl text-slate-400 dark:bg-[hsl(240deg_3.7%_22%)] dark:text-[hsl(240deg_5%_64.9%)]"
|
|
||||||
>
|
|
||||||
{{ userInfo?.realName?.charAt(0) || '?' }}
|
|
||||||
</div>
|
|
||||||
<div
|
|
||||||
class="absolute inset-0 flex items-center justify-center rounded-full bg-black/40 opacity-0 transition-opacity hover:opacity-100"
|
|
||||||
>
|
|
||||||
<span class="text-sm text-white">点击更换</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</Upload>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<p class="font-medium text-slate-800 dark:text-[hsl(0deg_0%_95%)]">
|
|
||||||
头像
|
|
||||||
</p>
|
|
||||||
<p class="mt-1 text-xs text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]">
|
|
||||||
点击选择新头像,保存后生效,建议大小不超过 2MB
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- 登录用户名(可选,用于账号登录) -->
|
|
||||||
<Form.Item
|
|
||||||
label="登录用户名"
|
|
||||||
name="username"
|
|
||||||
:rules="[
|
|
||||||
{ min: 3, max: 20, message: '登录用户名长度为3-20个字符' },
|
|
||||||
{
|
|
||||||
pattern: /^$|^[a-zA-Z0-9_]+$/,
|
|
||||||
message: '登录用户名只能包含字母、数字和下划线',
|
|
||||||
},
|
|
||||||
]"
|
|
||||||
>
|
|
||||||
<Input
|
|
||||||
v-model:value="formData.username"
|
|
||||||
placeholder="可选,设置后可用账号登录"
|
|
||||||
allow-clear
|
|
||||||
/>
|
|
||||||
</Form.Item>
|
|
||||||
|
|
||||||
<!-- 真实姓名 -->
|
|
||||||
<Form.Item
|
|
||||||
label="真实姓名"
|
|
||||||
name="realName"
|
|
||||||
:rules="[{ required: true, message: '请输入真实姓名' }]"
|
|
||||||
>
|
|
||||||
<Input v-model:value="formData.realName" placeholder="请输入真实姓名" />
|
|
||||||
</Form.Item>
|
|
||||||
|
|
||||||
<!-- 手机号 -->
|
|
||||||
<Form.Item
|
|
||||||
label="手机号"
|
|
||||||
name="phone"
|
|
||||||
:rules="[
|
|
||||||
{ required: true, message: '请输入手机号' },
|
|
||||||
{ pattern: /^1[3-9]\d{9}$/, message: '请输入有效的手机号' },
|
|
||||||
]"
|
|
||||||
>
|
|
||||||
<Input
|
|
||||||
v-model:value="formData.phone"
|
|
||||||
placeholder="请输入手机号"
|
|
||||||
:maxlength="11"
|
|
||||||
/>
|
|
||||||
</Form.Item>
|
|
||||||
|
|
||||||
<!-- 备注 -->
|
|
||||||
<Form.Item label="备注" name="remark">
|
|
||||||
<Input.TextArea
|
|
||||||
v-model:value="formData.remark"
|
|
||||||
placeholder="请输入备注"
|
|
||||||
:rows="3"
|
|
||||||
/>
|
|
||||||
</Form.Item>
|
|
||||||
</Form>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- 底部按钮 -->
|
|
||||||
<div class="flex justify-end gap-3 border-t border-slate-100 bg-slate-50/50 px-8 py-4 dark:border-[hsl(240deg_3.7%_22%)] dark:bg-[hsl(240deg_3.7%_15.9%/30%)]">
|
|
||||||
<Button @click="handleReset">重置</Button>
|
|
||||||
<Button type="primary" :loading="submitting" @click="handleSubmit">
|
|
||||||
保存
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</Spin>
|
<Menu
|
||||||
|
:selected-keys="selectedKeys"
|
||||||
|
mode="inline"
|
||||||
|
class="!border-none !bg-transparent"
|
||||||
|
@click="onMenuClick"
|
||||||
|
>
|
||||||
|
<Menu.Item key="profile">
|
||||||
|
<template #icon>
|
||||||
|
<ProfileIcon class="size-4" />
|
||||||
|
</template>
|
||||||
|
个人信息
|
||||||
|
</Menu.Item>
|
||||||
|
<Menu.Item key="password">
|
||||||
|
<template #icon>
|
||||||
|
<LockIcon class="size-4" />
|
||||||
|
</template>
|
||||||
|
修改密码
|
||||||
|
</Menu.Item>
|
||||||
|
</Menu>
|
||||||
|
</aside>
|
||||||
|
|
||||||
|
<div class="min-h-0 min-w-0 flex-1 overflow-y-auto p-6">
|
||||||
|
<div class="mx-auto max-w-2xl">
|
||||||
|
<ProfileInfoCard v-if="activeTab === 'profile'" />
|
||||||
|
<ChangePasswordCard v-else />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</Page>
|
</Page>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -0,0 +1,125 @@
|
|||||||
|
<script lang="ts" setup>
|
||||||
|
import type { FormItemProps } from 'ant-design-vue';
|
||||||
|
|
||||||
|
import { reactive, ref } from 'vue';
|
||||||
|
|
||||||
|
import { Button, Form, Input, message } from 'ant-design-vue';
|
||||||
|
|
||||||
|
import { changePasswordApi } from '#/api/core/auth';
|
||||||
|
|
||||||
|
defineOptions({ name: 'ChangePasswordCard' });
|
||||||
|
|
||||||
|
const loading = ref(false);
|
||||||
|
const formRef = ref<any>(null);
|
||||||
|
const formData = reactive({
|
||||||
|
oldPassword: '',
|
||||||
|
newPassword: '',
|
||||||
|
confirmPassword: '',
|
||||||
|
});
|
||||||
|
|
||||||
|
/** 确认密码校验放在 script,避免模板内 Promise 被误解析 */
|
||||||
|
const confirmPasswordRules: NonNullable<FormItemProps['rules']> = [
|
||||||
|
{
|
||||||
|
required: true,
|
||||||
|
async validator(_rule: unknown, value: string) {
|
||||||
|
if (!value?.trim()) {
|
||||||
|
throw new Error('请确认密码');
|
||||||
|
}
|
||||||
|
if (value !== formData.newPassword) {
|
||||||
|
throw new Error('两次输入的密码不一致');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
trigger: 'change',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
async function handleSubmit() {
|
||||||
|
try {
|
||||||
|
await formRef.value?.validate();
|
||||||
|
} catch {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
loading.value = true;
|
||||||
|
try {
|
||||||
|
await changePasswordApi({
|
||||||
|
oldPassword: formData.oldPassword,
|
||||||
|
newPassword: formData.newPassword,
|
||||||
|
});
|
||||||
|
message.success('密码修改成功');
|
||||||
|
handleReset();
|
||||||
|
} catch (err: any) {
|
||||||
|
message.error(err?.message || '密码修改失败');
|
||||||
|
} finally {
|
||||||
|
loading.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleReset() {
|
||||||
|
formRef.value?.resetFields();
|
||||||
|
formData.oldPassword = '';
|
||||||
|
formData.newPassword = '';
|
||||||
|
formData.confirmPassword = '';
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<div class="mb-6">
|
||||||
|
<h2 class="text-lg font-bold text-slate-800 dark:text-[hsl(0deg_0%_95%)]">
|
||||||
|
修改密码
|
||||||
|
</h2>
|
||||||
|
<p class="mt-1 text-sm text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]">
|
||||||
|
请妥善保管您的新密码
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Form ref="formRef" :model="formData" layout="vertical">
|
||||||
|
<Form.Item
|
||||||
|
label="旧密码"
|
||||||
|
name="oldPassword"
|
||||||
|
:rules="[{ required: true, message: '请输入旧密码' }]"
|
||||||
|
>
|
||||||
|
<Input.Password
|
||||||
|
v-model:value="formData.oldPassword"
|
||||||
|
placeholder="请输入旧密码"
|
||||||
|
autocomplete="current-password"
|
||||||
|
/>
|
||||||
|
</Form.Item>
|
||||||
|
|
||||||
|
<Form.Item
|
||||||
|
label="新密码"
|
||||||
|
name="newPassword"
|
||||||
|
:rules="[
|
||||||
|
{ required: true, message: '请输入新密码' },
|
||||||
|
{ min: 6, max: 20, message: '密码长度为6-20个字符' },
|
||||||
|
]"
|
||||||
|
>
|
||||||
|
<Input.Password
|
||||||
|
v-model:value="formData.newPassword"
|
||||||
|
placeholder="请输入新密码"
|
||||||
|
autocomplete="new-password"
|
||||||
|
/>
|
||||||
|
</Form.Item>
|
||||||
|
|
||||||
|
<Form.Item
|
||||||
|
label="确认密码"
|
||||||
|
name="confirmPassword"
|
||||||
|
:rules="confirmPasswordRules"
|
||||||
|
>
|
||||||
|
<Input.Password
|
||||||
|
v-model:value="formData.confirmPassword"
|
||||||
|
placeholder="请再次输入新密码"
|
||||||
|
autocomplete="new-password"
|
||||||
|
/>
|
||||||
|
</Form.Item>
|
||||||
|
</Form>
|
||||||
|
|
||||||
|
<div class="mt-2 flex justify-end gap-3">
|
||||||
|
<Button @click="handleReset">重置</Button>
|
||||||
|
<Button type="primary" :loading="loading" @click="handleSubmit">
|
||||||
|
确定
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
@@ -1,147 +0,0 @@
|
|||||||
<script lang="ts" setup>
|
|
||||||
import { reactive, ref } from 'vue';
|
|
||||||
import { useRouter } from 'vue-router';
|
|
||||||
|
|
||||||
import { Page } from '@vben/common-ui';
|
|
||||||
|
|
||||||
import type { FormItemProps } from 'ant-design-vue';
|
|
||||||
|
|
||||||
import { Button, Form, Input, message, Spin } from 'ant-design-vue';
|
|
||||||
|
|
||||||
import { changePasswordApi } from '#/api/core/auth';
|
|
||||||
|
|
||||||
defineOptions({ name: 'ChangePassword' });
|
|
||||||
|
|
||||||
const router = useRouter();
|
|
||||||
const loading = ref(false);
|
|
||||||
|
|
||||||
const formRef = ref<any>(null);
|
|
||||||
const formData = reactive({
|
|
||||||
oldPassword: '',
|
|
||||||
newPassword: '',
|
|
||||||
confirmPassword: '',
|
|
||||||
});
|
|
||||||
|
|
||||||
/** 放在 script 中,避免模板内联 validator 里 Promise 被误解析为组件属性 */
|
|
||||||
const confirmPasswordRules: NonNullable<FormItemProps['rules']> = [
|
|
||||||
{
|
|
||||||
required: true,
|
|
||||||
async validator(_rule: unknown, value: string) {
|
|
||||||
if (!value?.trim()) {
|
|
||||||
throw new Error('请确认密码');
|
|
||||||
}
|
|
||||||
if (value !== formData.newPassword) {
|
|
||||||
throw new Error('两次输入的密码不一致');
|
|
||||||
}
|
|
||||||
},
|
|
||||||
trigger: 'change',
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
async function handleSubmit() {
|
|
||||||
try {
|
|
||||||
await formRef.value?.validate();
|
|
||||||
} catch {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
loading.value = true;
|
|
||||||
try {
|
|
||||||
await changePasswordApi({
|
|
||||||
oldPassword: formData.oldPassword,
|
|
||||||
newPassword: formData.newPassword,
|
|
||||||
});
|
|
||||||
message.success('密码修改成功');
|
|
||||||
formRef.value?.resetFields();
|
|
||||||
router.push('/system/profile');
|
|
||||||
} catch (err: any) {
|
|
||||||
message.error(err?.message || '密码修改失败');
|
|
||||||
} finally {
|
|
||||||
loading.value = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleReset() {
|
|
||||||
formRef.value?.resetFields();
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<Page
|
|
||||||
title="修改密码"
|
|
||||||
auto-content-height
|
|
||||||
class="bg-[#f8fafc] dark:bg-[hsl(222.34deg_10.43%_12.27%)]"
|
|
||||||
>
|
|
||||||
<div class="mx-auto max-w-2xl">
|
|
||||||
<div
|
|
||||||
class="overflow-hidden rounded-2xl border border-slate-200/80 bg-white shadow-sm dark:border-[hsl(240deg_3.7%_22%)] dark:bg-[hsl(222.34deg_10.43%_12.27%)]"
|
|
||||||
>
|
|
||||||
<!-- 头部 -->
|
|
||||||
<div class="border-b border-slate-100 bg-slate-50/50 px-8 py-6 dark:border-[hsl(240deg_3.7%_22%)] dark:bg-[hsl(240deg_3.7%_15.9%/30%)]">
|
|
||||||
<h2 class="text-xl font-bold text-slate-800 dark:text-[hsl(0deg_0%_95%)]">
|
|
||||||
修改密码
|
|
||||||
</h2>
|
|
||||||
<p class="mt-1 text-sm text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]">
|
|
||||||
请妥善保管您的新密码
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- 表单 -->
|
|
||||||
<div class="px-8 py-6">
|
|
||||||
<Spin :spinning="loading">
|
|
||||||
<Form
|
|
||||||
ref="formRef"
|
|
||||||
:model="formData"
|
|
||||||
layout="vertical"
|
|
||||||
class="space-y-6"
|
|
||||||
>
|
|
||||||
<!-- 旧密码 -->
|
|
||||||
<Form.Item
|
|
||||||
label="旧密码"
|
|
||||||
name="oldPassword"
|
|
||||||
:rules="[{ required: true, message: '请输入旧密码' }]"
|
|
||||||
>
|
|
||||||
<Input.Password
|
|
||||||
v-model:value="formData.oldPassword"
|
|
||||||
placeholder="请输入旧密码"
|
|
||||||
/>
|
|
||||||
</Form.Item>
|
|
||||||
|
|
||||||
<!-- 新密码 -->
|
|
||||||
<Form.Item
|
|
||||||
label="新密码"
|
|
||||||
name="newPassword"
|
|
||||||
:rules="[{ required: true, message: '请输入新密码' }]"
|
|
||||||
>
|
|
||||||
<Input.Password
|
|
||||||
v-model:value="formData.newPassword"
|
|
||||||
placeholder="请输入新密码"
|
|
||||||
/>
|
|
||||||
</Form.Item>
|
|
||||||
|
|
||||||
<!-- 确认密码 -->
|
|
||||||
<Form.Item
|
|
||||||
label="确认密码"
|
|
||||||
name="confirmPassword"
|
|
||||||
:rules="confirmPasswordRules"
|
|
||||||
>
|
|
||||||
<Input.Password
|
|
||||||
v-model:value="formData.confirmPassword"
|
|
||||||
placeholder="请再次输入新密码"
|
|
||||||
/>
|
|
||||||
</Form.Item>
|
|
||||||
</Form>
|
|
||||||
</Spin>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- 底部按钮 -->
|
|
||||||
<div class="flex justify-end gap-3 border-t border-slate-100 bg-slate-50/50 px-8 py-4 dark:border-[hsl(240deg_3.7%_22%)] dark:bg-[hsl(240deg_3.7%_15.9%/30%)]">
|
|
||||||
<Button @click="handleReset">重置</Button>
|
|
||||||
<Button type="primary" :loading="loading" @click="handleSubmit">
|
|
||||||
确定
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</Page>
|
|
||||||
</template>
|
|
||||||
@@ -0,0 +1,242 @@
|
|||||||
|
<script lang="ts" setup>
|
||||||
|
import type { UserDTO, UserVO } from '#/api/sys/user/types';
|
||||||
|
|
||||||
|
import { computed, reactive, ref } from 'vue';
|
||||||
|
|
||||||
|
import { Button, Form, Input, message, Spin, Upload } from 'ant-design-vue';
|
||||||
|
|
||||||
|
import { getUserInfoApi, updateUserInfoApi } from '#/api/sys/user';
|
||||||
|
import { FileCategory } from '#/constants/file-category';
|
||||||
|
import { useAuthStore } from '#/store';
|
||||||
|
import { uploadFile } from '#/utils/upload-file';
|
||||||
|
|
||||||
|
defineOptions({ name: 'ProfileInfoCard' });
|
||||||
|
|
||||||
|
const authStore = useAuthStore();
|
||||||
|
|
||||||
|
const loading = ref(false);
|
||||||
|
const submitting = ref(false);
|
||||||
|
const userInfo = ref<UserVO.UserInfoVO | null>(null);
|
||||||
|
const avatarLoading = ref(false);
|
||||||
|
const pendingAvatarFile = ref<File | null>(null);
|
||||||
|
|
||||||
|
const avatarSrc = computed(() => {
|
||||||
|
if (pendingAvatarFile.value) {
|
||||||
|
return URL.createObjectURL(pendingAvatarFile.value);
|
||||||
|
}
|
||||||
|
if (userInfo.value?.avatar) {
|
||||||
|
return userInfo.value.avatar;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
const formData = reactive<UserDTO.UpdateUserInfoDTO>({
|
||||||
|
username: '',
|
||||||
|
realName: '',
|
||||||
|
phone: '',
|
||||||
|
remark: '',
|
||||||
|
});
|
||||||
|
|
||||||
|
const formRef = ref<any>(null);
|
||||||
|
|
||||||
|
async function fetchUserInfo() {
|
||||||
|
try {
|
||||||
|
loading.value = true;
|
||||||
|
const data = await getUserInfoApi();
|
||||||
|
userInfo.value = data;
|
||||||
|
formData.username = data.username || '';
|
||||||
|
formData.realName = data.realName || '';
|
||||||
|
formData.phone = data.phone || '';
|
||||||
|
formData.remark = data.remark || '';
|
||||||
|
pendingAvatarFile.value = null;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('获取用户信息失败:', error);
|
||||||
|
message.error('获取用户信息失败');
|
||||||
|
} finally {
|
||||||
|
loading.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleAvatarChange({ file }: { file: any }) {
|
||||||
|
const actualFile = file.originFileObj || file;
|
||||||
|
if (!actualFile) return;
|
||||||
|
|
||||||
|
if (!actualFile.type.startsWith('image/')) {
|
||||||
|
message.error('只能上传图片文件');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (actualFile.size > 2 * 1024 * 1024) {
|
||||||
|
message.error('图片大小不能超过 2MB');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pendingAvatarFile.value = actualFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleSubmit() {
|
||||||
|
try {
|
||||||
|
await formRef.value?.validate();
|
||||||
|
submitting.value = true;
|
||||||
|
|
||||||
|
if (pendingAvatarFile.value) {
|
||||||
|
avatarLoading.value = true;
|
||||||
|
try {
|
||||||
|
const fileInfo = await uploadFile(pendingAvatarFile.value, {
|
||||||
|
category: FileCategory.AVATAR,
|
||||||
|
errorMessage: '头像上传失败',
|
||||||
|
});
|
||||||
|
formData.avatarImageId = fileInfo.id;
|
||||||
|
} finally {
|
||||||
|
avatarLoading.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await updateUserInfoApi(formData);
|
||||||
|
message.success('保存成功');
|
||||||
|
formData.avatarImageId = undefined;
|
||||||
|
await fetchUserInfo();
|
||||||
|
await authStore.fetchUserInfo();
|
||||||
|
} catch (error: any) {
|
||||||
|
if (error?.errorFields) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.error('保存失败:', error);
|
||||||
|
message.error('保存失败');
|
||||||
|
} finally {
|
||||||
|
submitting.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleReset() {
|
||||||
|
if (userInfo.value) {
|
||||||
|
formData.username = userInfo.value.username || '';
|
||||||
|
formData.realName = userInfo.value.realName || '';
|
||||||
|
formData.phone = userInfo.value.phone || '';
|
||||||
|
formData.remark = userInfo.value.remark || '';
|
||||||
|
}
|
||||||
|
formData.avatarImageId = undefined;
|
||||||
|
formRef.value?.resetFields();
|
||||||
|
pendingAvatarFile.value = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchUserInfo();
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<div class="mb-6">
|
||||||
|
<h2 class="text-lg font-bold text-slate-800 dark:text-[hsl(0deg_0%_95%)]">
|
||||||
|
个人信息
|
||||||
|
</h2>
|
||||||
|
<p class="mt-1 text-sm text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]">
|
||||||
|
编辑头像、登录用户名与联系方式
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Spin :spinning="loading">
|
||||||
|
<Form ref="formRef" :model="formData" layout="vertical">
|
||||||
|
<div class="mb-6 flex items-center">
|
||||||
|
<div class="mr-5">
|
||||||
|
<Upload
|
||||||
|
accept="image/*"
|
||||||
|
:show-upload-list="false"
|
||||||
|
@change="handleAvatarChange"
|
||||||
|
>
|
||||||
|
<div class="relative cursor-pointer">
|
||||||
|
<Spin
|
||||||
|
v-if="avatarLoading"
|
||||||
|
class="absolute inset-0 z-10 flex items-center justify-center bg-white/80"
|
||||||
|
/>
|
||||||
|
<img
|
||||||
|
v-if="avatarSrc"
|
||||||
|
:src="avatarSrc"
|
||||||
|
alt="头像"
|
||||||
|
class="h-20 w-20 rounded-full object-cover"
|
||||||
|
/>
|
||||||
|
<div
|
||||||
|
v-else
|
||||||
|
class="flex h-20 w-20 items-center justify-center rounded-full bg-slate-200 text-xl text-slate-400 dark:bg-[hsl(240deg_3.7%_22%)] dark:text-[hsl(240deg_5%_64.9%)]"
|
||||||
|
>
|
||||||
|
{{ userInfo?.realName?.charAt(0) || '?' }}
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
class="absolute inset-0 flex items-center justify-center rounded-full bg-black/40 opacity-0 transition-opacity hover:opacity-100"
|
||||||
|
>
|
||||||
|
<span class="text-xs text-white">更换</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Upload>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<p class="font-medium text-slate-800 dark:text-[hsl(0deg_0%_95%)]">
|
||||||
|
头像
|
||||||
|
</p>
|
||||||
|
<p
|
||||||
|
class="mt-1 text-xs text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]"
|
||||||
|
>
|
||||||
|
点击更换,保存后生效,不超过 2MB
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Form.Item
|
||||||
|
label="登录用户名"
|
||||||
|
name="username"
|
||||||
|
:rules="[
|
||||||
|
{ min: 3, max: 20, message: '登录用户名长度为3-20个字符' },
|
||||||
|
{
|
||||||
|
pattern: /^$|^[a-zA-Z0-9_]+$/,
|
||||||
|
message: '登录用户名只能包含字母、数字和下划线',
|
||||||
|
},
|
||||||
|
]"
|
||||||
|
>
|
||||||
|
<Input
|
||||||
|
v-model:value="formData.username"
|
||||||
|
allow-clear
|
||||||
|
placeholder="可选,设置后可用账号登录"
|
||||||
|
/>
|
||||||
|
</Form.Item>
|
||||||
|
|
||||||
|
<Form.Item
|
||||||
|
label="真实姓名"
|
||||||
|
name="realName"
|
||||||
|
:rules="[{ required: true, message: '请输入真实姓名' }]"
|
||||||
|
>
|
||||||
|
<Input
|
||||||
|
v-model:value="formData.realName"
|
||||||
|
placeholder="请输入真实姓名"
|
||||||
|
/>
|
||||||
|
</Form.Item>
|
||||||
|
|
||||||
|
<Form.Item
|
||||||
|
label="手机号"
|
||||||
|
name="phone"
|
||||||
|
:rules="[
|
||||||
|
{ required: true, message: '请输入手机号' },
|
||||||
|
{ pattern: /^1[3-9]\d{9}$/, message: '请输入有效的手机号' },
|
||||||
|
]"
|
||||||
|
>
|
||||||
|
<Input
|
||||||
|
v-model:value="formData.phone"
|
||||||
|
:maxlength="11"
|
||||||
|
placeholder="请输入手机号"
|
||||||
|
/>
|
||||||
|
</Form.Item>
|
||||||
|
|
||||||
|
<Form.Item label="备注" name="remark">
|
||||||
|
<Input.TextArea
|
||||||
|
v-model:value="formData.remark"
|
||||||
|
:rows="3"
|
||||||
|
placeholder="请输入备注"
|
||||||
|
/>
|
||||||
|
</Form.Item>
|
||||||
|
</Form>
|
||||||
|
</Spin>
|
||||||
|
|
||||||
|
<div class="mt-2 flex justify-end gap-3">
|
||||||
|
<Button @click="handleReset">重置</Button>
|
||||||
|
<Button type="primary" :loading="submitting" @click="handleSubmit">
|
||||||
|
保存
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
Reference in New Issue
Block a user