let APP = getApp(); import * as STORAGE from './utils/storage' import * as UTIL from './utils/util' import * as API from './utils/API' App({ onLaunch() { var that = this; //存储storage初始化globalData数据-- //何时存储,用来判断,不用获取code that.initGlobalData(); //获取设备信息 wx.getSystemInfo({ success: function (res) { that.globalData.systemType = res.system.indexOf("Android") >= 0 ? "Android" : "IOS"; that.globalData.isIphoneX = res.model.indexOf("iPhone X") >= 0 || res.model.indexOf("iPhone 1") >= 0; } }); var that = this ; // iphoneX顶部适配 wx.getSystemInfo({ success: function (res) { if (res.statusBarHeight > 20) { console.log("Device is iPhoneX!!!"); that.globalData.isIPX = true; } } }) }, onShow() { //更新机制 this.wxappUpdateManager(); }, /** * 从服务端获取openId */ getOpenIdFromFW(code) { let sendData = { code: code } console.log(wx.getStorageSync('dressCode')+API.URL_GET_OPENID); UTIL.httpRequestNoneDetal(API.URL_GET_OPENID, sendData, "POST", { success: (res) => { console.log(res); if (res.code == API.SUCCESS_CODE) { // UTIL.showToastNoneIcon("openId:" + res._data.openid); wx.setStorageSync('token', res.token); wx.setStorageSync('openId', res.data.openId); wx.setStorageSync('sessionKey', res.data.sessionKey); getApp().globalData.userInfo.token = res.token; } else { //未获取到openId wx.setStorageSync('openId', res.data.openId); wx.setStorageSync('sessionKey', res.data.sessionKey); } } }) }, /** * 初始化globalData */ initGlobalData() { var userInfo = { token: wx.getStorageSync('token') } console.log(userInfo) this.globalData.userInfo = userInfo; } , /** * 小程序更新机制 * 获取小程序更新机制兼容 */ wxappUpdateManager() { if (wx.canIUse('getUpdateManager')) { const updateManager = wx.getUpdateManager(); if (!!updateManager) { updateManager.onCheckForUpdate(function (res) { // 请求完新版本信息的回调 if (res.hasUpdate) { updateManager.onUpdateReady(function () { wx.showModal({ title: '更新提示', content: '新版本已经准备好,是否重启应用?', success: function (res) { if (res.confirm) { // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启 updateManager.applyUpdate() } } }) }) updateManager.onUpdateFailed(function () { // 新的版本下载失败 wx.showModal({ title: '已经有新版本了哟~', content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~', }) }) } }) } } else { // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示 wx.showModal({ title: '提示', content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。' }) } }, showToast(msg, selfClass = '') { clearTimeout(this.globalData.toastTimeout); const page = getCurrentPages(); const currPage = page[page.length - 1]; currPage.setData({ toastData: { showFlag: true, toastMsg: msg, selfClass, }, }); this.globalData.toastTimeout = setTimeout(() => { currPage.setData({ toastData: { showFlag: false, selfClass: '', }, }); }, 2000); }, globalData: { // 系统用户登录信息(用户id、token) userInfo: { token: '', toastTimeout:null }, //微信用户登陆信息(昵称、头像、省、城市) wxUserInfo: { nickName: '', avatarUrl: '', province: '', city: '' }, /** * 小程序设置 */ setInfo: { //定位授权 locationOpenIdWX: false, //纬度 latitude:'', //经度 longitude:'', }, systemType:'',//设备类型 Android IOS isIphoneX: false, // 用来标识当前手机机型是否为 iPhone X isIPX:false, userDress:'重庆市', APIURL:wx.getStorageSync('dressCode') }, })