<template>
  <view class="main">
    <code-elf-guide v-if="guidePages && list.length > 0" :s="s" :list="list"></code-elf-guide>
    <view v-if="guidePages && list.length == 0" class="jump-over" @tap="launchFlag()">{{jumpover}}</view>
  </view>
</template>

<script>
  import codeElfGuide from '@/components/code-elf-guide/code-elf-guide.vue'
  let timer
  export default {
    components: {
      codeElfGuide
    },
    data() {
      return {
        guidePages: false,
        list: [],
        jumpover: '跳过',
        s: 6
      }
    },
    onLoad() {
      // this.loadExecution()
      // #ifdef APP-PLUS
      if (plus.os.name == 'iOS') {
        // 首先获取一次网络状态
        uni.getNetworkType({
          success: (res) => {
            console.log(res, 111)
            if (res.networkType != 'none') {
              this.getAd()
            }
          }
        });
      } else {
        this.getAd()
      }
      // #endif

      // #ifdef H5
      this.getAd()
      // #endif

    },
    onShow() {
      // 监听网络变化
      uni.onNetworkStatusChange((res) => {
        console.log(res, 222)
        if (res.isConnected) {
          this.getAd()
        }
      });
    },
    onHide() {
      uni.offNetworkStatusChange(() => {})
    },
    onUnload() {
      clearInterval(timer)
    },
    methods: {
      loadExecution: function() {
        /**
         * 获取本地存储中launchFlag的值
         * 若存在，说明不是首次启动，直接进入首页；
         * 若不存在，说明是首次启动，进入引导页；
         */
        try {
          // 获取本地存储中launchFlag标识
          const value = uni.getStorageSync('launchFlag');
          if (value) {
            this.guidePages = false
            // launchFlag=true直接跳转到首页
            uni.reLaunch({
              url: '/pages/index/news'
            });
          } else {
            // launchFlag!=true显示引导页
            this.guidePages = true
          }
        } catch (e) {
          // error 
          uni.setStorage({
            key: 'launchFlag',
            data: true,
            success: function() {
              console.log('error时存储launchFlag');
            }
          });
          this.guidePages = true
        }
        return;
        uni.reLaunch({
          url: '/pages/index/news'
        });
      },
      getAd() {
        this.$http.get('/ad/ad/getShopAdByAdL/0/startAdvertising').then(res => {
          if (res.code == 200) {
            this.list = res.list
            if (res.list.length > 0) {
              this.loadExecution()
              timer = setInterval(() => {
                this.s--
                if (this.s == 0) {
                  uni.setStorage({
                    key: 'launchFlag',
                    data: true,
                    success: function() {
                      console.log('error时存储launchFlag');
                    }
                  });
                  uni.reLaunch({
                    url: '/pages/index/news'
                  });
                  clearInterval(timer)
                }
              }, 1000)
            } else {
              uni.reLaunch({
                url: '/pages/index/news'
              });
            }
          }
        }).catch(err => {
          console.log(err, 222)
          this.guidePages = true
          this.getAd()
        })
      },
      launchFlag: function() {
        /**
         * 向本地存储中设置launchFlag的值，即启动标识；
         */
        uni.setStorage({
          key: 'launchFlag',
          data: true,
        });
        uni.reLaunch({
          url: '/pages/index/news'
        });

      },
    }
  }
</script>

<style>
  page,
  .main {
    width: 100%;
    height: 100%;
  }

  .jump-over {
    position: absolute;
    height: 60upx;
    line-height: 60upx;
    padding: 0 40upx;
    border-radius: 30upx;
    font-size: 32upx;
    color: #454343;
    border: 1px solid #454343;
    z-index: 999;
  }

  .jump-over {
    right: 45upx;
    top: 125upx;
  }
</style>
