//app.js App({ globalData: { title: '晋爱人才', http_host: 'http://bd.jarc.com/', // http_host: 'http://jarc.jucai.gov.cn/', api_host: 'http://bd.jarc.com/api/', // api_host: 'https://jarc.jucai.gov.cn/api/', }, onLaunch: function() { var self = this; // 登录 if (!this.checkLogin()) { wx.login({ success: res => { // 发送 res.code 到后台换取 openId, sessionKey, unionId self.post('applet/applet/login',{code:res.code},function(res){ wx.setStorageSync('token', res.token); wx.setStorageSync('token_exp', res.expires_time); self.get('user/profile/userInfo',function(res){ self.globalData.userInfo = res; },false) },false); } }) } else { self.get('user/profile/userInfo',function(res){ self.globalData.userInfo = res; },false) } // 获取系统状态栏信息 wx.getSystemInfo({ success: e => { this.globalData.StatusBar = e.statusBarHeight; let capsule = wx.getMenuButtonBoundingClientRect(); if (capsule) { this.globalData.Custom = capsule; this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight; } else { this.globalData.CustomBar = e.statusBarHeight + 50; } } }); }, /** * 弹出框 */ msg(title, url = '', time = 2000, mask = false) { wx.showToast({ title: title, icon: 'none', mask: mask, duration: time }); if (url) { if (typeof url == 'function') { setTimeout(url,time); } else { setTimeout(function(){ wx.navigateTo({url: url}); },time); } } }, loading(title) { wx.showLoading({ title: title, icon: 'none', mask: true }) }, //服务器请求 post: function (domain, param, success, is_load=true, error='') { if (is_load) { wx.showLoading({ title: '加载中', mask: true, }); } var that = this; var token = wx.getStorageSync('token'); wx.request({ url: that.globalData.api_host + domain, data : param, header: { 'XX-Device-Type': 'wxapp' , 'XX-Token': token}, method: 'post', success: function (res) { if (is_load) { wx.hideLoading(); } if (res.data.code == 1) { if (success != undefined) { success.call(this, res.data.data); } } else if (res.data.code == 10001) { wx.clearStorageSync(); wx.showToast({ title: '小程序异常,请退出后重新进入', icon: 'none', duration: 2000 }) } else { if (typeof error === 'function') { error(res.data); } else { wx.showToast({ title: res.data.msg, icon: 'none', duration: 2000 }) } } }, error : function () { if (is_load) { wx.hideLoading(); } wx.showToast({ title: '网络异常,请退出小程序重新登录', icon: 'loading' }) } }) }, //服务器请求 get: function (domain, success, is_load=true, error='') { if (is_load) { wx.showLoading({ title: '加载中', mask: true, }); } var that = this; var token = wx.getStorageSync('token'); wx.request({ url: that.globalData.api_host + domain, header: { 'XX-Device-Type': 'wxapp' , 'XX-Token': token}, success: function (res) { if (is_load) { wx.hideLoading(); } if (res.data.code == 1) { if (success != undefined) { success.call(this, res.data.data); } } else if (res.data.code == 10001) { wx.clearStorageSync(); wx.showToast({ title: '小程序异常,请退出后重新进入', icon: 'none', duration: 2000 }) } else { if (typeof error === 'function') { error(res.data); } else { wx.showToast({ title: res.data.msg, icon: 'none', duration: 2000 }) } } }, error : function () { if (is_load) { wx.hideLoading(); } wx.showToast({ title: '网络异常,请退出小程序重新登录', icon: 'loading' }) } }) }, /** * 登录验证 */ checkLogin: function () { let token = wx.getStorageSync('token'); if (!token) { return false; } let token_exp = wx.getStorageSync('token_exp'); let now = Date.parse(new Date())/1000 if (token_exp < now) { return false; } return true; }, loading: function (title) { wx.showLoading({ title: title, icon: 'none', mask: true }) }, confirm: function (content,success) { wx.showModal({ title: '提示', content: content, success(res) { if (res.confirm) { wx.hideToast(); success(res); } else { wx.hideToast(); } } }) } })