微信小程序接口封装
时间:2020-04-21 13:29:27
收藏:0
阅读:105
function baseRequest({ url, method, header, data }, resolve, reject) { wx.request({ url, method, header, data, success: function (res) { // 需要判断服务器code的用这一段 // 返回0表示真正的成功,其他code表示各种错误码 if (res.data.code === 0) { resolve(res) } else { reject(res) } resolve(res) }, fail: function (res) { reject(res) } }) }
使用promise
function requestPromise(options) { let req = new Promise((resolve, reject) => { baseRequest(options, resolve, reject) }) return req }
get方法
function get(options) { options.method = ‘GET‘ return requestPromise(options) }
post方法
function post(options) { options.method = ‘POST‘ if (!options.header) { options.header = {} } options.header["Content-Type"] = "application/json" return requestPromise(options) }
put方法
function put(options) { options.method = ‘PUT‘ if (!options.header) { options.header = {} } options.header["Content-Type"] = "application/json" return requestPromise(options) }
评论(0)