You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.7 KiB
65 lines
1.7 KiB
import Request from 'luch-request'
|
|
import {
|
|
API_BASE_URL,
|
|
TENANT_ID
|
|
} from '@/config/setting';
|
|
|
|
const http = new Request();
|
|
/**
|
|
* @description 修改全局默认配置
|
|
* @param {Function}
|
|
*/
|
|
http.setConfig((config) => {
|
|
/* config 为默认全局配置*/
|
|
config.baseURL = API_BASE_URL; /* 根域名 */
|
|
|
|
return config
|
|
})
|
|
|
|
// 拦截器(请求之前拦截)
|
|
http.interceptors.request.use((config) => { // 可使用async await 做异步操作
|
|
const token = uni.getStorageSync('access_token')
|
|
config.header = {
|
|
...config.header,
|
|
tenantId: TENANT_ID
|
|
}
|
|
if (token) {
|
|
config.header.Authorization = token
|
|
}
|
|
if (config.data) {
|
|
config.data.tenantId = TENANT_ID
|
|
}
|
|
if (!token) {
|
|
// 如果token不存在,需要加签
|
|
// config.params.tenantId = TENANT_ID
|
|
// config.params.sign = getSign(config.params, appSecret);
|
|
}
|
|
return config
|
|
}, config => { // 可使用async await 做异步操作
|
|
return Promise.reject(config)
|
|
})
|
|
|
|
// 拦截器(请求之后拦截)
|
|
http.interceptors.response.use((response) => {
|
|
/* 对响应成功做点什么 可使用async await 做异步操作*/
|
|
// if (response.data.code !== 200) { // 服务端返回的状态码不等于200,则reject()
|
|
// return Promise.reject(response) // return Promise.reject 可使promise状态进入catch
|
|
// if (response.config.custom.verification) { // 演示自定义参数的作用
|
|
// return response.data
|
|
// }
|
|
|
|
// token过期
|
|
if (response.data.code == 401) {
|
|
|
|
}
|
|
if (response.data.code == 1) {
|
|
return Promise.reject(response.data)
|
|
}
|
|
return response
|
|
}, (response) => {
|
|
/* 对响应错误做点什么 (statusCode !== 200)*/
|
|
console.log(response)
|
|
return Promise.reject(response)
|
|
})
|
|
|
|
export default http
|