/** * Purpose: authApi supports index.js by holding or executing the auth api page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const authApi = require('../../api/auth') /** * Purpose: couponApi supports index.js by holding or executing the coupon api page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const couponApi = require('../../api/coupon') /** * Purpose: orderApi supports index.js by holding or executing the order api page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const orderApi = require('../../api/order') const growthApi = require('../../api/growth') /** * Purpose: userApi supports index.js by holding or executing the user api page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const userApi = require('../../api/user') /** * Purpose: wishlistApi supports index.js by holding or executing the wishlist api page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const wishlistApi = require('../../api/wishlist') /** * Purpose: actionGuard supports index.js by holding or executing the action guard page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const actionGuard = require('../../utils/action-guard') /** * Purpose: auth supports index.js by holding or executing the auth page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const auth = require('../../utils/auth') /** * Purpose: navigation supports index.js by holding or executing the navigation page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const navigation = require('../../utils/navigation') /** * Purpose: theme supports index.js by holding or executing the theme page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const theme = require('../../utils/theme') const { DEFAULT_AVATAR } = require('../../utils/config') const { runWithLoading } = require('../../utils/loading') function formatGrowthSummary(summary) { if (!summary) { return null } return { levelName: summary.levelName || '成长新客', growthValue: Number(summary.growthValue || 0), appointmentCount: Number(summary.appointmentCount || 0), reviewCount: Number(summary.reviewCount || 0) } } /** * profile page logic. * Call guide: page data drives the WXML template, and each method is called by user events or lifecycle callbacks. */ Page({ data: Object.assign(theme.pageThemeData(getApp().globalData.shopTheme), { isLoggedIn: false, loading: false, needSetPassword: false, userInfo: null, orderCount: 0, pendingReviewCount: 0, couponCount: 0, wishlistCount: 0, unreadMessageCount: 0, growthSummary: null, defaultAvatar: DEFAULT_AVATAR }), /** * `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. */ onShow() { this.loadProfile() }, async loadProfile() { this._avatarReloadSource = '' 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. */ if (!isLoggedIn) { this.setData({ userInfo: null, orderCount: 0, couponCount: 0, wishlistCount: 0, growthSummary: null, loading: false }) return } try { const [userInfo, orders, coupons, wishlist, unreadMessages, pendingReviews, growthSummary] = await Promise.all([ userApi.getUserInfo(), orderApi.getOrderList(), couponApi.listUserCoupons(), wishlistApi.getWishlist(), userApi.getMyUnreadMessageCount(), orderApi.listMyPendingReviews().catch(() => []), growthApi.getGrowthSummary().catch(() => null) ]) getApp().refreshUserInfo(userInfo) this.setData({ userInfo, orderCount: Array.isArray(orders) ? orders.length : 0, pendingReviewCount: Array.isArray(pendingReviews) ? pendingReviews.length : 0, couponCount: Array.isArray(coupons) ? coupons.length : 0, wishlistCount: Array.isArray(wishlist) ? wishlist.length : 0, unreadMessageCount: Number((unreadMessages && unreadMessages.unreadCount) || 0), growthSummary: formatGrowthSummary(growthSummary) }) await this.checkPopupMessages() } catch (error) { wx.showToast({ title: error.message || '\u4e2a\u4eba\u4e2d\u5fc3\u52a0\u8f7d\u5931\u8d25', 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. */ 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. */ 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. */ 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. */ handleOpenOrders() { if (!navigation.ensureLogin('\u67e5\u770b\u8ba2\u5355')) { return } navigation.navigate('/pages/orders/index') }, /** * Purpose: handleOpenPendingReviews jumps straight to the order-page pending review focus. * Usage: bind the pending-review card in profile WXML. */ handleOpenPendingReviews() { if (!navigation.ensureLogin('查看待评价')) { return } navigation.navigate('/pages/orders/index', { review: 'pending', source: 'profile' }) }, /** * `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. */ handleOpenWishlist() { if (!navigation.ensureLogin('\u67e5\u770b\u6536\u85cf')) { 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. */ handleOpenCoupons() { if (!navigation.ensureLogin('\u67e5\u770b\u4f18\u60e0\u5238')) { 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 globalApp = getApp() if (globalApp && typeof globalApp.checkPopupMessages === 'function') { await globalApp.checkPopupMessages() return } 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) const opened = await navigation.openSystemMessage(item) if (opened) { break } } } catch (error) { console.error('加载待弹窗消息失败', error) } finally { this._checkingPopupMessages = false } }, async handleSwitchShop() { if (!navigation.ensureLogin('\u5207\u6362\u95e8\u5e97')) { 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. */ if (!shopIds.length) { wx.showToast({ title: '\u5f53\u524d\u8d26\u53f7\u8fd8\u6ca1\u6709\u53ef\u7528\u95e8\u5e97', 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. */ 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' }) } catch (error) { if (actionGuard.isGuardError(error)) { return } wx.showToast({ title: error.message || '\u95e8\u5e97\u5207\u6362\u5931\u8d25', 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. */ 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)) }) }) /** * `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) { 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 } 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': '' }) } } })