//app.js var appInstance = getApp() // console.log(appInstance.onShow()) App({ data:{ }, globalData: { common: {}, userInfo: null, HOST: 'https://chaoshi.henhaojie.com', platform: 0, title: '', img: '' }, // 在这个app里面this就是app实例 // 监听小程序初始化 onLaunch: function (){ var system = '', app = this; wx.getSystemInfo({ success: function(res) { system = res.system console.log(system) if (system.indexOf('iOS') == -1) { app.globalData.platform = 1; }else{ app.globalData.platform = 2; } console.log(app.globalData.platform); } }) }, // 监听小程序显示 onShow: function (options) { }, // 监听小程序隐藏 onHide: function () { }, // 错误监听函数 onError: function (msg) { console.log(msg) }, login: function (that, func) { let obj = {}, app = this, user = '', source = ''; if (that.data.source) { source = that.data.source } wx.login({ success: function(res) { if (res.code) { wx.getUserInfo({ success: function(resInfo) { // console.log(resInfo); wx.request({ url: app.globalData.HOST + '/user/wx/login', method: "POST", data: { code: res.code, iv: resInfo.iv, encryptedData: resInfo.encryptedData, source: source }, success: function (resLogin) { if (resLogin.data.errno == 0) { user = resLogin.data.rst; wx.setStorageSync("user", user) app.globalData.common['token'] = user.token if (func) { func(); } app.initShare(); } } }) }, fail: function () { // 用户拒绝授权后再次确认 app.confirmModal('必须授权登录之后才正常使用,是否重新授权', function(){ if (wx.openSetting) { wx.openSetting({ success: (ress) => { if (ress.authSetting["scope.userInfo"]) { wx.getUserInfo({ success: function(resInfo) { wx.request({ url: app.globalData.HOST + '/user/wx/login', method: "POST", data: { code: res.code, iv: resInfo.iv, encryptedData: resInfo.encryptedData }, success: function (resLogin) { if (resLogin.data.errno == 0) { user = resLogin.data.rst; wx.setStorageSync("user", user) app.globalData.common['token'] = user.token if (func) { func(); } } } }) } }) } } }) } }); } }) } else { console.log('登录失败!' + res.errMsg) } } }); }, initShare () { var app = this; var title='', img = ''; wx.request({ url: app.globalData.HOST + '/user/wx/share', method: 'POST', data: app.globalData.common, success: function (res) { if (res.data.errno == '0'){ title = res.data.rst.title; img = res.data.rst.pic; } app.globalData.title = title; app.globalData.img = img; } }); }, confirmModal (content, func) { wx.showModal({ title: '小提示', content: content, success: function(res) { if (res.confirm) { func(); } else if (res.cancel) { } } }) }, noticeModal (content, func) { wx.showModal({ title: '小提示', content: content, showCancel: false, confirmText: '我知道了', success: function(res) { if (res.confirm) { if (func) { func(); } } } }) } })