一、代码
//request通用 const request = (url, param, method = 'get', dataType = 'json', responseType = 'text') => { var method = method.toLowerCase() return new Promise((resolve, reject) => { if (method == 'get') { var header = { 'content-type': 'application/json' } } else if (method == 'post') { var header = { 'content-type': 'application/x-www-form-urlencoded' } }else{ reject('header is error') } wx.request({ url: url, data: param, header: header, method: method, dataType: dataType, responseType: responseType, success(res) { console.log(res) resolve(res.data) }, fail(err) { console.log(err) reject(err) } }) }) } // request get 请求 const getData = (url, param) => { return new Promise((resolve, reject) => { wx.request({ url: url, method: 'GET', data: param, success(res) { console.log(res) resolve(res.data) }, fail(err) { console.log(err) reject(err) } }) }) } // request post 请求 const postData = (url, param) => { return new Promise((resolve, reject) => { wx.request({ url: url, method: 'POST', data: param, success(res) { console.log(res) resolve(res.data) }, fail(err) { console.log(err) reject(err) } }) }) } // loading加载提示 const showLoading = () => { return new Promise((resolve, reject) => { wx.showLoading({ title: '加载中...', mask: true, success(res) { console.log('显示loading') resolve(res) }, fail(err) { reject(err) } }) }) } // 关闭loading const hideLoading = () => { return new Promise((resolve) => { wx.hideLoading() console.log('隐藏loading') resolve() }) } module.exports = { request, getData, postData, showLoading, hideLoading }