xinelu-applet-ui/api/request.js
2024-06-25 18:25:50 +08:00

59 lines
1.6 KiB
JavaScript

import baseurl from './baseurl.js'
import {
encrypt,
decrypt
} from './crypto.js'
var witch = '1' //1加密 2不加密
var request = function(config) {
if (uni.getStorageSync('openid')) {
if (!config.header) {
config.header = {}
}
config.header.openid = uni.getStorageSync('openid')
config.header.appletType = ''
} else {
config.header = {
appletType: ''
}
}
if (witch == '1' && config.method == 'post' && config.url != '/applet/score/prizeExchange/approvalList') {
config.data = encrypt(JSON.stringify(config.data))
}
if (witch == '1' && config.url == '/nurseApplet/chatRecord/updateReadStatus' || config.url ==
'/applet/register/editPrimaryAccountFlag') {
config.data = encrypt(JSON.stringify(config.data))
}
const urls = config.url.split('?')[0]
if (config.url != '/nurseApplet/chatRecord/updateReadStatus' && config.url !=
"/nurseApplet/chatRecord/sendMessage" && config.url !=
`/applet/sign/apply/checkSignApply/` + uni.getStorageSync('userinfo').cardNo && urls !=
`/nurseApplet/chatRecord/getChatRecord` && urls != '/applet/register/wxSportDecrypt') {
uni.showLoading({
title: ''
});
}
return new Promise((resolve, rejected) => {
uni.request({
url: baseurl + config.url,
data: config.data,
method: config.method,
timeout: 60000,
header: config.header,
success(res) {
uni.hideLoading();
if (witch == '1') {
res.data = decrypt(res.data)
res.data = JSON.parse(res.data)
}
console.log(res.data)
resolve(res.data)
},
fail(err) {
uni.hideLoading();
rejected(err)
}
})
})
}
export default request