标记店务小程序版本更新
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
const { BRAND_CONFIG } = require('../../utils/config')
|
||||
const userApi = require('../../api/user')
|
||||
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 buildProfileForm(profile) {
|
||||
return {
|
||||
nickname: profile.nickname || '',
|
||||
avatar: resolveDisplayAvatar(profile.avatar || profile.displayAvatar || ''),
|
||||
contactPhone: profile.contactPhone || '',
|
||||
wechatId: profile.wechatId || '',
|
||||
signature: profile.signature || ''
|
||||
}
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
shopName: BRAND_CONFIG.defaultShopName,
|
||||
roleText: '管理端',
|
||||
userInfo: {},
|
||||
loading: false,
|
||||
profileSaving: false,
|
||||
avatarUploading: false,
|
||||
profileForm: buildProfileForm({})
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.refreshFromSession()
|
||||
this.loadProfile()
|
||||
},
|
||||
|
||||
refreshFromSession() {
|
||||
const app = getApp()
|
||||
const userInfo = app.globalData.userInfo || {}
|
||||
const nextUserInfo = {
|
||||
...userInfo,
|
||||
displayAvatar: resolveDisplayAvatar(userInfo.avatar)
|
||||
}
|
||||
this.setData({
|
||||
shopName: app.globalData.shopName || BRAND_CONFIG.appName,
|
||||
roleText: resolveRoleText(nextUserInfo.type),
|
||||
userInfo: nextUserInfo,
|
||||
profileForm: buildProfileForm(nextUserInfo)
|
||||
})
|
||||
},
|
||||
|
||||
async loadProfile() {
|
||||
this.setData({ loading: true })
|
||||
try {
|
||||
const profile = await userApi.info()
|
||||
this.syncProfileToSession(profile)
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '个人信息加载失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ loading: false })
|
||||
}
|
||||
},
|
||||
|
||||
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,
|
||||
roleText: resolveRoleText(nextUserInfo.type),
|
||||
profileForm: buildProfileForm(nextUserInfo)
|
||||
})
|
||||
},
|
||||
|
||||
onProfileInput(event) {
|
||||
const field = event.currentTarget.dataset.field
|
||||
this.setData({
|
||||
[`profileForm.${field}`]: event.detail.value
|
||||
})
|
||||
},
|
||||
|
||||
async chooseProfileAvatar() {
|
||||
if (this.data.loading || 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() {
|
||||
if (this.data.avatarUploading || this.data.profileSaving) {
|
||||
return
|
||||
}
|
||||
this.setData({ 'profileForm.avatar': '' })
|
||||
},
|
||||
|
||||
async saveProfile() {
|
||||
if (this.data.loading || this.data.profileSaving || this.data.avatarUploading) {
|
||||
return
|
||||
}
|
||||
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)
|
||||
wx.showToast({ title: '已保存', icon: 'success' })
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '保存失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ profileSaving: false })
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user