2025-07-03 13:14:39 +08:00

118 lines
3.1 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.

import {formatUrl} from "../utils/formatParams"
import {getCurrentPageUrl} from "../utils/getUrl"
import {hostConfig} from "../utils/config"
let host=hostConfig().host;
let tokenStr=''
const { envVersion } = wx.getAccountInfoSync().miniProgram;
if(envVersion=="develop" || envVersion=="trial"){
tokenStr="DEV_CASE_TOKEN"
}else{
tokenStr="PROD_CASE_TOKEN"
}
//loding 是否加loading弹窗
function request(url, method, data, loding = false,contentType="application/json") {
if (loding) {
wx.showLoading({
title: '加载中',
mask: true
})
}
// if (!token) {
// let freelist = ["/wx/user/", '/login/mobile_login','/code/phone','/patient/index',"/popup","/sign/im"];//接口白名单
// let currentUrl=getCurrentPageUrl();
// if (freelist.indexOf(url) == -1 && currentUrl!="pages/personCenter/personCenter") {
// let redirectUrl=formatUrl();
// wx.reLaunch({
// url: '/case/pages/login/login?redirectUrl='+redirectUrl
// });
// }
// }
let token = wx.getStorageSync(tokenStr);
//console.log(token)
let header = {
'content-type': contentType,
'x-access-token': token
// 'Authorization': 'Bearer ' + token
}
return new Promise((resolve, reject) => {
wx.request({
url: host + url,
method: method,
data: data,
header: header,
success: function (res) {
//console.log(res.data);
// var Authorization_token = res.header['x-access-token'];
// if (Authorization_token) {
// wx.setStorageSync(tokenStr, Authorization_token); //当token快过期时服务器会返回新token本地刷新
// }
if (loding) {
wx.hideLoading()
wx.stopPullDownRefresh();
}
if (Number(res.data.code) == 200 || Number(res.data.code) == 0) {
resolve(res.data.data);
}else if (Number(res.data.code) == 30007 ) {
wx.hideLoading()
let redirectUrl=formatUrl();
wx.reLaunch({
url: '/case/pages/mobileLogin/mobileLogin?redirectUrl='+redirectUrl
});
}else {
console.log(res.data)
reject(res.data);
wx.showToast({
title: res.data.msg.length>=30?'操作失败':res.data.msg,
icon: 'none',
duration: 3000
})
}
//wx.hideLoading()
},
fail: function (res) {
console.log(res)
// wx.showToast({
// title: '网络错误,请稍后再试',
// icon: 'none',
// duration: 2000
// })
// reject(false)
}
})
})
}
function uploadFile(url, data) {
return new Promise((resolve, reject) => {
wx.uploadFile({
url: host + url,
formData: data,
success(res) {
const result = res.data
resolve(result)
},
// fail(res){
// wx.showToast({
// title: '网络错误,请稍后再试',
// icon: 'none',
// })
// reject(false)
// }
})
})
}
module.exports = {
request,
uploadFile
}