功能:新增微信小程序界面
This commit is contained in:
@@ -0,0 +1,244 @@
|
||||
const authApi = require('../../api/auth')
|
||||
const userApi = require('../../api/user')
|
||||
const auth = require('../../utils/auth')
|
||||
const navigation = require('../../utils/navigation')
|
||||
const { DEFAULT_AVATAR } = require('../../utils/config')
|
||||
const { runWithLoading } = require('../../utils/loading')
|
||||
|
||||
Page({
|
||||
data: {
|
||||
isLoggedIn: false,
|
||||
loading: false,
|
||||
savingProfile: false,
|
||||
updatingPassword: false,
|
||||
countdown: 0,
|
||||
userInfo: null,
|
||||
defaultAvatar: DEFAULT_AVATAR,
|
||||
form: {
|
||||
nickname: '',
|
||||
signature: ''
|
||||
},
|
||||
passwordForm: {
|
||||
verificationCode: '',
|
||||
password: ''
|
||||
}
|
||||
},
|
||||
|
||||
onShow() {
|
||||
this.loadSettings()
|
||||
},
|
||||
|
||||
async loadSettings() {
|
||||
const isLoggedIn = auth.isLoggedIn()
|
||||
this.setData({
|
||||
isLoggedIn,
|
||||
loading: isLoggedIn
|
||||
})
|
||||
|
||||
if (!isLoggedIn) {
|
||||
this.setData({
|
||||
userInfo: null,
|
||||
loading: false
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const userInfo = await userApi.getUserInfo()
|
||||
getApp().refreshUserInfo(userInfo)
|
||||
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
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
handleBack() {
|
||||
navigation.goBack('/pages/profile/index')
|
||||
},
|
||||
|
||||
handleOpenAuth() {
|
||||
navigation.openAuth('/pages/settings/index')
|
||||
},
|
||||
|
||||
handleFormInput(event) {
|
||||
const field = event.currentTarget.dataset.field
|
||||
this.setData({
|
||||
[`form.${field}`]: event.detail.value
|
||||
})
|
||||
},
|
||||
|
||||
handlePasswordInput(event) {
|
||||
const field = event.currentTarget.dataset.field
|
||||
this.setData({
|
||||
[`passwordForm.${field}`]: event.detail.value
|
||||
})
|
||||
},
|
||||
|
||||
async handleSaveProfile() {
|
||||
if (!this.data.form.nickname.trim()) {
|
||||
wx.showToast({
|
||||
title: '请输入昵称',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
this.setData({
|
||||
savingProfile: true
|
||||
})
|
||||
|
||||
try {
|
||||
await runWithLoading(() => userApi.updateUserInfo({
|
||||
nickname: this.data.form.nickname.trim(),
|
||||
signature: this.data.form.signature.trim()
|
||||
}))
|
||||
wx.showToast({
|
||||
title: '资料已更新',
|
||||
icon: 'none'
|
||||
})
|
||||
this.loadSettings()
|
||||
} catch (error) {
|
||||
wx.showToast({
|
||||
title: error.message || '资料更新失败',
|
||||
icon: 'none'
|
||||
})
|
||||
} finally {
|
||||
this.setData({
|
||||
savingProfile: false
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
startCountdown() {
|
||||
this.setData({
|
||||
countdown: 60
|
||||
})
|
||||
|
||||
const timer = setInterval(() => {
|
||||
const next = this.data.countdown - 1
|
||||
this.setData({
|
||||
countdown: next
|
||||
})
|
||||
if (next <= 0) {
|
||||
clearInterval(timer)
|
||||
}
|
||||
}, 1000)
|
||||
},
|
||||
|
||||
async handleSendPasswordCode() {
|
||||
if (this.data.countdown > 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const userInfo = this.data.userInfo
|
||||
if (!userInfo || !userInfo.phone) {
|
||||
wx.showToast({
|
||||
title: '当前账号未绑定手机号',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await runWithLoading(() => authApi.sendVerificationCode({
|
||||
phone: userInfo.phone
|
||||
}))
|
||||
wx.showToast({
|
||||
title: '验证码已发送',
|
||||
icon: 'none'
|
||||
})
|
||||
this.startCountdown()
|
||||
} catch (error) {
|
||||
wx.showToast({
|
||||
title: error.message || '验证码发送失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
async handleUpdatePassword() {
|
||||
if (!this.data.passwordForm.verificationCode.trim()) {
|
||||
wx.showToast({
|
||||
title: '请输入验证码',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
if (!this.data.passwordForm.password || this.data.passwordForm.password.length < 6) {
|
||||
wx.showToast({
|
||||
title: '新密码至少 6 位',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
this.setData({
|
||||
updatingPassword: true
|
||||
})
|
||||
|
||||
try {
|
||||
await runWithLoading(() => userApi.updatePassword({
|
||||
password: this.data.passwordForm.password,
|
||||
verificationCode: this.data.passwordForm.verificationCode.trim()
|
||||
}))
|
||||
auth.setNeedSetPassword(false)
|
||||
wx.showToast({
|
||||
title: '密码已更新',
|
||||
icon: 'none'
|
||||
})
|
||||
this.setData({
|
||||
passwordForm: {
|
||||
verificationCode: '',
|
||||
password: ''
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
wx.showToast({
|
||||
title: error.message || '密码更新失败',
|
||||
icon: 'none'
|
||||
})
|
||||
} finally {
|
||||
this.setData({
|
||||
updatingPassword: false
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
async handleLogout() {
|
||||
const confirmed = await new Promise(resolve => {
|
||||
wx.showModal({
|
||||
title: '退出登录',
|
||||
content: '退出后需要重新登录才能查看订单和继续下单。',
|
||||
success: res => resolve(Boolean(res.confirm))
|
||||
})
|
||||
})
|
||||
|
||||
if (!confirmed) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await runWithLoading(() => authApi.logout())
|
||||
} catch (error) {}
|
||||
|
||||
getApp().clearUserSession()
|
||||
wx.showToast({
|
||||
title: '已退出登录',
|
||||
icon: 'none'
|
||||
})
|
||||
wx.switchTab({
|
||||
url: '/pages/home/index'
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user