Files
JayuCloud/WeChatProgram/pages/profile/index.js
T

377 lines
12 KiB
JavaScript
Raw Normal View History

2026-04-01 15:18:35 +08:00
const authApi = require('../../api/auth')
const couponApi = require('../../api/coupon')
const orderApi = require('../../api/order')
const userApi = require('../../api/user')
const wishlistApi = require('../../api/wishlist')
const actionGuard = require('../../utils/action-guard')
2026-04-01 15:18:35 +08:00
const auth = require('../../utils/auth')
const navigation = require('../../utils/navigation')
const theme = require('../../utils/theme')
2026-04-01 15:18:35 +08:00
const { DEFAULT_AVATAR } = require('../../utils/config')
const { runWithLoading } = require('../../utils/loading')
/**
* profile page logic.
* Call guide: page data drives the WXML template, and each method is called by user events or lifecycle callbacks.
*/
2026-04-01 15:18:35 +08:00
Page({
data: Object.assign(theme.pageThemeData(getApp().globalData.shopTheme), {
2026-04-01 15:18:35 +08:00
isLoggedIn: false,
loading: false,
needSetPassword: false,
userInfo: null,
orderCount: 0,
couponCount: 0,
wishlistCount: 0,
unreadMessageCount: 0,
2026-04-01 15:18:35 +08:00
defaultAvatar: DEFAULT_AVATAR
}),
2026-04-01 15:18:35 +08:00
/**
* `onShow` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
onShow() {
this.loadProfile()
},
async loadProfile() {
this._avatarReloadSource = ''
2026-04-01 15:18:35 +08:00
const isLoggedIn = auth.isLoggedIn()
this.setData({
isLoggedIn,
needSetPassword: auth.getNeedSetPassword(),
loading: isLoggedIn
})
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
if (!isLoggedIn) {
this.setData({
userInfo: null,
orderCount: 0,
couponCount: 0,
wishlistCount: 0,
loading: false
})
return
}
try {
const [userInfo, orders, coupons, wishlist, unreadMessages] = await Promise.all([
2026-04-01 15:18:35 +08:00
userApi.getUserInfo(),
orderApi.getOrderList(),
couponApi.listUserCoupons(),
wishlistApi.getWishlist(),
userApi.getMyUnreadMessageCount()
2026-04-01 15:18:35 +08:00
])
getApp().refreshUserInfo(userInfo)
this.setData({
userInfo,
orderCount: Array.isArray(orders) ? orders.length : 0,
couponCount: Array.isArray(coupons) ? coupons.length : 0,
wishlistCount: Array.isArray(wishlist) ? wishlist.length : 0,
unreadMessageCount: Number((unreadMessages && unreadMessages.unreadCount) || 0)
2026-04-01 15:18:35 +08:00
})
await this.checkPopupMessages()
2026-04-01 15:18:35 +08:00
} catch (error) {
wx.showToast({
title: error.message || '\u4e2a\u4eba\u4e2d\u5fc3\u52a0\u8f7d\u5931\u8d25',
2026-04-01 15:18:35 +08:00
icon: 'none'
})
} finally {
this.setData({
loading: false
})
}
},
/**
* `handleOpenAuth` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
handleOpenAuth() {
navigation.openAuth('/pages/profile/index')
},
/**
* `handleOpenSettings` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
handleOpenSettings() {
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
if (!this.data.isLoggedIn) {
this.handleOpenAuth()
return
}
navigation.navigate('/pages/settings/index')
},
/**
* `handleOpenOrders` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
handleOpenOrders() {
if (!navigation.ensureLogin('\u67e5\u770b\u8ba2\u5355')) {
2026-04-01 15:18:35 +08:00
return
}
navigation.navigate('/pages/orders/index')
},
/**
* `handleOpenWishlist` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
handleOpenWishlist() {
if (!navigation.ensureLogin('\u67e5\u770b\u6536\u85cf')) {
2026-04-01 15:18:35 +08:00
return
}
navigation.navigate('/pages/wishlist/index')
},
/**
* `handleOpenCoupons` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
handleOpenCoupons() {
if (!navigation.ensureLogin('\u67e5\u770b\u4f18\u60e0\u5238')) {
2026-04-01 15:18:35 +08:00
return
}
navigation.navigate('/pages/coupons/index')
},
/**
* `handleOpenVipCenter` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
handleOpenVipCenter() {
if (!navigation.ensureLogin('查看 VIP 中心')) {
return
}
navigation.navigate('/pages/vip-center/index')
},
/**
* `handleOpenNotifications` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
handleOpenNotifications() {
if (!navigation.ensureLogin('查看消息通知')) {
return
}
navigation.navigate('/pages/notifications/index')
},
async checkPopupMessages() {
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
if (this._checkingPopupMessages) {
return
}
this._checkingPopupMessages = true
try {
const messages = await userApi.listMyPopupMessages()
/**
* `for` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
for (const item of messages || []) {
await new Promise(resolve => {
wx.showModal({
title: item.title || '消息通知',
content: item.content || '',
showCancel: false,
confirmText: '我知道了',
complete: resolve
})
})
await userApi.markMessagePopupShown(item.id)
2026-05-23 15:16:40 +08:00
const opened = await navigation.openActionPath(item.actionPath)
if (opened) {
break
}
}
} catch (error) {
console.error('加载待弹窗消息失败', error)
} finally {
this._checkingPopupMessages = false
}
},
2026-04-01 15:18:35 +08:00
async handleSwitchShop() {
if (!navigation.ensureLogin('\u5207\u6362\u95e8\u5e97')) {
2026-04-01 15:18:35 +08:00
return
}
const userInfo = this.data.userInfo
const shopIds = userInfo && Array.isArray(userInfo.shopIds) ? userInfo.shopIds : []
const shopNames = userInfo && Array.isArray(userInfo.shopNames) ? userInfo.shopNames : []
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
if (!shopIds.length) {
wx.showToast({
title: '\u5f53\u524d\u8d26\u53f7\u8fd8\u6ca1\u6709\u53ef\u7528\u95e8\u5e97',
2026-04-01 15:18:35 +08:00
icon: 'none'
})
return
}
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
if (shopIds.length === 1) {
navigation.navigate('/pages/shop-detail/index', {
id: shopIds[0],
returnTo: navigation.getCurrentPageUrl()
})
return
}
try {
await actionGuard.run(this, 'user:switch-shop', async () => {
const selection = await new Promise(resolve => {
wx.showActionSheet({
itemList: shopNames.map((name, index) => name || `\u95e8\u5e97 ${shopIds[index]}`),
success: res => resolve(res.tapIndex),
fail: () => resolve(-1)
})
})
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
if (selection < 0 || selection >= shopIds.length) {
return
}
const payload = await runWithLoading(() => authApi.switchShop({
shopId: shopIds[selection]
}))
getApp().setUserSession(payload)
wx.showToast({
title: '\u95e8\u5e97\u5df2\u5207\u6362',
icon: 'none'
})
await this.loadProfile()
}, {
message: '\u95e8\u5e97\u5207\u6362\u4e2d\uff0c\u8bf7\u52ff\u91cd\u590d\u64cd\u4f5c'
2026-04-01 15:18:35 +08:00
})
} catch (error) {
if (actionGuard.isGuardError(error)) {
return
}
2026-04-01 15:18:35 +08:00
wx.showToast({
title: error.message || '\u95e8\u5e97\u5207\u6362\u5931\u8d25',
2026-04-01 15:18:35 +08:00
icon: 'none'
})
}
},
async handleLogout() {
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
2026-04-01 15:18:35 +08:00
if (!this.data.isLoggedIn) {
this.handleOpenAuth()
return
}
try {
await actionGuard.run(this, 'auth:logout', async () => {
const confirmed = await new Promise(resolve => {
wx.showModal({
title: '\u9000\u51fa\u767b\u5f55',
content: '\u9000\u51fa\u540e\u9700\u8981\u91cd\u65b0\u767b\u5f55\u624d\u80fd\u67e5\u770b\u8ba2\u5355\u548c\u7ee7\u7eed\u4e0b\u5355\u3002',
success: res => resolve(Boolean(res.confirm))
})
})
2026-04-01 15:18:35 +08:00
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
if (!confirmed) {
return
}
try {
await runWithLoading(() => authApi.logout())
} catch (error) {}
getApp().clearUserSession()
await this.loadProfile()
wx.showToast({
title: '\u5df2\u9000\u51fa\u767b\u5f55',
icon: 'none'
})
}, {
message: '\u9000\u51fa\u5904\u7406\u4e2d\uff0c\u8bf7\u52ff\u91cd\u590d\u64cd\u4f5c'
})
} catch (error) {
if (!actionGuard.isGuardError(error)) {
throw error
}
}
},
async handleAvatarError() {
const currentAvatar = this.data.userInfo && this.data.userInfo.avatar
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
if (!currentAvatar) {
2026-04-01 15:18:35 +08:00
return
}
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
if (this._avatarReloadSource === currentAvatar) {
this.setData({
'userInfo.avatar': ''
})
return
}
2026-04-01 15:18:35 +08:00
this._avatarReloadSource = currentAvatar
try {
const userInfo = await userApi.getUserInfo()
getApp().refreshUserInfo(userInfo)
/**
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
*/
if (!userInfo || !userInfo.avatar || userInfo.avatar === currentAvatar) {
this.setData({
'userInfo.avatar': ''
})
return
}
this.setData({
userInfo
})
} catch (error) {
this.setData({
'userInfo.avatar': ''
})
}
2026-04-01 15:18:35 +08:00
}
})