兼兼街公众号代码
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.
 
 
 

69 lines
1.8 KiB

import Request from './request'
import apiList from './shopro'
// import store from '@/common/store/index.js'
export default function api(url, data = {}) {
const request = new Request();
let api = getApiObj(url);
request.interceptor.request((config, cancel) => { /* 请求之前拦截器 */
if (api.auth) {
let userToken = uni.getStorageSync('userToken');
if (!userToken) {
// store.commit('LOGIN_TIP', true)
// store.commit('OUT_LOGIN');
throw('暂未登录,已阻止此次API请求~');
}
}
if (uni.getStorageSync('userToken')) {
config.header['X-Access-Token'] = uni.getStorageSync('userToken');
}
return config
});
request.interceptor.response((response) => { /* 请求之后拦截器 */
// if (response.data.code === 0) { // 服务端返回的状态码不等于200,则reject()
// uni.showToast({
// title: response.data.msg || '请求出错,稍后重试',
// icon: 'none',
// duration: 1000,
// mask: true
// });
// }
if (response.data.status === 7001) { // 服务端返回的状态码不等于200,则reject()
uni.clearStorageSync()
uni.showToast({
title: '登录过期,请重新登录'
})
uni.reLaunch({
url: '/pages/login/login'
})
// store.commit('LOGIN_TIP', true)
}
// if (response.config.custom.verification) { // 演示自定义参数的作用
// return response.data
// }
return response
}, (response) => { // 预留可以日志上报
return response
})
let option = {
url: api.url,
data,
method: api.method
}
if (api.headers!=null){
option.header = api.headers
}
return request.request(option)
}
function getApiObj(url) {
let apiArray = url.split(".");
let api = apiList;
apiArray.forEach(v => {
api = api[v];
});
return api;
}