<template>
  <div>
    <div v-if="msg">
      <br><br><br><br><br>
      <van-empty image="error" :description="msg"/>
      <van-button style="margin-left: 10%;width: 80%" round type="danger" color="#EC808D" @click="handleBack()">
        返回
      </van-button>
    </div>
  </div>
</template>
<script>
import {callMobile, isIOSWebKit} from '@/utils/common';
import {Dialog} from 'vant';

export default {
  components: {},
  data() {
    return {
      msg: null,
      scrollPosition: 0,
    }
  },
  created() {
    let vm = this;
    window['init'] = (result) => {
      vm.initUserInfo(result);
      vm.showMsg();
    };
    window['qrcodeContent'] = (result) => {
      vm.scanQRCallBack(result);
    };
  },
  mounted() {
    if (process.env.NODE_ENV == "dev") {
      let userInfo = {
        account: '18487125843',
        token: '7de354fd8be1484cacbc41e96fe88d7e',
        userId: '11133351501171512',
      };
      window.sessionStorage.setItem('userInfo', JSON.stringify(userInfo));
      this.showMsg();
    } else {
      let query = this.$route.query;
      let userInfo = {
        account: query.account,
        token: query.token,
        userId: query.userId,
      };
      if (!userInfo.userId && !userInfo.account) {
        this.msg = "用户信息获取失败！"
        callMobile('init', {});
      } else {
        if (!userInfo.userId || !userInfo.account) {
          this.msg = "用户信息获取失败！"
          return;
        }
        window.sessionStorage.setItem('userInfo', JSON.stringify(userInfo));
        this.showMsg();
      }
    }
  },
  activated() {
    window.scrollTo(0, this.scrollPosition);
  },
  methods: {
    handleBack() {
      callMobile('goIndex', {});
    },
    initUserInfo(val) {
      let ios = isIOSWebKit();
      if (ios) {
        window.sessionStorage.setItem('userInfo', val);
      } else {
        window.sessionStorage.setItem('userInfo', JSON.stringify(val));
      }
    },
    showMsg(){
      Dialog.alert({
        title: '温馨提示',
        message: '备孕妇女从孕前3个月，坚持每天服用0.4mg叶酸或含叶酸的复合维生素至少到怀孕满3个月，可预防胎儿神经管缺陷的发生,还能减少唇腭裂、先心病、早产等风险。特殊情况妇女如肥胖、糖尿病等要加量。',
        confirmButtonText: "好的，知道了"
      }).then(() => {
        this.$router.push('provideUnit');
      });
    }
  },
  beforeRouteLeave(to, from, next) {
    this.scrollPosition = document.documentElement.scrollTop || document.body.scrollTop;
    next();
  },
}
</script>
<style lang="less" scoped>

</style>