//app.js App({ //全局变量 globalData: { //服务器地址 serverUrl: 'https://mdyg.sdmingde.cn/api/v1/', uploadServerUrl: 'https://mdyg.sdmingde.cn/api/v1/', visitorServerUrl: 'https://mdyg.sdmingde.cn/api/h5/', appVersion: '1.0.0', platform: 'wxapp', userinfo:{} }, onLaunch: function (options) { // let orgId = 3 // wx.setStorageSync("orgId", orgId); // wx.setStorageSync("token", 3914262743650); // wx.setStorageSync("userId", 3); 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(); } } }, logout(){ wx.removeStorageSync("token"); wx.removeStorageSync("userId"); wx.removeStorageSync("visitorToken"); wx.removeStorageSync("visitorUserId"); }, 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){ 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 = app.globalData.userinfo.userId; var token = app.globalData.userinfo.token; var orgId = wx.getStorageSync("orgId"); 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; 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) { 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 }) } }) }, loginVisitor:function(type){ wx.login({ success: function (loginRes) { var app = getApp(); var serverUrl = app.globalData.visitorServerUrl; if (loginRes) { wx.request({ url: serverUrl + 'oauth/checkCode', data: { type: 2, code: loginRes.code//临时登录凭证 }, method: 'POST', header: { 'content-type': 'application/x-www-form-urlencoded' }, success: function (res) { if (res.data.code == -101) { wx.removeStorageSync("visitorToken"); if(type == 'visitorAdd'){ wx.redirectTo({ url: '/pages/splash/splash?type=visitorAdd' }) }else{ wx.redirectTo({ url: '/pages/splash/splash?type=visitor' }) } } if (res.data.code == 0) { try { wx.setStorageSync("visitorToken", res.data.data.token); wx.setStorageSync("visitorUserId", res.data.data.userId); } catch (e) { this.logout(); wx.showModal({ title: '提示', content: '登录异常!', showCancel: false }) return false; }; if(type == 'visitorAdd'){ wx.redirectTo({ url: '/pages/visitor/add/index' }); }else{ wx.redirectTo({ url: '/pages/visitor/index' }); } } }, fail: function (err) { wx.showModal({ title: '提示', content: '登录异常!', showCancel: false }) } }) } else { wx.showModal({ title: '提示', content: '登录异常!', showCancel: false }) } } }) }, checkCode2:function(code){ if(code == -100){ wx.showToast({ title: '登录超时,请重新登录', icon: 'none', duration: 2000 }) wx.removeStorageSync('visitorToken'); wx.removeStorageSync('visitorUserId'); setTimeout(function(){ var vtype = wx.getStorageSync('visitorType'); if(vtype == 'visitorAdd'){ wx.redirectTo({ //关闭所有页面,打开到应用内的某个页面 url: '/pages/splash/splash?type=visitorAdd' }) }else{ wx.redirectTo({ //关闭所有页面,打开到应用内的某个页面 url: '/pages/splash/splash?type=visitor' }) } },2000); return false; } return true; }, ajax2:function(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 token = wx.getStorageSync('visitorToken'); var userId = wx.getStorageSync('visitorUserId'); var vtype = wx.getStorageSync('visitorType'); if (!token || token == undefined) { //未登录 wx.showToast({ title: '未登录', icon: 'none', duration: 2000 }); setTimeout(function () { if(vtype == 'visitorAdd'){ wx.redirectTo({ //关闭所有页面,跳转到闪屏页 url: '/pages/splash/splash?type=visitorAdd' }) }else{ wx.redirectTo({ //关闭所有页面,跳转到闪屏页 url: '/pages/splash/splash?type=visitor' }) } }, 2000); return false; } data['token'] = token; data['userId'] = userId; 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) { wx.hideLoading(); wx.stopPullDownRefresh(); var ret = app.checkCode2(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 }) } }) }, gotopage: function (url, data) { //传递数据到下一页面的跳转,在新页面onload中获取 wx.setStorage({ key: "gotopage", data: data, success: function () { wx.navigateTo({ url: url, }) }, fail: function () { wx.showToast({ title: '跳转失败', icon: 'none', duration: 2000 }) return false; } }); }, getgotodata: function (url, data) { //传递数据到下一页面的跳转,在新页面onload中获取 wx.getStorage({ key: 'gotopage', success(res) { return res.data; }, fail(res) { wx.navigateBack({ delta: 1 }); return false; } }); } })