产品化: 统一title
This commit is contained in:
@@ -17,6 +17,38 @@ function getTenantId() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 跳转到登录页面
|
||||
*/
|
||||
function redirectToLogin() {
|
||||
const loginPage = '/uni_modules/uni-id-pages/pages/login/login-withpwd'
|
||||
const pages = getCurrentPages()
|
||||
|
||||
// 如果当前已经在登录页,不重复跳转
|
||||
if (pages.length > 0) {
|
||||
const currentPage = pages[pages.length - 1]
|
||||
if (currentPage.route && currentPage.route.includes('login')) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
uni.reLaunch({
|
||||
url: loginPage,
|
||||
fail: (err) => {
|
||||
console.error('跳转登录页失败:', err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否为登录相关接口(不需要租户ID的接口)
|
||||
*/
|
||||
function isLoginApi(url) {
|
||||
if (!url) return false
|
||||
// 登录接口不需要租户ID
|
||||
return url.includes('/api/sys/auth/login')
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 token(从本地存储)
|
||||
*/
|
||||
@@ -84,6 +116,15 @@ export function request(options = {}) {
|
||||
// 技术方案:登录返回的 LoginResponse 中包含 tenantId,前端保存后自动添加到所有请求的 header 中
|
||||
if (needTenantId) {
|
||||
const tenantId = getTenantId()
|
||||
|
||||
// 检查是否需要登录:如果不是登录接口且租户ID为空,则跳转到登录页
|
||||
if (!isLoginApi(fullUrl) && !tenantId) {
|
||||
console.warn('[Request] 租户ID为空,跳转到登录页面')
|
||||
redirectToLogin()
|
||||
// 返回一个被拒绝的Promise,阻止请求继续执行
|
||||
return Promise.reject(new Error('未登录,请先登录'))
|
||||
}
|
||||
|
||||
if (tenantId) {
|
||||
// 将 tenantId 放入 X-Tenant-Id header(适用于所有请求类型:GET、POST、PUT、DELETE)
|
||||
requestHeader['X-Tenant-Id'] = tenantId
|
||||
|
||||
Reference in New Issue
Block a user