const { $Message} = require('/component/iView/base/index'); const md5 = require('./utils/md5.js'); const secret = "117c0743819088468e590246464cc75e" const {baseUrl, showMsg} = require("./utils/util.js"); const util = require('./utils/util.js'); App({ globalData: { pageSize: 10, userInfo: {}, departments: [], 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(){ console.log("gologin") 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 token = wx.getStorageSync('@token'); if(!token) return cb && cb({}); wx.login({ success(wxres) { if( !wxres.code){ util.showMsg("登入失败") }else{ _this.doLoginByCode( wxres.code, cb ) } } }) }, doLoginByCode( code, cb ){ let param = {code}; let _this = this; _this.formPost('Auth.wxLogin', param).then( res =>{ if (res.code == 200) { _this.setUserInfo(res.data); cb && cb(res.data); }else{ return cb && cb({code:401}); } }) }, message: function (content, type) { $Message({ content: content, type: type }); }, formPost: function (action, data) { let _this = this let userInfo = this.globalData.userInfo||{} let user_id = userInfo.userId||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(); } }) }) }, getSystemInfo( cb ){ wx.getSystemInfo({ success: function(res) { cb &&cb( res.system ) } }) }, checkNavigateTo: function(url){ let token = wx.getStorageSync('@token'); if( !token ) return this.gotoLogin() wx.navigateTo({url}) }, setUserInfo: function (user) { this.globalData.userInfo = user; wx.setStorageSync('@token', user.token) } })