<template>
  <!-- 授权页面 -->
  <view></view>
</template>

<script>
import { mapState, mapMutations } from "vuex"; //引入vuex存储idnum与name
export default {
  data() {
    return {
      homeUrl: "https://jtishfw.ncjti.edu.cn/yinxin/ncjtSecurityManagement",
    };
  },
  computed: mapState(["position", "userData"]),
  onLoad(options) {
    //拉取微校授权获取cardNumber
    this.loginFilter();
  },
  methods: {
    ...mapMutations(["getPosition", "getUserData"]),
    // 授权获取信息
    loginFilter() {
      let cardNumber = this.getQueryString("cardNumber");

      let name = this.getQueryString("name");

      let error = this.getQueryString("error");

      let errorMsg = this.getQueryString("errorMsg");

      let homeWeb = `https://open.wecard.qq.com/connect/oauth/authorize?app_key=8C41E2FECF2E9925&response_type=code&scope=snsapi_userinfo&ocode=1015730314&redirect_uri=${this.homeUrl}/getUserInfo&state=1`;

      if (error) {
        uni.showToast({
          title: errorMsg,
          icon: "error",
          mask: true,
          duration: 1000,
        });
        setTimeout(() => {
          uni.navigateTo({
            url: "/pages/index/index",
          });
        }, 1000);
      } else if (!cardNumber) {
        window.location.href = homeWeb;
      } else {
        localStorage.setItem("cardNumber", cardNumber);
        this.$store.state.name = name;
        this.$store.state.idnum = cardNumber;
        this.judgeGrade();
      }
    },
    //判断老生无权限
    judgeGrade() {
      let cardNumber = localStorage.getItem("cardNumber");

      let date = new Date();

      let year = date.getFullYear().toString();

      let grade = this.getQueryString("grade");

      if (grade == year && cardNumber) {
        this.judgeWrite();
      } else {
        uni.showToast({
          title: "无权限,仅对新生请退出",
          icon: "error",
          mask: true,
          duration: 120000,
        });
      }
    },
    //判断是否填写问卷
    judgeWrite() {
      let cardNumber = localStorage.getItem("cardNumber");

      let url = this.homeUrl + "/freshmanSurveyDoneByIdnum";

      uni.request({
        url: url,
        data: {
          idnum: cardNumber,
        },
        header: {
          Accept: "application/json",
          "Content-Type": " application/x-www-form-urlencoded;charset=utf-8",
          "X-Requested-With": "XMLHttpRequest",
        },
        method: "GET",
        sslVerify: true,
        success: ({ data, statusCode, header }) => {
          // console.log(data);
          if (data.result === "success") {
            uni.navigateTo({
              url: "/pages/index/index",
            });
          } else {
            uni.navigateTo({ url: "/pages/faceSearch/faceSea" });
          }
        },
        fail: (error) => {},
      });
    },
    //获取当前URL指定参数
    getQueryString(name) {
      let url = window.location.href; // 获取URL

      let pattern = new RegExp("[\?\&]" + name + "=([^\&]+)", "i"); // 正则匹配URL

      let matcher = pattern.exec(url);

      if (matcher == null || matcher.length < 1) {
        return false;
      }
      return decodeURIComponent(matcher[1]); // 输出指定的参数值 中文也可以
    },
  },
};
</script>

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