eeds_app/common/request.js
2024-07-17 08:31:04 +08:00

155 lines
4.9 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.

/**
* @license
*
* 网络请求拦截器 版本1.0.1
* (c) 2023年 LinXi
* Apache 2.0 开源协议
*/
import {URL_BASE,URL_KAOSHI} from '/common/config.js'
export default {
config: {
// 请求拦截器(给请求统一添加 公共请求头、baserUrl、method)
beforeRequest(options = {}) {
// console.log("beforeRequest");
if(typeof options.def === "undefined"){
uni.showLoading({ title: '请求中' })
}
return new Promise((resolve, reject) => {
let selectType = uni.getStorageSync('select_type');
// console.log("request中selectType的值"+selectType);
let baseUrl = "";
if(selectType===1){
baseUrl = URL_BASE;
}else if(selectType===2){
baseUrl = URL_KAOSHI;
}
let token = '';
let tokenName = '';
try {
token = uni.getStorageSync('token')
} catch (e) {
uni.hideLoading()
reject('获取token失败')
}
if (!selectType || '' === token || typeof token === 'undefined' || null === token) {
uni.hideLoading()
uni.showModal({
title: '提示',
content: '登录失效,请重新登录',
confirmText: '重新登录',
showCancel: false,
complete: function(res) {
uni.redirectTo({ url: '/pages/base/denglu' })
}
});
reject('登录失效')
}
// 添加公共请求参数
options.url = baseUrl + options.url;
let headerObj = {};
if(selectType===1){
headerObj = { token: token }
}else if(selectType===2){
tokenName = uni.getStorageSync('tokenName');
headerObj[tokenName] = token;
// headerObj["content-type"] = "application/json;charset=utf-8";
// headerObj["content-type"] = "application/x-www-form-urlencoded";
}
// console.log("headerObj的值");
// console.log(headerObj);
options.header = headerObj;
options.method = options.method || 'GET';
options.timeout = 20000;
options.sslVerify = false;
resolve(options);
})
},
// 响应拦截器 (接收参数请求后得到的数据处理非成功数据reject并将数据剥离返回resolve)
responseRequest(res) {
// console.log(res);
uni.hideLoading()
//正常返回数据
return new Promise((resolve, reject) => {
// console.info('-------------', JSON.stringify(res.data))
if(!res.data && res.statusCode===502){
uni.showToast({ title:'token失效请重新登录', duration: 2000, icon: 'none' });
uni.redirectTo({ url: '/pages/base/denglu' });
return reject("token失效请重新登录")
}
let selectType = uni.getStorageSync('select_type');
if(selectType===1){
// console.log(res);
if (res.data && res.data.code !== '200') {
uni.showToast({ title: res.data.msg || '请求失败', duration: 2000, icon: 'none' })
if (msg === 'Token 令牌不合法,请重新登录') {
uni.redirectTo({ url: '/pages/base/denglu' })
}
return reject(msg)
}
// console.info('=============', JSON.stringify(res.data.data))
return resolve(res.data.data)
}else if(selectType===2){
// console.log("111");
// console.log(res);
if (!res.data.flag) {
uni.showToast({ title: res.data.message || '请求失败', duration: 2000, icon: 'none' })
if (res.data.message === '登录异常') {
uni.redirectTo({ url: '/pages/base/denglu' });
}
return reject(res.data.message)
}
return resolve(res.data.data);
}
})
}
},
request(options) {
// console.log(options);
return this.config.beforeRequest(options).then(opt => {
// console.log(opt);
return uni.request(opt)
}).then(this.config.responseRequest,(error)=>{
uni.hideLoading();
uni.showToast({ title: '服务器异常,请联系系统管理员', duration: 2000, icon: 'none' });
// uni.showToast({ '服务器请求异常,请联系系统管理员', duration: 2000, icon: 'none' });
})
},
get(url, params = null, options = {}) {
options.url = url
options.url += params ? ('?' + Object.keys(params).map(key => key + '=' + params[key]).join('&')) : ''
options.method = 'GET'
return this.request(options)
},
post(url, data = null, options = {}) {
options.url = url
options.data = data
options.method = 'POST'
return this.request(options)
},
delete(url, params = null, options = {}) {
options.url = url
options.url += params ? ('/' + Object.keys(params).map(key =>params[key]).join('/')) : ''
options.method = 'DELETE'
return this.request(options)
},
/* 静默GET请求[不显示‘请求中’] */
getDefault(url, params = null, options = {}) {
options.url = url
options.url += params ? ('?' + Object.keys(params).map(key => key + '=' + params[key]).join('&')) : ''
options.method = 'GET'
options.def = true
return this.request(options)
},
/* 静默POST请求[不显示‘请求中’] */
postDefault(url, data = null, options = {}) {
options.url = url
options.data = data
options.method = 'POST'
options.def = true
return this.request(options)
},
}