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.

79 lines
2.1 KiB

import store from "@/store"
export function host(host='default',suffix='/plant_manage'){
2 years ago
var url=process.env.NODE_ENV === 'development'?{
// default:`http://59syiv.natappfree.cc` //陈
default:`http://sfhtpp.natappfree.cc` //马
2 years ago
// default:`http://4gg9xn.natappfree.cc` //何
// default:`http://192.168.0.116:8080` //孙
}:{
default:``
}
2 years ago
return ({
default:`${url.default}${suffix}`,
imgUrl:`${url.default}/plant_manage/sys/common/static`
})[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||param.filePath){
console.log(param.suffix,'---suffix----')
2 years ago
var res=await uni.uploadFile({
2 years ago
url:`${param.host||host(undefined,param.suffix)}${('/'+url).replace(/\/{2}/,'/').replace(/\?.+/,"")}?${pathData}`,
file:param.file||{},
filePath:param.filePath||"",
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({
url:`${param.host||host(undefined,param.suffix)}${('/'+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){
2 years ago
store.commit('outLogin')
2 years ago
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