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.

74 lines
1.8 KiB

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