const { $Message} = require('/component/iView/base/index'); const md5 = require('./utils/md5.js'); const secret = "117c0743819088468e590246464cc75e" const {baseUrl} = require("./utils/util.js"); const util = require('./utils/util.js'); App({ globalData: { pageSize: 10, userInfo: {}, studyInfo: {}, }, onLaunch: function () { this.autoUpgrade() }, autoUpgrade: function () { if (wx.canIUse('getUpdateManager')) { const updateManager = wx.getUpdateManager() updateManager.onCheckForUpdate(function (res) { // 请求完新版本信息的回调 if (res.hasUpdate) { console.log('res.hasUpdate====') updateManager.onUpdateReady(function () { wx.showModal({ title: '更新提示', content: '新版本已经准备好,是否重启应用?', success: function (res) { console.log('success====', res) if (res.confirm) { updateManager.applyUpdate() } } }) }) updateManager.onUpdateFailed(function () { // 新的版本下载失败 wx.showModal({ title: '已经有新版本了哟~', content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~' }) }) } }) } }, gotoLogin: function(){ wx.navigateTo({ url: `/pages/user/identify/index`, }) }, checkLogin: function (cb) { let userInfo = this.globalData.userInfo if( userInfo && userInfo.uid ){ cb && cb( userInfo) return; } let _this = this let openid = wx.getStorageSync('@openid'); if( !openid ) { return } let param = {"openid": openid}; _this.formPost('Auth.wxLogin', param).then( res =>{ if (res.code == 200) { _this.setUserInfo(res.data); cb && cb(res.data); } }) }, // message: function (content, type="info") { $Message({ content: content, type: type }); }, formPost: function (action, data) { let _this = this let userInfo = this.globalData.userInfo||{} let user_id = userInfo.uid||0; let token = userInfo.token||''; let timestamp = Date.now() action = action.toLowerCase() let signstr = `weixin_${token}_${action}_${timestamp}_${secret}` let signsure = md5.md5(signstr).toLowerCase() let headers = { 'Content-Type': 'application/json', 'x-signsure': signsure, 'x-timestamp': timestamp, 'x-userId': user_id } return new Promise(function (resolve, reject) { wx.showNavigationBarLoading(); wx.request({ url: `${baseUrl}weixin/${action}`, header: headers, method: 'POST', data, success(res) { if (res.statusCode !== 200 || typeof res.data !== 'object') { reject('网络出错') return false; } if(res.data.code != 200 && res.data.code != 401){ util.showMsg(res.data.msg ) } if( res.data.code == 401 && action.indexOf( "auth.") == -1 ){ _this.gotoLogin() return } resolve(res.data); return true; }, fail(res) { reject(res.msg) return false; }, complete(res) { wx.hideNavigationBarLoading(); } }) }) }, studyPost: function (action, data) { let {token, uid } = this.globalData.userInfo.studyUser; let version = "1.0" let _this = this let mtime = parseInt(Date.now() / 1000) action = action.toLowerCase() let body = JSON.stringify(data); let signstr = `hall_${version}${token}${body}${action}${mtime}${secret}` let sign = md5.md5(signstr).toLowerCase() let headers = { 'Content-Type': 'application/json' } let studyUrl = `${baseUrl}rental/${method}?t=${mtime}&u=${uid}&v=${version}&s=${sign}` return new Promise(function (resolve, reject) { wx.showNavigationBarLoading(); wx.request({ url: studyUrl, header: headers, method: 'POST', data, success(res) { if (res.statusCode !== 200 || typeof res.data !== 'object') { reject('网络出错') return false; } resolve(res.data); return true; }, fail(res) { reject(res.errMsg) return false; }, complete(res) { wx.hideNavigationBarLoading(); } }) }) }, getSystemInfo( cb ){ wx.getSystemInfo({ success: function(res) { cb &&cb( res.system ) } }) }, checkNavigateTo: function(url){ let openid = wx.getStorageSync('@openid'); if( !openid ) return this.gotoLogin() wx.navigateTo({url}) }, setUserInfo: function (userInfo) { this.globalData.userInfo = userInfo; if( userInfo.openid ){ console.log("@openid", userInfo.openid) wx.setStorageSync('@openid', userInfo.openid ) } wx.setStorageSync('@user', userInfo ) } })