微信小程序 app.js 启动配置文件

utils/getsign.js

var utilmd5 = require('md5.js')
function getSign(time, datapar) {
  var key = '*********=';
  var str = key + time + datapar;
  var sign = '';
  sign = utilmd5.hexMD5(str);
  return sign;
}
function getSignwx(datapar) {
  var appid = '*********';
  var key = '**********';
  var str = 'appId=' + appid + datapar + '&key=' + key;
  var sign = '';
  sign = utilmd5.hexMD5(str);
  return sign;
}
module.exports = {
  getSign: getSign,
  getSignwx: getSignwx
}
var utiltime = require('utils/time.js')
var utilsign = require('utils/getsign.js')
var imgUrl = ''
App({
	globalData: {
    canvas:{},
    version:null,
    loginsatus: false,
    userInfo: null,
    userid: null,
    pageUrl:'',
    // APIUrl: '',
    APIUrl: '',  //测试接口地址
    // APIUrl: '',  //正式接口地址    
    imgUrl:'',  //图片地址
   
    tabBar: {
      "color": "#666666",
      "selectedColor": "#fa2422",
      "backgroundColor": "#fff",
      "borderStyle": "#efefef",
      "list": [
        {
          "pagePath": "/pages/homePage/productsIndex/index/index",
          "text": "首页",
          "iconPath": imgUrl +"indexDxyp.png",
          "selectedIconPath": imgUrl +"indexDxyp.png",
          "clas": "menu-item",
          "selectedColor": "#fa2422",
          active: true
        },
        {
          "pagePath": "/pages/find/find",
          "text": "发现",
          "iconPath": imgUrl +"findDxyp.png",
          "selectedIconPath": imgUrl +"findDxyp.png",
          "selectedColor": "#fa2422",
          "clas": "menu-item",
          active: false
        },
        {
          "pagePath": "/pages/classify/classify",
          "text": "分类",
          "iconPath": imgUrl +"classifyDxyp.png",
          "selectedIconPath": imgUrl +"classifyDxyp.png",
          "selectedColor": "#fa2422",
          "clas": "menu-item",
          active: false
        },
        {
          "pagePath": "/pages/userPage/myInfo/my/my",
          "text": "我的",
          "iconPath": imgUrl +"mineDxyp.png",
          "selectedIconPath": imgUrl +"mineDxyp.png",
          "selectedColor": "#fa2422",
          "clas": "menu-item",
          active: false
        }
      ],
      "position": "bottom"
    },
    tabBar2: {
      "color": "#666666",
      "selectedColor": "#fa2422",
      "backgroundColor": "#fff",
      "borderStyle": "#efefef",
      "list": [
        {
          "pagePath": "/pages/homePage/productsIndex/index/index",
          "text": "首页",
          "iconPath": imgUrl +"indexDxyp.png",
          "selectedIconPath": imgUrl +"indexDxyp.png",
          "clas": "menu-item2",
          "selectedColor": "#fa2422",
          active: true
        },
        {
          "pagePath": "/pages/find/find",
          "text": "发现",
          "iconPath": imgUrl +"findDxyp.png",
          "selectedIconPath": imgUrl +"findDxyp.png",
          "selectedColor": "#fa2422",
          "clas": "menu-item2",
          active: false
        },
        {
          "pagePath": "/pages/classify/classify",
          "text": "分类",
          "iconPath": imgUrl +"classifyDxyp.png",
          "selectedIconPath": imgUrl +"classifyDxyp.png",
          "selectedColor": "#fa2422",
          "clas": "menu-item2",
          active: false
        },
        {
          "pagePath": "/pages/orderPage/cartInfo/car/car",
          "text": "购物车",
          "iconPath": imgUrl +"cartDxyp.png",
          "selectedIconPath": imgUrl +"cartDxyp.png",
          "selectedColor": "#fa2422",
          "clas": "menu-item2",
          active: false,
          carNum:0
        },
        {
          "pagePath": "/pages/userPage/myInfo/my/my",
          "text": "我的",
          "iconPath": imgUrl +"mineDxyp.png",
          "selectedIconPath": imgUrl +"mineDxyp.png",
          "selectedColor": "#fa2422",
          "clas": "menu-item2",
          active: false
        }
      ],
      "position": "bottom"
    },
    "networkTimeout": {
      "request": 3000,
    },
  },
  onLaunch: function () {
    //检查是否存在新版本
    wx.getUpdateManager().onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      console.log("是否有新版本:" + res.hasUpdate);
      if (res.hasUpdate) {//如果有新版本
        wx.clearStorageSync() 
        // 小程序有新版本,会主动触发下载操作(无需开发者触发)
        wx.getUpdateManager().onUpdateReady(function () {//当新版本下载完成,会进行回调
          wx.showModal({
            title: '更新提示',
            content: '新版本已经准备好,单击确定重启应用',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
                wx.getUpdateManager().applyUpdate();
              }
            }
          })

        })
        // 小程序有新版本,会主动触发下载操作(无需开发者触发)
        wx.getUpdateManager().onUpdateFailed(function () {//当新版本下载失败,会进行回调
          wx.showModal({
            title: '提示',
            content: '检查到有新版本,但下载失败,请检查网络设置',
            showCancel: false,
          })
        })
      }
    })

    //用户静默登录
    that.auth_login()
  },
	auth_login:function(){
    // 静默获取用户信息
    var that = this;
    if (wx.getStorageSync('wxuserid')) {
      wx.setStorageSync('oldUsers', false)
      return false
    }
    wx.login({
      success: function (res) {
        var arys = {
          'code': res.code
        };
        that.doRequestService('/svc/api/user/getUserByCode', arys, function (res) {
          console.log(res)
          // return
          if (res.status == 'SUCCESS') {
            console.log(res.userBaseInfo)
            if (res.userBaseInfo && res.userBaseInfo.phone && res.userId) {
              wx.setStorageSync('wxuserid', res.userId)
              wx.setStorageSync('userinfo', res.userBaseInfo)
              
            } 
            
            wx.setStorageSync('oldUsers', false)
          }
        }, null, null)
      }
    })
  },
  // 分享
  onShare:function(obj){
    if(!obj) var obj = {};
    var shareObj={};
    var userName = wx.getStorageSync('userinfo').nickName;//用户名
    shareObj.path = obj.path || 'pages/homePage/productsIndex/index/index';
    shareObj.title = obj.title || userName+'分享给你一个小程序,快来看看吧';
    shareObj.imgUrl = obj.imgUrl || '';
    return shareObj
  },
  
  sort:function (arys){
    //先用Object内置类的keys方法获取要排序对象的属性名,再利用Array原型上的sort方法对获取的属性名进行排序,newkey是一个数组
    var newkey = Object.keys(arys).sort();
    var newObj = {}; //创建一个新的对象,用于存放排好序的键值对
    for(var i = 0; i<newkey.length; i++) {
      //遍历newkey数组 
      if ("object" == typeof arys[newkey[i]] && !Array.isArray(arys[newkey[i]])) {
        newObj[newkey[i]] = sort(arys[newkey[i]]);
      } else {
        newObj[newkey[i]] = arys[newkey[i]];
      }
      //向新创建的对象中按照排好的顺序依次增加键值对
    }
    return newObj;
  },
  objKeySort:function (arys) {
    var newObj = {};
    newObj = this.sort(arys);
    var jsonstr = '';
    for(var key in newObj) {
      var value = '';
      if ("object" == typeof newObj[key]) value = JSON.stringify(newObj[key]); else value = newObj[key];
      jsonstr += key + value;
    }
    return jsonstr; //返回排好序的新对象
  },
  
  //http封装
  
  doRequestService:function (url, postData, doSuccess, doFail, doComplete) {
    var _time = utiltime.getTimems();
    var jsonstr = this.objKeySort(postData); //函数执行
    console.log(jsonstr)
    var _sign = utilsign.getSign(_time, jsonstr);
    var dateStr = JSON.stringify(postData);
    // console.log("验签参数:" + jsonstr);
    var map = { paramJson: '{"data":\'' + dateStr + '\',"sign":"' + _sign + '","source":"weipro","timeStamp":' + _time + '}' }
    wx.request({
      url: this.globalData.APIUrl + url,
      data: map,
      async: false,    //或false,是否异步
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      method: 'POST',
      success: function (res) {
        typeof doSuccess == "function" && doSuccess(res.data)
      },
      fail: function (res) {
        console.log(res);
        wx.hideLoading();
        wx.showToast({
          title: '服务器异常,请求超时!', //提示的内容,
          icon: "none",
          duration: 3000, //延迟时间,
        });
        typeof doFail == "function" && doFail()
      },
      complete: function () {
        typeof doComplete == "function" && doComplete()
      }
    })
    return
  },
  
  //第一种状态的底部  
  editTabBar: function () {
    var that = this
    var _curPageArr = getCurrentPages();
    var _curPage = _curPageArr[_curPageArr.length - 1];
    var _pagePath = _curPage.__route__;
    if (_pagePath.indexOf('/') != 0) {
      _pagePath = '/' + _pagePath;
    }
    var tabBar = this.globalData.tabBar;
    if (_pagePath.indexOf('search') != -1) {
      that.globalData.tabBar2.list[0].pagePath = '/pages/homePage/productsList/search/search'
    }else{
      that.globalData.tabBar2.list[0].pagePath = '/pages/homePage/productsIndex/index/index'
      for (var i = 0; i < tabBar.list.length; i++) {
        tabBar.list[i].active = false;
        if (tabBar.list[i].pagePath == _pagePath) {
          tabBar.list[i].active = true;//根据页面地址设置当前页面状态    
        }
      }
    }   
    _curPage.setData({
      tabBar: tabBar
    });
  },
  
  //第二种状态的底部  
  editTabBar2: function () {
    var that=this;
    var _curPageArr = getCurrentPages();
    var _curPage = _curPageArr[_curPageArr.length - 1];
    var _pagePath = _curPage.__route__;
    if (_pagePath.indexOf('/') != 0) {
      _pagePath = '/' + _pagePath;
    }
    var tabBar = this.globalData.tabBar2;
    if (_pagePath.indexOf('search') != -1){
      that.globalData.tabBar2.list[0].pagePath ='/pages/homePage/productsList/search/search'
    }else{
      that.globalData.tabBar2.list[0].pagePath = '/pages/homePage/productsIndex/index/index'
      for (var i = 0; i < tabBar.list.length; i++) {
        tabBar.list[i].active = false;
        if (tabBar.list[i].pagePath == _pagePath) {
          tabBar.list[i].active = true;//根据页面地址设置当前页面状态    
        }
      }
    }
    _curPage.setData({
      tabBar: tabBar
    });
  },
  jump:function(url){
    if (wx.getStorageSync('wxuserid')){
      if(url){
        wx.navigateTo({
          url: url,
        })
      }else{
        return true
      }
    }else{
      wx.showModal({
        title: '提示',
        content: '您尚未登录,请前去登录吧',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login?noLogin=true',
            })
          } else if (res.cancel) {
            
          }
        }
      })
      return false
    }
  }
})
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值