import {
  getToken
} from '/utils/auth'

//全局分享
! function () {
  var PageTmp = Page;
  Page = function (pageConfig) {
    // 设置全局默认分享
    pageConfig = Object.assign({
      //右上角分享功能
      onShareAppMessage() {
        return {
          title: '智慧工地优管', //分享标题
          path: '/pages/login/login', //分享用户点开后页面
          success(res) {
            console.log('分享成功!')
          }
        }
      }
    }, pageConfig);
    PageTmp(pageConfig);
  };
}();

App({
  globalData: {
    userData: null,
    useProData: null,
    subDeptUserData: null,
    useProjectId: '',
    useProjectName: '',
    searchProject: {},
    projectInfoList: [],
  },

  onLaunch: function (options) {
    if (!wx.cloud) {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力')
    } else {
      wx.cloud.init({
        // env 参数说明:
        //   env 参数决定接下来小程序发起的云开发调用(wx.cloud.xxx)会默认请求到哪个云环境的资源
        //   此处请填入环境 ID, 环境 ID 可打开云控制台查看
        //   如不填则使用默认环境(第一个创建的环境)
        // env: 'my-env-id',
        traceUser: true,
      })
    }
    this.autoUpdate();
    
    /**
     * 初始化页面未登录时跳转到登录页
     */
    if(options && options.query && options.query.QRPID){
      //扫码进入时不效验登录...跳转授权登录页
      console.log("扫码进入时不效验登录...跳转授权登录页");
    }else{
      if (!getToken()) {
        // setTimeout(() => {
        //   this.toast("请使用手机号码登录");
        // }, 1000);
        wx.redirectTo({
          url: '/pages/login/index',
        });
      }
    }
  },

  onLoad() {

  },

  /**
   * 安全加载
   */
  securityLoading(){
    setTimeout(() => {
      wx.hideLoading();
    }, 60000);
  },

  //页面弹窗
  toast: function (msg, type, time) {
    wx.showToast({
      title: msg,
      icon: type === undefined ? 'none' : type,
      duration: time === undefined ? 1200: time,
      mask: true
    });
  },

  /**
   * 计算时差
   * @param {*} val 
   */
  getDurationDate: function (val) {
    // 计算出相差天数
    let days = Math.floor(val / (24 * 3600 * 1000))
    // 计算出小时数
    let leave1 = val % (24 * 3600 * 1000) // 计算天数后剩余的毫秒数
    let hours = Math.floor(leave1 / (3600 * 1000))
    // 计算相差分钟数
    let leave2 = leave1 % (3600 * 1000) // 计算小时数后剩余的毫秒数
    let minutes = Math.floor(leave2 / (60 * 1000))
    // 计算相差秒数
    let leave3 = leave2 % (60 * 1000) // 计算分钟数后剩余的毫秒数
    let seconds = Math.round(leave3 / 1000)
    if (days > 0) {
      if (days < 10) days = "0" + days;
      if (hours < 10) hours = "0" + hours;
      if (minutes < 10) minutes = "0" + minutes;
      if (seconds < 10) seconds = "0" + seconds;
      return days + '天' + hours + '小时' + minutes + '分钟' + seconds + '秒';
    }
    if (hours > 0) {
      if (hours < 10) hours = "0" + hours;
      if (minutes < 10) minutes = "0" + minutes;
      if (seconds < 10) seconds = "0" + seconds;
      return hours + '小时' + minutes + '分钟' + seconds + '秒';
    }
    if (minutes > 0) {
      if (minutes < 10) minutes = "0" + minutes;
      if (seconds < 10) seconds = "0" + seconds;
      return minutes + '分钟' + seconds + '秒';
    }
    if (seconds > 0) {
      if (seconds < 10) seconds = "0" + seconds;
      return seconds + '秒';
    }
  },

  //版本自动更新
  autoUpdate: function () {
    var self = this
    // 获取小程序更新机制兼容
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      //1. 检查小程序是否有新版本发布
      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          //2. 小程序有新版本,则静默下载新版本,做好更新准备
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好,是否重启应用?',
              success: function (res) {
                if (res.confirm) {
                  //3. 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate()
                } else if (res.cancel) {
                  //如果需要强制更新,则给出二次弹窗,如果不需要,则这里的代码都可以删掉了
                  wx.showModal({
                    title: '温馨提示~',
                    content: '本次更新可能会导致旧版本无法正常访问,请使用新版本',
                    success: function (res) {
                      self.autoUpdate()
                      //第二次提示后,强制更新
                      // if (res.confirm) {
                      // // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
                      // updateManager.applyUpdate()
                      // } else if (res.cancel) {
                      // //重新回到版本更新提示
                      // self.autoUpdate()
                      // }
                    }
                  })
                }
              }
            })
          })
          updateManager.onUpdateFailed(function () {
            // 新的版本下载失败
            wx.showModal({
              title: '已经有新版本了哟~',
              content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
            })
          })
        }
      })
    } else {
      // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
      })
    }
  }
})