const authApi = require('../../api/auth') const auth = require('../../utils/auth') const request = require('../../utils/request') const { showErrorToast } = require('../../utils/toast') Page({ data: { loading: true, agreement: null, mode: '', agreements: [], accepted: false, accepting: false }, onLoad(options) { if (options && options.mode === 'accept') { wx.setNavigationBarTitle({ title: '阅读并同意协议' }) this.setData({ mode: 'accept' }) this.loadAll() return } this.load(options && options.type) }, async load(type) { const agreementType = type === 'PRIVACY_POLICY' ? 'PRIVACY_POLICY' : 'USER_TERMS' this.setData({ loading: true }) try { const agreements = await authApi.getCurrentAgreements() const agreement = (Array.isArray(agreements) ? agreements : []).find(item => item.agreementType === agreementType) this.setData({ agreement: agreement || null }) } catch (error) { showErrorToast(error, '协议加载失败') } finally { this.setData({ loading: false }) } }, async loadAll() { this.setData({ loading: true }) try { const agreements = await authApi.getCurrentAgreements() this.setData({ agreements: Array.isArray(agreements) ? agreements : [] }) } catch (error) { showErrorToast(error, '协议加载失败') } finally { this.setData({ loading: false }) } }, /** * 切后台(来电、复制协议文本)只暂停挂起请求的超时计时,绝不结算; * 回到前台后按剩余可见时长恢复,若挂起请求已不存在则恢复逻辑自动 no-op。 */ onHide() { request.pausePendingAgreementTimeout() }, onShow() { request.resumePendingAgreementTimeout() }, onUnload() { this.discardPendingIfNotAccepted() }, /** * 只有真正离开协议页(返回导致页面卸载)才结算被 4510 挂起的请求,避免调用方 loading 永久卡住; * 切后台只暂停计时、不结算;同意成功后也不再触发。 */ discardPendingIfNotAccepted() { if (this.data.mode !== 'accept' || this.agreementAccepted) { return } request.discardPendingAgreement() }, toggleAgreement() { if (this.data.accepting) { return } this.setData({ accepted: !this.data.accepted }) }, async acceptAndContinue() { if (!this.data.accepted || this.data.accepting) { return } this.setData({ accepting: true }) try { await authApi.acceptAgreements('WECHAT_MINI') this.agreementAccepted = true wx.showToast({ title: '已同意', icon: 'success' }) // 同意成功后重放首个被 4510 拦下的请求一次;失败不再循环。 request.retryPendingAfterAgreement() setTimeout(() => { wx.navigateBack({ fail: () => wx.reLaunch({ url: '/pages/planning/index' }) }) }, 300) } catch (error) { // accept 返回 401 时请求层已清会话并跳登录页,这里不再叠加提示。 if (auth.getAccessToken()) { showErrorToast(error, '同意失败,请重试') } } finally { this.setData({ accepting: false }) } } })