const app = getApp() Page({ data: { statusBarHeight: 44, navBarHeight: 88, isLoggedIn: false, user: null, userInitial: 'U', userIdSuffix: '---', earningsText: '0.00', pendingEarningsText: '0.00', earningsDisplay: '--', purchasedCount: 0, totalSections: 62, matchEnabled: false, activeTab: 'overview', completedOrders: 0, recentChapters: [], totalReadTime: 50, matchHistoryCount: 0, showBindModal: false, bindType: 'phone', bindValue: '', isBinding: false, bindError: '' }, onLoad() { this.setNavBarHeight() this.syncUser() app.loadFeatureConfig().then(() => { this.setData({ matchEnabled: app.globalData.matchEnabled === true }) }) }, onShow() { if (typeof this.getTabBar === 'function' && this.getTabBar()) this.getTabBar().setData({ selected: 3 }) this.setNavBarHeight() this.syncUser() app.loadFeatureConfig().then(() => { this.setData({ matchEnabled: app.globalData.matchEnabled === true }) }) }, setNavBarHeight() { const statusBarHeight = app.globalData.statusBarHeight || 44 const navBarHeight = app.globalData.navBarHeight || (statusBarHeight + 44) this.setData({ statusBarHeight, navBarHeight }) }, syncUser() { const isLoggedIn = !!app.globalData.isLoggedIn const user = app.globalData.userInfo || null const total = app.globalData.bookData ? (Array.isArray(app.globalData.bookData) ? app.globalData.bookData.length : 62) : 62 const purchasedSections = app.globalData.purchasedSections || [] const hasFullBook = !!app.globalData.hasFullBook const purchasedCount = hasFullBook ? total : purchasedSections.length const recentChapters = (purchasedSections && purchasedSections.slice(-5)) || [] const userInitial = user && user.nickname ? String(user.nickname).charAt(0) : 'U' const userIdSuffix = user && user.id ? String(user.id).slice(-8) : '---' const earningsText = Number(user && user.earnings != null ? user.earnings : 0).toFixed(2) const pendingEarningsText = Number(user && user.pendingEarnings != null ? user.pendingEarnings : 0).toFixed(2) const earningsDisplay = user && (user.earnings != null) && Number(user.earnings) > 0 ? '¥' + Number(user.earnings).toFixed(0) : '--' this.setData({ isLoggedIn, user, userInitial, userIdSuffix, earningsText, pendingEarningsText, earningsDisplay, totalSections: total, purchasedCount, completedOrders: 0, recentChapters, totalReadTime: 50 + Math.floor(Math.random() * 150), matchHistoryCount: 0 }) }, doLogin() { app.login().then(() => { this.syncUser() wx.showToast({ title: '登录成功', icon: 'success' }) }) }, setActiveTab(e) { const tab = e.currentTarget.dataset.tab this.setData({ activeTab: tab }) }, goAbout() { wx.navigateTo({ url: '/pages/about/about' }) }, goPurchases() { wx.navigateTo({ url: '/pages/purchases/purchases' }) }, goReferral() { wx.navigateTo({ url: '/pages/referral/referral' }) }, goSettings() { wx.navigateTo({ url: '/pages/settings/settings' }) }, goMatch() { wx.switchTab({ url: '/pages/match/match' }) }, goChapters() { wx.switchTab({ url: '/pages/chapters/chapters' }) }, goToRead(e) { const id = e.currentTarget.dataset.id if (id) wx.navigateTo({ url: '/pages/read/read?id=' + encodeURIComponent(id) }) }, 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 } = 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: this.data.user && this.data.user.id, [bindType]: bindValue } }).then(() => { const user = { ...this.data.user, [bindType]: bindValue } app.globalData.userInfo = user wx.setStorageSync('userInfo', user) this.setData({ user, showBindModal: false, bindValue: '', isBinding: false }) wx.showToast({ title: '绑定成功', icon: 'success' }) }).catch(() => { this.setData({ bindError: '绑定失败,请重试', isBinding: false }) }) } })