Files
fangcheyun/miniprogram/utils/request.js
2021-05-12 09:21:10 +08:00

102 lines
3.0 KiB
JavaScript

const BASEURL = "http://127.0.0.1:8080";
const APIURL = "http://kaoshi-shangpin.theluyuan.com"
const type = true; // true 走request false 走云函数
export function get(url,data = {}, baseurl = BASEURL){
return new Promise((res,err)=>{
console.time(url)
let openid = wx.getStorageSync('currentUser').openid
if(!data.openid){
data.openid = openid
}
if(type){
let d = "?";
for(let i in data){
d += i + '=' + data[i] + "&";
}
if(d == "?"){
d = ""
}
wx.request({
url: baseurl + url + d,
success(r){
try {
r.data.data = JSON.parse(r.data.data)
res(r.data)
} catch (error) {
res(r.data)
}
console.timeEnd(url)
},
error(e){
err(e)
console.timeEnd(url)
}
})
}else{
wx.cloud.callFunction({
name: 'proxy',
data: {
url: APIURL + url,
method:"GET",
params:data
},
success: r => {
res(r.result)
console.timeEnd(url)
},
error(e){
err(e)
console.timeEnd(url)
}
})
}
})
}
export function post(url,data = {}, baseurl = BASEURL){
return new Promise((res,err)=>{
console.time(url)
let openid = wx.getStorageSync('currentUser').openid
if(!data.openid){
data.openid = openid
}
if(type){
wx.request({
url: baseurl + url,
data,
method:"POST",
success(r){
try {
r.data.data = JSON.parse(r.data.data)
res(r.data)
} catch (error) {
res(r.data)
}
console.timeEnd(url)
},
error(e){
err(e)
console.timeEnd(url)
}
})
}else{
wx.cloud.callFunction({
name: 'proxy',
data: {
url: APIURL + url + d,
method:"POST",
params:data
},
success: r => {
res(r.result)
console.timeEnd(url)
},
error(e){
err(e)
console.timeEnd(url)
}
})
}
})
}