LoginForm.vue 6.63 KB
<template>
  <div class="login-form-wrapper">
    <div class="login-content">
        <div class="login-card-left">
          <login-left-side></login-left-side>
        </div>
        <div class="login-card">
          <div v-if="showAlert">
            <a-alert v-for="(item, index) in alertArr" 
                    :key="index" 
                    :message="item" type="info" show-icon />
          </div>
          <h1 class="login-title">后台管理系统</h1>
          <a-form layout="inline" :model="formInline" @submit="handleSubmit">
              <a-form-item class="username-wrapper">
                  <a-input class="login-input username-input" size="large" :max="50" v-model:value="formInline.user" placeholder="用户名">
                      <template v-slot:prefix>
                          <UserOutlined style="color:rgba(0,0,0,.25)"/>
                      </template>
                  </a-input>
              </a-form-item>
              <a-form-item class="form-wrapper pwd-wrapper">
                  <a-input class="login-input" size="large" :max="50" v-model:value="formInline.password" type="password" placeholder="密码">
                      <template v-slot:prefix>
                          <LockOutlined style="color:rgba(0,0,0,.25)"/>
                      </template>
                  </a-input>
              </a-form-item>
              <a-form-item class="form-wrapper captcha-wrapper">
                  <a-input class="login-input" size="large" :max="6" v-model:value="formInline.captcha" placeholder="验证码"></a-input>
                  <div class="captcha-content" @click="updateCaptcha">
                    <img class="captcha-img" :src="`data:image/jpeg;base64,${captchaUrl}`" alt="验证码" />
                  </div>
              </a-form-item>
              <a-form-item class="login-button-wrapper">
                  <a-button
                      class="login-button"
                      type="primary"
                      html-type="submit"
                  >
                      登录
                  </a-button>
              </a-form-item>
          </a-form>
        </div>
    </div>
  </div>
</template>

<script>
import LoginLeftSide from './LoginLeftSide';

import { trimSpace, validate_username, 
         debounce } from '@/utils/tools/tools';
import LoginService from '@/services/Login';

import { UserOutlined, LockOutlined } from '@ant-design/icons-vue';

import { reactive, toRefs, getCurrentInstance, computed } from 'vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';

export default {
  components: {
    LoginLeftSide,
    UserOutlined,
    LockOutlined,
  },
  setup(props, {attrs, emit, slots}) {
    const router = useRouter();
    const store = useStore();
    const state = reactive({
      captchaUrl: '',
      captchaId: '', // 后台需要的字段
      alertMsg: '',
      showAlert: false,
      alertArr: [],
      randomVal: '0',
      formInline: {
        user: '',
        password: '',
        captcha: '',
      }
    });
    const loginService = new LoginService();

    const getCaptcha = () => {
      // 初始化获取验证码
      loginService.getCaptchaUrl().then(res => {
        if(res.code == 200) {
          const resData = res.data;
          state.captchaId = resData['Captcha-ID'];
          state.captchaUrl = resData['captcha'];
        }
      }, err => {

      })
    }
    getCaptcha();

    // 登录提交验证
    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;
      }
      
      const params = { account: user, password, captcha, captchaId};
      await loginService.loginAction(params).then((res) => {
        if(res.code === 200) {
          const { token, user } = res.data;
          localStorage.setItem('token', token);
          localStorage.setItem('userInfo', JSON.stringify(user));
          store.dispatch('app/setUserInfo', user);
          store.dispatch('app/setToken', token);
          console.log('save token', token);
        } else {
          const { message } = res;
          state.alertArr.push(message);
          state.showAlert = true;
        }
      }, (err) => {

      })
      
    };

    // 点击刷新验证码
    const updateCaptcha = debounce(() => {
      getCaptcha();
    }, 1000, true);

    return {
      ...toRefs(state),
      handleSubmit,
      updateCaptcha
    }
  }
};
</script>
<style lang="less" scoped>
@import 'styles/mixins.less';

.login-form-wrapper {
  margin-top: 150px;

  .login-content {
    display: flex;
    width: 1190px;
    height: 450px;
  }

  .login-card {
      position: relative;
      display: -webkit-box;
      display: -ms-flexbox;
      .flex-column;
      -webkit-box-orient: vertical;
      -webkit-box-direction: normal;
      min-width: 0;
      word-wrap: break-word;
      background-color: #f7f9fa;
      background-clip: border-box;
      border-left: none;
      border-radius: 4px;
      width: 420px;
      padding: 30px;
      margin-left: 50px;

      .ant-alert {
        margin-bottom: 15px;
      }

      .login-title {
        height: 55px;
        font-size: 20px;
        color: #2196F3;
      }

      .login-input {
        margin-top: 30px;
      }

      .username-input {
        margin-top: 0;
      }

      .captcha-wrapper {
        .v-center;
        justify-content: space-between;

        .login-input {
          width: 200px;
        }

        .captcha-content {
          flex: 1;
          margin-top: 30px;
          height: 40px;
          cursor: pointer;

          .captcha-img {
            width: 100%;
            height: 100%;
          }
        }
      }
      
      .login-button-wrapper {
        width: 100%;
        display: flex;
        justify-content: center;
        margin-top: 45px;
        .login-button {
          width: 160px;
          height: 40px;
          box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.15), 0 1px 1px rgba(0, 0, 0, 0.075);
        }
      }
  }
};
</style>