合并人才集团代码
This commit is contained in:
149
packageRc/utils/request.js
Normal file
149
packageRc/utils/request.js
Normal file
@@ -0,0 +1,149 @@
|
||||
// 导入当前目录下的auth.js,使用更具体的路径以避免混淆
|
||||
import { getToken } from './auth.js';
|
||||
|
||||
// 配置API基础URL
|
||||
const baseURL = 'http://10.160.0.5:8907/'
|
||||
|
||||
// 是否显示重新登录
|
||||
export let isRelogin = { show: false }
|
||||
|
||||
/**
|
||||
* 封装uni.request
|
||||
* @param {Object} options - 请求配置
|
||||
*/
|
||||
export function request(options) {
|
||||
// 显示加载状态
|
||||
if (options.load) {
|
||||
uni.showLoading({
|
||||
title: '请稍候',
|
||||
mask: true
|
||||
})
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
// 是否需要设置token
|
||||
const isToken = options.headers && options.headers.isToken === false
|
||||
|
||||
// 添加固定的Authorization token
|
||||
if (!isToken) {
|
||||
options.headers = options.headers || {}
|
||||
options.headers['Authorization'] = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiJzeXNfdXNlcjoxIiwicm5TdHIiOiJsNDRvNmhxckdOOW1XcG1pWlpJcXZ0UXJMdGsyTlFhQSIsInVzZXJJZCI6MX0.K5H-Rpof9oI1CCIEtheEQ96XGAxPWS7tzyjPbXwAXrQ'
|
||||
}
|
||||
|
||||
// 确保URL是字符串类型
|
||||
let url = options.url;
|
||||
console.log("options.url:", url)
|
||||
if (typeof url !== 'string') {
|
||||
console.error('URL must be a string:', url);
|
||||
url = String(url);
|
||||
}
|
||||
|
||||
// 发起请求
|
||||
uni.request({
|
||||
url: baseURL + url,
|
||||
method: options.method || 'GET',
|
||||
data: options.data || {},
|
||||
header: options.headers || {
|
||||
'Content-Type': 'application/json;charset=utf-8'
|
||||
},
|
||||
timeout: options.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 (options.load) {
|
||||
uni.hideLoading()
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// 封装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请求
|
||||
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