解决小程序上传时出现的问题。

This commit is contained in:
cst61
2026-02-01 10:55:35 +08:00
parent 802ffbb2f3
commit eef85dce4a
22 changed files with 735 additions and 645 deletions

View File

@@ -54,41 +54,38 @@ function setupRequestInterceptor() {
// 重写 uni.request自动添加 tenantId 和 token
uni.request = function(options = {}) {
const { url, header = {}, method = 'GET', ...restOptions } = options
// 详细的拦截器日志
console.log('[Request Interceptor] 开始拦截请求:', {
url: url,
method: method.toUpperCase(),
isLoginApi: isLoginApi(url),
originalOptions: options
})
// 获取 tenantId
let tenantId = ''
try {
tenantId = uni.getStorageSync('backend-tenant-id') || ''
} catch (e) {
console.error('获取 tenantId 失败:', e)
console.error('[Request Interceptor] 获取 tenantId 失败:', e)
}
console.log('[Request Interceptor] tenantId状态:', {
tenantId: tenantId,
hasTenantId: !!tenantId
})
// 检查是否需要登录如果不是登录接口且租户ID为空
// 只在页面完全加载后才跳转,避免扫码进入时页面空白
// 在体验版中,不立即跳转到登录页面,而是让请求继续执行
// 由后端返回401状态码前端再处理登录跳转
if (!isLoginApi(url) && !tenantId) {
// 获取当前页面栈,检查是否页面已完全加载
const pages = getCurrentPages()
const hasLoadedPages = pages.length > 0 && pages[pages.length - 1].route
// 如果页面已加载,检查是否在首页或登录相关页面
if (hasLoadedPages) {
const currentPage = pages[pages.length - 1]
const currentRoute = '/' + currentPage.route
// 在首页或登录相关页面时,不立即跳转,让页面先加载
const isHomePage = currentRoute.includes('/pages/furniture_reception/furniture_reception')
const isLoginRelated = currentRoute.includes('/uni_modules/uni-id-pages/pages/login') ||
currentRoute.includes('/uni_modules/uni-id-pages/pages/register') ||
currentRoute.includes('/uni_modules/uni-id-pages/pages/retrieve')
if (!isHomePage && !isLoginRelated) {
console.warn('[Request Interceptor] 租户ID为空跳转到登录页面')
redirectToLogin()
// 返回一个被拒绝的Promise阻止请求继续执行
return Promise.reject(new Error('未登录,请先登录'))
}
}
// 如果页面还没加载完成,不立即跳转,避免扫码进入时空白
console.warn('[Request Interceptor] 租户ID为空但允许请求继续执行由后端处理认证')
// 不阻止请求让后端返回401前端再处理
} else if (isLoginApi(url)) {
console.log('[Request Interceptor] 这是登录接口跳过租户ID检查')
} else {
console.log('[Request Interceptor] 租户ID存在正常处理请求')
}
// 构建新的 header保留原有的 header避免覆盖
@@ -133,15 +130,35 @@ function setupRequestInterceptor() {
console.error('获取 roleName 或 scenario 失败:', e)
}
// 记录最终的请求参数
const finalRequestOptions = {
...restOptions,
url,
method,
header: newHeader
}
console.log('[Request Interceptor] 准备发送请求:', {
url: url,
method: method.toUpperCase(),
headers: newHeader,
hasData: !!restOptions.data
})
// 统一使用 Promise 方式,确保正确返回响应
// 这样无论调用方使用 await 还是回调,都能正常工作
return new Promise((resolve, reject) => {
console.log('[Request Interceptor] 开始调用原始 uni.request')
originalRequest.call(uni, {
...restOptions,
url,
method,
header: newHeader,
...finalRequestOptions,
success: (res) => {
console.log('[Request Interceptor] 请求成功:', {
url: url,
method: method.toUpperCase(),
statusCode: res.statusCode,
hasData: !!res.data
})
// 如果提供了 success 回调,先执行它
if (options.success) {
options.success(res)
@@ -153,7 +170,8 @@ function setupRequestInterceptor() {
console.error('[Request Interceptor] 请求失败:', {
url: url,
method: method.toUpperCase(),
error: err
error: err,
errorMessage: err.errMsg || '未知错误'
})
// 如果提供了 fail 回调,先执行它
if (options.fail) {
@@ -163,6 +181,11 @@ function setupRequestInterceptor() {
reject(err)
},
complete: (res) => {
console.log('[Request Interceptor] 请求完成:', {
url: url,
method: method.toUpperCase(),
hasResponse: !!res
})
// 如果提供了 complete 回调,执行它
if (options.complete) {
options.complete(res)