<template>
  <div>
    <div v-if="msg">
      <van-nav-bar fixed>
        <template #title>
          <div @click="debugInfo">
            提示
          </div>
        </template>
      </van-nav-bar>
      <br> <br>
      <van-divider/>
      <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';
import {getUserByToken} from "@/axios/api";

export default {
  components: {},
  data() {
    return {
      msg: null,
      scrollPosition: 0,
      userInfo: {
        account: null,
        token: "44c1ef4e-de9e-4d32-9af4-3c0196e1ec83",
        userId: null,
        residentId: null,
        nickName: null,
        realName: null,
        gender: null,
        headPortrait: null,
        domain: window.location.href
      }
    }
  },
  created() {
    let vm = this;
    window['init'] = (result) => {
      vm.msg = null
      let path = vm.$route.path;
      if (path != '/') {
        return;
      }
      let ios = isIOSWebKit();
      if (ios) {
        let data = JSON.parse(result);
        this.userInfo.token = data.token;
      } else {
        this.userInfo.token = result.token;
      }
      vm.getUserByToken(this.userInfo);
    };
  },
  mounted() {
    this.init();
  },
  activated() {
    window.scrollTo(0, this.scrollPosition);
  },
  methods: {
    init() {
      if (process.env.NODE_ENV == "dev") {
        this.initUserInfo(this.userInfo);
        this.getUserByToken(this.userInfo);
      } else {
        let query = this.$route.query;
        this.userInfo.token = query.token
        this.initUserInfo(this.userInfo);
        if (!this.userInfo.token) {
          this.msg = "获取用户信息异常！"
          callMobile('init', {});
        } else {
          this.getUserByToken(this.userInfo)
        }
      }
    },
    getUserByToken(userInfo) {
      this.initUserInfo(userInfo);
      getUserByToken(userInfo.token).then(res => {
        if (res.code == 'SUCCESS') {
          userInfo.userId = res.data.userId;
          userInfo.account = res.data.account;
          userInfo.residentId = res.data.residentId;
          userInfo.nickName = res.data.nickName;
          userInfo.gender = res.data.gender;
          userInfo.headPortrait = res.data.headPortrait;
          this.initUserInfo(userInfo);
          this.showMsg();
        } else {
          this.msg = "获取用户信息异常！"
        }
      }).catch(res => {
        this.msg = "获取用户信息异常！"
      })
    },
    handleBack() {
      callMobile('goIndex', {});
    },
    initUserInfo(val) {
      window.sessionStorage.setItem('userInfo', JSON.stringify(val));
    },
    showMsg() {
      Dialog.alert({
        title: '温馨提示',
        message: '备孕妇女从孕前3个月，坚持每天服用0.4mg叶酸或含叶酸的复合维生素至少到怀孕满3个月，可预防胎儿神经管缺陷的发生,还能减少唇腭裂、先心病、早产等风险。特殊情况妇女如肥胖、糖尿病等要加量。',
        confirmButtonText: "好的，知道了"
      }).then(() => {

      });
      this.$router.push('provideUnit');
    },
    debugInfo() {
      this.$router.push('debugInfo');
    },
  },
  beforeRouteLeave(to, from, next) {
    this.scrollPosition = document.documentElement.scrollTop || document.body.scrollTop;
    next();
  },
}
</script>
<style lang="less" scoped>

</style>