Files
JayuCloud/JY-Salon-Front/src/views/ProfileView.vue
T

381 lines
9.8 KiB
Vue

<template>
<MobileShell>
<section class="card profile-card">
<div class="profile-main">
<div class="shop-logo">指</div>
<div class="profile-copy">
<strong>{{ shopName }}</strong>
<span>{{ phoneText }}</span>
</div>
</div>
<span class="role-badge">{{ roleText }}</span>
</section>
<section class="card menu-list section">
<button v-for="item in menus" :key="item.title" class="menu-row" type="button" @click="go(item)">
<span>{{ item.title }}</span>
<ChevronRight :size="18" aria-hidden="true" />
</button>
</section>
<section class="card logout-section section">
<button class="logout-btn" type="button" :disabled="logoutLoading" @click="logout">
{{ logoutLoading ? '退出中...' : '退出登录' }}
</button>
</section>
<BaseModal v-model="editModalVisible" title="个人信息" :close-disabled="profileSaving || avatarUploading">
<div class="profile-avatar-row">
<img v-if="profileForm.avatar" class="profile-avatar profile-avatar--image" :src="profileForm.avatar" alt="头像" />
<div v-else class="profile-avatar">头像</div>
<label class="secondary-btn avatar-btn">
{{ avatarUploading ? '上传中...' : '上传头像' }}
<input class="hidden-file" type="file" accept="image/*" :disabled="avatarUploading || profileSaving" @change="uploadAvatar" />
</label>
<button v-if="profileForm.avatar" class="text-btn" type="button" @click="profileForm.avatar = ''">移除</button>
</div>
<div class="profile-form">
<label class="field">
<span class="field-label required">昵称</span>
<input v-model.trim="profileForm.nickname" placeholder="请输入昵称" />
</label>
<div class="field readonly-field">
<span class="field-label">登录账号</span>
<strong>{{ userInfo?.account || '未设置' }}</strong>
</div>
<div class="field readonly-field">
<span class="field-label">身份</span>
<strong>{{ roleText }}</strong>
</div>
<div class="field readonly-field">
<span class="field-label">登录手机号</span>
<strong>{{ userInfo?.phone || '未绑定' }}</strong>
</div>
<label class="field">
<span class="field-label">联系电话</span>
<input v-model.trim="profileForm.contactPhone" inputmode="tel" placeholder="请输入联系电话" />
</label>
<label class="field">
<span class="field-label">微信号</span>
<input v-model.trim="profileForm.wechatId" placeholder="请输入微信号" />
</label>
<label class="field field--stack">
<span class="field-label">签名</span>
<textarea v-model.trim="profileForm.signature" placeholder="请输入个人签名"></textarea>
</label>
</div>
<template #footer>
<button class="primary-btn" type="button" :disabled="profileSaving || avatarUploading" @click="saveProfile">
{{ profileSaving ? '保存中...' : '保存个人信息' }}
</button>
</template>
</BaseModal>
</MobileShell>
</template>
<script setup lang="ts">
import { computed, onMounted, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ChevronRight } from 'lucide-vue-next'
import BaseModal from '@/components/BaseModal.vue'
import MobileShell from '@/components/MobileShell.vue'
import { authApi } from '@/api/auth'
import { fileApi } from '@/api/file'
import { userApi } from '@/api/user'
import { useSessionStore } from '@/stores/session'
import { useToastStore } from '@/stores/toast'
import type { UserInfo } from '@/types/common'
interface ProfileMenu {
title: string
url?: string
action?: 'editProfile'
}
const router = useRouter()
const session = useSessionStore()
const toast = useToastStore()
const editModalVisible = ref(false)
const profileSaving = ref(false)
const avatarUploading = ref(false)
const logoutLoading = ref(false)
const userInfo = ref<UserInfo | null>(session.userInfo)
const profileForm = reactive({
nickname: '',
avatar: '',
contactPhone: '',
wechatId: '',
signature: ''
})
const menus: ProfileMenu[] = [
{ title: '个人信息', action: 'editProfile' },
{ title: '店铺设置', url: '/shop-settings' },
{ title: '数据导出', url: '/export' },
{ title: '修改密码', url: '/password' },
{ title: '关于系统', url: '/about' }
]
const roleText = computed(() => {
const role = Number(userInfo.value?.type)
if (role === 2) return '店长'
if (role === 3) return '美甲师'
if (role === 4) return '管理员'
return '管理端'
})
const shopName = computed(() => userInfo.value?.shopName || '甲屿云')
const phoneText = computed(() => userInfo.value?.contactPhone || userInfo.value?.phone || userInfo.value?.account || '店长端')
const syncProfileToSession = (profile: UserInfo): void => {
const currentUserInfo: Partial<UserInfo> = session.userInfo || {}
const nextUserInfo: UserInfo = {
...currentUserInfo,
id: profile.id || currentUserInfo.id || '',
account: profile.account || currentUserInfo.account,
nickname: profile.nickname || '',
avatar: profile.avatar || '',
phone: profile.phone || currentUserInfo.phone || '',
contactPhone: profile.contactPhone || '',
wechatId: profile.wechatId || '',
signature: profile.signature || '',
type: profile.type || currentUserInfo.type,
shopId: profile.shopId || currentUserInfo.shopId,
shopName: profile.shopName || currentUserInfo.shopName
}
session.patchUserInfo(nextUserInfo)
userInfo.value = nextUserInfo
}
const loadProfile = async (): Promise<void> => {
try {
const profile = await userApi.info()
syncProfileToSession(profile)
} catch (error) {
toast.error(error instanceof Error ? error.message : '个人信息加载失败')
}
}
const openEditProfile = (): void => {
const profile: Partial<UserInfo> = userInfo.value || {}
profileForm.nickname = profile.nickname || ''
profileForm.avatar = profile.avatar || ''
profileForm.contactPhone = profile.contactPhone || ''
profileForm.wechatId = profile.wechatId || ''
profileForm.signature = profile.signature || ''
editModalVisible.value = true
}
const go = async (item: ProfileMenu): Promise<void> => {
if (item.action === 'editProfile') {
openEditProfile()
return
}
if (item.url) {
await router.push(item.url)
}
}
const logout = async (): Promise<void> => {
if (logoutLoading.value) {
return
}
logoutLoading.value = true
try {
await authApi.logout()
} catch (error) {
const message = error instanceof Error && error.message ? error.message : '服务端退出失败,已清除本地登录状态'
if (!toast.visible || toast.message !== message) {
toast.error(message)
}
} finally {
session.clear()
await router.replace('/login')
}
}
const uploadAvatar = async (event: Event): Promise<void> => {
const target = event.target as HTMLInputElement
const file = target.files?.[0]
target.value = ''
if (!file) {
return
}
avatarUploading.value = true
try {
profileForm.avatar = await fileApi.uploadProfileImage(file)
} finally {
avatarUploading.value = false
}
}
const saveProfile = async (): Promise<void> => {
const nickname = profileForm.nickname.trim()
if (!nickname) {
toast.error('请填写昵称')
return
}
profileSaving.value = true
try {
await userApi.update({
nickname,
avatar: profileForm.avatar,
contactPhone: profileForm.contactPhone.trim(),
wechatId: profileForm.wechatId.trim(),
signature: profileForm.signature.trim()
})
const profile = await userApi.info()
syncProfileToSession(profile)
editModalVisible.value = false
toast.success('已保存')
} finally {
profileSaving.value = false
}
}
onMounted(loadProfile)
</script>
<style scoped>
.profile-card {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
padding: 18px;
}
.profile-main {
display: flex;
align-items: center;
min-width: 0;
gap: 12px;
}
.shop-logo,
.profile-avatar {
display: grid;
place-items: center;
background: var(--accent-soft);
color: var(--accent);
font-weight: 900;
}
.shop-logo {
flex: 0 0 48px;
width: 48px;
height: 48px;
border-radius: 18px;
font-size: 22px;
}
.profile-copy {
display: grid;
min-width: 0;
gap: 4px;
}
.profile-copy strong,
.profile-copy span {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.profile-copy span {
color: var(--muted);
font-size: 12px;
}
.role-badge {
flex: 0 0 auto;
padding: 5px 10px;
border-radius: 999px;
background: var(--subtle);
color: #6b645c;
font-size: 12px;
font-weight: 800;
}
.menu-list {
overflow: hidden;
}
.menu-row {
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
min-height: 54px;
padding: 0 16px;
border-bottom: 1px solid var(--border);
color: #3e3932;
font-weight: 750;
}
.menu-row:last-child {
border-bottom: 0;
}
.logout-section {
padding: 0;
overflow: hidden;
}
.logout-btn {
width: 100%;
min-height: 54px;
color: #c4483c;
font-weight: 800;
}
.logout-btn:disabled {
opacity: 0.6;
}
.profile-avatar-row {
display: flex;
align-items: center;
gap: 10px;
padding: 14px 16px;
}
.profile-avatar {
flex: 0 0 54px;
width: 54px;
height: 54px;
border-radius: 18px;
object-fit: cover;
}
.profile-avatar--image {
background: var(--subtle);
}
.avatar-btn {
position: relative;
width: auto;
min-width: 108px;
}
.hidden-file {
position: absolute;
inset: 0;
opacity: 0;
}
.profile-form {
padding: 0 16px 12px;
}
.readonly-field strong {
overflow: hidden;
color: #504a43;
font-size: 13px;
font-weight: 700;
text-overflow: ellipsis;
white-space: nowrap;
}
</style>