JM-MiniApp/api/request.js

219 lines
5.0 KiB
JavaScript
Raw Normal View History

2023-10-08 21:21:21 +08:00
import config from '../config'
// import store from '../store'
const loginUrl = 'pages/user/login';
2023-10-18 21:00:42 +08:00
// function JSON_to_URLEncoded(element,key,list){
// var list = list || [];
// if(typeof(element)=='object'){
// for (var idx in element)
// JSON_to_URLEncoded(element[idx],key?key+'['+idx+']':idx,list);
// } else {
// list.push(key+'='+encodeURIComponent(element));
// }
// return list.join('&');
// }
2023-10-08 21:21:21 +08:00
export default {
REQUEST(url, method = 'GET', data, checkLogin = true, header) {
2023-10-18 21:00:42 +08:00
let token = uni.getStorageSync('access_token') || 'Basic ZGV2OmRldjEyMw==';
2023-10-08 21:21:21 +08:00
const headers = {
2023-10-18 21:00:42 +08:00
// "Content-Type": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
2023-10-08 21:21:21 +08:00
"Authorization": token,
"x-token": token,
"token":token,
// "X-Forwarded-For":'client_ip'
// 'Referer':'twzxjy.com'
}
var pages = getCurrentPages();
var page = pages[pages.length - 1];
return uni.request({
url: config.baseUrl + url,
method,
header: {
...header,
...headers
},
data
}).then(res => {
// console.log(res)
if (res.statusCode === 200 && res.data) {
if (res.data.code === 409) {
uni.showModal({
title:"提示",
content:"您的账号已在其他设备登录,已强制下线!",
confirmColor: '#006647',
showCancel: false,
success: (res) => {
if (res.confirm) {
uni.removeStorageSync('access_token');
uni.removeStorageSync('user_info');
if (checkLogin && page.route != loginUrl) {
uni.navigateTo({
url: '/' + loginUrl
});
}
}
}
});
return {
...res.data
}
}
if (res.data.code === 5) { // 用户认证失败
uni.removeStorageSync('access_token');
if(page.route != loginUrl && checkLogin) {
uni.navigateTo({
url: '/' + loginUrl
});
}
}
return res.data;
} else {
const reg = /abort/;
let code = 0;
let msg = (res[0] && res[0].errMsg) || '未知错误';
if ((res[0] && res[0].errMsg) && reg.test(res[0].errMsg)) {
msg = '网络请求中断'
}
return {
code,
msg,
data: null
}
}
}).catch(parmas => {
// console.log(params)
return parmas
//      return Promise.reject()
})
},
GET(url, body, checkLogin = true, header) {
return this.REQUEST(url, 'GET', body, checkLogin, header);
},
POST(url, body, checkLogin = true, header) {
return this.REQUEST(url, 'POST', body, checkLogin, header);
},
PUT(url, body, header) {
return this.REQUEST(url, 'PUT', body, header);
},
DELETE(url, body, header) {
return this.REQUEST(url, 'DELETE', body, header);
},
UPLOAD(data) {
return new Promise((resolve, reject) => {
let token = uni.getStorageSync('access_token') || '';
uni.uploadFile({
...data,
url: config.base_url + data.url,
header: {
"Access-Token": token
},
success(res) {
res.data = JSON.parse(res.data);
if (res.data.code === 401) {
uni.removeStorageSync('access_token');
uni.removeStorageSync('userInfo');
uni.navigateTo({
url: '/pages/login/login'
});
}
resolve(res);
},
fail(e) {
console.log(e);
reject(e)
},
});
});
},
getShareProvider: () => {
return uni.getProvider({
service: "share"
}).then(res => {
let data = []
for (let i = 0; i < res.provider.length; i++) {
switch (res.provider[i]) {
case 'weixin':
data.push({
name: '微信好友',
id: 'weixin',
icon: '/static/wx.png'
})
data.push({
name: '朋友圈',
id: 'weixin',
type: 'WXSenceTimeline',
icon: '/static/pyq.png'
})
break;
case 'qq':
data.push({
name: 'QQ好友',
id: 'qq',
icon: '/static/qq.png'
})
break;
default:
break;
}
}
return data;
}).catch(parmas => {
return Promise.reject()
});
},
getPaymentProvider: () => {
return uni.getProvider({
service: "payment"
}).then(res => {
let data = [];
const aliPay = {
name: '支付宝支付',
id: 'alipay',
icon: '/static/order/zfbp@3x.png'
};
const wxPay = {
name: '微信支付',
id: 'wxpay',
icon: '/static/order/wxp@3x.png'
};
const yuePay = {
name: '余额支付',
id: 'yepay',
icon: '/static/order/yep@3x.png'
}
for (let i = 0; i < res[1].provider.length; i++) {
switch (res[1].provider[i]) {
case 'alipay':
data.push({
...aliPay
})
break
case 'wxpay':
data.push({
...wxPay
})
break;
default:
break;
}
}
//#ifdef H5
return [aliPay, wxPay, yuePay];
//#endif
return [...data, yuePay];
}).catch(parmas => {
return Promise.reject()
});
}
}