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.
69 lines
1.7 KiB
69 lines
1.7 KiB
2 years ago
|
var global = require('./config.js')
|
||
|
const request = function (url, options) {
|
||
|
let token = wx.getStorageSync('token')
|
||
|
if (token==undefined || token==null) {
|
||
|
token = ''
|
||
|
}
|
||
|
return new Promise((resolve, reject) => {
|
||
|
wx.request({
|
||
|
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
|
||
|
}
|