123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- const {
- globalData: {
- baseUrl
- }
- } = getApp();
- export class Http {
- constructor() {}
- request({
- url,
- data = {},
- method = 'GET',
- header,
- callback = '',
- responseType,
- loadingTips = 'Loading...',
- } = {}) {
- let _this = this;
- uni.showLoading({
- title: loadingTips,
- // mask: mask ? mask : false
- });
- // mask ? uni.showNavigationBarLoading() : '';
- const apiList = ['/customer/account/customerWechartLog', '/basic/area/selectByInternational',
- '/customer/account/getLoginVerifyCodeImg', '/customer/account/apply/getApplyVerifCode', '/customer/account/apply/addApply'
- ];
- const index = apiList.indexOf(url);
- const token = index > -1 ? '' : uni.getStorageSync('token');
- const locale = uni.getStorageSync('locale') || 'zh_CN';
- return new Promise((resolve, reject) => {
- uni.request({
- url: `${baseUrl}${url}`,
- data,
- method,
- header: {
- 'Content-Type': 'application/json',
- Authorization: token ? 'bearer ' + token : '',
- 'Accept-Language': locale,
- 'portalType': 2
- },
- callback,
- responseType,
- success: (res) => {
- let statusCode = res.statusCode;
- let resData = res.data;
- if (callback) return callback(resData.data);
- if (statusCode == 200) {
- // 如果请求返回的是文件流,需要单独处理
- if (responseType == 'arraybuffer') {
- resolve(resData);
- return;
- }
- if (resData.code === '0000') {
- resolve(resData.data);
- } else {
- reject(resData, statusCode)
- }
- } else if (statusCode == 404) {
- console.log('接口不存在');
- } else if (statusCode == 401) {
- uni.reLaunch({
- url: '/pages/login/login'
- });
- } else {
- uni.showModal({
- title: '提示',
- content: res.data.message,
- showCancel: false,
- confirmText: '确认'
- });
- reject(resData, statusCode)
- }
- },
- fail: (err) => {
- console.log('fail', err);
- uni.showModal({
- title: '提示',
- content: `Network request failed`,
- showCancel: false,
- confirmText: '确认'
- });
- },
- complete: function(res) {
- uni.hideLoading && uni.hideLoading();
- uni.hideNavigationBarLoading() && uni.hideNavigationBarLoading();
- }
- })
- })
- }
- }
|