第一次提交
This commit is contained in:
85
api/index.js
Normal file
85
api/index.js
Normal file
@@ -0,0 +1,85 @@
|
||||
import Request from 'luch-request'
|
||||
import storage from '@/utils/storage'
|
||||
import {
|
||||
apiUrl,
|
||||
tenantId,
|
||||
appId,
|
||||
appSecret
|
||||
} from '@/config.js';
|
||||
import {
|
||||
getSign
|
||||
} from '@/utils/util.js'
|
||||
import {
|
||||
ACCESS_TOKEN
|
||||
} from '@/store/mutation-types'
|
||||
|
||||
const http = new Request();
|
||||
|
||||
|
||||
/**
|
||||
* @description 修改全局默认配置
|
||||
* @param {Function}
|
||||
*/
|
||||
http.setConfig((config) => {
|
||||
/* config 为默认全局配置*/
|
||||
config.baseURL = apiUrl; /* 根域名 */
|
||||
|
||||
return config
|
||||
})
|
||||
|
||||
// 拦截器(请求之前拦截)
|
||||
http.interceptors.request.use((config) => { // 可使用async await 做异步操作
|
||||
const token = storage.get(ACCESS_TOKEN)
|
||||
config.header = {
|
||||
...config.header,
|
||||
tenantId
|
||||
}
|
||||
if(token){
|
||||
config.header.Authorization = token
|
||||
config.header.appId = appId
|
||||
}
|
||||
if (config.data) {
|
||||
config.data.tenantId = tenantId
|
||||
}
|
||||
if (!token) {
|
||||
// 如果token不存在,需要加签
|
||||
config.params.tenantId = tenantId
|
||||
config.params.sign = getSign(config.params,appSecret);
|
||||
}else {
|
||||
|
||||
}
|
||||
/**
|
||||
/* 演示
|
||||
if (!token) { // 如果token不存在,return Promise.reject(config) 会取消本次请求
|
||||
return Promise.reject(config)
|
||||
}
|
||||
**/
|
||||
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.data
|
||||
}, (response) => {
|
||||
/* 对响应错误做点什么 (statusCode !== 200)*/
|
||||
console.log(response)
|
||||
return Promise.reject(response)
|
||||
})
|
||||
|
||||
export default http
|
||||
Reference in New Issue
Block a user