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.

80 lines
1.9 KiB

import store from "@/store"
function host(host='default'){
return(
2 years ago
process.env.NODE_ENV === 'development'?{
default:"http://vpep38.natappfree.cc/plant_manage"
}:{
2 years ago
default:"http://vtm3hk.natappfree.cc/plant_manage"
}
)[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("&")
2 years ago
var header={
...param.header||{},
"X-Access-Token":store.getters.token,
}
if(param.file){
var res=await uni.uploadFile({
url:(param.host||host())+('/'+url).replace(/\/{2}/,'/').replace(/\?.+/,"/")+`?${pathData}`,
files:param.file,
// name:"mfs",
timeout:param.timeout||60*1000,
header,
})
console.log(res,'-----------res-------111----------')
setTimeout(()=>{
loadNum--;
if(loadNum<=0){
uni.hideLoading()
}
},600)
}else{
2 years ago
var res=await uni.request({
url:(param.host||host())+('/'+url).replace(/\/{2}/,'/').replace(/\?.+/,"")+`?${pathData}`,
method:param.method||"get",
data:param.body,
timeout:30*1000,
header,
})
setTimeout(()=>{
loadNum--;
if(loadNum<=0){
uni.hideLoading()
}
},600)
if(res[1]){
if(res[1].data.code==401){
uni.reLaunch({
url:"pages/home/login",
})
return{statu:false, msg:"登录超时!"}
}
else if(res[1].data.code==200)return { statu:true, data:res[1].data.result }
else if(res[1].statusCode==404)return {statu:false,msg:"地址不存在!"}
else return {statu:false,msg:res[1]?.data?.message||"服务器错误!"}
}else{
return { statu:false, msg:"服务器错误!"}
}
}
}
export default request