|
|
|
import store from "@/store"
|
|
|
|
|
|
|
|
export function host(host='default',suffix='/plant_manage'){
|
|
|
|
return(
|
|
|
|
process.env.NODE_ENV === 'development'?{
|
|
|
|
default:`http://qv6mj8.natappfree.cc${suffix}`
|
|
|
|
}:{
|
|
|
|
default:`http://qv6mj8.natappfree.cc${suffix}`
|
|
|
|
}
|
|
|
|
)[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 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}`,
|
|
|
|
file:param.file,
|
|
|
|
name:"file",
|
|
|
|
formData:param.body||{},
|
|
|
|
timeout:param.timeout||60*1000,
|
|
|
|
header,
|
|
|
|
}).then(data=>({...data[1],data:JSON.parse(data[1].data)}))
|
|
|
|
}else{
|
|
|
|
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,
|
|
|
|
}).then(data=>data[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(()=>{
|
|
|
|
loadNum--;
|
|
|
|
if(loadNum<=0){
|
|
|
|
uni.hideLoading()
|
|
|
|
}
|
|
|
|
},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
|