mes_mobile/utils/api.js

157 lines
3.6 KiB
JavaScript
Raw Normal View History

2024-03-13 16:07:11 +08:00
import config from './envConfig.js';
2024-05-07 19:37:20 +08:00
import wf from './public.js';
2024-03-13 16:07:11 +08:00
2024-04-23 11:48:55 +08:00
const request = (url = '', data = {}, header = {
2024-03-13 16:07:11 +08:00
'Authorization': 'Bearer '+uni.getStorageSync('mes_token') ? 'Bearer '+uni.getStorageSync('mes_token') : '',
}) => {
return new Promise((resolve, reject) => {
uni.request({
2024-04-23 11:48:55 +08:00
url: config.dev.VITE_BASE_API + url,
method: "POST",
2024-03-13 16:07:11 +08:00
dataType: "json",
2024-04-23 11:48:55 +08:00
data: data,
header: header,
2024-03-13 16:07:11 +08:00
success: (res) => {
2024-04-23 11:48:55 +08:00
if (res.data.code == 401){
setTimeout( ()=> {
uni.hideLoading();
}, 200);
2024-03-13 16:07:11 +08:00
uni.showModal({
showCancel: false,
title: '温馨提示',
2024-04-23 11:48:55 +08:00
confirmColor:'#009688',
content: res.data.message,
success: (result)=> {
2024-03-13 16:07:11 +08:00
if (result.confirm) {
2024-04-23 11:48:55 +08:00
uni.reLaunch({
url: '/pages/login/login'
});
wf.removeLoginData();
2024-03-13 16:07:11 +08:00
}
}
});
2024-04-30 16:08:07 +08:00
return
2024-03-13 16:07:11 +08:00
}
if(res.header.Authorization){
uni.removeStorageSync('mes_token');
let obj = new Object();
2024-04-23 11:48:55 +08:00
let str = String(res.header.Authorization)
2024-03-13 16:07:11 +08:00
let newStr = str.slice(0, 0) + str.slice(7)
2024-04-23 11:48:55 +08:00
obj.mes_token = newStr;
2024-03-13 16:07:11 +08:00
uni.setStorageSync("mes_token",obj.mes_token);
}
2024-04-23 11:48:55 +08:00
// 成功的回调
if(res.data.code == 200){
2024-04-30 16:08:07 +08:00
resolve(res.data);
}else{
uni.showToast({
icon:'none',
title:res.data.message
})
2024-04-23 11:48:55 +08:00
}
2024-03-13 16:07:11 +08:00
},
fail: (err) => {
uni.showToast({
title: "" + err.msg,
icon: 'none'
});
reject(err)
}
})
})
}
2024-05-07 19:37:20 +08:00
const postFuncLoading = (url = '', data = {}, header = {
2024-03-13 16:07:11 +08:00
'Authorization': 'Bearer '+uni.getStorageSync('mes_token') ? 'Bearer '+uni.getStorageSync('mes_token') : '',
2024-05-07 19:37:20 +08:00
}) => {
uni.showLoading({
2024-03-13 16:07:11 +08:00
title: '加载中',
mask: true
2024-05-07 19:37:20 +08:00
})
2024-03-13 16:07:11 +08:00
return new Promise((resolve, reject) => {
uni.request({
url: config.dev.VITE_BASE_API + url, //接口地址:前缀+方法中传入的地址
method: "POST", //请求方法
dataType: "json",
data: data, //传递参数
header: header, //自定义头部,和后端商同后编写
success: (res) => {
2024-04-23 11:48:55 +08:00
setTimeout( ()=> {
2024-03-13 16:07:11 +08:00
uni.hideLoading();
2024-04-23 11:48:55 +08:00
}, 200);
if (res.data.code == 401){
wf.removeLoginData();
2024-03-13 16:07:11 +08:00
uni.showModal({
showCancel: false,
2024-04-23 11:48:55 +08:00
title: '温馨提示',
confirmColor:'#009688',
content: res.data.message,
success: (result)=> {
2024-03-13 16:07:11 +08:00
if (result.confirm) {
2024-04-23 11:48:55 +08:00
uni.reLaunch({
url: '/pages/login/login'
});
2024-03-13 16:07:11 +08:00
}
}
2024-04-30 16:08:07 +08:00
});
return
2024-03-13 16:07:11 +08:00
}
if(res.header.Authorization){
uni.removeStorageSync('mes_token');
let obj = new Object();
2024-04-23 11:48:55 +08:00
let str = String(res.header.Authorization)
2024-03-13 16:07:11 +08:00
let newStr = str.slice(0, 0) + str.slice(7)
2024-04-23 11:48:55 +08:00
obj.mes_token = newStr;
2024-03-13 16:07:11 +08:00
uni.setStorageSync("mes_token",obj.mes_token);
2024-04-23 11:48:55 +08:00
}
// 成功的回调
if(res.data.code == 200){
resolve(res.data)
2024-04-30 16:08:07 +08:00
}else{
uni.showToast({
icon:'none',
title:res.data.message
})
2024-04-23 11:48:55 +08:00
}
2024-03-13 16:07:11 +08:00
},
fail: (err) => {
uni.showToast({
title: "" + err.msg,
icon: 'none'
});
reject(err)
}
})
})
2024-05-07 19:37:20 +08:00
}
const getOss = (url = '', data = {}, header = {
'content-type':'application/json; charset=UTF-8',
'Authorization':''
})=>{
return new Promise((resolve, reject) => {
uni.request({
url: config.dev.VITE_OSS_URL + url, //接口地址:前缀+方法中传入的地址
method: "POST",
dataType: "json",
data: data,
header: header,
success:(res)=>{
if(res.data.code == 200){
resolve(res.data);
}
},
fail:(err)=>{
reject(err);
}
})
})
}
2024-03-13 16:07:11 +08:00
export default {
request,
2024-05-07 19:37:20 +08:00
postFuncLoading,
getOss
2024-03-13 16:07:11 +08:00
};