Login.vue 7.57 KB
<template>
  <el-container class="container">
    <!-- <home-header></home-header>
        <home-content></home-content>
        <home-footer></home-footer> -->
    <div class="login_page">
      <div class="login_header">
        <img class="home-logo-img" :src="logoPath" :key="logoPath" alt="company-logo">
        <span class="home-logo-title">{{ homeTitle }}</span>
      </div>
      <div class="login_box">
        <div class="left"></div>
        <div class="right">
          <div class="login_card">
            <div class="login-content">
              <div class="small_logo">
                <img class="home-logo-img" :src="logo" :key="logo" alt="company-logo" />
              </div>
              <h1 class="login-title">{{title}}</h1>
              <div v-if="showAlert">
                <el-alert v-for="(item, index) in alertArr" :key="index" :title="item" type="error" show-icon />
              </div>
              <el-form>
                <el-form-item class="username-wrapper" prop="name">
                  <el-input class="login-input username-input" placeholder="请输入用户名" maxLength="50" v-model="formInline.user">
                  </el-input>
                </el-form-item>
                <el-form-item class="form-wrapper pwd-wrapper" prop="pass">
                  <el-input class="login-input" placeholder="请输入密码" v-model="formInline.password" maxLength="20" show-password autocomplete="off">
                  </el-input>
                </el-form-item>
                <el-form-item class="login-button-wrapper" @keyup.enter="handleSubmit" @click.native.prevent="handleSubmit">
                  <el-button class="login-button" type="primary" html-type="submit">
                    登录
                  </el-button>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </div>
      <div class="login_footer">
        Copyright ©2012-2021 粤ICP备12034057号 深圳广联赛讯有限公司 版权所有
      </div>
    </div>

  </el-container>
</template>

<script>
import HomeHeader from "components/Header/HomeHeader";
import HomeContent from "components/HomeContent/HomeContent";
import HomeFooter from "components/Footer/HomeFooter";
import {
  onMounted,
  watch,
  reactive,
  toRefs,
  getCurrentInstance,
  onBeforeMount,
} from "vue";
import { useStore } from "vuex";
import LoginService from "@/services/Login";
import { trimSpace } from "@/utils/tools/tools";
export default {
  name: "LoginPage",
  components: {
    HomeHeader,
    HomeContent,
    HomeFooter,
  },
  setup() {
    const store = useStore();
    const state = reactive({
      logoPath: store.state.app.logo,
      homeTitle: "",
      alertMsg: "",
      showAlert: false,
      alertArr: [],
      randomVal: "0",
      formInline: {
        user: "",
        password: "",
        captcha: "",
      },
      title: store.state.app.title,
      logo: store.state.app.logo,
    });
    let logoPath = store.state.app.logo;

    const { ctx: _this } = getCurrentInstance();
    const loginService = new LoginService();

    onBeforeMount(() => {
      document.onkeydown = (e) => {
        if (e.keyCode === 13) {
          handleSubmit();
        }
      };
    });

    // 登录提交验证 用户名只限制长度50 验证码暂时屏蔽
    const handleSubmit = async () => {
      const value = state.formInline,
        captchaId = state.captchaId,
        { user, password, captcha } = value;

      state.alertArr = [];
      state.showAlert = false;

      if (trimSpace(user).length <= 0) {
        state.alertArr.push("请输入用户名");
        state.showAlert = true;
        return false;
      }

      // if(!validate_username(user)) {
      //   state.alertArr.push('用户名格式不正确');
      //   state.showAlert = true;
      //   return false;
      // }

      if (trimSpace(password).length <= 0) {
        state.alertArr.push("请输入密码");
        state.showAlert = true;
        return false;
      }

      // if(trimSpace(captcha).length <= 0) {
      //   state.alertArr.push('请输入验证码');
      //   state.showAlert = true;
      //   return false;
      // }
      // params: captcha, captchaId 验证码暂时屏蔽
      store.dispatch("app/showLoading", true);
      const params = { account: user, password };
      await loginService.loginAction(params).then(
        (res) => {
          store.dispatch("app/showLoading", false);
          if (res.code === 200) {
            const { token, user } = res.data;
            store.dispatch("app/setUserInfo", user);
            store.dispatch("app/setToken", token);
          } else {
            const { message } = res;
            state.alertArr.push(message);
            state.showAlert = true;
            store.dispatch("app/showLoading", false);
          }
        },
        (err) => {
          store.dispatch("app/showLoading", false);
        }
      );
    };

    watch(
      () => store.state.app.logo,
      (n, o) => {
        state.logoPath = n;
        console.log("logoPath:", n);
      }
    );

    watch(
      () => store.state.app.logo,
      (n, o) => {
        state.logo = n;
      }
    );

    watch(
      () => store.state.app.title,
      (n, o) => {
        state.title = n;
      }
    );

    console.log(logoPath);
    return { ...toRefs(state), handleSubmit };
  },
};
</script>

<style lang="less" scoped>
@import "styles/variables.less";

// .container {
//     position: fixed;
//     height: 100%;
//     top: 0;
//     left: 0;
//     width: 100%;
//     min-width: 1190px;
//     overflow-y: scroll;
// }
.login_page {
  height: 100vh;
  width: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  .login_header {
    width: 100%;
    height: 60px;
    .home-logo-img {
      max-width: 100%;
    }
    // padding:20px;
  }
  .login_box {
    width: 100%;
    height: calc(100vh - 120px);
    //background: url("../../assets/images/car-bg.png") no-repeat;
    background: url("../assets/images/car-bg.png") no-repeat;
    background-size: cover;
    display: flex;
    justify-content: space-between;
    .right{
        flex:2;
        display:flex;
        align-items: center;
        justify-content: flex-end;
        padding:0 60px 0 0;
        @media (max-width: 414px) {
          padding: 0;
        }
    }
    .login_card {
      background-color: #fff;
      background-clip: border-box;
      border-radius: 4px;
      width: 410px;
      @media (max-width: 414px) {
        width: 100%;
      }
      padding: 45px 0;
      box-sizing: border-box;
      display: flex;
      justify-content: center;
      align-items: center;
      .login-content{
        width: 320px;
        .home-logo-img {
        max-width: 100%;
        height: 50px;
      }
      .login-title {
        height: 55px;
        font-size: 22px;
        color: #333;
        font-weight: bold;
      }
      .login-input {
        width: 100%;
        border:0;
      }
      .login-button-wrapper {
        margin-top: 45px;
        .login-button {
          width: 100%;
          height: 40px;
          background-color: #003457;
          border-color: transparent;
        }
      }
      }
      
    }
  }
  .login_footer {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 15px;
    height: 60px;
    // line-height: 60px;
    font-size: 12px;
    text-align: center;
    color: #999;
  }
}

</style>
<style scoped>
.login_box >>> .el-input__inner {
  border: none;
  border-radius: 0;
  border-bottom: 1px solid #97979742;
}
.login_box >>> .el-input__inner:-webkit-autofill {
  -webkit-box-shadow: 0 0 0px 1000px #fff inset;
}
</style>