var app = getApp(); 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) }) } } //無論promise對象最后狀態如何都會執行 Promise.prototype.finally = function (callback) { let P = this.constructor; return this.then( value => P.resolve(callback()).then(() => value), reason => P.resolve(callback()).then(() => { throw reason }) ); }; /** * 微信請求get方法 * url * data 以對象的格式傳入 */ function getRequest(url, data) { var getRequest = wxPromisify(wx.request) return getRequest({ url: app.globalData.url + url, method: 'GET', data: data, header: { 'Content-Type': 'application/json' } }) } /** * 微信請求post方法封裝 * url * data 以對象的格式傳入 */ function postRequest(url, data) { var postRequest = wxPromisify(wx.request) return postRequest({ url: app.globalData.url + url, method: 'POST', data: data, header: { "content-type": "application/x-www-form-urlencoded" }, }) } //封裝小程序冷更新 function upDateSmallProgram() { wx.getSystemInfo({ success: function (res) { var version = res.SDKVersion.slice(0, 5); console.log('res.SDKVersion', res.SDKVersion) version = version.replace(/\./g, ""); if (parseInt(version) >= 199) {// 大於1.9.90的版本 const updateManager = wx.getUpdateManager() updateManager.onCheckForUpdate(function (res) { // 請求完新版本信息的回調 console.log("9999999", res.hasUpdate) }) updateManager.onUpdateReady(function () { wx.showModal({ title: '更新提示', showCancel: false, content: '版本已更新,請點擊確定立即使用!', success: function (res) { if (res.confirm) { // 新的版本已經下載好,調用 applyUpdate 應用新版本並重啟 updateManager.applyUpdate() } } }) }) updateManager.onUpdateFailed(function () { // 新的版本下載失敗 }) } } }) } module.exports = { postRequest, getRequest, upDateSmallProgram }