/** * wxPromisify * @fn 传入的函数,如wx.request、wx.download */ function wxPromisify(fn) { return function (obj = {}) { return new Promise((resolve, reject) => { obj.success = function (res) { resolve(res) } obj.fail = function (res) { reject(res) } fn(obj)//执行函数,obj为传入函数的参数 }) } } module.exports = { wxPromisify: wxPromisify }
var getSystemInfo = Promisify.wxPromisify(wx.getSystemInfo); var getUserInfo = Promisify.wxPromisify(wx.getUserInfo); var downloadFile = Promisify.wxPromisify(wx.downloadFile); var request = Promisify.wxPromisify(wx.request); request({ url: ‘https://xindongpeixun.com/auth/news/page‘, method: ‘GET‘, header:{ ‘content-type‘: ‘json‘ } }).then(res => { console.log(res.data.data.records) })