<template>
  <div>
    <!-- <el-upload></el-upload> -->
  </div>
</template>

<script>
import { decrypt } from "@/utils/jsencrypt.js";
import { loginRequest, Login } from "/@/api/api.js";
import { ElMessage } from "element-plus";
import store from "@/store/index.js";
import router from "@/router/index.js";
export default {
  data() {
    return {
      userName: "",
      mark: "",
      publickey: "",
    };
  },
  created() {
    this.checkUser();
  },
  methods: {
    //先获取密钥，再去认证username和mark
    // async getKey() {
    //   this.publickey = await getPublickey();
    //   sessionStorage.setItem("publicKey", this.publickey);
    //   this.checkUser();
    // },

    async checkUser() {
      const username = this.$route.query.username;
      const a = decodeURIComponent(this.$route.query.jiami).replace(/\s/g, "+");
      const password = decrypt(a);
      //判断传过来的username和mark是否为空值，如果是，则弹出身份验证不合法
      if (username == null || password == null) {
        ElMessage.error("身份验证不合法！");
      } else {
        const {
          data: res,
          msg,
          code,
        } = await loginRequest({ username, password });
        if (code == 0) {
          store.commit("user/SET_TOKEN", res);

          Login({ userId: res.userId, token: res.accessToken }).then((res) => {
            if (res.code == 0&&res.data.code==200) {
              ElMessage.success("登录成功");
              router.push("/");
            }
          });
        } else {
          ElMessage.error(msg);
        }
      }
    },
  },
};
</script>