123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- // 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
- })
- }
- })
- },
- })
|