<template>
  <div>认证中,请稍等</div>
</template>

<script>
import Vue from "vue";
import { getAuthen } from "../assets/util";
import querystring from "querystring";
import { mapMutations, mapActions } from "vuex";

export default Vue.extend({
  head() {
    return {
      title: "认证",
    };
  },
  data() {
    return {
      redirect: "",
    };
  },
  created() {
    const [url, str] = this.$route.query.state.split(".");
    this.redirect = url;
    /**
     * 这一段作用是直接微信接口跳转
     **/
    if (str !== undefined) {
      const search = querystring.parse(
        str.replace(/-/g, "&").replace(/_/g, "=")
      );
      if (!search.stationId) {
        alert("你没有传递站点参数");
        return;
      }
      this.setStationId(search.stationId);
      if (!!search.appId) {
        this.setAppId(search.appId);
        this.havingAuthen();
      } else {
        this.getAppId(search.stationId).then((res) => {
          this.setStationId(res.stationId)
          this.havingAuthen();
        });
      }
    } else {
      alert("你没有传递站点参数");
      return;
    }
  },
  methods: {
    handleGoto() {
      this.$router.replace({
        path: this.redirect,
      });
    },
    havingAuthen() {
      if (!this.$route.query.code) {
        getAuthen();
      } else {
        this.$store
          .dispatch("authen/login", this.$route.query.code)
          .then((res) => {
            this.handleGoto();
          })
          .catch((res) => {
            alert(res);
            getAuthen();
          });
      }
    },
    ...mapMutations({
      setStationId: "authen/setStationId",
      setAppId: "authen/setAppId",
    }),
    ...mapActions({
      getAppId: "authen/getAppId",
    }),
  },
});
</script>

<style lang="less">
</style>
