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

1031 lines
22 KiB
Vue

<template>
<div class="mobile-container">
<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]">
<a-avatar :size="90" :src="userInfo.avatar || DEFAULT_AVATAR" class="user-avatar" />
</a-badge>
<div class="user-details">
<h2 class="username">{{ userInfo.nickname || '用户' }}</h2>
<p class="email">{{ userInfo.account }}</p>
<p class="signature" v-if="userInfo.signature">{{ userInfo.signature }}</p>
</div>
</div>
<div class="shop-section" v-if="userInfo.shopName">
<div class="shop-card" @click="handleShopClick">
<div class="shop-icon-wrapper">
<ShopOutlined class="shop-icon" />
</div>
<div class="shop-content">
<p class="shop-label">所属店铺</p>
<h3 class="shop-name">{{ userInfo.shopName }}</h3>
</div>
<div class="shop-arrow">
<RightOutlined />
</div>
</div>
</div>
<!-- 密码设置提示 -->
<div v-if="needSetPassword" class="password-tip">
<a-alert
type="warning"
message="请尽快设置密码"
description="为了账户安全,建议您设置一个安全的密码"
show-icon
:closable="false"
>
<template #action>
<a-button size="small" type="primary" @click="handleSetPassword">去设置</a-button>
</template>
</a-alert>
</div>
<a-button type="primary" block @click="handleEditProfile" class="edit-btn">
编辑资料
</a-button>
</a-card>
</div>
<div class="stats-section">
<a-card :bordered="false" class="stats-card">
<a-row :gutter="16">
<a-col :span="8">
<div class="stat-item" @click="router.push('/orders')" style="cursor: pointer;">
<div class="stat-icon" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
<ShoppingOutlined />
</div>
<a-statistic title="订单" :value="orderCount" />
</div>
</a-col>
<a-col :span="8">
<div class="stat-item">
<div class="stat-icon" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);">
<HeartOutlined />
</div>
<a-statistic title="收藏" :value="wishlistCount" />
</div>
</a-col>
<a-col :span="8">
<div class="stat-item" @click="handleCouponClick" style="cursor: pointer;">
<div class="stat-icon" style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);">
<CreditCardOutlined />
</div>
<a-statistic title="优惠券" :value="couponCount" />
</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 }">
<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>
<!-- 订单列表 -->
<a-card v-if="activeTab === 'orders'" :bordered="false" class="content-card">
<h3>我的订单</h3>
<a-empty v-if="orders.length === 0" description="暂无订单">
<a-button type="primary" @click="router.push('/products')">去购物</a-button>
</a-empty>
<div v-else class="order-list">
<a-card v-for="order in orders" :key="order.id" class="order-card">
<div class="order-header">
<span class="order-no">订单号: {{ order.orderNo }}</span>
<span :class="['order-status', order.status === 1 ? 'pending' : order.status === 2 ? 'completed' : 'cancelled']">
{{ order.statusText }}
</span>
</div>
<div class="order-items">
<div v-for="item in order.items" :key="item.id" class="order-item">
<div class="item-image" v-if="item.productImage">
<img :src="item.productImage" :alt="item.productTitle" />
</div>
<div class="item-image" v-else :style="{ backgroundColor: getRandomColor() }">
<span>{{ item.productTitle[0] }}</span>
</div>
<div class="item-info">
<div class="item-title">{{ item.productTitle }}</div>
<div class="item-price">¥{{ item.price.toFixed(2) }} x {{ item.quantity }}</div>
</div>
</div>
</div>
<div class="order-footer">
<div class="order-total">
总计: <span class="total-price">¥{{ order.actualAmount.toFixed(2) }}</span>
</div>
<div class="order-actions">
<a-button v-if="order.status === 1" size="small" danger @click="handleCancelOrder(order.id)">
取消订单
</a-button>
</div>
</div>
</a-card>
</div>
</a-card>
<div class="logout-section">
<a-button danger block @click="handleLogout">
退出登录
</a-button>
</div>
<a-modal
v-model:visible="editModalVisible"
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>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import {
SettingOutlined,
RightOutlined,
ShoppingOutlined,
HeartOutlined,
EnvironmentOutlined,
CreditCardOutlined,
BellOutlined,
QuestionCircleOutlined,
ShopOutlined
} from '@ant-design/icons-vue'
import { message, Modal } from 'ant-design-vue'
import type { UploadProps } from 'ant-design-vue'
import { authApi } from '@/api/auth'
import { userApi, type UserVO } from '@/api/user'
import { fileApi } from '@/api/file'
import { orderApi } from '@/api/order'
import { couponApi } from '@/api/coupon'
import { wishlistApi } from '@/api/wishlist'
import { DEFAULT_AVATAR } from '@/constants'
const router = useRouter()
const userInfo = ref<UserVO>({
id: 0,
nickname: '',
account: '',
avatar: '',
type: 0,
status: 0,
createTime: '',
updateTime: ''
})
const hasNotification = ref(true)
const needSetPassword = ref(false)
const editModalVisible = ref(false)
const saving = ref(false)
const uploading = ref(false)
// 订单相关状态
const activeTab = ref('')
const orders = ref<any[]>([])
const loading = ref(false)
const orderCount = ref(0)
const couponCount = ref(0)
const wishlistCount = ref(0)
const editForm = ref({
nickname: '',
avatar: '',
signature: ''
})
const menuItems = ref([
{
key: 'orders',
title: '我的订单',
icon: ShoppingOutlined,
bgColor: '#e6f7ff'
},
{
key: 'wishlist',
title: '我的收藏',
icon: HeartOutlined,
bgColor: '#fff1f0'
},
{
key: 'address',
title: '收货地址',
icon: EnvironmentOutlined,
bgColor: '#f6ffed'
},
{
key: 'payment',
title: '支付方式',
icon: CreditCardOutlined,
bgColor: '#fff7e6'
},
{
key: 'notifications',
title: '消息通知',
desc: '3条未读',
icon: BellOutlined,
bgColor: '#f9f0ff'
},
{
key: 'help',
title: '帮助与支持',
icon: QuestionCircleOutlined,
bgColor: '#e6f7ff'
}
])
const fetchUserInfo = async () => {
try {
const response = await userApi.getUserInfo() as any
userInfo.value = response
// 检查是否需要设置密码
needSetPassword.value = localStorage.getItem('needSetPassword') === 'true'
} catch (error) {
message.error('获取用户信息失败')
}
}
const handleEditProfile = () => {
editForm.value = {
nickname: userInfo.value.nickname || '',
avatar: userInfo.value.avatar || '',
signature: userInfo.value.signature || ''
}
editModalVisible.value = true
}
const beforeUploadAvatar: UploadProps['beforeUpload'] = (file) => {
const isImage = file.type.startsWith('image/')
const isLt20M = file.size / 1024 / 1024 < 20
if (!isImage) {
message.error('只能上传图片文件!')
}
if (!isLt20M) {
message.error('图片大小不能超过 20MB!')
}
return isImage && isLt20M
}
const handleAvatarUpload = async (options: any) => {
const { file } = options
uploading.value = true
try {
const res = await fileApi.uploadAvatar(file)
editForm.value.avatar = res as unknown as string
message.success('头像上传成功')
} catch (error) {
console.error('头像上传失败:', error)
message.error('头像上传失败')
} finally {
uploading.value = false
}
}
const handleDeleteAvatar = () => {
Modal.confirm({
title: '确认删除',
content: '确定要删除头像吗?',
onOk: async () => {
editForm.value.avatar = ''
userInfo.value.avatar = ''
try {
await userApi.updateUserInfo({ avatar: '' })
message.success('头像删除成功')
} catch (error) {
console.error('删除头像失败:', error)
message.error('删除头像失败')
}
}
})
}
const handleSaveProfile = async () => {
if (!editForm.value.nickname) {
message.error('请输入昵称')
return
}
saving.value = true
try {
await userApi.updateUserInfo(editForm.value)
message.success('资料更新成功')
editModalVisible.value = false
await fetchUserInfo()
} catch (error) {
console.error('保存资料失败:', error)
message.error('保存资料失败')
} finally {
saving.value = false
}
}
const handleCancelEdit = () => {
editModalVisible.value = false
}
// 加载订单列表
const loadOrders = async () => {
loading.value = true
try {
const response = await orderApi.getOrderList() as any
orders.value = response
orderCount.value = response.length
} catch (error) {
message.error('获取订单列表失败')
} finally {
loading.value = false
}
}
// 处理优惠券点击
const handleCouponClick = () => {
router.push('/coupons')
}
// 处理取消订单
const handleCancelOrder = async (orderId: number) => {
Modal.confirm({
title: '确认取消订单',
content: '确定要取消这个订单吗?',
onOk: async () => {
try {
await orderApi.cancelOrder(orderId)
message.success('订单已取消')
loadOrders()
} catch (error) {
message.error('取消订单失败')
}
}
})
}
const handleMenuClick = (key: string) => {
if (key === 'wishlist') {
router.push('/wishlist')
} else if (key === 'orders') {
router.push('/orders')
} else if (key === 'coupons') {
router.push('/coupons')
} else if (key === 'address') {
message.info('地址管理功能开发中...')
} else if (key === 'payment') {
message.info('支付方式功能开发中...')
} else if (key === 'notifications') {
message.info('消息通知功能开发中...')
} else if (key === 'help') {
message.info('帮助中心功能开发中...')
}
}
const handleLogout = () => {
Modal.confirm({
title: '确认退出登录',
content: '确定要退出登录吗?',
okText: '确认',
cancelText: '取消',
onOk: async () => {
try {
await authApi.logout()
} catch (error) {
console.error('登出接口调用失败:', error)
} finally {
localStorage.removeItem('accessToken')
localStorage.removeItem('refreshToken')
localStorage.removeItem('userInfo')
localStorage.removeItem('needSetPassword')
sessionStorage.removeItem('accessToken')
message.success('已退出登录')
router.push('/')
}
}
})
}
const handleSettingsClick = () => {
router.push('/settings')
}
const handleSetPassword = () => {
router.push('/settings/password')
}
const handleShopClick = () => {
if (userInfo.value.shopId) {
router.push(`/shop/${userInfo.value.shopId}`)
} else {
message.error('店铺信息不完整')
}
}
// 生成随机颜色
const getRandomColor = () => {
const colors = ['#ffe4d6', '#d6eaff', '#e2ffd6', '#fff2d6', '#d6ffe4', '#e4d6ff']
return colors[Math.floor(Math.random() * colors.length)]
}
onMounted(() => {
fetchUserInfo()
loadOrders()
// 异步加载优惠券数量
loadCouponCount()
// 异步加载收藏数量
loadWishlistCount()
})
// 加载优惠券数量
const loadCouponCount = async () => {
try {
const response = await couponApi.listUserCoupons() as any
couponCount.value = response.length
} catch (error) {
console.error('获取优惠券数量失败:', error)
}
}
// 加载收藏数量
const loadWishlistCount = async () => {
try {
const response = await wishlistApi.getWishlist() as any
wishlistCount.value = response.length
} catch (error) {
console.error('获取收藏数量失败:', error)
}
}
</script>
<style scoped>
.mobile-container {
padding-top: 60px;
min-height: 100vh;
background: #f5f5f5;
position: relative;
}
.header-bg {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 180px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
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;
background: transparent;
color: white;
}
.header h1 {
font-size: 18px;
font-weight: bold;
margin: 0;
}
.settings-btn {
width: 40px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
color: white;
}
.profile-info {
padding: 15px;
position: relative;
z-index: 1;
}
.profile-card {
border-radius: 20px;
overflow: hidden;
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
}
.profile-card :deep(.ant-card-body) {
padding: 28px 24px 24px;
}
.avatar-section {
display: flex;
align-items: center;
gap: 18px;
margin-bottom: 24px;
}
.user-avatar {
border: 4px solid white;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.user-details {
flex: 1;
}
.username {
font-size: 24px;
font-weight: 700;
margin: 0 0 6px 0;
color: #1a1a2e;
}
.email {
color: #8b8b9a;
font-size: 14px;
margin: 0 0 6px 0;
}
.signature {
color: #6b6b7a;
font-size: 13px;
margin: 0;
line-height: 1.6;
}
.shop-section {
margin-bottom: 24px;
}
.shop-card {
display: flex;
align-items: center;
gap: 14px;
padding: 18px;
background: linear-gradient(135deg, #f5f7ff 0%, #e8ecff 100%);
border-radius: 16px;
cursor: pointer;
transition: all 0.3s ease;
}
.shop-card:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.15);
}
.shop-icon-wrapper {
width: 52px;
height: 52px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 14px;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
}
.shop-icon {
font-size: 24px;
color: white;
}
.shop-content {
flex: 1;
}
.shop-label {
font-size: 12px;
color: #8b8b9a;
margin: 0 0 4px 0;
}
.shop-name {
font-size: 18px;
font-weight: 600;
color: #1a1a2e;
margin: 0;
}
.shop-arrow {
color: #c4c4d0;
font-size: 16px;
}
.edit-btn {
height: 48px;
border-radius: 12px;
font-size: 15px;
font-weight: 600;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border: none;
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}
.avatar-upload-section {
display: flex;
flex-direction: column;
align-items: center;
gap: 8px;
}
.upload-hint {
font-size: 12px;
color: #667eea;
margin-top: 4px;
}
.stats-section {
padding: 0 15px 15px;
position: relative;
z-index: 1;
}
.stats-card {
border-radius: 20px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
}
.stats-card :deep(.ant-card-body) {
padding: 20px 16px;
}
.stat-item {
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
}
.stat-icon {
width: 52px;
height: 52px;
border-radius: 14px;
display: flex;
align-items: center;
justify-content: center;
font-size: 24px;
color: white;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.stats-section :deep(.ant-statistic-title) {
font-size: 13px;
color: #8b8b9a;
font-weight: 500;
}
.stats-section :deep(.ant-statistic-content) {
font-size: 22px;
font-weight: 700;
color: #1a1a2e;
}
.menu-section {
padding: 0 15px 15px;
position: relative;
z-index: 1;
}
.menu-card {
border-radius: 20px;
overflow: hidden;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
}
.menu-card :deep(.ant-card-body) {
padding: 0;
}
.menu-item {
padding: 18px 16px;
cursor: pointer;
transition: all 0.3s ease;
border-bottom: 1px solid #f5f5f7;
}
.menu-item:hover {
background: #f8f9ff;
}
.menu-item:last-child {
border-bottom: none;
}
.menu-item :deep(.ant-list-item-action) {
margin-left: 8px;
color: #c4c4d0;
}
.menu-icon {
width: 44px;
height: 44px;
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
font-size: 20px;
color: white;
opacity: 1;
filter: none;
}
.menu-item :deep(.ant-list-item-meta-title) {
font-size: 15px;
font-weight: 600;
color: #1a1a2e;
}
.menu-item :deep(.ant-list-item-meta-description) {
font-size: 12px;
color: #8b8b9a;
margin-top: 4px;
}
.logout-section {
padding: 0 15px 25px;
position: relative;
z-index: 1;
}
.logout-section :deep(.ant-btn-dangerous) {
height: 50px;
border-radius: 25px;
font-weight: 700;
font-size: 17px;
background: linear-gradient(135deg, #ff6b6b 0%, #ee5a6f 100%);
border: none;
box-shadow: 0 4px 15px rgba(238, 90, 111, 0.3);
color: white !important;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}
.content-card {
margin: 0 15px 15px;
border-radius: 20px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
}
.content-card :deep(.ant-card-body) {
padding: 20px;
}
.content-card h3 {
margin: 0 0 20px 0;
font-size: 18px;
font-weight: 600;
color: #1a1a2e;
}
.order-list {
display: flex;
flex-direction: column;
gap: 15px;
}
.order-card {
border-radius: 12px;
overflow: hidden;
}
.order-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
padding-bottom: 10px;
border-bottom: 1px solid #f0f0f0;
}
.order-no {
font-size: 14px;
color: #666;
}
.order-status {
font-size: 14px;
font-weight: 600;
padding: 4px 12px;
border-radius: 12px;
}
.order-status.pending {
background: #fff7e6;
color: #fa8c16;
}
.order-status.completed {
background: #f6ffed;
color: #52c41a;
}
.order-status.cancelled {
background: #fff1f0;
color: #ff4d4f;
}
.order-items {
display: flex;
flex-direction: column;
gap: 10px;
margin-bottom: 15px;
}
.order-item {
display: flex;
gap: 12px;
align-items: center;
}
.item-image {
width: 60px;
height: 60px;
border-radius: 8px;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
font-weight: bold;
color: rgba(0,0,0,0.1);
flex-shrink: 0;
}
.item-image img {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 8px;
}
.item-info {
flex: 1;
display: flex;
flex-direction: column;
gap: 4px;
}
.item-title {
font-size: 14px;
font-weight: 600;
color: #1a1a2e;
}
.item-price {
font-size: 13px;
color: #666;
}
.order-footer {
display: flex;
justify-content: space-between;
align-items: center;
padding-top: 10px;
border-top: 1px solid #f0f0f0;
}
.order-total {
font-size: 14px;
color: #666;
}
.total-price {
font-size: 16px;
font-weight: 700;
color: #ff4d4f;
}
.order-actions {
display: flex;
gap: 8px;
}
.coupon-list {
display: flex;
flex-direction: column;
gap: 15px;
}
.coupon-card {
border-radius: 12px;
overflow: hidden;
background: linear-gradient(135deg, #f5f7ff 0%, #e8ecff 100%);
}
.coupon-content {
display: flex;
align-items: center;
gap: 20px;
}
.coupon-value {
font-size: 24px;
font-weight: 700;
color: #667eea;
padding: 20px;
background: rgba(102, 126, 234, 0.1);
border-radius: 8px;
min-width: 100px;
text-align: center;
}
.coupon-info {
flex: 1;
display: flex;
flex-direction: column;
gap: 8px;
}
.coupon-name {
font-size: 16px;
font-weight: 600;
color: #1a1a2e;
}
.coupon-condition {
font-size: 12px;
color: #667eea;
}
.coupon-time {
font-size: 12px;
color: #8b8b9a;
}
.coupon-status {
font-size: 12px;
font-weight: 600;
padding: 2px 8px;
border-radius: 8px;
align-self: flex-start;
}
.coupon-status.available {
background: #f6ffed;
color: #52c41a;
}
.coupon-status.used {
background: #f5f5f5;
color: #8b8b9a;
}
</style>