重构项目文件夹目录
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
const userApi = require('../../api/user')
|
||||
const auth = require('../../utils/auth')
|
||||
const navigation = require('../../utils/navigation')
|
||||
const theme = require('../../utils/theme')
|
||||
const { runWithLoading } = require('../../utils/loading')
|
||||
|
||||
Page({
|
||||
data: Object.assign(theme.pageThemeData(getApp().globalData.shopTheme), {
|
||||
loading: false,
|
||||
saving: false,
|
||||
isLoggedIn: false,
|
||||
userInfo: null,
|
||||
form: {
|
||||
nickname: '',
|
||||
signature: ''
|
||||
}
|
||||
}),
|
||||
|
||||
onShow() {
|
||||
this.loadProfile()
|
||||
},
|
||||
|
||||
handleBack() {
|
||||
navigation.goBack('/pages/settings/index')
|
||||
},
|
||||
|
||||
async loadProfile() {
|
||||
const isLoggedIn = auth.isLoggedIn()
|
||||
this.setData({ isLoggedIn })
|
||||
if (!isLoggedIn) {
|
||||
navigation.openAuth(navigation.getCurrentPageUrl())
|
||||
return
|
||||
}
|
||||
this.setData({ loading: true })
|
||||
try {
|
||||
const userInfo = await userApi.getUserInfo()
|
||||
this.setData({
|
||||
userInfo,
|
||||
'form.nickname': userInfo.nickname || '',
|
||||
'form.signature': userInfo.signature || ''
|
||||
})
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '资料加载失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ loading: false })
|
||||
}
|
||||
},
|
||||
|
||||
handleInput(event) {
|
||||
const field = event.currentTarget.dataset.field
|
||||
this.setData({
|
||||
[`form.${field}`]: event.detail.value
|
||||
})
|
||||
},
|
||||
|
||||
async saveProfile() {
|
||||
const nickname = String(this.data.form.nickname || '').trim()
|
||||
if (!nickname) {
|
||||
wx.showToast({ title: '请输入昵称', icon: 'none' })
|
||||
return
|
||||
}
|
||||
this.setData({ saving: true })
|
||||
try {
|
||||
await runWithLoading(() => userApi.updateUserInfo({
|
||||
nickname,
|
||||
signature: String(this.data.form.signature || '').trim()
|
||||
}))
|
||||
wx.showToast({ title: '资料已更新', icon: 'none' })
|
||||
await this.loadProfile()
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '资料更新失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ saving: false })
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user