|
|
@@ -1,15 +1,11 @@
|
|
|
import config, { getHeaders } from './config';
|
|
|
import LocalStorage from '@/LocalStorage';
|
|
|
|
|
|
-type Config = Partial<{
|
|
|
- baseURL: string,
|
|
|
-}>;
|
|
|
-
|
|
|
interface Api {
|
|
|
- get: (url: string, data?: any, config?: Config) => Promise<any>,
|
|
|
- post: (url: string, data?: any, config?: Config) => Promise<any>,
|
|
|
- put: (url: string, data?: any, config?: Config) => Promise<any>,
|
|
|
- delete: (url: string, data?: any, config?: Config) => Promise<any>,
|
|
|
+ get: (url: string, data?: any) => Promise<any>,
|
|
|
+ post: (url: string, data?: any) => Promise<any>,
|
|
|
+ put: (url: string, data?: any) => Promise<any>,
|
|
|
+ delete: (url: string, data?: any) => Promise<any>,
|
|
|
};
|
|
|
|
|
|
const apiGenerator = (): Api => {
|
|
|
@@ -18,7 +14,6 @@ const apiGenerator = (): Api => {
|
|
|
method: Method,
|
|
|
url: string,
|
|
|
data?: any,
|
|
|
- requestConfig?: Config,
|
|
|
): Promise<any> {
|
|
|
const options: {
|
|
|
header: Object,
|
|
|
@@ -30,9 +25,6 @@ const apiGenerator = (): Api => {
|
|
|
method: method,
|
|
|
url: config.baseURL + url,
|
|
|
};
|
|
|
- if (requestConfig?.baseURL) {
|
|
|
- options.url = requestConfig.baseURL + url;
|
|
|
- };
|
|
|
if (Object.keys(data).length !== 0) {
|
|
|
const params: { [key: string]: any } = {};
|
|
|
for (const key in data) {
|
|
|
@@ -53,30 +45,31 @@ const apiGenerator = (): Api => {
|
|
|
if (data.code === 200) {// 成功
|
|
|
resolve(data);
|
|
|
} else {// 失败
|
|
|
- reject(data);
|
|
|
+ if (data.code === 401) {
|
|
|
+ LocalStorage.clear();
|
|
|
+ uni.reLaunch({
|
|
|
+ url: '/pages/login/index/index',
|
|
|
+ success: () => {
|
|
|
+ uni.showToast({
|
|
|
+ icon: 'none',
|
|
|
+ mask: true,
|
|
|
+ duration: 2000,
|
|
|
+ title: '登录过期',
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ reject();
|
|
|
+ } else {
|
|
|
+ reject(data);
|
|
|
+ }
|
|
|
}
|
|
|
} else {// 错误信息
|
|
|
- if (statusCode === 401) {
|
|
|
- LocalStorage.clear();
|
|
|
- uni.reLaunch({
|
|
|
- url: '/pages/login/index',
|
|
|
- success: () => {
|
|
|
- uni.showToast({
|
|
|
- icon: 'none',
|
|
|
- mask: true,
|
|
|
- duration: 2000,
|
|
|
- title: '登录过期',
|
|
|
- });
|
|
|
- }
|
|
|
- });
|
|
|
- } else {
|
|
|
- uni.showToast({
|
|
|
- icon: 'none',
|
|
|
- mask: true,
|
|
|
- duration: 2000,
|
|
|
- title: '服务异常',
|
|
|
- });
|
|
|
- }
|
|
|
+ uni.showToast({
|
|
|
+ icon: 'none',
|
|
|
+ mask: true,
|
|
|
+ duration: 2000,
|
|
|
+ title: '服务异常',
|
|
|
+ });
|
|
|
reject();
|
|
|
}
|
|
|
},
|
|
|
@@ -93,17 +86,17 @@ const apiGenerator = (): Api => {
|
|
|
});
|
|
|
}
|
|
|
return {
|
|
|
- get: (url, data = {}, config) => {
|
|
|
- return request('GET', url, data, config);
|
|
|
+ get: (url, data = {}) => {
|
|
|
+ return request('GET', url, data);
|
|
|
},
|
|
|
- post: (url, data = {}, config) => {
|
|
|
- return request('POST', url, data, config);
|
|
|
+ post: (url, data = {}) => {
|
|
|
+ return request('POST', url, data);
|
|
|
},
|
|
|
- put: (url, data = {}, config) => {
|
|
|
- return request('PUT', url, data, config);
|
|
|
+ put: (url, data = {}) => {
|
|
|
+ return request('PUT', url, data);
|
|
|
},
|
|
|
- delete: (url, data = {}, config) => {
|
|
|
- return request('DELETE', url, data, config);
|
|
|
+ delete: (url, data = {}) => {
|
|
|
+ return request('DELETE', url, data);
|
|
|
},
|
|
|
}
|
|
|
};
|