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

182 lines
5.1 KiB
JavaScript

const { BRAND_CONFIG } = require('../../utils/config')
const userApi = require('../../api/user')
const auth = require('../../utils/auth')
const { chooseImageFilePath, uploadProfileImage } = require('../../utils/upload')
function resolveRoleText(type) {
const role = Number(type)
if (role === 2) return '店长'
if (role === 3) return '美甲师'
if (role === 4) return '管理员'
return '管理端'
}
Page({
data: {
shopName: BRAND_CONFIG.defaultShopName,
phone: '店长端',
roleText: '管理端',
userInfo: {},
editModalVisible: false,
profileSaving: false,
avatarUploading: 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' }
]
},
onShow() {
this.refreshFromSession()
this.loadProfile()
},
refreshFromSession() {
const app = getApp()
const userInfo = app.globalData.userInfo || {}
this.setData({
shopName: app.globalData.shopName || BRAND_CONFIG.appName,
phone: userInfo.contactPhone || userInfo.phone || userInfo.account || '店长端',
roleText: resolveRoleText(userInfo.type),
userInfo
})
},
go(event) {
const action = event.currentTarget.dataset.action
if (action === 'editProfile') {
this.openEditProfile()
return
}
wx.navigateTo({ url: event.currentTarget.dataset.url })
},
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
}
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.avatar || '',
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 uploadProfileImage(filePath)
this.setData({ 'profileForm.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 })
}
}
})