new_shop_app/utils/requestSupplier.js
2024-05-22 17:46:00 +08:00

131 lines
3.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// +----------------------------------------------------------------------
// | 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_TWO,
HEADER,
TOKENNAME
} from '@/config/app';
import {
checkLogin
} from '../libs/login';
import store from '../store';
import pako from '../plugin/pako/pako.es5.min.js'
import {
Tips
} from "@/utils/util.js"
function toLogin() {
}
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;
}
var HTTP_list = new Map();
/**
* 发送请求
*/
let that = this
function baseRequest(url, method, data, {
noAuth = false,
noVerify = false,
tokenName = 0, //判断token名称值的Boolean值为true时为X-token,一般为token
enLoad = false //终止上一个接口相同类型正在请求的接口, 防止快速切换tab时页面抖动
}) {
let Url = HTTP_REQUEST_URL_TWO,
header = uni.$u.deepClone(HEADER);
let URL = Url + '/api/' + url
if (enLoad) {
let http = HTTP_list.get(URL);
if (http) {
http.abort();
}
}
HTTP_list.delete(URL);
if (tokenName) {
// if (store.state.app.token) header[TOKENNAME] = 'Bearer' + store.state.app.token;
if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token;
} else {
if (store.state.app.s_token) header['Token'] = store.state.app.s_token;
}
if (store.state.app.uuid) header['uuid'] = store.state.app.uuid
return new Promise((reslove, reject) => {
let http = uni.request({
url: URL,
method: method || 'GET',
header: header,
data: data || {},
success: (res) => {
console.log(res.data.show)
if (res.data.code == 1) {
reslove(res.data, res);
}
if (res.data.show) {
uni.showToast({
title: res.data.msg,
icon: 'none'
});
} else {
reslove(res.data, res);
}
},
fail: (message) => {
reject('请求失败');
},
complete: () => {
// if(enLoad) HTTP_list.delete(URL);
}
})
if (enLoad) HTTP_list.set(URL, http);
});
}
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;