uniapp-app/utils/request.js
2025-08-19 11:34:07 +08:00

98 lines
2.4 KiB
JavaScript

/**
* @Method Description
* @Author: zjd@
* @Description: 数据请求整合 处理
* @BASE_URL server
* @param {a===Object||file} 传给后台参数Method 请求方法 url 所请求的接口路径
* @return Promise对象 所有数据信息
* @createTime: 2024-7-22 15:05:06
*/
import BASE_URL from "./config.js";
export const request = (url, data = {}, method = 'post', loading = false, contentType =
'application/json',extraHeader={}) => {
if (loading) {
uni.showLoading({
title: '加载中',
mask: true
})
};
let token = '';
if (process.env.UNI_PLATFORM == "h5") {
if (window.location.href.indexOf('//App.igandan.com') > -1) {
token = uni.getStorageSync('AUTH_TOKEN_App');
} else {
token = uni.getStorageSync('DEV_AUTH_TOKEN_App');
}
} else if(process.env.UNI_PLATFORM == "mp-weixin") {
const {
envVersion
} = uni.getAccountInfoSync().miniProgram;
if (envVersion == "release") {
token = uni.getStorageSync('AUTH_TOKEN_App');
} else {
token = uni.getStorageSync('DEV_AUTH_TOKEN_App');
}
}
let header = {
'content-type': contentType,
'Authorization': 'Bearer ' + token
}
return new Promise(function(e, n) {
let timestamp = Date.now();
uni.request({
data,
url: url.indexOf('http') != -1 ? url : encodeURI(BASE_URL + url),
method: method,
sslVerify: false,
header:{...header,...extraHeader},
timeout: 10000,
success: async (res) => {
var Authorization_token = res.header.Authorization;
if (Authorization_token) {
if (process.env.NODE_ENV === 'development') {
uni.setStorageSync('DEV_AUTH_TOKEN_App', Authorization_token);
} else {
uni.setStorageSync('AUTH_TOKEN_App', Authorization_token);
}
}
if (loading) {
uni.hideLoading();
};
if(url.indexOf('manager/getSystemTimeStamp')!=-1){
e(res)
}else if (res.data.code == 200 || res.data.code ==1){
e(res)
}else if (res.data.code == 401 || res.data.code == 403 || res.data.code ==
405 || res.data.code == 406) {
uni.redirectTo({
url: '/pages_app/login/login'
});
} else if (res.data.code == 500) {
uni.showToast({
title: res.data.message,
icon: 'none',
})
n(res)
} else {
uni.showToast({
title: res.data.message,
icon: 'none',
});
n(res)
}
},
fail: function(err) {
"request:fail " === err.errMsg && msg("请求数据失败!"), n(err.data);
}
});
});
}