You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.4 KiB

import store from "@/store"
function host(host='default'){
return(
process.env.NODE_ENV === 'development' ?{
default:"http://vtm3hk.natappfree.cc/jeecgboot"
}:{
default:"http://vtm3hk.natappfree.cc/jeecgboot"
}
)[host]
}
//加载状态
let loadNum=0
// 自定义封装uniapp请求
const request=async function(url,param={}){
loadNum++;
uni.showLoading({
title:"加载中",
mask:true,
})
//路径数据
var pathData=Object.entries({...(url.match(/\?(.+)/)||{1:""})[1].split("&").map(v=>v.split('=')).filter(v=>v[0]&&v[1]).reduce((a,b)=>{a[b[0]]=b[1];return a},{}), ...(param.params||{})}).map(v=>`${v[0]}=${v[1]}`).join("&")
var res=await uni.request({
url:(param.host||host())+('/'+url).replace(/\/{2}/,'/').replace(/\?.+/,"")+`?${pathData}`,
method:param.method||"get",
data:param.data,
header:Object.assign(param.header||{}, {"token":uni.getStorageSync('token')})
})
setTimeout(()=>{
loadNum--;
if(loadNum<=0){
uni.hideLoading()
}
},600)
if(res[1]){
if(res[1].data.code==401){
return{statu:false, msg:"登录超时!"}
}
else if(res[1].data.code==0)return { statu:true, data:res[1].data }
else if(res[1].statusCode==404)return {statu:false,msg:"地址不存在!"}
else return {statu:false,msg:res[1]?.data?.msg||"服务器错误!", code:res[1]?.data?.code??res[1].statusCode}
}else{
return { statu:false, msg:"服务器错误!"}
}
}
export default request