This commit is contained in:
@@ -7,6 +7,12 @@ const HEADER_NONCE = 'X-Nonce'
|
||||
const HEADER_SIGNATURE = 'X-Signature'
|
||||
const HEADER_SIGNATURE_VERSION = 'X-Sign-Version'
|
||||
|
||||
/**
|
||||
* 服务端协议同意拦截返回的业务码:登录用户未同意最新版协议时返回 4510。
|
||||
*/
|
||||
const AGREEMENT_VERSION_REQUIRED_CODE = 4510
|
||||
let agreementPrompting = false
|
||||
|
||||
function buildUrl(url, params) {
|
||||
const sourceParams = params || {}
|
||||
const baseUrl = sourceParams.__baseUrl ? sourceParams.__baseUrl : API_DOMAIN
|
||||
@@ -65,6 +71,37 @@ function handleUnauthorized(response, url) {
|
||||
return normalizeError(message, response)
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理 4510:提示协议已更新并跳转登录页重新阅读同意。
|
||||
* 登录页已有协议勾选与弹层,重新登录时会带上最新版本同意,避免重复弹窗用 agreementPrompting 去重。
|
||||
*/
|
||||
function handleAgreementVersionRequired(response, url) {
|
||||
const message = resolveResponseMessage(response, '用户协议与隐私政策已更新,请阅读并同意后继续使用')
|
||||
if (isAuthFreePath(url)) {
|
||||
return normalizeError(message, response)
|
||||
}
|
||||
if (!agreementPrompting) {
|
||||
agreementPrompting = true
|
||||
clearRuntimeSession()
|
||||
wx.showModal({
|
||||
title: '协议版本已更新',
|
||||
content: message,
|
||||
showCancel: false,
|
||||
confirmText: '去同意',
|
||||
success() {
|
||||
agreementPrompting = false
|
||||
wx.reLaunch({
|
||||
url: '/pages/login/index'
|
||||
})
|
||||
},
|
||||
fail() {
|
||||
agreementPrompting = false
|
||||
}
|
||||
})
|
||||
}
|
||||
return normalizeError(message, response)
|
||||
}
|
||||
|
||||
function extractPath(value) {
|
||||
const target = String(value || '')
|
||||
if (!target) {
|
||||
@@ -174,6 +211,10 @@ function request(options) {
|
||||
resolve(payload.data)
|
||||
return
|
||||
}
|
||||
if (Number(payload.code) === AGREEMENT_VERSION_REQUIRED_CODE) {
|
||||
reject(handleAgreementVersionRequired({ data: payload }, options.url))
|
||||
return
|
||||
}
|
||||
if (Number(payload.code) === 401) {
|
||||
reject(handleUnauthorized({ data: payload }, options.url))
|
||||
return
|
||||
|
||||
@@ -4,6 +4,7 @@ const storage = {}
|
||||
const requests = []
|
||||
const navigations = []
|
||||
const scheduled = []
|
||||
const modals = []
|
||||
let clearedSessions = 0
|
||||
let globalData = { wechatLoginState: { status: 'SUCCESS', accessToken: 'expired-token' } }
|
||||
|
||||
@@ -15,6 +16,7 @@ global.wx = {
|
||||
removeStorageSync: key => { delete storage[key] },
|
||||
request: options => requests.push(options),
|
||||
showToast: () => {},
|
||||
showModal: options => modals.push(options),
|
||||
reLaunch: options => navigations.push(options.url)
|
||||
}
|
||||
global.getApp = () => ({
|
||||
@@ -45,6 +47,18 @@ async function rejectWithUnauthorized(options) {
|
||||
return pending
|
||||
}
|
||||
|
||||
async function rejectWithAgreementRequired(options, message) {
|
||||
const pending = request(options).then(
|
||||
() => { throw new Error('Expected request to reject') },
|
||||
error => error
|
||||
)
|
||||
requests[requests.length - 1].success({
|
||||
statusCode: 200,
|
||||
data: { code: 4510, message }
|
||||
})
|
||||
return pending
|
||||
}
|
||||
|
||||
async function run() {
|
||||
auth.setSession({ accessToken: 'expired-token' }, true)
|
||||
|
||||
@@ -70,6 +84,21 @@ async function run() {
|
||||
assert.strictEqual(scheduled.length, 1)
|
||||
scheduled.shift()()
|
||||
assert.deepStrictEqual(navigations, ['/pages/login/index', '/pages/login/index'])
|
||||
|
||||
const agreementMessage = '用户协议与隐私政策已更新(当前版本 2026.11.04-R2),请阅读并同意后继续使用'
|
||||
auth.setSession({ accessToken: 'agreement-token' }, true)
|
||||
const agreementError = await rejectWithAgreementRequired({ url: '/manager/calendar' }, agreementMessage)
|
||||
assert.strictEqual(agreementError.message, agreementMessage)
|
||||
assert.strictEqual(modals.length, 1)
|
||||
assert.strictEqual(clearedSessions, 3)
|
||||
assert.strictEqual(globalData.wechatLoginState, null)
|
||||
|
||||
const duplicatedError = await rejectWithAgreementRequired({ url: '/manager/customers' }, agreementMessage)
|
||||
assert.strictEqual(duplicatedError.message, agreementMessage)
|
||||
assert.strictEqual(modals.length, 1)
|
||||
|
||||
modals[0].success()
|
||||
assert.deepStrictEqual(navigations, ['/pages/login/index', '/pages/login/index', '/pages/login/index'])
|
||||
}
|
||||
|
||||
run().catch(error => {
|
||||
|
||||
Reference in New Issue
Block a user