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.

191 lines
5.1 KiB

12 months ago
import Taro, { clearStorage } from '@tarojs/taro';
import { CommonRes, CommonResNew } from '../services/common';
import { setGlobalData, getGlobalData } from './global-data';
11 months ago
import { BASE_URL, BASE_URL_ZQ } from './request-enhance';
12 months ago
import { showModal } from './wechat-ui';
type Method =
| 'OPTIONS'
| 'GET'
| 'HEAD'
| 'POST'
| 'PUT'
| 'DELETE'
| 'TRACE'
| 'CONNECT';
const request = (
url: string,
data?: unknown,
method: Method = 'POST',
headers = {},
isShowLoading = true
): Promise<any> => {
const option = {
url,
data,
method,
header: {
Authorization: Taro.getStorageSync('authorization'),
...headers,
},
};
if (isShowLoading) {
Taro.showLoading({ title: '加载中..' });
}
return Taro.request(option)
.then(({ statusCode, data }: { statusCode: number; data: CommonRes }) => {
console.log('response data', data);
if (isShowLoading) {
Taro.hideLoading();
}
if (statusCode >= 200 && statusCode < 300) {
if (data.code === 1000) {
return data.data || '请求成功';
// throw new Error('故意的');
} else if (data.code === 1002) {
clearStorage();
if (!getGlobalData('isNavToLogin')) {
setGlobalData('isNavToLogin', true);
showModal(
'提示',
'凭证过期需要重新登录以访问更多功能,是否需要登录?'
).then((res) => {
if (res.confirm) {
Taro.navigateTo({ url: '/pages/login/login' }).then(() => {
setTimeout(() => {
setGlobalData('isNavToLogin', false);
}, 3000);
});
}
});
}
throw new Error(data.msg);
} else {
throw new Error(data.msg);
}
} else {
const msg = `Error: code ${statusCode}`;
throw new Error(msg);
}
})
.catch((error) => {
if (isShowLoading) {
Taro.hideLoading();
}
Taro.showToast({
11 months ago
title: error.message || 'error(─‿─)1',
12 months ago
icon: 'none',
duration: 1500,
mask: true,
});
return null;
});
};
const requestNew = (
url: string,
data?: unknown,
method: Method = 'POST',
headers = {'Content-Type':'application/x-www-form-urlencoded'},
isShowLoading = true
): Promise<any> => {
const option = {
url,
data,
method,
header: {
Authorization: Taro.getStorageSync('authorization'),
...headers,
},
};
if (isShowLoading) {
Taro.showLoading({ title: '加载中..' });
}
return Taro.request(option)
.then(({ statusCode, data }: { statusCode: number; data: CommonResNew }) => {
console.log('response data', data);
if (isShowLoading) {
Taro.hideLoading();
}
if (statusCode >= 200 && statusCode < 300) {
if (data.code === 1000) {
return data || '请求成功';
// throw new Error('故意的');
} else if (data.code === 1002) {
clearStorage();
if (!getGlobalData('isNavToLogin')) {
setGlobalData('isNavToLogin', true);
showModal(
'提示',
'凭证过期需要重新登录以访问更多功能,是否需要登录?'
).then((res) => {
if (res.confirm) {
Taro.navigateTo({ url: '/pages/login/login' }).then(() => {
setTimeout(() => {
setGlobalData('isNavToLogin', false);
}, 3000);
});
}
});
}
throw new Error(data.msg);
} else {
throw new Error(data.msg);
}
} else {
const msg = `Error: code ${statusCode}`;
throw new Error(msg);
}
})
.catch((error) => {
if (isShowLoading) {
Taro.hideLoading();
}
Taro.showToast({
11 months ago
title: error.message || 'error(─‿─)2',
12 months ago
icon: 'none',
duration: 1500,
mask: true,
});
return null;
});
};
export const RzRequest = {
get<T>(url = '/', data = {}, headers = {}): Promise<T | null> {
return request(BASE_URL + url, data, 'GET', headers);
},
post<T>(url = '/', data = {}, isShowLoading = true): Promise<T | null> {
return request(BASE_URL + url, data, 'POST', undefined, isShowLoading);
},
postNew(url = '/', data = {}, isShowLoading = true) {
11 months ago
let baseUrl = BASE_URL;
if (url.includes('ZhuQue')) {
baseUrl = BASE_URL_ZQ;
}
return requestNew(baseUrl + url, data, 'POST', undefined, isShowLoading);
12 months ago
},
getNew(url = '/', data = {}, isShowLoading = true) {
11 months ago
let baseUrl = BASE_URL;
if (url.includes('ZhuQue')) {
baseUrl = BASE_URL_ZQ;
}
return requestNew(baseUrl + url, data, 'GET', undefined, isShowLoading);
12 months ago
},
put<T>(url = '/', data = {}): Promise<T | null> {
return request(BASE_URL + url, data, 'PUT');
},
delete<T>(url = '/', data = {}): Promise<T | null> {
return request(BASE_URL + url, data, 'DELETE');
},
};