179 lines
5.5 KiB
JavaScript
179 lines
5.5 KiB
JavaScript
const userApi = require('../../api/user')
|
|
const auth = require('../../utils/auth')
|
|
const { chooseImageFilePath, uploadProfileImage } = require('../../utils/upload')
|
|
const { buildProfileCard, normalizeAvatar } = require('../../utils/profile-card')
|
|
const { BRAND_CONFIG } = require('../../utils/config')
|
|
const profileCache = require('../../utils/profile-cache')
|
|
|
|
function resolveRoleText(type) {
|
|
const role = Number(type)
|
|
if (role === 2) return '店长'
|
|
if (role === 3) return '美甲师'
|
|
if (role === 4) return '管理员'
|
|
return '管理端'
|
|
}
|
|
|
|
function buildPageState(profile, fallbackShopName) {
|
|
const source = profile || {}
|
|
const card = buildProfileCard(source, fallbackShopName)
|
|
return {
|
|
profile: source,
|
|
avatarText: card.avatarText,
|
|
roleText: resolveRoleText(source.type),
|
|
shopName: card.shopName,
|
|
phoneDisplay: source.maskedPhone || source.phone || '未绑定',
|
|
form: {
|
|
nickname: source.nickname || '',
|
|
avatar: normalizeAvatar(source.avatar),
|
|
wechatId: source.wechatId || '',
|
|
signature: source.signature || ''
|
|
}
|
|
}
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
loading: true,
|
|
saving: false,
|
|
avatarUploading: false,
|
|
profile: {},
|
|
avatarText: '用',
|
|
roleText: '管理端',
|
|
shopName: BRAND_CONFIG.defaultShopName,
|
|
phoneDisplay: '未绑定',
|
|
form: { nickname: '', avatar: '', wechatId: '', signature: '' }
|
|
},
|
|
|
|
onLoad() {
|
|
const app = getApp()
|
|
const state = buildPageState(app.globalData.userInfo || {}, app.globalData.shopName || BRAND_CONFIG.defaultShopName)
|
|
this.setData(state)
|
|
this.loadProfile()
|
|
},
|
|
|
|
onShow() {
|
|
if (!this._refreshAfterAccountSettings) return
|
|
this._refreshAfterAccountSettings = false
|
|
this.loadProfile()
|
|
},
|
|
|
|
async loadProfile(options = {}) {
|
|
this.setData({ loading: true })
|
|
try {
|
|
const profile = await profileCache.getProfile(options)
|
|
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 current = session.userInfo || app.globalData.userInfo || {}
|
|
const next = {
|
|
...current,
|
|
...profile,
|
|
nickname: profile.nickname || '',
|
|
avatar: profile.avatar || '',
|
|
contactPhone: profile.contactPhone || '',
|
|
wechatId: profile.wechatId || '',
|
|
signature: profile.signature || '',
|
|
shopName: profile.shopName || current.shopName || session.shopName || app.globalData.shopName || ''
|
|
}
|
|
app.setSession({ ...session, shopName: next.shopName || session.shopName, userInfo: next })
|
|
this.setData(buildPageState(next, app.globalData.shopName || BRAND_CONFIG.defaultShopName))
|
|
},
|
|
|
|
onInput(event) {
|
|
const field = event.currentTarget.dataset.field
|
|
const value = event.detail.value
|
|
const nextData = { [`form.${field}`]: value }
|
|
if (field === 'nickname') {
|
|
nextData.avatarText = Array.from(String(value || '').trim())[0] || '用'
|
|
}
|
|
this.setData(nextData)
|
|
},
|
|
|
|
onWechatNicknameInput(event) {
|
|
const nickname = event.detail.value
|
|
this.setData({
|
|
'form.nickname': nickname,
|
|
avatarText: Array.from(String(nickname || '').trim())[0] || '用'
|
|
})
|
|
},
|
|
|
|
goAccountSettings() {
|
|
this._refreshAfterAccountSettings = true
|
|
wx.navigateTo({
|
|
url: '/pages/account-settings/index',
|
|
fail: () => { this._refreshAfterAccountSettings = false }
|
|
})
|
|
},
|
|
|
|
async uploadAvatar(filePath) {
|
|
if (!filePath) return
|
|
const previousAvatar = this.data.form.avatar
|
|
this.setData({ avatarUploading: true, 'form.avatar': filePath })
|
|
try {
|
|
const avatar = await uploadProfileImage(filePath)
|
|
this.setData({ 'form.avatar': avatar })
|
|
} catch (error) {
|
|
this.setData({ 'form.avatar': previousAvatar })
|
|
throw error
|
|
} finally {
|
|
this.setData({ avatarUploading: false })
|
|
}
|
|
},
|
|
|
|
async chooseAvatar() {
|
|
if (this.data.loading || this.data.avatarUploading || this.data.saving) return
|
|
try {
|
|
const filePath = await chooseImageFilePath()
|
|
if (!filePath) return
|
|
await this.uploadAvatar(filePath)
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message || '头像上传失败', icon: 'none' })
|
|
}
|
|
},
|
|
|
|
async chooseWechatAvatar(event) {
|
|
if (this.data.loading || this.data.avatarUploading || this.data.saving) return
|
|
try {
|
|
await this.uploadAvatar(event.detail && event.detail.avatarUrl)
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message || '微信头像获取失败', icon: 'none' })
|
|
}
|
|
},
|
|
|
|
async save() {
|
|
if (this.data.saving || this.data.avatarUploading) return
|
|
const form = this.data.form
|
|
const nickname = String(form.nickname || '').trim()
|
|
if (!nickname) {
|
|
wx.showToast({ title: '请填写昵称', icon: 'none' })
|
|
return
|
|
}
|
|
this.setData({ saving: true })
|
|
try {
|
|
await userApi.updateInfo({
|
|
nickname,
|
|
avatar: form.avatar || '',
|
|
wechatId: String(form.wechatId || '').trim(),
|
|
signature: String(form.signature || '').trim()
|
|
})
|
|
profileCache.invalidate()
|
|
const profile = await profileCache.getProfile({ force: true })
|
|
this.syncProfileToSession(profile)
|
|
wx.showToast({ title: '已保存', icon: 'success' })
|
|
setTimeout(() => wx.navigateBack(), 350)
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message || '保存失败', icon: 'none' })
|
|
} finally {
|
|
this.setData({ saving: false })
|
|
}
|
|
}
|
|
})
|