121 lines
3.2 KiB
JavaScript
121 lines
3.2 KiB
JavaScript
const app = getApp()
|
|
|
|
Page({
|
|
data: {
|
|
statusBarHeight: 44,
|
|
navBarHeight: 88,
|
|
user: null,
|
|
showBindModal: false,
|
|
bindType: 'phone',
|
|
bindValue: '',
|
|
isBinding: false,
|
|
bindError: ''
|
|
},
|
|
|
|
onLoad() {
|
|
const statusBarHeight = app.globalData.statusBarHeight || 44
|
|
const navBarHeight = app.globalData.navBarHeight || (statusBarHeight + 44)
|
|
this.setData({ statusBarHeight, navBarHeight })
|
|
this.syncUser()
|
|
},
|
|
|
|
onShow() {
|
|
this.syncUser()
|
|
},
|
|
|
|
syncUser() {
|
|
const user = app.globalData.userInfo || null
|
|
this.setData({ user })
|
|
},
|
|
|
|
goBack() {
|
|
wx.navigateBack({ fail: () => wx.switchTab({ url: '/pages/my/my' }) })
|
|
},
|
|
|
|
goAddresses() {
|
|
wx.navigateTo({ url: '/pages/address-list/address-list' })
|
|
},
|
|
|
|
openBindModal(e) {
|
|
const type = e.currentTarget.dataset.type
|
|
const user = this.data.user
|
|
let bindValue = ''
|
|
if (type === 'phone' && user && user.phone) bindValue = user.phone
|
|
if (type === 'wechat' && user && user.wechat) bindValue = user.wechat
|
|
if (type === 'alipay' && user && user.alipay) bindValue = user.alipay
|
|
this.setData({
|
|
showBindModal: true,
|
|
bindType: type,
|
|
bindValue,
|
|
bindError: ''
|
|
})
|
|
},
|
|
|
|
closeBindModal() {
|
|
if (!this.data.isBinding) this.setData({ showBindModal: false, bindValue: '', bindError: '' })
|
|
},
|
|
|
|
onBindInput(e) {
|
|
this.setData({ bindValue: (e.detail && e.detail.value) || '', bindError: '' })
|
|
},
|
|
|
|
submitBind() {
|
|
const { bindType, bindValue, user } = this.data
|
|
if (!bindValue || !bindValue.trim()) {
|
|
this.setData({ bindError: '请输入内容' })
|
|
return
|
|
}
|
|
if (bindType === 'phone' && !/^1[3-9]\d{9}$/.test(bindValue)) {
|
|
this.setData({ bindError: '请输入正确的手机号' })
|
|
return
|
|
}
|
|
if (bindType === 'wechat' && bindValue.length < 6) {
|
|
this.setData({ bindError: '微信号至少6位' })
|
|
return
|
|
}
|
|
if (bindType === 'alipay' && !bindValue.includes('@') && !/^1[3-9]\d{9}$/.test(bindValue)) {
|
|
this.setData({ bindError: '请输入正确的支付宝账号' })
|
|
return
|
|
}
|
|
this.setData({ isBinding: true, bindError: '' })
|
|
app.request('/api/user/update', {
|
|
method: 'POST',
|
|
data: {
|
|
userId: user && user.id,
|
|
[bindType]: bindValue
|
|
}
|
|
}).then(() => {
|
|
const newUser = { ...user, [bindType]: bindValue }
|
|
app.globalData.userInfo = newUser
|
|
wx.setStorageSync('userInfo', newUser)
|
|
this.setData({
|
|
user: newUser,
|
|
showBindModal: false,
|
|
bindValue: '',
|
|
isBinding: false
|
|
})
|
|
wx.showToast({ title: '绑定成功', icon: 'success' })
|
|
}).catch(() => {
|
|
this.setData({ bindError: '绑定失败,请重试', isBinding: false })
|
|
})
|
|
},
|
|
|
|
logout() {
|
|
wx.showModal({
|
|
title: '提示',
|
|
content: '确定退出登录吗?',
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
app.globalData.userInfo = null
|
|
app.globalData.isLoggedIn = false
|
|
app.globalData.purchasedSections = []
|
|
app.globalData.hasFullBook = false
|
|
wx.removeStorageSync('userInfo')
|
|
wx.removeStorageSync('token')
|
|
wx.switchTab({ url: '/pages/index/index' })
|
|
}
|
|
}
|
|
})
|
|
}
|
|
})
|