|
@@ -1,29 +1,56 @@
|
|
|
<template>
|
|
|
<div class="login-container">
|
|
|
- <el-form ref="loginForm" class="login-form" auto-complete="on" label-position="left">
|
|
|
+ <el-form
|
|
|
+ ref="loginForm"
|
|
|
+ :model="loginForm"
|
|
|
+ :rules="loginRules"
|
|
|
+ class="login-form"
|
|
|
+ auto-complete="on"
|
|
|
+ label-position="left"
|
|
|
+ >
|
|
|
<div class="title-container">
|
|
|
<h3 class="title">系统登录</h3>
|
|
|
</div>
|
|
|
|
|
|
- <el-form-item>
|
|
|
- <el-input placeholder="用户名" />
|
|
|
+ <el-form-item prop="username">
|
|
|
+ <el-input
|
|
|
+ type="text"
|
|
|
+ v-model="loginForm.username"
|
|
|
+ ref="username"
|
|
|
+ name="username"
|
|
|
+ auto-complete="on"
|
|
|
+ placeholder="用户名"
|
|
|
+ />
|
|
|
</el-form-item>
|
|
|
- <el-form-item>
|
|
|
- <el-input placeholder="密码" />
|
|
|
+
|
|
|
+ <el-form-item prop="password">
|
|
|
+ <el-input
|
|
|
+ type="password"
|
|
|
+ v-model="loginForm.password"
|
|
|
+ ref="password"
|
|
|
+ name="password"
|
|
|
+ placeholder="密码"
|
|
|
+ auto-complete="on"
|
|
|
+ @keyup.enter.native="handleLogin"
|
|
|
+ />
|
|
|
</el-form-item>
|
|
|
+
|
|
|
<el-row>
|
|
|
<el-col :span="12">
|
|
|
<el-button
|
|
|
class="login-button"
|
|
|
type="primary"
|
|
|
+ :loading="loading"
|
|
|
@click.native.prevent="handleLogin('teacher')"
|
|
|
>教师登录</el-button
|
|
|
>
|
|
|
</el-col>
|
|
|
+
|
|
|
<el-col :span="12">
|
|
|
<el-button
|
|
|
class="login-button"
|
|
|
type="primary"
|
|
|
+ :loading="loading"
|
|
|
@click.native.prevent="handleLogin('student')"
|
|
|
>学员登录</el-button
|
|
|
>
|
|
@@ -36,11 +63,61 @@
|
|
|
<script>
|
|
|
export default {
|
|
|
data() {
|
|
|
- return {};
|
|
|
+ const validateUsername = (rule, value, callback) => {
|
|
|
+ if (!value) {
|
|
|
+ callback(new Error('请填写用户名!'));
|
|
|
+ } else {
|
|
|
+ callback();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ const validatePassword = (rule, value, callback) => {
|
|
|
+ if (value.length < 6) {
|
|
|
+ callback(new Error('密码不能少于六位'));
|
|
|
+ } else {
|
|
|
+ callback();
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ return {
|
|
|
+ loginForm: {
|
|
|
+ username: 'teacher',
|
|
|
+ password: '1234567a'
|
|
|
+ },
|
|
|
+ loginRules: {
|
|
|
+ username: [{ require: true, trigger: 'blur', validator: validateUsername }],
|
|
|
+ password: [{ require: true, trigger: 'blur', validator: validatePassword }]
|
|
|
+ },
|
|
|
+ loading: false,
|
|
|
+ redirect: null
|
|
|
+ };
|
|
|
+ },
|
|
|
+ watch: {
|
|
|
+ $router: {
|
|
|
+ handler: function (router) {
|
|
|
+ this.redirect = router.query && router.query.redirect;
|
|
|
+ }
|
|
|
+ }
|
|
|
},
|
|
|
methods: {
|
|
|
- handleLogin(loginType) {
|
|
|
- console.log(loginType);
|
|
|
+ handleLogin(userType) {
|
|
|
+ this.$refs.loginForm.validate(valid => {
|
|
|
+ if (valid) {
|
|
|
+ this.loading = true;
|
|
|
+ this.$store
|
|
|
+ .dispatch('user/login', { loginForm: this.loginForm, userType })
|
|
|
+ .then(() => {
|
|
|
+ this.$router.push({ path: this.redirect || '/' });
|
|
|
+ this.loading = false;
|
|
|
+ this.$message.success('登录成功!');
|
|
|
+ })
|
|
|
+ .catch(() => {
|
|
|
+ this.loading = false;
|
|
|
+ this.$message.error('登录失败!');
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
}
|
|
|
};
|