Files
JayuCloud/JY-Salon-WeChat/pages/profile/index.js
T

280 lines
7.8 KiB
JavaScript

const { BRAND_CONFIG } = require('../../utils/config')
const userApi = require('../../api/user')
const shopApi = require('../../api/shop')
const authApi = require('../../api/auth')
const auth = require('../../utils/auth')
const { chooseImageFilePath, uploadAvatar } = require('../../utils/upload')
const DEFAULT_BACKEND_AVATAR_URL = 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
function resolveRoleText(type) {
const role = Number(type)
if (role === 2) return '店长'
if (role === 3) return '美甲师'
if (role === 4) return '管理员'
return '管理端'
}
function normalizeAvatarUrl(avatar) {
const value = String(avatar || '').trim()
if (!value) return ''
return value.split('?')[0]
}
function resolveDisplayAvatar(avatar) {
const value = String(avatar || '').trim()
if (!value) return ''
if (normalizeAvatarUrl(value) === DEFAULT_BACKEND_AVATAR_URL) return ''
return value
}
function resolveShopCoverImage(settings) {
if (!settings || !Array.isArray(settings.coverImageUrls)) {
return ''
}
return String(settings.coverImageUrls[0] || '').trim()
}
Page({
data: {
shopName: BRAND_CONFIG.defaultShopName,
shopCoverImage: '',
phone: '店长端',
roleText: '管理端',
userInfo: {},
editModalVisible: false,
profileSaving: false,
avatarUploading: false,
loggingOut: false,
profileForm: {
nickname: '',
avatar: '',
contactPhone: '',
wechatId: '',
signature: ''
},
menus: [
{ title: '个人信息', action: 'editProfile' },
{ title: '店铺设置', url: '/pages/shop-settings/index' },
{ title: '数据导出', url: '/pages/export/index' },
{ title: '修改密码', url: '/pages/password/index' },
{ title: '关于系统', url: '/pages/about/index' },
{ title: '退出登录', action: 'logout', danger: true }
]
},
onShow() {
this.refreshFromSession()
this.loadShopHeader()
this.loadProfile()
},
refreshFromSession() {
const app = getApp()
const userInfo = app.globalData.userInfo || {}
const displayAvatar = resolveDisplayAvatar(userInfo.avatar)
this.setData({
shopName: app.globalData.shopName || BRAND_CONFIG.appName,
shopCoverImage: app.globalData.shopCoverImage || '',
phone: userInfo.contactPhone || userInfo.phone || userInfo.account || '店长端',
roleText: resolveRoleText(userInfo.type),
userInfo: {
...userInfo,
displayAvatar
}
})
},
async loadShopHeader() {
try {
const settings = await shopApi.settings()
const app = getApp()
const shopName = settings.shopName || settings.name || app.globalData.shopName || BRAND_CONFIG.appName
const shopCoverImage = resolveShopCoverImage(settings)
app.globalData.shopName = shopName
app.globalData.shopCoverImage = shopCoverImage
this.setData({
shopName,
shopCoverImage
})
} catch (error) {
wx.showToast({ title: error.message || '店铺信息加载失败', icon: 'none' })
}
},
go(event) {
const action = event.currentTarget.dataset.action
if (action === 'editProfile') {
this.openEditProfile()
return
}
if (action === 'logout') {
this.confirmLogout()
return
}
wx.navigateTo({ url: event.currentTarget.dataset.url })
},
confirmLogout() {
if (this.data.loggingOut) {
return
}
wx.showModal({
title: '退出登录',
content: '确认退出当前账号吗?',
confirmText: '退出',
confirmColor: '#d98d8d',
success: res => {
if (res.confirm) {
this.logout()
}
}
})
},
async logout() {
if (this.data.loggingOut) {
return
}
this.setData({ loggingOut: true })
try {
await authApi.logout()
const app = getApp()
if (app && typeof app.clearSession === 'function') {
app.clearSession()
} else {
auth.clearSession()
}
wx.reLaunch({ url: '/pages/login/index' })
} catch (error) {
wx.showToast({ title: error.message || '退出登录失败', icon: 'none' })
} finally {
this.setData({ loggingOut: false })
}
},
async loadProfile() {
try {
const profile = await userApi.info()
this.syncProfileToSession(profile)
} catch (error) {
wx.showToast({ title: error.message || '个人信息加载失败', icon: 'none' })
}
},
syncProfileToSession(profile) {
const app = getApp()
const session = auth.getSession()
const currentUserInfo = session.userInfo || app.globalData.userInfo || {}
const nextUserInfo = {
...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,
displayAvatar: resolveDisplayAvatar(profile.avatar || '')
}
app.setSession({
...session,
userInfo: nextUserInfo
})
this.setData({
userInfo: nextUserInfo,
phone: nextUserInfo.contactPhone || nextUserInfo.phone || nextUserInfo.account || '店长端',
roleText: resolveRoleText(nextUserInfo.type)
})
},
openEditProfile() {
const userInfo = this.data.userInfo || {}
this.setData({
editModalVisible: true,
profileForm: {
nickname: userInfo.nickname || '',
avatar: userInfo.displayAvatar || '',
contactPhone: userInfo.contactPhone || '',
wechatId: userInfo.wechatId || '',
signature: userInfo.signature || ''
}
})
},
closeEditProfile() {
if (this.data.profileSaving || this.data.avatarUploading) {
return
}
this.setData({ editModalVisible: false })
},
stopTap() {
},
onProfileInput(event) {
const field = event.currentTarget.dataset.field
this.setData({
[`profileForm.${field}`]: event.detail.value
})
},
async chooseProfileAvatar() {
if (this.data.avatarUploading || this.data.profileSaving) {
return
}
try {
const filePath = await chooseImageFilePath()
if (!filePath) {
return
}
this.setData({ avatarUploading: true })
const avatar = await uploadAvatar(filePath)
const profile = await userApi.info()
this.syncProfileToSession({
...profile,
avatar: profile.avatar || avatar
})
this.setData({ 'profileForm.avatar': resolveDisplayAvatar(profile.avatar || avatar) })
} catch (error) {
wx.showToast({ title: error.message || '头像上传失败', icon: 'none' })
} finally {
this.setData({ avatarUploading: false })
}
},
removeProfileAvatar() {
this.setData({ 'profileForm.avatar': '' })
},
async saveProfile() {
const form = this.data.profileForm
const nickname = String(form.nickname || '').trim()
if (!nickname) {
wx.showToast({ title: '请填写昵称', icon: 'none' })
return
}
this.setData({ profileSaving: true })
try {
await userApi.updateInfo({
nickname,
avatar: form.avatar || '',
contactPhone: String(form.contactPhone || '').trim(),
wechatId: String(form.wechatId || '').trim(),
signature: String(form.signature || '').trim()
})
const profile = await userApi.info()
this.syncProfileToSession(profile)
this.setData({ editModalVisible: false })
wx.showToast({ title: '已保存', icon: 'success' })
} catch (error) {
wx.showToast({ title: error.message || '保存失败', icon: 'none' })
} finally {
this.setData({ profileSaving: false })
}
}
})