//app.js
var that=null
App({
  onLaunch: function () {
    console.log('App Launch')
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    that=this
  },
  getUserInfo:function(){
    var userId = wx.getStorageSync("userId")
    var LoginId = wx.getStorageSync("LoginId")
    if(!userId||!LoginId){
      //调用登录接口
       that.onLogin() 
    }else{
      var viewid = wx.getStorageSync('viewid') 
                      var viewpoid = wx.getStorageSync('viewpoid') 
                      if(viewid){
                         wx.removeStorageSync('viewid')
                         wx.removeStorageSync('viewpoid')
                         wx.navigateTo({  
                            url: "../details/index?id=" + viewid + (viewpoid?"&poid=" +viewpoid:"")
                         })
                      }
                      
                      var videoid = wx.getStorageSync('videoid0') 
                      if(videoid){
                        if(videoid!=""){
                        wx.switchTab({
                           url: "../myvideo/index"
                        })
                        }
                     }
    }
  }, 
  onLogin: function () {
   
    wx.login({
      success: function (res) {
        
        if (res.code) {
          //发起网络请求
         
          var tguid = wx.getStorageSync('tguid');
         
          wx.request({
            url: that.globalData.apiurl+'/xapiajax.ashx',
            method:"Get",
            dataType:"json",
            data: {
              action:"onLogin",
              tguid:(!tguid?"":tguid),
              code: res.code
            },
            success: function(res0) {
              
              if(res0.data.status==1){
                  wx.setStorageSync('userId', res0.data.userId)
                  wx.setStorageSync('LoginId', res0.data.LoginId)  
                  wx.removeStorageSync('openid')
                  wx.removeStorageSync('IFLogin') //是微信登录0还是账号登录1
                  wx.removeStorageSync('IFYYD')
                  that.globalData.IFRefreshmy=1
                  that.globalData.IFRefreshindex=1 
                  that.globalData.IFRefreshshop=1 
                  wx.setStorageSync('userInfo', JSON.stringify(res0.data.userInfo)) 
                  if(res0.data.userInfo.uLevel2==2){
                       wx.setStorageSync('IFagent', 1);
                  }else{
                       wx.setStorageSync('IFagent', 0);
                  }
                  if(that.globalData.IFwxLogin==1){
                      that.globalData.IFwxLogin=0
                       wx.showToast({
                           title: res0.data.msg,
                           icon: "none",
                           duration: 2000
                       })
                       var sopenFrame=setInterval(function (){
                             clearInterval(sopenFrame);
                             wx.navigateBack({}) 
                       }, 1000)
                  }else{
                      var viewid = wx.getStorageSync('viewid') 
                      var viewpoid = wx.getStorageSync('viewpoid') 
                      if(viewid){
                         wx.removeStorageSync('viewid')
                         wx.removeStorageSync('viewpoid')
                         wx.navigateTo({  
                            url: "../details/index?id=" + viewid + (viewpoid?"&poid=" +viewpoid:"")
                         })
                      }
                      
                      var videoid = wx.getStorageSync('videoid0') 
                      if(videoid){
                        if(videoid!=""){
                            wx.navigateTo({
                               url: "../myvideo/index"
                            })
                        }
                     }
                  }
              }else{
                  wx.showToast({
                      title: res0.data.msg,
                      icon: "none",
                      duration: 3000
                  })
              }
            }
          })
        } else {
                wx.showToast({
                  title: '登录失败！' + res.errMsg,
                  icon: "none",
                  duration: 3000
              })
        }
        
      }
    })
  },
  onShow: function () {
    console.log('App Show')
  },
  onHide: function () {
    console.log('App Hide')
  },isNumber:function (s) {//判断是否是数字
    var regu = "^[0-9]+$";
    var re = new RegExp(regu);
    if (re.test(s)) {
        return true;
    } else {
        return false;
    }
},isamount:function(s) {
       var fix_amountTest=/^(([1-9]\d*)|\d)(\.\d{1,2})?$/;
       if(fix_amountTest.test(s)==false){
          return false;
       }else{
          return true;
       }
  },isNumberOr_Letter:function(s) {//判断是否是数字或字母 
    var regu = "^[0-9a-zA-Z]+$";
    var re = new RegExp(regu);
    if (re.test(s)) {
        return true;
    } else {
        b = false;
    }
  },changeTwoDecimal_f:function(x) {
    var f_x = parseFloat(x);
        
        var f_x = Math.round(x * 100) / 100;
        var s_x = f_x.toString();
        var pos_decimal = s_x.indexOf('.');
        if (pos_decimal < 0) {
            pos_decimal = s_x.length;
            s_x += '.';
        }
        while (s_x.length <= pos_decimal + 2) {
            s_x += '0';
        }
        return s_x;
  },ccityname: function(value){
    if(value){
       value=value.replace("市","").replace("自治州","").replace("同治州","").replace("地区","");
    }
    return value
  },replacebody: function(value){
    if(value){
       value=value.replace("<br>","");
    }
    return value
  }, 
  globalData:{
    userInfo:null,
    apiurl:"https://vip.agqyjs.com",//"http://localhost:8080",//http://localhost:24382  //https://vip.agqyjs.com
    IFRefreshCart:0,
    IFRefreshsell:0,
    IFRefreshsell0:0,
    IFRefreshsell1:0,
    IFRefreshmy:0, 
    IFRefreshindex:0,
    IFRefreshshop:0,
    IFRefreshordershop0:0,
    IFRefreshmypt:0,
    IFRefreshmydhm:0,
    IFRefreshshangquan:0,
    IFRefreshmyaddress:0,
    IFRefreshmyagent_products:0,
    IFRefreshmyhongbao:0,
    IFRefreshmyorder:0,
    IFRefreshduihuan:0,
    IFRefreshmybalance:0,
    IFRefreshzzjf:0,
    selladdress:{},
    selladdress0:{},
    IFYGM:0,
    IFwxLogin:0,//是否点击微信一键登录1还是扫码自动登录0
    servermsg:"",
    // 全局菜单配置
    tabBarList: [
      {
        "pagePath": "/pages/index/index",
        "text": "首页",
        "iconPath": "/images/footer-icon-001.png",
        "selectedIconPath": "/images/footer-icon-001-active.png"
      },
      {
        "pagePath": "/pages/news/index",
        "iconPath": "/images/footer-icon-008.png",
        "selectedIconPath": "/images/footer-icon-008-active.png",
        "text": "资讯"
      },
      {
        "pagePath": "/pages/shop/index",
        "iconPath": "/images/footer-icon-002.png",
        "selectedIconPath": "/images/footer-icon-002-active.png",
        "text": "商城"
      },
      {
        "pagePath": "/pages/business/index",
        "iconPath": "/images/footer-icon-005.png",
        "selectedIconPath": "/images/footer-icon-005-active.png",
        "text": "健康驿站"
      },
      {
        "pagePath": "/pages/my/index",
        "text": "我的",
        "iconPath": "/images/footer-icon-004.png",
        "selectedIconPath": "/images/footer-icon-004-active.png"
      }
    ],
    // 特殊权限菜单配置（bdshEnable=1）
    tabBarListBdsh: [
      {
        "pagePath": "/pages/index/index",
        "text": "首页",
        "iconPath": "/images/footer-icon-001.png",
        "selectedIconPath": "/images/footer-icon-001-active.png"
      },
      {
        "pagePath": "/pages/news/index",
        "iconPath": "/images/footer-icon-008.png",
        "selectedIconPath": "/images/footer-icon-008-active.png",
        "text": "资讯"
      },
      {
        "pagePath": "/pages/shop/index",
        "iconPath": "/images/footer-icon-002.png",
        "selectedIconPath": "/images/footer-icon-002-active.png",
        "text": "商城"
      },
      {
        "pagePath": "/pages/my/index",
        "text": "我的",
        "iconPath": "/images/footer-icon-004.png",
        "selectedIconPath": "/images/footer-icon-004-active.png"
      }
    ]
  } 
})