提交1
This commit is contained in:
209
utils/request.js
209
utils/request.js
@@ -1,5 +1,170 @@
|
||||
import config from "@/config.js"
|
||||
import useUserStore from '@/stores/useUserStore';
|
||||
|
||||
// 是否显示重新登录
|
||||
export let isRelogin = { show: false };
|
||||
|
||||
// ======================================
|
||||
// 以下是分包(packageRc)专用请求函数区域
|
||||
// 这些函数使用固定的baseURL和token,适用于分包页面
|
||||
// ======================================
|
||||
|
||||
// 为分包添加的固定配置请求函数
|
||||
export function packageRcRequest({
|
||||
url,
|
||||
method = 'GET',
|
||||
data = {},
|
||||
load = false,
|
||||
header = {}
|
||||
} = {}) {
|
||||
// 分包使用的固定baseURL和token
|
||||
const fixedBaseURL = 'http://10.160.0.5:8907/';
|
||||
const fixedToken = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiJzeXNfdXNlcjoxIiwicm5TdHIiOiJsNDRvNmhxckdOOW1XcG1pWlpJcXZ0UXJMdGsyTlFhQSIsInVzZXJJZCI6MX0.K5H-Rpof9oI1CCIEtheEQ96XGAxPWS7tzyjPbXwAXrQ';
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
// 显示加载状态
|
||||
if (load) {
|
||||
uni.showLoading({
|
||||
title: '请稍候',
|
||||
mask: true
|
||||
})
|
||||
}
|
||||
|
||||
// 是否需要设置token
|
||||
const isToken = header && header.isToken === false
|
||||
|
||||
// 添加固定的Authorization token
|
||||
if (!isToken) {
|
||||
header = header || {}
|
||||
header['Authorization'] = fixedToken
|
||||
}
|
||||
|
||||
// 确保URL是字符串类型
|
||||
if (typeof url !== 'string') {
|
||||
console.error('URL must be a string:', url);
|
||||
url = String(url);
|
||||
}
|
||||
|
||||
// 发起请求,使用固定的baseURL
|
||||
uni.request({
|
||||
url: fixedBaseURL + url,
|
||||
method,
|
||||
data: data,
|
||||
header: {
|
||||
'Content-Type': 'application/json;charset=utf-8',
|
||||
...header
|
||||
},
|
||||
timeout: 60000,
|
||||
success: (res) => {
|
||||
// 二进制数据直接返回
|
||||
if (res.header['content-type'] && res.header['content-type'].includes('application/octet-stream')) {
|
||||
resolve(res.data)
|
||||
return
|
||||
}
|
||||
|
||||
// 处理状态码
|
||||
const code = res.data.code || 200
|
||||
|
||||
// 401错误处理
|
||||
if (code === 401) {
|
||||
if (!isRelogin.show) {
|
||||
isRelogin.show = true
|
||||
uni.showModal({
|
||||
title: '登录过期',
|
||||
content: '登录状态已过期,是否重新登录?',
|
||||
success: (res) => {
|
||||
isRelogin.show = false
|
||||
if (res.confirm) {
|
||||
// 跳转到登录页面
|
||||
uni.navigateTo({
|
||||
url: '/packageRc/pages/login/login'
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
reject(new Error('登录过期,请重新登录'))
|
||||
return
|
||||
}
|
||||
|
||||
// 其他错误处理
|
||||
if (code !== 200) {
|
||||
uni.showToast({
|
||||
title: res.data.msg || '请求失败',
|
||||
icon: 'none'
|
||||
})
|
||||
reject(res.data)
|
||||
return
|
||||
}
|
||||
|
||||
resolve(res.data)
|
||||
},
|
||||
fail: (error) => {
|
||||
uni.showToast({
|
||||
title: '网络错误,请稍后重试',
|
||||
icon: 'none'
|
||||
})
|
||||
reject(error)
|
||||
},
|
||||
complete: () => {
|
||||
// 隐藏加载状态
|
||||
if (load) {
|
||||
uni.hideLoading()
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// 为分包添加的GET请求函数
|
||||
export function packageRcGet(config) {
|
||||
if (typeof config === 'string') {
|
||||
// 兼容旧的调用方式: get(url, params, options)
|
||||
const params = arguments[1] || {};
|
||||
const options = arguments[2] || {};
|
||||
return packageRcRequest({
|
||||
url: config,
|
||||
method: 'GET',
|
||||
data: params,
|
||||
...options
|
||||
})
|
||||
}
|
||||
// 支持配置对象的调用方式: get({url, data, ...})
|
||||
return packageRcRequest({
|
||||
method: 'GET',
|
||||
...config
|
||||
})
|
||||
}
|
||||
|
||||
// 为分包添加的POST请求函数
|
||||
export function packageRcPost(config) {
|
||||
if (typeof config === 'string') {
|
||||
// 兼容旧的调用方式: post(url, data, options)
|
||||
const data = arguments[1] || {};
|
||||
const options = arguments[2] || {};
|
||||
return packageRcRequest({
|
||||
url: config,
|
||||
method: 'POST',
|
||||
data,
|
||||
...options
|
||||
})
|
||||
}
|
||||
// 支持配置对象的调用方式: post({url, data, ...})
|
||||
return packageRcRequest({
|
||||
method: 'POST',
|
||||
...config
|
||||
})
|
||||
}
|
||||
|
||||
// ======================================
|
||||
// 以上是分包(packageRc)专用请求函数区域
|
||||
// ======================================
|
||||
|
||||
// ======================================
|
||||
// 以下是主包专用请求函数区域
|
||||
// 这些是主包原有的请求函数,使用动态token和配置的baseURL
|
||||
// ======================================
|
||||
|
||||
export function request({
|
||||
url,
|
||||
method = 'GET',
|
||||
@@ -164,4 +329,48 @@ export function uploadFile(tempFilePaths, loading = false) {
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// 主包的GET请求函数 - 调用request函数,使用动态token和配置的baseURL
|
||||
// 此函数已经集成到主包中,与主包其他请求函数保持一致的行为
|
||||
// 封装GET请求
|
||||
export function get(config) {
|
||||
if (typeof config === 'string') {
|
||||
// 兼容旧的调用方式: get(url, params, options)
|
||||
const params = arguments[1] || {};
|
||||
const options = arguments[2] || {};
|
||||
return request({
|
||||
url: config,
|
||||
method: 'GET',
|
||||
data: params,
|
||||
...options
|
||||
})
|
||||
}
|
||||
// 支持配置对象的调用方式: get({url, data, ...})
|
||||
return request({
|
||||
method: 'GET',
|
||||
...config
|
||||
})
|
||||
}
|
||||
|
||||
// 主包的POST请求函数 - 调用request函数,使用动态token和配置的baseURL
|
||||
// 此函数已经集成到主包中,与主包其他请求函数保持一致的行为
|
||||
// 封装POST请求
|
||||
export function post(config) {
|
||||
if (typeof config === 'string') {
|
||||
// 兼容旧的调用方式: post(url, data, options)
|
||||
const data = arguments[1] || {};
|
||||
const options = arguments[2] || {};
|
||||
return request({
|
||||
url: config,
|
||||
method: 'POST',
|
||||
data,
|
||||
...options
|
||||
})
|
||||
}
|
||||
// 支持配置对象的调用方式: post({url, data, ...})
|
||||
return request({
|
||||
method: 'POST',
|
||||
...config
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user