<template src="./loginView.html"></template>

<script>
import util from "@/utils/util";
import { loginService } from "@/services";
import { setStorage, getStorage } from "@/utils/localStorage";
import router from "@/router";
import store from "@/store";
export default {
  data() {
    return {
      name: "",
      pwd: "",
      radio: true,
      softTest: false,
      showLoginView: true,
      panel: "",
      codeText: "获取验证码",
      checked: true,
      timerCode: null,
      isShow: true,
    };
  },
  mounted() {
    console.log("***login-view-mounted***");
    // if (location.href.indexOf("?softTest") != -1) {
    // 	//表明是软件测试链接
    // 	this.softTest = true;
    // } else {
    // 	this.softTest = false;
    // }
    // this.getIpAddr();
    let userInfo = getStorage("userInfoThing");
    if (userInfo) {
      common.userInfoHandle(JSON.parse(userInfo));
      console.log("login-data-success:");
      this.showLoginView = false;
      router.push({
        name: "index",
      });
    }
  },
  methods: {
    // 发送验证码
    async goSendCode() {
      if (this.name == "") {
        this.$message.warning("请输入手机号");
        return false;
      }
      // 验证手机号
      if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(this.name)) {
        this.$message.warning("请输入合法的手机号");
        return false;
      }
      if (this.codeText !== "获取验证码") {
        this.$message.warning("不可重复发送验证码，请稍后再试");
        return false;
      }
      // 发送请求
      let data = {
        phoneNumber: this.name,
        sign: "ELAB",
        verifyType: 2,
      };
      store.state.loadingMsg="获取中";
      const res = await requestConfig("verifyCode", data);
      if (res.success) {
        this.$message.success("发送成功");
        // 处理倒计时
        this.handleCountDown();
      } else {
        this.$message.error(res.message);
      }
    },
    clearTimer(id) {
      if (id) {
        clearInterval(id);
        id = null;
      }
    },
    updateTime(e) {
      if (e.target.currentTime > 0) {
        this.isShow = false; // 添加模糊出现的效果
      }
    },
    // 处理倒计时
    handleCountDown() {
      this.codeText = 120;
      // 先清除定时器
      this.clearTimer(this.timerCode);
      // 设置倒计时
      this.timerCode = setInterval(() => {
        if (this.codeText >= 2) {
          this.codeText--;
        } else {
          this.codeText = "获取验证码";
          this.clearTimer(this.timerCode);
        }
      }, 1000);
    },
    //登录接口
    async enter() {
      // console.log('标签');
      if (!this.radio) {
        return false;
      }
      if (this.name == "") {
        this.$message.warning("请输入手机号");
        return false;
      }
      // 验证手机号
      if (!/^1\d{10}$/.test(this.name)) {
        this.$message.warning("请输入合法的手机号");
        return false;
      }
      if (this.pwd == "") {
        this.$message.warning("请输入验证码");
        return false;
      }
      let data = {
        mobile: this.name,
        verifyCode: this.pwd,
        platform: 3,
      };
      store.state.loadingMsg="登录中";
      const res = await requestConfig("login", data);
      if (res.success) {
        setStorage("userInfoThing", res.single);
        common.userInfoHandle(res.single);
        console.log("login-data-success:");
        this.showLoginView = false;
        // 登录成功后.默认赋值上一次退出时选中的项目
        let disClearKey = '';
        for (let i = 0; i < window.localStorage.length; i++) {
          let key = window.localStorage.key(i);
          if (key.includes("currentProject")) {
            disClearKey = key
          }
        }
        let projectId = '';
        if(!disClearKey) {
          projectId = disClearKey.replace('currentProject_','')
        }
        let routerData = {
          name: "index"
        }
        if(projectId){
          routerData['projectId'] = projectId
        }
        router.push(routerData);
        // window.$bus.$emit('loginSuccess', true);//通知登录完成了-触发底图加载和显示等其他动作
      } else {
        this.$message.error(res.message || "登录错误");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@import "./loginView.scss";
</style>
