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

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

    checkUser() {
      const userName = this.$route.query.userName;
      const mark = this.$route.query.mark;
      //判断传过来的username和mark是否为空值，如果是，则弹出身份验证不合法
      if (userName == null || mark == null){
        ElMessage.error("身份验证不合法！");
      } else {
        const params = {
          publicKey: this.publickey,
          data: this.encryptionByPublickey({
            //   mark: "80a7916eef8f096d4d8b64f14b99c697",
            userName,
            mark,
          }),
        };

        loginRequest(params).then((res) => {
          if (res.status !== 20000) {
            ElMessage.error("单点登录错误：" + res.msg);
          } else {
            ElMessage.success("登录成功");
            sessionStorage.setItem("ms_username", res.data.userName);
            sessionStorage.setItem("ms_chinesename", res.data.chineseName);
            sessionStorage.setItem("ms_id", res.data.id);
            sessionStorage.setItem("token", res.token);
            sessionStorage.setItem("identity", res.data.identity);
            this.$router.replace("/");
          }
        });
      }
    },

    // 利用jsencrypt.js进行RSA加密
    encryptionByPublickey(formData) {
      let key = sessionStorage.getItem("publicKey");
      var encrypt = new JSEncrypt();
      encrypt.setPublicKey(key);

      var result = encrypt.encrypt(JSON.stringify(formData));
      return result;
    },
  },
};
</script> 