new_shop_app/utils/request.js

138 lines
4.0 KiB
JavaScript
Raw Normal View History

2024-02-19 10:44:33 +08:00
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import {
HTTP_REQUEST_URL,
HEADER,
TOKENNAME
} from '@/config/app';
import { checkLogin } from '../libs/login';
import store from '../store';
import pako from '../plugin/pako/pako.es5.min.js'
function toLogin(){
store.commit("LOGOUT");
uni.showToast({
title: '请登录',
icon: 'none',
duration: 1000
});
}
function decompress(str) {
return pako.inflateRaw(base64ToUint8Array(str), {
to: 'string'
});
}
function base64ToUint8Array(base64String) {
let padding = '='.repeat((4 - base64String.length % 4) % 4);
let base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');
let rawData = atob(base64);
let outputArray = new Uint8Array(rawData.length);
for (var i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
function atob(input) {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
let str = input.replace(/=+$/, '');
let output = '';
if (str.length % 4 === 1) {
throw new Error('InvalidLengthError');
}
for (let i = 0, len = str.length; i < len; i += 4) {
const a = chars.indexOf(str.charAt(i));
const b = chars.indexOf(str.charAt(i + 1));
const c = chars.indexOf(str.charAt(i + 2));
const d = chars.indexOf(str.charAt(i + 3));
const sum = (a << 18) | (b << 12) | (c << 6) | d;
output += String.fromCharCode((sum >> 16) & 0xFF, (sum >> 8) & 0xFF, sum & 0xFF);
}
return output;
}
2024-03-26 14:28:26 +08:00
var HTTP_list = new Map();
2024-02-19 10:44:33 +08:00
/**
* 发送请求
*/
function baseRequest(url, method, data, {
noAuth = false,
2024-03-26 14:28:26 +08:00
noVerify = false,
enLoad = false //终止上一个接口相同类型正在请求的接口, 防止快速切换tab时页面抖动
2024-02-19 10:44:33 +08:00
}) {
let Url = HTTP_REQUEST_URL,
header = HEADER;
if (!noAuth) {
//登录过期自动登录
if (!store.state.app.token && !checkLogin()) {
toLogin();
return Promise.reject({
msg: '未登录'
});
}
}
2024-03-26 14:28:26 +08:00
let URL = Url + '/api/' + url
console.log(enLoad, HTTP_list);
if(enLoad) {
let http = HTTP_list.get(URL);
if(http){
http.abort();
}
}
2024-02-19 10:44:33 +08:00
if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token;
2024-03-26 14:28:26 +08:00
if(store.state.app.uuid) header['uuid'] = store.state.app.uuid
2024-02-19 10:44:33 +08:00
return new Promise((reslove, reject) => {
2024-03-26 14:28:26 +08:00
let http = uni.request({
url: URL,
2024-02-19 10:44:33 +08:00
method: method || 'GET',
header: header,
data: data || {},
success: (res) => {
if (res.data && res.data.encode) {
try
{
res.data = JSON.parse(decompress(res.data.data));
}catch(e){
res.data = decompress(decodeURI(res.data.data));
}
}
if (noVerify)
reslove(res.data, res);
else if (res.data.status == 200)
reslove(res.data, res);
else if ([410000, 410001, 410002, 40000].indexOf(res.data.status) !== -1) {
toLogin();
reject(res.data);
} else if (res.data.status == 501) {
uni.reLaunch({
url: '/pages/error/index'
})
reject(res.data);
} else
reject(res.data.message || '系统错误');
},
fail: (message) => {
reject('请求失败');
2024-03-26 14:28:26 +08:00
},
complete: () => {
// if(enLoad) HTTP_list.delete(URL);
}
2024-02-19 10:44:33 +08:00
})
2024-03-26 14:28:26 +08:00
if(enLoad) HTTP_list.set(URL, http);
2024-02-19 10:44:33 +08:00
});
}
const request = {};
['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
request[method] = (api, data, opt) => baseRequest(api, method, data, opt || {})
});
export default request;