mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 01:49:19 +08:00
feat: 添加个人信息编辑页面
- 创建 /system/profile 路由 - 创建个人信息编辑页面,支持编辑姓名、手机号、备注 - 支持上传头像 - 用户下拉菜单添加"个人信息"入口
This commit is contained in:
@@ -16,7 +16,7 @@ import {
|
|||||||
} from '@vben/layouts';
|
} from '@vben/layouts';
|
||||||
import { preferences } from '@vben/preferences';
|
import { preferences } from '@vben/preferences';
|
||||||
import { useAccessStore, useUserStore } from '@vben/stores';
|
import { useAccessStore, useUserStore } from '@vben/stores';
|
||||||
import { openWindow } from '@vben/utils';
|
import { openWindow, navigateTo } from '@vben/utils';
|
||||||
|
|
||||||
import ImportTaskButton from '#/components/ImportTaskButton.vue';
|
import ImportTaskButton from '#/components/ImportTaskButton.vue';
|
||||||
import SwitchAccountModal from '#/components/SwitchAccountModal.vue';
|
import SwitchAccountModal from '#/components/SwitchAccountModal.vue';
|
||||||
@@ -74,10 +74,21 @@ const switchAccountModalRef = ref();
|
|||||||
|
|
||||||
// 切换账户图标
|
// 切换账户图标
|
||||||
const SwitchAccountIcon = createIconifyIcon('lucide:arrow-left-right');
|
const SwitchAccountIcon = createIconifyIcon('lucide:arrow-left-right');
|
||||||
|
// 用户图标
|
||||||
|
const UserIcon = createIconifyIcon('lucide:user');
|
||||||
|
|
||||||
const menus = computed(() => {
|
const menus = computed(() => {
|
||||||
const menuItems = [];
|
const menuItems = [];
|
||||||
|
|
||||||
|
// 个人信息
|
||||||
|
menuItems.push({
|
||||||
|
handler: () => {
|
||||||
|
navigateTo('/system/profile');
|
||||||
|
},
|
||||||
|
icon: UserIcon,
|
||||||
|
text: '个人信息',
|
||||||
|
});
|
||||||
|
|
||||||
// 只有超级管理员才显示切换账户功能
|
// 只有超级管理员才显示切换账户功能
|
||||||
if (accessStore.userRole === 'super') {
|
if (accessStore.userRole === 'super') {
|
||||||
menuItems.push({
|
menuItems.push({
|
||||||
|
|||||||
@@ -19,6 +19,16 @@ const routes: RouteRecordRaw[] = [
|
|||||||
title: '菜单管理',
|
title: '菜单管理',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'PersonalProfile',
|
||||||
|
path: '/system/profile',
|
||||||
|
component: () => import('#/views/system/profile/index.vue'),
|
||||||
|
meta: {
|
||||||
|
icon: 'lucide:user',
|
||||||
|
title: '个人信息',
|
||||||
|
hideInMenu: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -0,0 +1,217 @@
|
|||||||
|
<script lang="ts" setup>
|
||||||
|
import type { UserVO } from '#/api/system/user/types';
|
||||||
|
|
||||||
|
import { ref } from 'vue';
|
||||||
|
|
||||||
|
import { Page } from '@vben/common-ui';
|
||||||
|
|
||||||
|
import { Button, Form, Input, message, Spin, Upload } from 'ant-design-vue';
|
||||||
|
|
||||||
|
import { getUserInfoApi, updateUserInfoApi } from '#/api/system/user';
|
||||||
|
import { uploadFileToOss } from '#/utils/oss-upload';
|
||||||
|
|
||||||
|
defineOptions({ name: 'PersonalProfile' });
|
||||||
|
|
||||||
|
const loading = ref(false);
|
||||||
|
const submitting = ref(false);
|
||||||
|
const userInfo = ref<UserVO.UserInfoVO | null>(null);
|
||||||
|
const avatarLoading = ref(false);
|
||||||
|
|
||||||
|
// 表单数据
|
||||||
|
const formData = ref({
|
||||||
|
realName: '',
|
||||||
|
phone: '',
|
||||||
|
remark: '',
|
||||||
|
});
|
||||||
|
|
||||||
|
const [formRef] = Form.useForm();
|
||||||
|
|
||||||
|
async function fetchUserInfo() {
|
||||||
|
try {
|
||||||
|
loading.value = true;
|
||||||
|
const data = await getUserInfoApi();
|
||||||
|
userInfo.value = data;
|
||||||
|
formData.value = {
|
||||||
|
realName: data.realName || '',
|
||||||
|
phone: data.phone || '',
|
||||||
|
remark: data.remark || '',
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error('获取用户信息失败:', error);
|
||||||
|
message.error('获取用户信息失败');
|
||||||
|
} finally {
|
||||||
|
loading.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleAvatarUpload(file: File) {
|
||||||
|
if (!file.type.startsWith('image/')) {
|
||||||
|
message.error('只能上传图片文件');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (file.size > 2 * 1024 * 1024) {
|
||||||
|
message.error('图片大小不能超过 2MB');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
avatarLoading.value = true;
|
||||||
|
const fileInfo = await uploadFileToOss(file, {
|
||||||
|
category: 'user-avatar',
|
||||||
|
errorMessage: '头像上传失败',
|
||||||
|
});
|
||||||
|
// 更新用户头像
|
||||||
|
await updateUserInfoApi({ avatarImageId: fileInfo.id });
|
||||||
|
userInfo.value!.avatar = fileInfo.storagePath;
|
||||||
|
message.success('头像上传成功');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('头像上传失败:', error);
|
||||||
|
} finally {
|
||||||
|
avatarLoading.value = false;
|
||||||
|
}
|
||||||
|
return false; // 阻止默认上传
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleSubmit() {
|
||||||
|
try {
|
||||||
|
await formRef.value?.validate();
|
||||||
|
submitting.value = true;
|
||||||
|
await updateUserInfoApi(formData.value);
|
||||||
|
message.success('保存成功');
|
||||||
|
await fetchUserInfo();
|
||||||
|
} catch (error: any) {
|
||||||
|
if (error?.errorFields) {
|
||||||
|
// 表单验证错误
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.error('保存失败:', error);
|
||||||
|
message.error('保存失败');
|
||||||
|
} finally {
|
||||||
|
submitting.value = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleReset() {
|
||||||
|
if (userInfo.value) {
|
||||||
|
formData.value = {
|
||||||
|
realName: userInfo.value.realName || '',
|
||||||
|
phone: userInfo.value.phone || '',
|
||||||
|
remark: userInfo.value.remark || '',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
formRef.value?.resetFields();
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchUserInfo();
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<Page>
|
||||||
|
<div class="mx-auto max-w-2xl">
|
||||||
|
<Spin :spinning="loading">
|
||||||
|
<div
|
||||||
|
class="overflow-hidden rounded-2xl border border-slate-200/80 bg-white shadow-sm"
|
||||||
|
>
|
||||||
|
<!-- 头部 -->
|
||||||
|
<div class="border-b border-slate-100 bg-slate-50/50 px-8 py-6">
|
||||||
|
<h2 class="text-xl font-bold text-slate-800">个人信息</h2>
|
||||||
|
<p class="mt-1 text-sm text-slate-500">
|
||||||
|
编辑您的个人信息和头像
|
||||||
|
</p>
|
||||||
|
</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
|
||||||
|
:before-upload="handleAvatarUpload"
|
||||||
|
:show-upload-list="false"
|
||||||
|
accept="image/*"
|
||||||
|
>
|
||||||
|
<div class="relative cursor-pointer">
|
||||||
|
<Spin
|
||||||
|
v-if="avatarLoading"
|
||||||
|
class="absolute inset-0 flex items-center justify-center bg-white/80"
|
||||||
|
/>
|
||||||
|
<img
|
||||||
|
v-if="userInfo?.avatar"
|
||||||
|
:src="userInfo.avatar"
|
||||||
|
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"
|
||||||
|
>
|
||||||
|
{{ 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">头像</p>
|
||||||
|
<p class="mt-1 text-xs text-slate-500">
|
||||||
|
点击图片上传,建议大小不超过 2MB
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 用户名(只读) -->
|
||||||
|
<Form.Item label="用户名">
|
||||||
|
<Input :value="userInfo?.username" disabled />
|
||||||
|
</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">
|
||||||
|
<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">
|
||||||
|
<Button @click="handleReset">重置</Button>
|
||||||
|
<Button type="primary" :loading="submitting" @click="handleSubmit">
|
||||||
|
保存
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Spin>
|
||||||
|
</div>
|
||||||
|
</Page>
|
||||||
|
</template>
|
||||||
Reference in New Issue
Block a user