Files
JayuCloud/JC-Salon-WeChat/pages/legal/index.js
T
xiaowang 942718b8d9
自动发布开发环境 Nacos 配置 / publish (push) Successful in 18s
fix(wechat): 协议挂起请求仅在离开页面时结算并集中映射错误文案
2026-11-05 10:51:40 +08:00

93 lines
2.8 KiB
JavaScript

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 })
}
},
onUnload() {
this.discardPendingIfNotAccepted()
},
/**
* 只有真正离开协议页(返回导致页面卸载)才结算被 4510 挂起的请求,避免调用方 loading 永久卡住;
* 切后台(如来电、复制协议文本)只触发 onHide,不结算;同意成功后也不再触发。
*/
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 })
}
}
})