This commit is contained in:
@@ -167,6 +167,14 @@ module.exports = {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
acceptAgreements(clientType = 'WECHAT_MINI') {
|
||||||
|
return request({
|
||||||
|
url: '/legal-agreement/accept',
|
||||||
|
method: 'POST',
|
||||||
|
params: { clientType }
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
switchShop(shopId) {
|
switchShop(shopId) {
|
||||||
return request({
|
return request({
|
||||||
url: '/user/switch-shop',
|
url: '/user/switch-shop',
|
||||||
|
|||||||
@@ -1,9 +1,17 @@
|
|||||||
const authApi = require('../../api/auth')
|
const authApi = require('../../api/auth')
|
||||||
|
const auth = require('../../utils/auth')
|
||||||
|
const request = require('../../utils/request')
|
||||||
|
|
||||||
Page({
|
Page({
|
||||||
data: { loading: true, agreement: null },
|
data: { loading: true, agreement: null, mode: '', agreements: [], accepted: false, accepting: false },
|
||||||
|
|
||||||
onLoad(options) {
|
onLoad(options) {
|
||||||
|
if (options && options.mode === 'accept') {
|
||||||
|
wx.setNavigationBarTitle({ title: '阅读并同意协议' })
|
||||||
|
this.setData({ mode: 'accept' })
|
||||||
|
this.loadAll()
|
||||||
|
return
|
||||||
|
}
|
||||||
this.load(options && options.type)
|
this.load(options && options.type)
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -19,5 +27,49 @@ Page({
|
|||||||
} finally {
|
} finally {
|
||||||
this.setData({ loading: false })
|
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) {
|
||||||
|
wx.showToast({ title: error.message || '协议加载失败', icon: 'none' })
|
||||||
|
} finally {
|
||||||
|
this.setData({ loading: false })
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
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')
|
||||||
|
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()) {
|
||||||
|
wx.showToast({ title: error.message || '同意失败,请重试', icon: 'none' })
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
this.setData({ accepting: false })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,9 +1,25 @@
|
|||||||
<view class="page legal-page">
|
<view class="page legal-page {{mode === 'accept' ? 'legal-page-accept' : ''}}">
|
||||||
<view wx:if="{{loading}}" class="legal-state">正在加载…</view>
|
<view wx:if="{{loading}}" class="legal-state">正在加载…</view>
|
||||||
|
<block wx:elif="{{mode === 'accept'}}">
|
||||||
|
<view wx:for="{{agreements}}" wx:key="agreementType" class="legal-document card">
|
||||||
|
<text class="legal-title">{{item.title}}</text>
|
||||||
|
<text class="legal-version">版本 {{item.version}}</text>
|
||||||
|
<text class="legal-content" selectable>{{item.content}}</text>
|
||||||
|
</view>
|
||||||
|
<view wx:if="{{!agreements.length}}" class="legal-state">当前内容暂不可用,请稍后重试。</view>
|
||||||
|
</block>
|
||||||
<view wx:elif="{{agreement}}" class="legal-document card">
|
<view wx:elif="{{agreement}}" class="legal-document card">
|
||||||
<text class="legal-title">{{agreement.title}}</text>
|
<text class="legal-title">{{agreement.title}}</text>
|
||||||
<text class="legal-version">版本 {{agreement.version}}</text>
|
<text class="legal-version">版本 {{agreement.version}}</text>
|
||||||
<text class="legal-content" selectable>{{agreement.content}}</text>
|
<text class="legal-content" selectable>{{agreement.content}}</text>
|
||||||
</view>
|
</view>
|
||||||
<view wx:else class="legal-state">当前内容暂不可用,请稍后重试。</view>
|
<view wx:else class="legal-state">当前内容暂不可用,请稍后重试。</view>
|
||||||
|
|
||||||
|
<view wx:if="{{mode === 'accept' && !loading}}" class="legal-action-bar">
|
||||||
|
<view class="legal-consent" bindtap="toggleAgreement">
|
||||||
|
<view class="legal-check {{accepted ? 'checked' : ''}}">{{accepted ? '✓' : ''}}</view>
|
||||||
|
<text class="legal-consent-text">我已阅读并同意《用户隐私政策》《用户协议》</text>
|
||||||
|
</view>
|
||||||
|
<button class="legal-accept-button" disabled="{{!accepted || accepting}}" loading="{{accepting}}" bindtap="acceptAndContinue">同意并继续</button>
|
||||||
|
</view>
|
||||||
</view>
|
</view>
|
||||||
|
|||||||
@@ -1,6 +1,69 @@
|
|||||||
.legal-page { padding: 24rpx; }
|
.legal-page { padding: 24rpx; }
|
||||||
|
.legal-page-accept { padding-bottom: calc(220rpx + env(safe-area-inset-bottom)); }
|
||||||
.legal-document { padding: 32rpx; }
|
.legal-document { padding: 32rpx; }
|
||||||
.legal-title { display: block; color: #24332e; font-size: 36rpx; font-weight: 700; }
|
.legal-title { display: block; color: #24332e; font-size: 36rpx; font-weight: 700; }
|
||||||
.legal-version { display: block; margin-top: 12rpx; color: #75827d; font-size: 24rpx; }
|
.legal-version { display: block; margin-top: 12rpx; color: #75827d; font-size: 24rpx; }
|
||||||
.legal-content { display: block; margin-top: 28rpx; color: #3e4c47; font-size: 28rpx; line-height: 1.9; white-space: pre-wrap; }
|
.legal-content { display: block; margin-top: 28rpx; color: #3e4c47; font-size: 28rpx; line-height: 1.9; white-space: pre-wrap; }
|
||||||
.legal-state { padding: 80rpx 24rpx; color: #71807a; text-align: center; }
|
.legal-state { padding: 80rpx 24rpx; color: #71807a; text-align: center; }
|
||||||
|
|
||||||
|
.legal-action-bar {
|
||||||
|
position: fixed;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
bottom: 0;
|
||||||
|
padding: 20rpx 24rpx calc(20rpx + env(safe-area-inset-bottom));
|
||||||
|
background: #ffffff;
|
||||||
|
box-shadow: 0 -8rpx 24rpx rgba(36, 51, 46, .08);
|
||||||
|
}
|
||||||
|
|
||||||
|
.legal-consent {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
min-height: 40rpx;
|
||||||
|
}
|
||||||
|
|
||||||
|
.legal-check {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
width: 28rpx;
|
||||||
|
height: 28rpx;
|
||||||
|
flex: none;
|
||||||
|
margin-right: 10rpx;
|
||||||
|
border: 2rpx solid #b7bdb9;
|
||||||
|
border-radius: 7rpx;
|
||||||
|
color: #ffffff;
|
||||||
|
font-size: 19rpx;
|
||||||
|
}
|
||||||
|
|
||||||
|
.legal-check.checked {
|
||||||
|
border-color: #247b63;
|
||||||
|
background: #247b63;
|
||||||
|
}
|
||||||
|
|
||||||
|
.legal-consent-text {
|
||||||
|
color: #3e4c47;
|
||||||
|
font-size: 24rpx;
|
||||||
|
line-height: 1.5;
|
||||||
|
}
|
||||||
|
|
||||||
|
.legal-accept-button {
|
||||||
|
margin-top: 16rpx;
|
||||||
|
height: 92rpx;
|
||||||
|
border-radius: 999rpx;
|
||||||
|
background: #247b63;
|
||||||
|
color: #ffffff;
|
||||||
|
font-size: 30rpx;
|
||||||
|
font-weight: 500;
|
||||||
|
line-height: 92rpx;
|
||||||
|
}
|
||||||
|
|
||||||
|
.legal-accept-button::after {
|
||||||
|
border: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.legal-accept-button[disabled] {
|
||||||
|
background: #247b63;
|
||||||
|
color: #ffffff;
|
||||||
|
opacity: .45;
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,7 +11,13 @@ const HEADER_SIGNATURE_VERSION = 'X-Sign-Version'
|
|||||||
* 服务端协议同意拦截返回的业务码:登录用户未同意最新版协议时返回 4510。
|
* 服务端协议同意拦截返回的业务码:登录用户未同意最新版协议时返回 4510。
|
||||||
*/
|
*/
|
||||||
const AGREEMENT_VERSION_REQUIRED_CODE = 4510
|
const AGREEMENT_VERSION_REQUIRED_CODE = 4510
|
||||||
let agreementPrompting = false
|
const AGREEMENT_PAGE_URL = '/pages/legal/index?mode=accept'
|
||||||
|
/**
|
||||||
|
* 一屏多个请求同时命中 4510 时只允许弹一次弹窗;
|
||||||
|
* 首个被拦下的请求挂起等待用户在协议页同意后重放,其余请求直接按被拦下处理。
|
||||||
|
*/
|
||||||
|
let agreementPromptVisible = false
|
||||||
|
let agreementBlockedRequest = null
|
||||||
|
|
||||||
function buildUrl(url, params) {
|
function buildUrl(url, params) {
|
||||||
const sourceParams = params || {}
|
const sourceParams = params || {}
|
||||||
@@ -49,6 +55,9 @@ function redirectToLoginAfterExpired(message) {
|
|||||||
if (!auth.beginSessionExpiredHandling()) {
|
if (!auth.beginSessionExpiredHandling()) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// 真正的登录失效才清会话;同时丢弃协议重试目标,避免把旧会话的请求带到新会话。
|
||||||
|
agreementBlockedRequest = null
|
||||||
|
agreementPromptVisible = false
|
||||||
clearRuntimeSession()
|
clearRuntimeSession()
|
||||||
wx.showToast({
|
wx.showToast({
|
||||||
title: message,
|
title: message,
|
||||||
@@ -71,35 +80,92 @@ function handleUnauthorized(response, url) {
|
|||||||
return normalizeError(message, response)
|
return normalizeError(message, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function cloneRequestOptions(options) {
|
||||||
* 处理 4510:提示协议已更新并跳转登录页重新阅读同意。
|
return {
|
||||||
* 登录页已有协议勾选与弹层,重新登录时会带上最新版本同意,避免重复弹窗用 agreementPrompting 去重。
|
url: options.url,
|
||||||
*/
|
method: options.method,
|
||||||
function handleAgreementVersionRequired(response, url) {
|
data: options.data,
|
||||||
const message = resolveResponseMessage(response, '用户协议与隐私政策已更新,请阅读并同意后继续使用')
|
params: options.params,
|
||||||
if (isAuthFreePath(url)) {
|
header: options.header,
|
||||||
return normalizeError(message, response)
|
timeout: options.timeout
|
||||||
}
|
}
|
||||||
if (!agreementPrompting) {
|
}
|
||||||
agreementPrompting = true
|
|
||||||
clearRuntimeSession()
|
/**
|
||||||
|
* 处理 4510:不清会话,弹窗引导用户去协议页同意。
|
||||||
|
* 首个被拦下的请求挂起,用户同意后由 retryPendingAfterAgreement() 重新发起一次;
|
||||||
|
* 并发的其余请求直接按被拦下处理,不再重复弹窗。
|
||||||
|
*/
|
||||||
|
function handleAgreementVersionRequired(response, options, resolve, reject) {
|
||||||
|
const message = resolveResponseMessage(response, '用户协议与隐私政策已更新,请阅读并同意后继续使用')
|
||||||
|
if (isAuthFreePath(options.url) || Number(options._agreementRetry) > 0) {
|
||||||
|
reject(normalizeError(message, response))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (agreementBlockedRequest) {
|
||||||
|
reject(normalizeError(message, response))
|
||||||
|
} else {
|
||||||
|
agreementBlockedRequest = {
|
||||||
|
options: cloneRequestOptions(options),
|
||||||
|
resolve,
|
||||||
|
reject
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (agreementPromptVisible) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
agreementPromptVisible = true
|
||||||
wx.showModal({
|
wx.showModal({
|
||||||
title: '协议版本已更新',
|
title: '协议版本已更新',
|
||||||
content: message,
|
content: message,
|
||||||
showCancel: false,
|
confirmText: '查看并同意',
|
||||||
confirmText: '去同意',
|
cancelText: '稍后',
|
||||||
success() {
|
success(result) {
|
||||||
agreementPrompting = false
|
agreementPromptVisible = false
|
||||||
wx.reLaunch({
|
if (result && result.confirm) {
|
||||||
url: '/pages/login/index'
|
wx.navigateTo({
|
||||||
|
url: AGREEMENT_PAGE_URL,
|
||||||
|
fail: () => wx.redirectTo({ url: AGREEMENT_PAGE_URL })
|
||||||
})
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 稍后:停留当前页,只给轻提示,不清会话;被挂起的请求按失败返回。
|
||||||
|
wx.showToast({
|
||||||
|
title: '请先同意最新协议后再操作',
|
||||||
|
icon: 'none'
|
||||||
|
})
|
||||||
|
rejectBlockedAgreementRequest(message, response)
|
||||||
},
|
},
|
||||||
fail() {
|
fail() {
|
||||||
agreementPrompting = false
|
agreementPromptVisible = false
|
||||||
|
rejectBlockedAgreementRequest(message, response)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function rejectBlockedAgreementRequest(message, response) {
|
||||||
|
const blocked = agreementBlockedRequest
|
||||||
|
agreementBlockedRequest = null
|
||||||
|
if (blocked) {
|
||||||
|
blocked.reject(normalizeError(message, response))
|
||||||
}
|
}
|
||||||
return normalizeError(message, response)
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户在协议页同意成功后调用:重放首个被 4510 拦下的原请求一次。
|
||||||
|
* 重放会重新生成签名头(nonce/签名),不复用旧请求的一次性头;
|
||||||
|
* 若重放仍失败(包括再次 4510),直接把结果交回原调用方,不再弹窗、不再重试。
|
||||||
|
*/
|
||||||
|
function retryPendingAfterAgreement() {
|
||||||
|
const blocked = agreementBlockedRequest
|
||||||
|
agreementBlockedRequest = null
|
||||||
|
if (!blocked) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const options = Object.assign(cloneRequestOptions(blocked.options), { _agreementRetry: 1 })
|
||||||
|
request(options).then(blocked.resolve, blocked.reject)
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractPath(value) {
|
function extractPath(value) {
|
||||||
@@ -212,7 +278,7 @@ function request(options) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (Number(payload.code) === AGREEMENT_VERSION_REQUIRED_CODE) {
|
if (Number(payload.code) === AGREEMENT_VERSION_REQUIRED_CODE) {
|
||||||
reject(handleAgreementVersionRequired({ data: payload }, options.url))
|
handleAgreementVersionRequired({ data: payload }, options, resolve, reject)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (Number(payload.code) === 401) {
|
if (Number(payload.code) === 401) {
|
||||||
@@ -234,5 +300,6 @@ function request(options) {
|
|||||||
request.buildSignatureHeaders = buildSignatureHeaders
|
request.buildSignatureHeaders = buildSignatureHeaders
|
||||||
request.toBackendRequestPath = toBackendRequestPath
|
request.toBackendRequestPath = toBackendRequestPath
|
||||||
request.handleUnauthorized = handleUnauthorized
|
request.handleUnauthorized = handleUnauthorized
|
||||||
|
request.retryPendingAfterAgreement = retryPendingAfterAgreement
|
||||||
|
|
||||||
module.exports = request
|
module.exports = request
|
||||||
|
|||||||
@@ -3,8 +3,10 @@ const assert = require('assert')
|
|||||||
const storage = {}
|
const storage = {}
|
||||||
const requests = []
|
const requests = []
|
||||||
const navigations = []
|
const navigations = []
|
||||||
|
const navigateToPages = []
|
||||||
const scheduled = []
|
const scheduled = []
|
||||||
const modals = []
|
const modals = []
|
||||||
|
const toasts = []
|
||||||
let clearedSessions = 0
|
let clearedSessions = 0
|
||||||
let globalData = { wechatLoginState: { status: 'SUCCESS', accessToken: 'expired-token' } }
|
let globalData = { wechatLoginState: { status: 'SUCCESS', accessToken: 'expired-token' } }
|
||||||
|
|
||||||
@@ -15,9 +17,11 @@ global.wx = {
|
|||||||
setStorageSync: (key, value) => { storage[key] = value },
|
setStorageSync: (key, value) => { storage[key] = value },
|
||||||
removeStorageSync: key => { delete storage[key] },
|
removeStorageSync: key => { delete storage[key] },
|
||||||
request: options => requests.push(options),
|
request: options => requests.push(options),
|
||||||
showToast: () => {},
|
showToast: options => toasts.push(options),
|
||||||
showModal: options => modals.push(options),
|
showModal: options => modals.push(options),
|
||||||
reLaunch: options => navigations.push(options.url)
|
reLaunch: options => navigations.push(options.url),
|
||||||
|
navigateTo: options => navigateToPages.push(options.url),
|
||||||
|
redirectTo: options => navigateToPages.push(options.url)
|
||||||
}
|
}
|
||||||
global.getApp = () => ({
|
global.getApp = () => ({
|
||||||
globalData,
|
globalData,
|
||||||
@@ -35,27 +39,25 @@ global.setTimeout = callback => {
|
|||||||
const auth = require('./auth')
|
const auth = require('./auth')
|
||||||
const request = require('./request')
|
const request = require('./request')
|
||||||
|
|
||||||
|
function respondLatest(statusCode, data) {
|
||||||
|
requests[requests.length - 1].success({ statusCode, data })
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveWithData(data) {
|
||||||
|
respondLatest(200, { code: 200, data })
|
||||||
|
}
|
||||||
|
|
||||||
|
function startRequest(options) {
|
||||||
|
const promise = request(options)
|
||||||
|
return { promise }
|
||||||
|
}
|
||||||
|
|
||||||
async function rejectWithUnauthorized(options) {
|
async function rejectWithUnauthorized(options) {
|
||||||
const pending = request(options).then(
|
const pending = request(options).then(
|
||||||
() => { throw new Error('Expected request to reject') },
|
() => { throw new Error('Expected request to reject') },
|
||||||
error => error
|
error => error
|
||||||
)
|
)
|
||||||
requests[requests.length - 1].success({
|
respondLatest(401, { message: '登录已过期,请重新登录' })
|
||||||
statusCode: 401,
|
|
||||||
data: { message: '登录已过期,请重新登录' }
|
|
||||||
})
|
|
||||||
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
|
return pending
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,17 +89,76 @@ async function run() {
|
|||||||
|
|
||||||
const agreementMessage = '用户协议与隐私政策已更新(当前版本 2026.11.04-R2),请阅读并同意后继续使用'
|
const agreementMessage = '用户协议与隐私政策已更新(当前版本 2026.11.04-R2),请阅读并同意后继续使用'
|
||||||
auth.setSession({ accessToken: 'agreement-token' }, true)
|
auth.setSession({ accessToken: 'agreement-token' }, true)
|
||||||
const agreementError = await rejectWithAgreementRequired({ url: '/manager/calendar' }, agreementMessage)
|
const blocked = startRequest({ url: '/manager/orders', method: 'POST', data: { page: 1 } })
|
||||||
assert.strictEqual(agreementError.message, agreementMessage)
|
const originalNonce = requests[requests.length - 1].header['X-Nonce']
|
||||||
assert.strictEqual(modals.length, 1)
|
respondLatest(200, { code: 4510, message: agreementMessage })
|
||||||
assert.strictEqual(clearedSessions, 3)
|
|
||||||
assert.strictEqual(globalData.wechatLoginState, null)
|
|
||||||
|
|
||||||
const duplicatedError = await rejectWithAgreementRequired({ url: '/manager/customers' }, agreementMessage)
|
// 4510 不清会话、不跳登录页,只弹一次弹窗
|
||||||
|
assert.strictEqual(clearedSessions, 2)
|
||||||
|
assert.deepStrictEqual(navigations, ['/pages/login/index', '/pages/login/index'])
|
||||||
|
assert.strictEqual(modals.length, 1)
|
||||||
|
assert.strictEqual(modals[0].confirmText, '查看并同意')
|
||||||
|
assert.strictEqual(modals[0].cancelText, '稍后')
|
||||||
|
assert.strictEqual(modals[0].content, agreementMessage)
|
||||||
|
|
||||||
|
// 并发 4510:不重复弹窗,其余请求直接按被拦下处理
|
||||||
|
const duplicated = startRequest({ url: '/manager/customers', method: 'POST', data: { page: 2 } })
|
||||||
|
respondLatest(200, { code: 4510, message: agreementMessage })
|
||||||
|
const duplicatedError = await duplicated.promise.catch(error => error)
|
||||||
assert.strictEqual(duplicatedError.message, agreementMessage)
|
assert.strictEqual(duplicatedError.message, agreementMessage)
|
||||||
assert.strictEqual(modals.length, 1)
|
assert.strictEqual(modals.length, 1)
|
||||||
|
|
||||||
modals[0].success()
|
// 查看并同意:进入协议页,会话保持
|
||||||
|
modals[0].success({ confirm: true, cancel: false })
|
||||||
|
assert.deepStrictEqual(navigateToPages, ['/pages/legal/index?mode=accept'])
|
||||||
|
assert.strictEqual(clearedSessions, 2)
|
||||||
|
|
||||||
|
// 同意后自动重放首个被拦请求一次,重新生成 nonce/签名,原调用方无感
|
||||||
|
const requestsBeforeRetry = requests.length
|
||||||
|
request.retryPendingAfterAgreement()
|
||||||
|
assert.strictEqual(requests.length, requestsBeforeRetry + 1)
|
||||||
|
const retriedRequest = requests[requests.length - 1]
|
||||||
|
assert.strictEqual(retriedRequest.url.includes('/manager/orders'), true)
|
||||||
|
assert.deepStrictEqual(retriedRequest.data, { page: 1 })
|
||||||
|
assert.notStrictEqual(retriedRequest.header['X-Nonce'], originalNonce)
|
||||||
|
resolveWithData({ page: 1, items: [] })
|
||||||
|
const retriedValue = await blocked.promise
|
||||||
|
assert.deepStrictEqual(retriedValue, { page: 1, items: [] })
|
||||||
|
assert.strictEqual(modals.length, 1)
|
||||||
|
|
||||||
|
// 重试仍返回 4510:不再弹窗、不再循环,直接失败
|
||||||
|
auth.setSession({ accessToken: 'retry-token' }, true)
|
||||||
|
const retryFailed = startRequest({ url: '/manager/orders', method: 'POST', data: { page: 3 } })
|
||||||
|
respondLatest(200, { code: 4510, message: agreementMessage })
|
||||||
|
assert.strictEqual(modals.length, 2)
|
||||||
|
modals[1].success({ confirm: true, cancel: false })
|
||||||
|
assert.deepStrictEqual(navigateToPages, ['/pages/legal/index?mode=accept', '/pages/legal/index?mode=accept'])
|
||||||
|
request.retryPendingAfterAgreement()
|
||||||
|
respondLatest(200, { code: 4510, message: agreementMessage })
|
||||||
|
const retryError = await retryFailed.promise.catch(error => error)
|
||||||
|
assert.strictEqual(retryError.message, agreementMessage)
|
||||||
|
assert.strictEqual(modals.length, 2)
|
||||||
|
|
||||||
|
// 稍后:停留当前页,仅轻提示,不清会话
|
||||||
|
auth.setSession({ accessToken: 'later-token' }, true)
|
||||||
|
const later = startRequest({ url: '/manager/orders', method: 'POST', data: { page: 4 } })
|
||||||
|
respondLatest(200, { code: 4510, message: agreementMessage })
|
||||||
|
assert.strictEqual(modals.length, 3)
|
||||||
|
const laterPromise = later.promise.catch(error => error)
|
||||||
|
modals[2].success({ confirm: false, cancel: true })
|
||||||
|
const laterError = await laterPromise
|
||||||
|
assert.strictEqual(laterError.message, agreementMessage)
|
||||||
|
assert.strictEqual(toasts.length, 3)
|
||||||
|
assert.strictEqual(toasts[2].title, '请先同意最新协议后再操作')
|
||||||
|
assert.strictEqual(clearedSessions, 2)
|
||||||
|
assert.deepStrictEqual(navigations, ['/pages/login/index', '/pages/login/index'])
|
||||||
|
|
||||||
|
// accept 返回 401(token 确实过期)才允许清会话并跳登录页
|
||||||
|
auth.setSession({ accessToken: 'almost-expired-token' }, true)
|
||||||
|
await rejectWithUnauthorized({ url: '/legal-agreement/accept', method: 'POST' })
|
||||||
|
assert.strictEqual(clearedSessions, 3)
|
||||||
|
assert.strictEqual(scheduled.length, 1)
|
||||||
|
scheduled.shift()()
|
||||||
assert.deepStrictEqual(navigations, ['/pages/login/index', '/pages/login/index', '/pages/login/index'])
|
assert.deepStrictEqual(navigations, ['/pages/login/index', '/pages/login/index', '/pages/login/index'])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user