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.
70 lines
1.8 KiB
70 lines
1.8 KiB
2 years ago
|
var global = require("./config.js")
|
||
|
const request = function (url, options, baseURL = "") {
|
||
|
let token = wx.getStorageSync("token")
|
||
|
if (token==undefined || token==null) {
|
||
|
token = ""
|
||
|
}
|
||
|
return new Promise((resolve, reject) => {
|
||
|
wx.request({
|
||
|
url: baseURL ? `${baseURL}${url}` : `${global.BASEURL()}${url}`,
|
||
|
method: options.method,
|
||
|
data: options.method === "GET" ? options.data : JSON.stringify(options.data),
|
||
|
header: {
|
||
|
"Content-Type": "application/json; charset=UTF-8",
|
||
|
"Authorization": token
|
||
|
},
|
||
|
success (response) {
|
||
|
if (response.statusCode === 200) {
|
||
|
if(response.data.code===0){
|
||
|
resolve(response.data)
|
||
|
}else{
|
||
|
let errmsg = response.data.msg
|
||
|
if(errmsg==undefined || errmsg=="undefined" ){
|
||
|
errmsg = "未返回错误信息"
|
||
|
}
|
||
|
wx.showToast({
|
||
|
title: errmsg,
|
||
|
icon: "none",
|
||
|
duration: 3000
|
||
|
})
|
||
|
reject(response.data)
|
||
|
}
|
||
|
} else {
|
||
|
wx.showToast({
|
||
|
title: "网络问题,请稍后再试。",
|
||
|
icon: "none",
|
||
|
duration: 2000
|
||
|
})
|
||
|
reject(false)
|
||
|
}
|
||
|
},
|
||
|
fail (error) {
|
||
|
wx.showToast({
|
||
|
title: "网络问题,请稍后再试。",
|
||
|
icon: "none",
|
||
|
duration: 2000
|
||
|
})
|
||
|
reject(error.data)
|
||
|
}
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
function get(url, options = {}) {
|
||
|
return request(url, { method: "GET", data: options })
|
||
|
}
|
||
|
|
||
|
function post(url, options = {}) {
|
||
|
return request(url, { method: "POST", data: options })
|
||
|
}
|
||
|
|
||
|
function put(url, options = {}) {
|
||
|
return request(url, { method: "PUT", data: options })
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
get: get,
|
||
|
post: post,
|
||
|
put: put,
|
||
|
request: request
|
||
|
}
|