// app.js App({ //全局变量 globalData: { //服务器地址 serverUrl: 'https://wy.dazhengyun.com/api/h5/', h5Url: 'https://wy.dazhengyun.com/public/wap/ph2', appVersion: '1.0.0', platform: 'wxapp', userinfo:{} }, onLaunch: function (options) { // let orgId = 3 // wx.setStorageSync("orgId", orgId); this.getScene(options); }, getScene(options){ const scene = decodeURIComponent(options.scene); let ss = scene.split('-'); if(ss.length == 2){ var orgId = wx.getStorageSync("orgId"); if(!orgId || (orgId && Number(orgId) != Number(ss[1]))){ wx.setStorageSync("orgId", ss[1]); this.logout(); } } var orgId = wx.getStorageSync("orgId"); if(!orgId){ var orgId = 3; wx.setStorageSync("orgId", 3); } }, getCommonParam(){ var orgId = wx.getStorageSync("orgId"); var userId = wx.getStorageSync("userId"); var token = wx.getStorageSync("token"); return "orgId="+orgId+"&userId="+userId+"&token="+token; }, logout(){ wx.removeStorageSync("token"); wx.removeStorageSync("userId"); }, checkCode:function(code){ if(code == -100){ wx.showToast({ title: '登录超时,请重新登录', icon: 'none', duration: 2000 }) this.logout(); setTimeout(function(){ wx.reLaunch({ //关闭所有页面,打开到应用内的某个页面 url: '/pages/splash/splash' }) },2000); return false; } return true; }, ajax:function(params){ console.log('params',params) if (!params.type) { var type = 'POST'; }else{ var type = params.type; } if (!params.apiname) { var apiname = '1'; } else { var apiname = params.apiname; } if (!params.data) { var data = []; }else{ var data = params.data; } var app = getApp(); var userId = wx.getStorageSync("userId"); var token = wx.getStorageSync("token"); var orgId = wx.getStorageSync("orgId"); console.log('params2',userId,token,token); if (!userId || userId == undefined) { //未登录 wx.showToast({ title: '未登录', icon: 'none', duration: 2000 }); this.logout(); setTimeout(function () { wx.reLaunch({ //关闭所有页面,跳转到闪屏页 url: '/pages/splash/splash' }) }, 2000); return false; } data['userId'] = userId; data['token'] = token; data['orgId'] = orgId; console.log('params3',data); wx.showLoading({ title: '加载中', mask: true }); wx.request({ url: params.url, data: data, method: type, header: { 'content-type': 'application/x-www-form-urlencoded' }, success: function (res) { console.log('params4',res); wx.hideLoading(); wx.stopPullDownRefresh(); var ret = app.checkCode(res.data.code); //检查token是否失效 if(ret == false){ return false; } if (res.data.code == 0) { if (app.ajaxReadyCallback) { //在onShow中调用数据的,添加在onShow方法内,防止不同页面覆盖 res.data['apiname'] = apiname; app.ajaxReadyCallback(res) } } else { let showMsg = true; if(params.showMsg != undefined){ showMsg = params.showMsg; } if(showMsg){ wx.showToast({ title: res.data.message, icon: 'none', duration: 2000 }) } } }, fail: function (err) { wx.hideLoading(); wx.showToast({ title: '请求失败', icon: 'none', duration: 2000 }) } }) }, })