//app.js var appInstance = getApp() // console.log(appInstance.onShow()) App({ data:{ }, globalData: { common: { source: 0, app_type: 6 }, userInfo: null, HOST: 'https://chaoshi.henhaojie.com', platform: 0, title: '', img: '' }, // 在这个app里面this就是app实例 // 监听小程序初始化 onLaunch: function (){ let system = '', app = this; const shareInfo = wx.getStorageSync('shareInfo'); if (shareInfo) { app.globalData.title = shareInfo.title; app.globalData.img = shareInfo.img; }else{ wx.redirectTo({url: '/pages/login/login'}) } 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 () { }, onUnload () { wx.clearStorage(); }, // 错误监听函数 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.request({ url: app.globalData.HOST + '/user/wx/login', method: "POST", data: { code: res.code, iv: app.globalData.userInfo.iv, encryptedData: app.globalData.userInfo.encryptedData, source: source, app_type: 6 }, success: function (resLogin) { if (resLogin.data.errno == 0) { user = resLogin.data.rst; wx.setStorageSync("user", user) app.globalData.common['token'] = user.token wx.switchTab({'url': '/pages/index/index'}) if (func) { func(); } app.initShare(); } } }) } 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; wx.setStorage({ key: 'shareInfo', data: { title: title, 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(); } } } }) } })