Files
crmeb-mp/utils/request.js
karlcarlo a5c5d753b6 feat: 添加静态资源文件及基础组件
- 新增大量图片资源,包括图标、背景图、海报等,用于页面展示
- 添加字体文件及样式配置,支持自定义字体
- 引入 uni-ui 组件库,包括图标、弹窗、日期选择器等常用组件
- 添加网络状态监听、加密工具等基础工具函数
- 配置 Vuex 状态管理,包括购物车数量、热门搜索词等
- 增加页面组件,如用户协议、分销信息、提现状态等
- 配置项目构建和忽略文件,优化开发体验
2026-02-26 17:59:51 +08:00

143 lines
3.4 KiB
JavaScript
Raw Permalink 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~2021 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 {
toLogin,
checkLogin
} from '../libs/login';
import store from '../store';
function toLoginMp(){
uni.showToast({
title: '请登录',
icon: 'none',
duration: 1000
});
}
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 baseRequest(url, method, data, {
noAuth = false,
noVerify = false
}) {
let Url = HTTP_REQUEST_URL,
header = HEADER;
if (!noAuth) {
//登录过期自动登录
if (!store.state.app.token && !checkLogin()) {
toLogin();
return Promise.reject({
msg: '未登录'
});
}
}
if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token;
return new Promise((reslove, reject) => {
uni.request({
url: Url + '/api/' + url,
method: method || 'GET',
header: header,
data: data || {},
success: (res) => {
if (res.data.data && res.data.gzde === 1) {
try{
res.data.data = JSON.parse(decompress(res.data.data));
}catch(e){
res.data.data = decompress(res.data.data);
}
}
if (noVerify)
reslove(res.data, res);
else if (res.data.status == 200)
reslove(res.data, res);
else if ([410000, 410001, 410002].indexOf(res.data.status) !== -1) {
// #ifndef MP
toLogin();
// #endif
// #ifdef MP
toLoginMp();
// #endif
reject(res.data);
} else if (res.data.status == 410010) {
uni.showModal({
title: '提示',
content: res.data.msg,
showCancel: false,
confirmText: '我知道了',
success() {
uni.switchTab({
url: '/pages/index/index'
})
}
});
} else if(res.data.status == 410020){
// reject(res.data.msg);
uni.showModal({
title: '提示',
content: res.data.msg,
showCancel: false,
confirmText: '我知道了'
});
uni.setStorageSync('authIng', true)
} else if(res.data.status == 403){
reject(res.data);
} else
reject(res.data.msg || '系统错误');
},
fail: (msg) => {
let data = {
mag: '请求失败',
status: 1 //1没网
}
// #ifdef APP-PLUS
reject(data);
// #endif
// #ifndef APP-PLUS
reject('请求失败');
// #endif
}
})
});
}
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;