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
2.2 KiB

import store from "@/store"
export function host(host='default',suffix='/plant_manage'){
var url=process.env.NODE_ENV === 'development'?{
// default:`http://i5skii.natappfree.cc` //陈
// default:`http://jjytvc.natappfree.cc` //马
// default:`http://6exd5b.natappfree.cc` //何
default:"http://192.168.0.43:7000"
}:{
default:``
}
return ({
default:`${url.default}${suffix}`,
version:`${url.default}/plant_manage/sys/common/static/${suffix}`,
imgUrl:`${url.default}/plant_manage/sys/common/static`
})[host]
}
//加载状态
let loadNum=0
// 自定义封装uniapp请求
const request=async function(url,param={}){
if(!param.disabledLoad){
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||param.filePath){
var res=await uni.uploadFile({
url:`${param.host||host(undefined,param.suffix)}${('/'+url).replace(/\/{2}/,'/').replace(/\?.+/,"")}?${pathData}`,
file:param.file||{},
filePath:param.filePath||"",
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(undefined,param.suffix)}${('/'+url).replace(/\/{2}/,'/').replace(/\?.+/,"")}?${pathData}`,
method:param.method||"get",
data:param.body,
timeout:30*1000,
header,
}).then(data=>data[1])
}
setTimeout(()=>{
if(!param.disabledLoad){
loadNum--;
}
if(loadNum<=0){
uni.hideLoading()
}
},600)
if(res){
if(res.data.code==401&&!param.skipToken){
store.commit('outLogin')
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