Files
shanghaiganxiangtuzhuangwor…/utils/request.js

104 lines
2.9 KiB
JavaScript
Raw Normal View History

2024-03-12 09:27:48 +08:00
import store from '@/store'
import config from '@/config'
2024-03-15 17:56:22 +08:00
import {
getBaseUrl,
setBaseUrl,
removeBaseUrl
} from '@/utils/baseUrl';
import {
getToken
} from '@/utils/auth'
2024-03-12 09:27:48 +08:00
import errorCode from '@/utils/errorCode'
2024-03-15 17:56:22 +08:00
import {
toast,
showConfirm,
tansParams
} from '@/utils/common'
2024-03-12 09:27:48 +08:00
let timeout = 10000
const request = config => {
2024-03-22 17:28:39 +08:00
// 浏览器测试用
2024-04-15 11:36:59 +08:00
// const storageBaseUrl = 'http://' + getBaseUrl() + '/api';
2024-03-22 17:28:39 +08:00
// 真机测试以及部署用
2024-04-14 11:17:15 +08:00
const storageBaseUrl = 'http://' + getBaseUrl();
2024-03-15 17:56:22 +08:00
// 是否需要设置 token
const isToken = (config.headers || {}).isToken === false
config.header = config.header || {}
2024-04-14 11:17:15 +08:00
// 假设你使用的是PHP作为你的后端语言
config.header['Access-Control-Allow-Origin']= '*'; // 允许任何源访问
config.header['Access-Control-Allow-Methods']='GET, POST, OPTIONS'; // 允许的HTTP方法
config.header['Access-Control-Allow-Headers']= 'X-Requested-With'; // 允许的HTTP头
2024-03-15 17:56:22 +08:00
if (getToken() && !isToken) {
config.header['Authorization'] = 'Bearer ' + getToken()
2024-04-14 11:17:15 +08:00
2024-03-15 17:56:22 +08:00
}
// get请求映射params参数
if (config.params) {
let url = config.url + '?' + tansParams(config.params)
url = url.slice(0, -1)
config.url = url
}
return new Promise((resolve, reject) => {
2024-04-24 19:52:37 +08:00
2024-03-15 17:56:22 +08:00
uni.request({
method: config.method || 'get',
timeout: config.timeout || timeout,
url: config.baseUrl || (storageBaseUrl + config.url),
data: config.data,
header: config.header,
dataType: 'json'
}).then(response => {
let [error, res] = response
2024-04-14 11:17:15 +08:00
// console.log('token:','Bearer ' + getToAccess-Control-Allow-Originken());
2024-03-22 17:28:39 +08:00
// console.log('request:',storageBaseUrl + config.url,response);
2024-03-15 17:56:22 +08:00
if (error) {
2024-04-24 14:56:11 +08:00
showConfirm(error)
// toast('后端接口连接异常')
2024-03-15 17:56:22 +08:00
reject('后端接口连接异常')
return
}
const code = res.data.code || 200
const msg = errorCode[code] || res.data.msg || errorCode['default']
if (code === 401) {
showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
if (res.confirm) {
store.dispatch('LogOut').then(res => {
uni.reLaunch({
url: '/pages/login'
})
})
}
})
reject('无效的会话,或者会话已过期,请重新登录。')
} else if (code === 500) {
2024-04-24 14:56:11 +08:00
// toast(msg)
2024-04-24 16:42:51 +08:00
showConfirm(msg)
2024-03-15 17:56:22 +08:00
reject('500')
} else if (code !== 200) {
2024-04-24 14:56:11 +08:00
// toast(msg)
2024-04-24 16:42:51 +08:00
showConfirm(msg)
2024-03-15 17:56:22 +08:00
reject(code)
}
resolve(res.data)
})
.catch(error => {
2024-04-24 19:52:37 +08:00
2024-03-15 17:56:22 +08:00
let {
message
} = error
if (message === 'Network Error') {
2024-04-24 14:54:20 +08:00
message = '后端接口连接异常,Network Error'
2024-03-15 17:56:22 +08:00
} else if (message.includes('timeout')) {
message = '系统接口请求超时'
} else if (message.includes('Request failed with status code')) {
message = '系统接口' + message.substr(message.length - 3) + '异常'
}
2024-04-24 14:56:11 +08:00
// toast(message)
showConfirm(message)
2024-03-15 17:56:22 +08:00
reject(error)
})
})
2024-03-12 09:27:48 +08:00
}
2024-03-15 17:56:22 +08:00
export default request