const app = getApp()
const request = options => {
if(options) {
if(options.header === undefined || options.header === null) {
options.header = {}
}
let authentication = wx.getStorageSync('authentication')
if(token_status) {
options.header['authentication'] = authentication
}
let timestamp = new Date().getTime()
if(timestamp) {
options.header['timestamp'] = timestamp
}
const BASE_URL = app.data.host
if(options.url.indexOf('http') != 0) {
options.url = BASE_URL + options.url
}
if(options.method === undefined || options.method === null) {
options.method = 'post'
}
if(options.success && typeof(options.success === 'function')) {
let successCallback = options.success
options.success = res=> {
wx.hideLoading()
if(res.data.code == 200) {
successCallback(res.data)
}else if(res.data.code == 202) {
wx.showToast({
title: res.data.message,
icon: 'none'
})
successCallback(res.data.message)
}
}
}
if(!options.fail) {
let failCallback = options.fail = res=> {
wx.showToast({
title: res.data.message,
icon: 'none'
})
}
}
if(!options.complete) {
let completeCallback = options.complete = res=> {
}
}
console.log(options)
wx.showLoading({
title: '加载中',
})
wx.request(options)
}
}
module.exports = {
request: request
}
- 在需要调用的js中引用该js文件,像平时使用wx.request()一样使用它就成~
import {request} from '../../../utils/request'
....
request({
url: `/receive/${id}`,
method: 'put',
data: {
...
},
success: res => {
if (res.code == 200) {
wx.showToast({
title: '更新成功',
icon: 'none'
})
}
}
})