<template>
  <div style="width:100%; height: 100%; position: relative;">
     <vc-keep-alive :ignorePaths="ignorePaths" :ignoreParams="ignoreParams">
        <router-view style="width:100%;height:100%"></router-view>
     </vc-keep-alive>
  </div>
</template>
<script>
  import Vue from 'vue';
  import FastClick from 'fastclick'
  import VcKeepAlive from 'vc-keep-alive';
  Vue.use(VcKeepAlive);
  export default {
    name: 'App',
    data() {
      return {
        ignorePaths:['/help_detail'],
        ignoreParams:[]
      };
    },
    methods: {
      IsPC() {
        var userAgentInfo = navigator.userAgent;
        var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
        var flag = true;
        for (var v = 0; v < Agents.length; v++) {
          if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
          }
        }
        return flag;
      },
      // 检查更新
      get_ver() {
        let self = this
        if (!window.plus) {
          return false
        }
        plus.runtime.getProperty(plus.runtime.appid, function(inf) {
          wgtVer = inf.version
          if (!localStorage.getItem('a-token-temp')) {
            return false
          }
          if (plus.webview.currentWebview().id != plus.runtime.appid) {
            return false
          }
          self.$axios('post', '/api/version/check', {
            version: wgtVer.toString()
          }).then((res) => {
            self.info = res.data.data
            if (res.data.code == 1) {
              if (res.data.data.status) {} else {
                self.downWgt(res.data.data.new_version.app_path)
              }
            }
          })

        })
      },
      downWgt(wgtUrl) {
        let self = this;
        plus.downloader.createDownload(wgtUrl, {
          filename: '_doc/update/'
        }, function(d, status) {
          if (status == 200) {
            self.$removeCache('user_info');
            self.installWgt(d.filename);
          }
        }).start();
      },
      installWgt(path) {
        plus.runtime.install(path, {
            force: true
          },
          function() {
            plus.runtime.restart()
          },
          function(e) {
            plus.runtime.restart()
          }
        )
      },

      // 缓存选择周数据
      getColumns() {
        let columns = [];
        for (let i = 2020; i < 2051; i++) {
          let obj = {
            value: i,
            text: i.toString() + '年',
            children: this.cycleTypeArr(i)
          }
          columns.push(obj)
        }
        this.$setCache('weekArr', columns);
      },
      cycleTypeArr(year) {
        let options = [{
            text: '1月'
          },
          {
            text: '2月'
          },
          {
            text: '3月'
          },
          {
            text: '4月'
          },
          {
            text: '5月'
          },
          {
            text: '6月'
          },
          {
            text: '7月'
          },
          {
            text: '8月'
          },
          {
            text: '9月'
          },
          {
            text: '10月'
          },
          {
            text: '11月'
          },
          {
            text: '12月'
          },
        ];
        options.forEach((e, index) => {
          let week_count = this.getWeeks(year, index + 1);
          e.children = this.returnWeekArr(week_count)
        })
        return options;
      },
      returnWeekArr(count) {
        if (count == 4) {
          return [{
            text: '第一周'
          }, {
            text: '第二周'
          }, {
            text: '第三周'
          }, {
            text: '第四周'
          }]
        } else {
          return [{
            text: '第一周'
          }, {
            text: '第二周'
          }, {
            text: '第三周'
          }, {
            text: '第四周'
          }, {
            text: '第五周'
          }]
        }
      },
      getWeeks(year, month) {
        var d = new Date();
        // 该月第一天
        d.setFullYear(year, month - 1, 1);
        var w1 = d.getDay();
        if (w1 == 0) w1 = 7;
        // 该月天数
        d.setFullYear(year, month, 0);
        var dd = d.getDate();
        // 第一个周一
        let d1;
        if (w1 != 1) d1 = 7 - w1 + 2;
        else d1 = 1;
        let week_count = Math.ceil((dd - d1 + 1) / 7);
        return week_count;
      },
      init() {
        if (!this.$getCache('weekArr')) { //缓存周信息
          this.getColumns();
        }
        // 是否是安卓
        if (navigator.userAgent.indexOf('Android') > 0) {
          this.$setCache('isAndroid', true)
        } else {
          this.$setCache('isAndroid', false)
        }
        //  是否是需要兼容IOS特殊机型         // 下边横杠兼容
        if (window.plus) {
          plus.navigator.setStatusBarStyle('light');
          plus.runtime.setBadgeNumber(0);  //清除角标
          var str = window.plus.device.model;
          // console.log("iPhone",str)
          if (str.indexOf('iPhone') >= 0) {
            if (
              str === 'iPhone4' ||
              str === 'iPhone4s' ||
              str === 'iPhone5' ||
              str === 'iPhone5c' ||
              str === 'iPhone5s' ||
              str === 'iPhone6' ||
              str === 'iPhone6 Plus' ||
              str === 'iPhone6s' ||
              str === 'iPhone6s Plus' ||
              str === 'iPhone SE' ||
              str === 'iPhone7' ||
              str === 'iPhone7 Plus' ||
              str === 'iPhone8' ||
              str === 'iPhone8 Plus'
            ) {
              this.$setCache('iPhone', false);
            } else {
              this.$setCache('iPhone', true);
            }
          }
        }
      },
    },
    mounted() {
      if (process.env.NODE_ENV == 'production') {
        // if (this.IsPC()) {
        //   this.$router.replace({name: 'error'}); // 提示去APP端
        //   return false
        // } else {
          this.get_ver(); //热更新
        // }
      }
    },
    created() {
      document.documentElement.style.backgroundColor = '#26A2FF';
      FastClick.attach(document.body); //300毫秒点击问题
      window.sessionStorage.setItem('__VCKEEPALIVE__', JSON.stringify(['/','/home']))
      window.sessionStorage.setItem('routers', '/');
      this.init()
    }
  };
</script>

<style>
  html,
  body {
    height: 100%;
    margin: 0;
    padding: 0;
  }

  #app {
    width: 100%;
    margin: 0 auto;
    height: 100%;
  }

  .suspendButton {
    height: 1.3rem;
    width: 1.3rem;
    /*1.3 如果碰到滑动问题，请检查 z-index。z-index需比web大一级*/
    z-index: 999;
    position: fixed;
    bottom: 1.3rem;
    right: 0.1rem;
    border-radius: 50%;
    background-color: rgb(38 161 255);
    color: #fff;
    touch-action: none;
  }

  .yuanqiu {
    font-size: 0.2rem;
    text-align: center;
    position: relative;
    top: 50%;
    transform: translate(0, -50%);
  }
</style>
