parent
c0a2dc6eab
commit
e4f86ae640
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,82 +1,79 @@
|
|||||||
|
|
||||||
|
|
||||||
import Request from './request'
|
import Request from './request'
|
||||||
import apiList from './apis.js'
|
import apiList from './apis.js'
|
||||||
import store from '@/shopro/store/index.js'
|
import store from '@/shopro/store/index.js'
|
||||||
|
|
||||||
const shoproRequest = new Request();
|
const shoproRequest = new Request();
|
||||||
|
|
||||||
export default {
|
export default function jsonrpc(
|
||||||
install(Vue) {
|
url,
|
||||||
Vue.prototype.$jsonrpc = function (
|
data = {},
|
||||||
url,
|
toastBefore = '', // 请求前加载提示
|
||||||
data = {},
|
toastAfter = true, // 请求后错误提示
|
||||||
toastBefore = '', // 请求前加载提示
|
) {
|
||||||
toastAfter = true, // 请求后错误提示
|
|
||||||
) {
|
if (typeof url !== "string") {
|
||||||
|
return;
|
||||||
if (typeof url !== "string") {
|
}
|
||||||
return;
|
let api = getApiPath(url);
|
||||||
}
|
/* 请求之前拦截器 */
|
||||||
let api = getApiPath(url);
|
shoproRequest.interceptor.request((config, cancel) => {
|
||||||
/* 请求之前拦截器 */
|
let token = uni.getStorageSync('token');
|
||||||
shoproRequest.interceptor.request((config, cancel) => {
|
if (api.auth && !token) {
|
||||||
let token = uni.getStorageSync('token');
|
console.log(token)
|
||||||
if (api.auth && !token) {
|
store.dispatch('showAuthModal', "smsLogin");
|
||||||
store.dispatch('showAuthModal',"smsLogin");
|
uni.hideLoading();
|
||||||
uni.hideLoading();
|
throw (`暂未登录,已阻止此次API请求: '${api.url}'`);
|
||||||
throw (`暂未登录,已阻止此次API请求: '${api.url}'`);
|
}
|
||||||
}
|
token && shoproRequest.setConfig(config => {
|
||||||
token && shoproRequest.setConfig(config => {
|
config.header.token = token;
|
||||||
config.header.token = token;
|
});
|
||||||
});
|
if (toastBefore !== '') {
|
||||||
if (toastBefore !== '') {
|
uni.showLoading({
|
||||||
uni.showLoading({
|
title: toastBefore,
|
||||||
title: toastBefore,
|
mask: true
|
||||||
mask: true
|
});
|
||||||
});
|
}
|
||||||
}
|
return config;
|
||||||
return config;
|
});
|
||||||
});
|
|
||||||
|
|
||||||
/* 请求之后拦截器 */
|
/* 请求之后拦截器 */
|
||||||
shoproRequest.interceptor.response((response) => {
|
shoproRequest.interceptor.response((response) => {
|
||||||
uni.hideLoading();
|
uni.hideLoading();
|
||||||
if (response.code === 0) {
|
if (response.code === 0) {
|
||||||
if (toastAfter) {
|
if (toastAfter) {
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: response.msg || '请求出错,稍后重试',
|
title: response.msg || '请求出错,稍后重试',
|
||||||
icon: 'none',
|
icon: 'none',
|
||||||
duration: 1000,
|
duration: 1000,
|
||||||
mask: true
|
mask: true
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// token过期注销
|
// token过期注销
|
||||||
if (response.code === 401) {
|
if (response.code === 401) {
|
||||||
store.dispatch('logout');
|
store.dispatch('logout');
|
||||||
store.dispatch('showAuthModal');
|
store.dispatch('showAuthModal');
|
||||||
throw (`登录已过期或注销,已阻止此次API请求: '${api.url}'`);
|
throw (`登录已过期或注销,已阻止此次API请求: '${api.url}'`);
|
||||||
}
|
}
|
||||||
return response;
|
return response;
|
||||||
});
|
});
|
||||||
|
|
||||||
return shoproRequest.requestJson({
|
return shoproRequest.requestJson({
|
||||||
url: api.url,
|
url: api.url,
|
||||||
data,
|
data,
|
||||||
method: api.method
|
method: api.method
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
;
|
||||||
|
|
||||||
// 组装接口路径
|
// 组装接口路径
|
||||||
function getApiPath(url) {
|
function getApiPath(url) {
|
||||||
let apiArray = url.split(".");
|
let apiArray = url.split(".");
|
||||||
let api = apiList;
|
let api = apiList;
|
||||||
apiArray.forEach(v => {
|
apiArray.forEach(v => {
|
||||||
api = api[v];
|
api = api[v];
|
||||||
});
|
});
|
||||||
return api;
|
return api;
|
||||||
}
|
}
|
||||||
Loading…
Reference in new issue