- Add loadUserInfo() calling /system/user/getAppUserInfo for user profile - Show real-name verification badge (已认证/未认证) - Show balance card with amount from API - Add menu: 我的钱包, 我的订单, 联系客服, 关于 - Clean up dead code and unused lifecycle hooks - Refresh user info on every onShow when logged in Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
146 lines
3.2 KiB
JavaScript
146 lines
3.2 KiB
JavaScript
const app = getApp()
|
|
const { request } = require('../../utils/request')
|
|
|
|
Page({
|
|
|
|
data: {
|
|
phone: wx.getStorageSync('phone'),
|
|
uidFlag: wx.getStorageSync('uidFlag'),
|
|
logging: false,
|
|
// 用户信息
|
|
userInfo: null,
|
|
balance: '0.00',
|
|
isAuth: false,
|
|
authName: ''
|
|
},
|
|
|
|
onLoad: function () {
|
|
this.setData(getApp().getNavBarHeight());
|
|
},
|
|
|
|
onShow: function () {
|
|
if (!this.data.uidFlag) {
|
|
this.getLogin()
|
|
} else {
|
|
this.loadUserInfo()
|
|
}
|
|
},
|
|
|
|
// 微信登录获取 code
|
|
getLogin() {
|
|
wx.login({
|
|
success: res => {
|
|
app.globalData.code = res.code
|
|
this.setData({ code: res.code })
|
|
}
|
|
})
|
|
},
|
|
|
|
// 拉取用户信息(含实名状态、余额)
|
|
async loadUserInfo() {
|
|
try {
|
|
var res = await request({
|
|
url: '/system/user/getAppUserInfo',
|
|
method: 'GET',
|
|
loading: false,
|
|
showError: false
|
|
})
|
|
if (res.code === 200 && res.data) {
|
|
var d = res.data;
|
|
this.setData({
|
|
userInfo: d,
|
|
balance: d.yue || '0.00',
|
|
isAuth: d.attestation === '1',
|
|
authName: d.name || ''
|
|
});
|
|
// 同步头像/昵称到缓存
|
|
if (d.nickName) wx.setStorageSync('nickName', d.nickName);
|
|
}
|
|
} catch (e) {}
|
|
},
|
|
|
|
// 获取电话号码并登录
|
|
async getPhoneNumber(e) {
|
|
if (this.data.logging) return
|
|
this.setData({ logging: true })
|
|
|
|
try {
|
|
var res = await request({
|
|
url: '/weixinLogin',
|
|
method: 'POST',
|
|
loading: true,
|
|
loadingText: '登录中...',
|
|
data: {
|
|
encryptedData: e.detail.encryptedData,
|
|
iv: e.detail.iv,
|
|
wxcode: app.globalData.code
|
|
}
|
|
})
|
|
|
|
app.globalData.phone = res.data.phoneNumber;
|
|
wx.setStorageSync('phone', res.data.phoneNumber);
|
|
wx.setStorageSync('uid', res.data.userId);
|
|
wx.setStorageSync('token', res.data.token);
|
|
wx.setStorageSync('uidFlag', true);
|
|
|
|
this.setData({
|
|
uidFlag: true,
|
|
phone: res.data.phoneNumber
|
|
})
|
|
app.globalData.uid = res.data.userId;
|
|
|
|
wx.showToast({ title: '登录成功', icon: 'success' })
|
|
|
|
// 同步登录信息
|
|
this.syncLogin()
|
|
// 拉取用户信息
|
|
this.loadUserInfo()
|
|
|
|
// 返回上一页,若无法返回则到首页
|
|
var pages = getCurrentPages()
|
|
if (pages.length > 1) {
|
|
wx.navigateBack()
|
|
} else {
|
|
wx.switchTab({ url: '/pages/index/index' })
|
|
}
|
|
} catch (e) {
|
|
this.setData({ logging: false })
|
|
}
|
|
},
|
|
|
|
// 同步登录信息
|
|
syncLogin() {
|
|
request({
|
|
url: '/member/logintwo',
|
|
method: 'POST',
|
|
loading: false,
|
|
showError: false,
|
|
data: {
|
|
uid: wx.getStorageSync('uid'),
|
|
phone: wx.getStorageSync('phone'),
|
|
token: wx.getStorageSync('token')
|
|
}
|
|
})
|
|
},
|
|
|
|
// 我的钱包
|
|
goWallet() {
|
|
wx.navigateTo({ url: '/pages/wodeqianbao/qianbao' })
|
|
},
|
|
|
|
// 我的订单
|
|
goOrders() {
|
|
wx.switchTab({ url: '/pages/order/order' })
|
|
},
|
|
|
|
// 联系客服
|
|
callService() {
|
|
wx.makePhoneCall({ phoneNumber: '4001234567' })
|
|
},
|
|
|
|
// 关于
|
|
goAbout() {
|
|
wx.navigateTo({ url: '/pages/guanyu/guanyu' })
|
|
}
|
|
})
|