Files
JayuCloud/Frontend/src/views/ProfileView.vue
T

756 lines
18 KiB
Vue
Raw Normal View History

<template>
<div class="mobile-container">
2026-02-24 16:07:09 +08:00
<div class="header-bg"></div>
<header class="header">
<h1>个人中心</h1>
<a-button class="settings-btn" type="text" @click="handleSettingsClick">
<SettingOutlined :style="{ fontSize: '20px' }" />
</a-button>
</header>
<div class="profile-info">
<a-card :bordered="false" class="profile-card">
<div class="avatar-section">
<a-badge :dot="hasNotification" :offset="[-5, 5]">
2026-02-24 16:07:09 +08:00
<a-avatar :size="90" :src="userInfo.avatar || DEFAULT_AVATAR" class="user-avatar" />
</a-badge>
<div class="user-details">
2026-02-24 16:07:09 +08:00
<h2 class="username">{{ userInfo.nickname || '用户' }}</h2>
<p class="account">{{ userInfo.account }}</p>
2026-02-24 16:07:09 +08:00
<p class="signature" v-if="userInfo.signature">{{ userInfo.signature }}</p>
</div>
</div>
<div class="shop-card" @click="handleShopCardClick">
<div class="shop-icon-wrapper">
<ShopOutlined class="shop-icon" />
</div>
<div class="shop-content">
<p class="shop-label">当前登录门店</p>
<h3 class="shop-name">{{ currentShopName }}</h3>
<p class="shop-tip">{{ shopSwitchTip }}</p>
</div>
<div class="shop-arrow">
<RightOutlined />
2026-02-24 16:07:09 +08:00
</div>
</div>
<div v-if="needSetPassword" class="password-tip">
<a-alert
type="warning"
message="建议尽快设置密码"
description="你当前更适合使用手机号验证码登录,设置密码后账号会更方便也更安全。"
show-icon
>
<template #action>
<a-button size="small" type="primary" @click="handleSetPassword">去设置</a-button>
</template>
</a-alert>
</div>
2026-02-24 16:07:09 +08:00
<a-button type="primary" block @click="handleEditProfile" class="edit-btn">
编辑资料
</a-button>
</a-card>
</div>
<div class="stats-section">
2026-02-24 16:07:09 +08:00
<a-card :bordered="false" class="stats-card">
<a-row :gutter="16">
<a-col :span="8">
<div class="stat-item" @click="router.push('/orders')">
<div class="stat-icon orders">
2026-02-24 16:07:09 +08:00
<ShoppingOutlined />
</div>
<a-statistic title="订单" :value="orderCount" />
2026-02-24 16:07:09 +08:00
</div>
</a-col>
<a-col :span="8">
<div class="stat-item" @click="router.push('/wishlist')">
<div class="stat-icon wishlist">
2026-02-24 16:07:09 +08:00
<HeartOutlined />
</div>
<a-statistic title="收藏" :value="wishlistCount" />
2026-02-24 16:07:09 +08:00
</div>
</a-col>
<a-col :span="8">
<div class="stat-item" @click="router.push('/coupons')">
<div class="stat-icon coupons">
2026-02-24 16:07:09 +08:00
<CreditCardOutlined />
</div>
2026-02-28 14:32:28 +08:00
<a-statistic title="优惠券" :value="couponCount" />
2026-02-24 16:07:09 +08:00
</div>
</a-col>
</a-row>
</a-card>
</div>
<div class="menu-section">
<a-card :bordered="false" class="menu-card">
<a-list :data-source="menuItems" :split="false">
<template #renderItem="{ item }">
2026-02-24 16:07:09 +08:00
<a-list-item class="menu-item" @click="handleMenuClick(item.key)">
<template #actions>
<RightOutlined />
</template>
<a-list-item-meta>
<template #avatar>
<div class="menu-icon" :style="{ backgroundColor: item.bgColor }">
<component :is="item.icon" />
</div>
</template>
<template #title>
<span>{{ item.title }}</span>
</template>
<template #description v-if="item.desc">
<span>{{ item.desc }}</span>
</template>
</a-list-item-meta>
</a-list-item>
</template>
</a-list>
</a-card>
</div>
<div class="logout-section">
<a-button danger block @click="handleLogout">
退出登录
</a-button>
</div>
2026-02-24 16:07:09 +08:00
<a-modal
v-model:open="editModalVisible"
2026-02-24 16:07:09 +08:00
title="编辑资料"
@ok="handleSaveProfile"
@cancel="handleCancelEdit"
:confirmLoading="saving"
>
<a-form layout="vertical">
<a-form-item label="头像">
<div class="avatar-upload-section">
<a-upload
name="file"
:show-upload-list="false"
:before-upload="beforeUploadAvatar"
:custom-request="handleAvatarUpload"
accept="image/*"
>
<a-avatar :size="80" :src="editForm.avatar || DEFAULT_AVATAR" />
<div class="upload-hint">点击更换头像</div>
</a-upload>
<a-button v-if="editForm.avatar" type="text" danger size="small" @click="handleDeleteAvatar">
删除头像
</a-button>
</div>
</a-form-item>
<a-form-item label="昵称">
<a-input v-model:value="editForm.nickname" placeholder="请输入昵称" />
</a-form-item>
<a-form-item label="个性签名">
<a-textarea v-model:value="editForm.signature" placeholder="请输入个性签名" :rows="3" />
</a-form-item>
</a-form>
</a-modal>
<a-modal
v-model:open="shopSwitchVisible"
title="切换登录门店"
:confirm-loading="shopSwitchLoading"
@ok="handleConfirmSwitchShop"
>
<a-radio-group v-model:value="selectedShopId" class="shop-option-group">
<a-radio
v-for="shop in shopOptions"
:key="String(shop.id)"
:value="String(shop.id)"
class="shop-option"
>
<div class="shop-option-name">{{ shop.shopName }}</div>
</a-radio>
</a-radio-group>
<a-empty v-if="!shopOptions.length" description="当前账号没有可切换的门店" />
</a-modal>
</div>
</template>
<script setup lang="ts">
import { computed, onMounted, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
2026-02-24 16:07:09 +08:00
import {
BellOutlined,
CreditCardOutlined,
HeartOutlined,
2026-02-24 16:07:09 +08:00
QuestionCircleOutlined,
RightOutlined,
SettingOutlined,
ShopOutlined,
ShoppingOutlined
} from '@ant-design/icons-vue'
2026-02-25 09:13:45 +08:00
import { message, Modal } from 'ant-design-vue'
import type { UploadProps } from 'ant-design-vue'
2026-02-24 14:12:27 +08:00
import { authApi } from '@/api/auth'
import { couponApi } from '@/api/coupon'
2026-02-24 16:07:09 +08:00
import { fileApi } from '@/api/file'
import { orderApi } from '@/api/order'
import { userApi, type UserVO } from '@/api/user'
import { wishlistApi } from '@/api/wishlist'
2026-02-24 16:07:09 +08:00
import { DEFAULT_AVATAR } from '@/constants'
const router = useRouter()
2026-02-24 16:07:09 +08:00
const userInfo = ref<UserVO>({
id: 0,
nickname: '',
account: '',
2026-03-02 10:13:20 +08:00
avatar: '',
type: 0,
status: 0,
2026-02-24 16:07:09 +08:00
createTime: '',
updateTime: ''
})
const editModalVisible = ref(false)
const saving = ref(false)
const needSetPassword = ref(false)
const hasNotification = ref(false)
const orderCount = ref(0)
2026-02-28 14:32:28 +08:00
const couponCount = ref(0)
const wishlistCount = ref(0)
const shopSwitchVisible = ref(false)
const shopSwitchLoading = ref(false)
const selectedShopId = ref<string>()
const editForm = reactive({
2026-02-24 16:07:09 +08:00
nickname: '',
avatar: '',
signature: ''
})
const shopOptions = computed(() => {
const ids = userInfo.value.shopIds || []
const names = userInfo.value.shopNames || []
return ids.map((id, index) => ({
id,
shopName: names[index] || `门店 ${id}`
}))
})
const currentShopName = computed(() => userInfo.value.shopName || '暂未选择门店')
const shopSwitchTip = computed(() => {
if (shopOptions.value.length > 1) {
return `可切换 ${shopOptions.value.length} 家门店`
}
if (shopOptions.value.length === 1) {
return '点击查看当前门店详情'
}
return '当前账号暂无可切换门店'
})
const menuItems = [
2026-02-24 16:07:09 +08:00
{
key: 'orders',
title: '我的订单',
desc: '查看预约状态、取消订单',
icon: ShoppingOutlined,
bgColor: '#e6f7ff'
},
2026-02-24 16:07:09 +08:00
{
key: 'wishlist',
title: '我的收藏',
desc: '快速回到喜欢的作品',
icon: HeartOutlined,
bgColor: '#fff1f0'
},
2026-02-24 16:07:09 +08:00
{
key: 'coupons',
title: '我的优惠券',
desc: '查看可用优惠和 VIP 折扣',
icon: CreditCardOutlined,
bgColor: '#fff7e6'
},
2026-02-24 16:07:09 +08:00
{
key: 'notifications',
title: '消息通知',
desc: '消息中心功能开发中',
icon: BellOutlined,
bgColor: '#f9f0ff'
},
2026-02-24 16:07:09 +08:00
{
key: 'help',
title: '帮助与支持',
desc: '常见问题功能开发中',
icon: QuestionCircleOutlined,
bgColor: '#eef9ff'
}
]
const persistUserSession = (payload: any) => {
localStorage.setItem('accessToken', payload.accessToken)
localStorage.setItem('refreshToken', payload.refreshToken)
localStorage.setItem('userInfo', JSON.stringify(payload.userInfo))
sessionStorage.setItem('accessToken', payload.accessToken)
if (payload.needSetPassword) {
localStorage.setItem('needSetPassword', 'true')
} else {
localStorage.removeItem('needSetPassword')
}
}
2026-02-24 16:07:09 +08:00
const fetchUserInfo = async () => {
try {
const response = await userApi.getUserInfo()
2026-03-02 10:13:20 +08:00
userInfo.value = response
needSetPassword.value = localStorage.getItem('needSetPassword') === 'true'
selectedShopId.value = response.shopId ? String(response.shopId) : undefined
2026-02-24 16:07:09 +08:00
} catch (error) {
console.error('获取用户信息失败:', error)
2026-02-24 16:07:09 +08:00
message.error('获取用户信息失败')
}
}
const loadCounts = async () => {
try {
const [orders, coupons, wishlist] = await Promise.all([
orderApi.getOrderList(),
couponApi.listUserCoupons(),
wishlistApi.getWishlist()
])
orderCount.value = Array.isArray(orders) ? orders.length : 0
couponCount.value = Array.isArray(coupons) ? coupons.length : 0
wishlistCount.value = Array.isArray(wishlist) ? wishlist.length : 0
} catch (error) {
console.error('加载个人中心统计失败:', error)
2026-02-24 16:07:09 +08:00
}
}
const handleEditProfile = () => {
editForm.nickname = userInfo.value.nickname || ''
editForm.avatar = userInfo.value.avatar || ''
editForm.signature = userInfo.value.signature || ''
2026-02-24 16:07:09 +08:00
editModalVisible.value = true
}
const beforeUploadAvatar: UploadProps['beforeUpload'] = file => {
2026-02-24 16:07:09 +08:00
const isImage = file.type.startsWith('image/')
2026-02-27 11:05:15 +08:00
const isLt20M = file.size / 1024 / 1024 < 20
2026-02-24 16:07:09 +08:00
if (!isImage) {
message.error('只能上传图片文件')
2026-02-24 16:07:09 +08:00
}
2026-02-27 11:05:15 +08:00
if (!isLt20M) {
message.error('图片大小不能超过 20MB')
2026-02-24 16:07:09 +08:00
}
2026-02-27 11:05:15 +08:00
return isImage && isLt20M
2026-02-24 16:07:09 +08:00
}
const handleAvatarUpload = async (options: any) => {
const { file } = options
try {
const res = await fileApi.uploadAvatar(file)
editForm.avatar = res as unknown as string
2026-02-24 16:07:09 +08:00
message.success('头像上传成功')
} catch (error) {
console.error('头像上传失败:', error)
message.error('头像上传失败')
}
}
const handleDeleteAvatar = () => {
Modal.confirm({
title: '确认删除头像',
content: '删除后会恢复为默认头像。',
okText: '确认',
cancelText: '取消',
2026-02-25 09:13:45 +08:00
onOk: async () => {
editForm.avatar = ''
2026-02-25 09:13:45 +08:00
try {
await userApi.updateUserInfo({ avatar: '' })
userInfo.value.avatar = ''
2026-02-25 09:13:45 +08:00
message.success('头像删除成功')
} catch (error) {
console.error('删除头像失败:', error)
message.error('删除头像失败')
}
2026-02-24 16:07:09 +08:00
}
})
}
const handleSaveProfile = async () => {
if (!editForm.nickname.trim()) {
2026-02-24 16:07:09 +08:00
message.error('请输入昵称')
return
}
saving.value = true
try {
await userApi.updateUserInfo({
nickname: editForm.nickname,
avatar: editForm.avatar,
signature: editForm.signature
})
2026-02-24 16:07:09 +08:00
message.success('资料更新成功')
editModalVisible.value = false
await fetchUserInfo()
} catch (error) {
console.error('保存资料失败:', error)
message.error('保存资料失败')
} finally {
saving.value = false
}
}
const handleCancelEdit = () => {
editModalVisible.value = false
}
const handleShopCardClick = () => {
if (shopOptions.value.length > 1) {
shopSwitchVisible.value = true
return
}
if (userInfo.value.shopId) {
router.push(`/shop/${userInfo.value.shopId}`)
return
}
message.info('当前账号还没有可用门店')
}
const handleConfirmSwitchShop = async () => {
if (!selectedShopId.value) {
message.warning('请选择要切换的门店')
return
}
if (String(userInfo.value.shopId || '') === selectedShopId.value) {
shopSwitchVisible.value = false
return
}
shopSwitchLoading.value = true
try {
const response = await authApi.switchShop({ shopId: selectedShopId.value })
persistUserSession(response as any)
message.success('门店已切换')
shopSwitchVisible.value = false
await Promise.all([fetchUserInfo(), loadCounts()])
} catch (error: any) {
console.error('切换门店失败:', error)
message.error(error?.message || '切换门店失败')
} finally {
shopSwitchLoading.value = false
}
}
const handleMenuClick = (key: string) => {
if (key === 'orders') {
router.push('/orders')
} else if (key === 'wishlist') {
router.push('/wishlist')
} else if (key === 'coupons') {
2026-02-28 15:45:16 +08:00
router.push('/coupons')
} else if (key === 'notifications') {
message.info('消息通知功能开发中')
} else if (key === 'help') {
message.info('帮助中心功能开发中')
}
}
const handleLogout = () => {
2026-02-24 14:12:27 +08:00
Modal.confirm({
title: '确认退出登录',
content: '退出后需要重新登录才能继续下单和查看订单。',
okText: '确认',
2026-02-24 14:12:27 +08:00
cancelText: '取消',
onOk: async () => {
try {
await authApi.logout()
} catch (error) {
console.error('退出登录接口调用失败:', error)
2026-02-24 14:12:27 +08:00
} finally {
localStorage.removeItem('accessToken')
localStorage.removeItem('refreshToken')
localStorage.removeItem('userInfo')
localStorage.removeItem('needSetPassword')
2026-02-24 14:12:27 +08:00
sessionStorage.removeItem('accessToken')
message.success('已退出登录')
router.push('/')
2026-02-24 14:12:27 +08:00
}
}
})
}
const handleSettingsClick = () => {
router.push('/settings')
}
2026-02-24 16:07:09 +08:00
const handleSetPassword = () => {
router.push('/settings/password')
}
onMounted(async () => {
await Promise.all([fetchUserInfo(), loadCounts()])
2026-02-24 16:07:09 +08:00
})
</script>
<style scoped>
.mobile-container {
padding-top: 60px;
min-height: 100vh;
background: #f5f5f5;
2026-02-24 16:07:09 +08:00
position: relative;
}
.header-bg {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 200px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 45%, #f093fb 100%);
2026-02-24 16:07:09 +08:00
z-index: 0;
}
.header {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 100;
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 15px;
2026-02-24 16:07:09 +08:00
color: white;
}
.header h1 {
margin: 0;
font-size: 18px;
font-weight: 700;
}
.settings-btn {
2026-02-24 16:07:09 +08:00
color: white;
}
.profile-info,
.stats-section,
.menu-section,
.logout-section {
2026-02-24 16:07:09 +08:00
position: relative;
z-index: 1;
padding: 0 15px 15px;
}
.profile-info {
padding-top: 15px;
}
.profile-card,
.stats-card,
.menu-card {
border-radius: 22px;
box-shadow: 0 12px 30px rgba(15, 23, 42, 0.08);
}
.profile-card :deep(.ant-card-body) {
2026-02-24 16:07:09 +08:00
padding: 28px 24px 24px;
}
.avatar-section {
display: flex;
align-items: center;
2026-02-24 16:07:09 +08:00
gap: 18px;
}
.user-avatar {
border: 4px solid white;
}
.username {
margin: 0 0 6px;
2026-02-24 16:07:09 +08:00
font-size: 24px;
font-weight: 700;
color: #1a1a2e;
}
.account,
2026-02-24 16:07:09 +08:00
.signature {
margin: 0;
color: #7c7c8a;
line-height: 1.7;
2026-02-24 16:07:09 +08:00
}
.shop-card {
display: flex;
align-items: center;
gap: 14px;
margin-top: 24px;
2026-02-24 16:07:09 +08:00
padding: 18px;
border-radius: 18px;
2026-02-24 16:07:09 +08:00
background: linear-gradient(135deg, #f5f7ff 0%, #e8ecff 100%);
cursor: pointer;
}
.shop-icon-wrapper {
width: 52px;
height: 52px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 14px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
2026-02-24 16:07:09 +08:00
}
.shop-icon {
font-size: 24px;
color: white;
}
.shop-content {
flex: 1;
}
.shop-label {
margin: 0 0 4px;
2026-02-24 16:07:09 +08:00
font-size: 12px;
color: #8b8b9a;
}
.shop-name {
margin: 0;
font-size: 18px;
font-weight: 700;
color: #1a1a2e;
}
.shop-tip {
margin: 6px 0 0;
color: #6b6b7a;
font-size: 13px;
2026-02-24 16:07:09 +08:00
}
.shop-arrow {
color: #c4c4d0;
}
.password-tip {
margin-top: 20px;
2026-02-24 16:07:09 +08:00
}
.edit-btn {
margin-top: 20px;
2026-02-24 16:07:09 +08:00
height: 48px;
border-radius: 14px;
2026-02-24 16:07:09 +08:00
font-size: 15px;
font-weight: 700;
}
.stats-card :deep(.ant-card-body) {
padding: 20px 16px;
}
.stat-item {
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
cursor: pointer;
}
.stat-icon {
width: 52px;
height: 52px;
border-radius: 16px;
display: flex;
align-items: center;
justify-content: center;
font-size: 24px;
color: white;
}
.stat-icon.orders {
2026-02-24 16:07:09 +08:00
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.stat-icon.wishlist {
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}
.stat-icon.coupons {
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}
.menu-card :deep(.ant-card-body) {
padding: 0;
}
.menu-item {
padding: 18px 16px;
cursor: pointer;
border-bottom: 1px solid #f5f5f7;
}
.menu-item:last-child {
border-bottom: none;
}
.menu-icon {
width: 44px;
height: 44px;
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
font-size: 20px;
color: white;
}
.logout-section :deep(.ant-btn-dangerous) {
height: 50px;
border-radius: 25px;
font-weight: 700;
font-size: 16px;
2026-02-24 16:07:09 +08:00
}
.avatar-upload-section {
display: flex;
flex-direction: column;
align-items: center;
gap: 8px;
}
.upload-hint {
font-size: 12px;
color: #667eea;
}
.shop-option-group {
2026-02-24 16:07:09 +08:00
display: flex;
flex-direction: column;
gap: 12px;
}
.shop-option {
margin: 0;
padding: 12px 14px;
border: 1px solid #e8ecff;
border-radius: 14px;
}
.shop-option-name {
font-weight: 600;
}
2026-03-24 09:50:55 +08:00
@media (min-width: 992px) {
.profile-info,
.stats-section,
.menu-section,
.logout-section {
max-width: var(--frontend-content-max-width);
margin-left: auto;
margin-right: auto;
}
2026-03-24 09:50:55 +08:00
}
</style>