完善小程序登录排班与个人资料流程
This commit is contained in:
@@ -25,6 +25,8 @@ function buildPageState(profile, fallbackShopName) {
|
||||
form: {
|
||||
nickname: source.nickname || '',
|
||||
avatar: normalizeAvatar(source.avatar),
|
||||
avatarFileId: '',
|
||||
avatarChanged: false,
|
||||
wechatId: source.wechatId || '',
|
||||
signature: source.signature || ''
|
||||
}
|
||||
@@ -41,7 +43,7 @@ Page({
|
||||
roleText: '管理端',
|
||||
shopName: BRAND_CONFIG.defaultShopName,
|
||||
phoneDisplay: '未绑定',
|
||||
form: { nickname: '', avatar: '', wechatId: '', signature: '' }
|
||||
form: { nickname: '', avatar: '', avatarFileId: '', avatarChanged: false, wechatId: '', signature: '' }
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
@@ -116,12 +118,21 @@ Page({
|
||||
async uploadAvatar(filePath) {
|
||||
if (!filePath) return
|
||||
const previousAvatar = this.data.form.avatar
|
||||
const previousAvatarFileId = this.data.form.avatarFileId
|
||||
this.setData({ avatarUploading: true, 'form.avatar': filePath })
|
||||
try {
|
||||
const avatar = await uploadProfileImage(filePath)
|
||||
this.setData({ 'form.avatar': avatar })
|
||||
const uploaded = await uploadProfileImage(filePath)
|
||||
this.setData({
|
||||
'form.avatar': filePath,
|
||||
'form.avatarFileId': uploaded && uploaded.fileId ? String(uploaded.fileId) : '',
|
||||
'form.avatarChanged': true
|
||||
})
|
||||
} catch (error) {
|
||||
this.setData({ 'form.avatar': previousAvatar })
|
||||
this.setData({
|
||||
'form.avatar': previousAvatar,
|
||||
'form.avatarFileId': previousAvatarFileId,
|
||||
'form.avatarChanged': false
|
||||
})
|
||||
throw error
|
||||
} finally {
|
||||
this.setData({ avatarUploading: false })
|
||||
@@ -160,21 +171,26 @@ Page({
|
||||
wx.showToast({ title: '请填写昵称', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (form.avatarChanged && !form.avatarFileId) {
|
||||
wx.showToast({ title: '头像上传未完成,请重新选择', icon: 'none', duration: 3000 })
|
||||
return
|
||||
}
|
||||
this.setData({ saving: true })
|
||||
try {
|
||||
await userApi.updateInfo({
|
||||
const payload = {
|
||||
nickname,
|
||||
avatar: form.avatar || '',
|
||||
wechatId: String(form.wechatId || '').trim(),
|
||||
signature: String(form.signature || '').trim()
|
||||
})
|
||||
}
|
||||
if (form.avatarChanged) payload.avatarFileId = form.avatarFileId || undefined
|
||||
await userApi.updateInfo(payload)
|
||||
profileCache.invalidate()
|
||||
const profile = await profileCache.getProfile({ force: true })
|
||||
this.syncProfileToSession(profile)
|
||||
wx.showToast({ title: '已保存', icon: 'success' })
|
||||
wx.showToast({ title: '已保存', icon: 'success', duration: 3000 })
|
||||
setTimeout(() => wx.navigateBack(), 350)
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '保存失败', icon: 'none' })
|
||||
wx.showToast({ title: error.message || '保存失败', icon: 'none', duration: 3000 })
|
||||
} finally {
|
||||
this.setData({ saving: false })
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
const assert = require('assert')
|
||||
|
||||
let definition
|
||||
let toast
|
||||
let savedPayload
|
||||
|
||||
global.Page = value => { definition = value }
|
||||
global.wx = {
|
||||
showToast: options => { toast = options },
|
||||
navigateBack: () => {}
|
||||
}
|
||||
global.getApp = () => ({ globalData: { userInfo: { id: 7 } }, setSession: () => {} })
|
||||
global.setTimeout = callback => { callback(); return 1 }
|
||||
|
||||
const auth = require('../../utils/auth')
|
||||
auth.getSession = () => ({ userInfo: { id: 7 } })
|
||||
const profileCache = require('../../utils/profile-cache')
|
||||
profileCache.invalidate = () => {}
|
||||
profileCache.getProfile = async () => ({ id: 7, nickname: '员工', avatar: 'profile/7/avatar.png' })
|
||||
const userApi = require('../../api/user')
|
||||
userApi.updateInfo = async payload => { savedPayload = payload }
|
||||
require('./index')
|
||||
|
||||
function createPage(form) {
|
||||
return {
|
||||
...definition,
|
||||
data: { ...definition.data, form: { ...definition.data.form, ...form } },
|
||||
setData(values) {
|
||||
Object.entries(values).forEach(([key, value]) => {
|
||||
const parts = key.split('.')
|
||||
if (parts.length === 1) this.data[key] = value
|
||||
else this.data[parts[0]] = { ...this.data[parts[0]], [parts[1]]: value }
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function run() {
|
||||
const missingFilePage = createPage({ nickname: '員工', avatarChanged: true, avatarFileId: '' })
|
||||
await missingFilePage.save()
|
||||
assert.strictEqual(toast.title, '头像上传未完成,请重新选择')
|
||||
assert.strictEqual(toast.duration, 3000)
|
||||
assert.strictEqual(savedPayload, undefined)
|
||||
|
||||
const validPage = createPage({ nickname: '员工', avatarChanged: true, avatarFileId: '90071992547409931' })
|
||||
await validPage.save()
|
||||
assert.strictEqual(savedPayload.avatarFileId, '90071992547409931')
|
||||
assert.strictEqual(toast.title, '已保存')
|
||||
assert.strictEqual(toast.duration, 3000)
|
||||
}
|
||||
|
||||
run().catch(error => {
|
||||
console.error(error)
|
||||
process.exitCode = 1
|
||||
})
|
||||
Reference in New Issue
Block a user