const app = getApp()
const request = (url, options) => {
return new Promise((resolve, reject) => {
wx.showLoading({
title: '加載中',
})
wx.request({
url: `${app.globalData.baseurl}${url}`,
method: options.method,
data: options.data,
header: {
'content-type': options.isObj ? 'application/json' : 'application/x-www-form-urlencoded',
// 'x-token': 'x-token' // 看自己是否需要
},
success(request) {
console.log(request);
resolve(request)
// if (request.data.code == 0) {
// resolve(request)
// } else {
// reject(request)
// }
},
fail(error) {
console.log(error);
reject(error.data)
}, complete: () => {
setTimeout(() => {
wx.hideLoading();
}, 100);
}
})
})
}
const get = (url, options = {}) => {
return request(url, { method: 'GET', data: options })
}
//post對象
const postObj = (url, options) => {
return request(url, { method: 'POST', data: options, isObj: true })
}
//post參數
const post = (url, options) => {
return request(url, { method: 'POST', data: options, isObj: false })
}
const put = (url, options) => {
return request(url, { method: 'PUT', data: options })
}
// 不能聲明DELETE(關鍵字)
const remove = (url, options) => {
return request(url, { method: 'DELETE', data: options })
}
module.exports = {
get,
post,
put,
remove,
postObj,
}