login.vue 4.77 KB
<script setup lang="ts">
import { login, menuData } from "@/api/user/login";
// import * as store from "@/pinia/index";
import router from "@/router/index";
import { useLoginStore } from "@/pinia/login";
import { reactive, ref, getCurrentInstance, onMounted } from "vue";
import type { ElForm } from "element-plus";
import { UserFilled, Lock } from "@element-plus/icons-vue";
const {
  appContext: {
    config: { globalProperties },
  },
} = getCurrentInstance();
interface Login {
  userName: string;
  password: string;
}
/**
 * 登录表单数据
 */
const loginForm: Login = reactive({
  userName: "",
  password: "",
});

let loading = ref(false);
/**
 * 登录框上面标题
 */
const title = ref(import.meta.env.VITE_APP_TITLE).value;
const ruleFormRef = ref<InstanceType<typeof ElForm>>();
/**
 * 表单提交
 */
const submitForm = (formEl: InstanceType<typeof ElForm> | undefined) => {
  loading.value = true;
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      login(loginForm)
        .then((res) => {
          if (res?.data) {
            globalProperties.$message.success("登录成功");
            // 登录成功返回的参数含义,到pinia/modules/login.ts中查看
            // store.family.loginStore().setUserInfo(res?.data?.data);
            useLoginStore()?.setUserInfo(res?.data?.data);
            getMenu().then((res) => {
              setTimeout(() => {
                router.push({ path: "/" });
              }, 1000);
            });
          }
        })
        .catch((error) => {
          console.log(error);
        })
        .finally(() => {
          loading.value = false;
        });
    } else {
      loading.value = false;
      console.log("error");
    }
  });
};
/**
 * 获取权限菜单
 */
const getMenu = async () => {
  try {
    let res = await menuData();
    // store.family.loginStore().setMenuList(res?.data?.data);
    useLoginStore()?.setMenuList(res?.data?.data);
  } catch (e) {
    console.log(e);
  }
};
onMounted(() => {
  window.localStorage.setItem('isCnlive', false)
})
</script>

<template>
  <div class="login-container">
    <el-form
      :inline="true"
      :model="loginForm"
      ref="ruleFormRef"
      auto-complete="on"
      label-position="left"
      label-width="120px"
      class="login-form"
    >
      <div class="title-container">
        <h3 class="title">{{ title }}</h3>
      </div>

      <el-row>
        <el-form-item prop="userName" style="width: 100%">
          <el-input
            v-model.trim="loginForm.userName"
            placeholder="请填写用户名"
            maxlength="15"
            :validate-event="false"
            tabindex="1"
            type="text"
            auto-complete="off"
            clearable
            :prefix-icon="UserFilled"
            style="width: 448px"
          />
        </el-form-item>
      </el-row>
      <el-form-item prop="password" style="width: 100%">
        <el-input
          v-model.trim="loginForm.password"
          placeholder="请填写密码"
          :validate-event="false"
          maxlength="15"
          tabindex="2"
          :show-password="true"
          clearable
          type="password"
          auto-complete="off"
          :prefix-icon="Lock"
          @keyup.enter.native="submitForm(ruleFormRef)"
        >
        </el-input>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
          type="primary"
          @click.prevent="submitForm(ruleFormRef)"
          style="width: 100%"
          :loading="loading"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<style lang="scss">
$bg: #283443;
$dark_gray: #889aa4;
$light_gray: #fff;
$cursor: #fff;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
.login {
  &-container {
    min-height: 100%;
    text-align: center;
    background-color: $bg;
    .el-input {
      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;
        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }
    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
  }
  &-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }
}
.title-container {
  position: relative;
  .title {
    font-size: 26px;
    color: $light_gray;
    margin: 0px auto 40px auto;
    text-align: center;
    font-weight: bold;
  }
}
</style>