feat(salon): 服务端拦截未同意最新协议的用户并补充端侧 4510 处理
自动发布开发环境 Nacos 配置 / publish (push) Successful in 18s

This commit is contained in:
2026-11-05 09:43:01 +08:00
parent 88589e2575
commit fb19fcf05f
10 changed files with 503 additions and 45 deletions
+32 -1
View File
@@ -11,7 +11,7 @@ import type { IdType } from '@/utils/id'
import { clearAdminSessionStorage } from '@/utils/admin-session'
import { attachRequestSignature } from '@/utils/request-signature'
import { isApiAuthFreePath } from './auth-free-paths'
import { isAgreementPending } from '../utils/agreement-gate'
import { AGREEMENT_REQUIRED_EVENT, isAgreementPending, setAgreementPending } from '../utils/agreement-gate'
import {
getApiResponseMessage,
isPermissionErrorMessage,
@@ -89,6 +89,12 @@ const isAdminScopedUrl = (url?: string) => {
}
const AGREEMENT_GATE_ALLOWLIST = ['/legal-agreement/', '/admin/auth/', '/user/logout', '/user/info']
/**
* 服务端协议同意拦截返回的业务码。收到后前端锁定业务请求并引导用户去阅读/同意最新版协议。
*/
const AGREEMENT_VERSION_REQUIRED_CODE = 4510
const AGREEMENT_REQUIRED_DEDUPE_MS = 5000
let lastAgreementRequiredTime = 0
/**
* Purpose: getStoredRefreshToken supports index.ts by holding or executing the get stored refresh token page or API role.
@@ -154,6 +160,26 @@ const showError = (msg: string) => {
message.error(msg)
}
/**
* 处理服务端 4510(未同意最新协议):
* 锁定后续业务请求、广播事件让后台布局弹出既有的同意弹层,并提示用户。
* 非后台场景没有弹层,直接新开协议阅读页,引导用户阅读后重新登录同意。
*/
const handleAgreementRequired = (res: any, isAdminRequest: boolean) => {
setAgreementPending(true)
if (typeof window !== 'undefined') {
window.dispatchEvent(new CustomEvent(AGREEMENT_REQUIRED_EVENT, { detail: res?.data || {} }))
}
if (Date.now() - lastAgreementRequiredTime < AGREEMENT_REQUIRED_DEDUPE_MS) {
return
}
lastAgreementRequiredTime = Date.now()
showError(normalizeApiErrorMessage(res?.message || '用户协议与隐私政策已更新,请阅读并同意后继续使用'))
if (!isAdminRequest && typeof window !== 'undefined') {
window.open('/legal/user-terms.html', '_blank', 'noopener')
}
}
const resetSystemFailureWindow = () => {
systemFailureTimes = []
}
@@ -285,6 +311,11 @@ api.interceptors.response.use(
response => {
resetSystemFailureWindow()
const res = response.data
if (Number(res?.code) === AGREEMENT_VERSION_REQUIRED_CODE) {
const errorMsg = normalizeApiErrorMessage(res?.message || '用户协议与隐私政策已更新,请阅读并同意后继续使用')
handleAgreementRequired(res, isAdminScopedUrl(response.config?.url))
return Promise.reject(new Error(errorMsg))
}
if (res.code !== 200) {
const errorMsg = normalizeApiErrorMessage(res.message || '请求失败')
if (!isPermissionErrorMessage(errorMsg)) {
@@ -4,6 +4,11 @@
*/
let agreementPending = false
/**
* 服务端返回 4510 时广播的事件名,页面层(如后台布局)监听后重新检查协议状态并展示同意弹层。
*/
export const AGREEMENT_REQUIRED_EVENT = 'jayucloud:agreement-required'
export const setAgreementPending = (value: boolean) => {
agreementPending = value
}
@@ -93,7 +93,7 @@
</template>
<script setup lang="ts">
import { onMounted, ref, watch } from 'vue'
import { onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { message, Modal } from 'ant-design-vue'
import { DownOutlined, HomeOutlined, LogoutOutlined, MenuFoldOutlined, MenuOutlined, MenuUnfoldOutlined, SafetyCertificateOutlined, TeamOutlined, ToolOutlined, UserOutlined } from '@ant-design/icons-vue'
@@ -104,7 +104,7 @@ import { completeAdminLogout } from '@/utils/admin-logout'
import { runWithGlobalLoading } from '@/utils/action-loading'
import { useAdminViewport } from '@/composables/useAdminViewport'
import { legalAgreementApi, type AgreementPending } from '@/api/auth'
import { setAgreementPending } from '@/utils/agreement-gate'
import { AGREEMENT_REQUIRED_EVENT, setAgreementPending } from '@/utils/agreement-gate'
const router = useRouter()
const route = useRoute()
@@ -157,10 +157,18 @@ const acceptAgreements = async () => {
}
watch(() => route.path, path => { selectedKeys.value = [path] }, { immediate: true })
/**
* 服务端返回 4510 时由请求层广播该事件,这里复用既有弹层逻辑重新检查协议状态。
*/
const handleAgreementRequired = () => { checkAgreementGate() }
onMounted(() => {
try { userInfo.value = JSON.parse(localStorage.getItem('adminUserInfo') || 'null') } catch { userInfo.value = null }
window.addEventListener(AGREEMENT_REQUIRED_EVENT, handleAgreementRequired)
checkAgreementGate()
})
onBeforeUnmount(() => {
window.removeEventListener(AGREEMENT_REQUIRED_EVENT, handleAgreementRequired)
})
</script>
<style scoped>