login.vue 3.3 KB
Newer Older
M
maguohua 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<template>
  	<div class="login_page fillcontain">
	  	<transition name="form-fade" mode="in-out">
	  		<section class="form_contianer" v-show="showLogin">
		  		<div class="manage_tip">
		  			<p>elm后台管理系统</p>
		  		</div>
		    	<el-form :model="loginForm" :rules="rules" ref="loginForm">
					<el-form-item prop="username">
						<el-input v-model="loginForm.username" placeholder="用户名"><span>dsfsf</span></el-input>
					</el-form-item>
					<el-form-item prop="password">
						<el-input type="password" placeholder="密码" v-model="loginForm.password"></el-input>
					</el-form-item>
					<el-form-item>
L
Lym 已提交
16
				    	<el-button type="primary" @click="submitForm('loginForm')" class="submit_btn">登录</el-button>
M
maguohua 已提交
17 18
				  	</el-form-item>
				</el-form>
M
maguohua 已提交
19 20 21
				<p class="tip">温馨提示:</p>
				<p class="tip">未登录过的新用户,自动注册</p>
				<p class="tip">注册过的用户可凭账号密码登录</p>
M
maguohua 已提交
22 23 24 25 26 27
	  		</section>
	  	</transition>
  	</div>
</template>

<script>
M
maguohua 已提交
28 29 30
	import {login, getAdminInfo} from '@/api/getData'
	import {mapActions, mapState} from 'vuex'

M
maguohua 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
	export default {
	    data(){
			return {
				loginForm: {
					username: '',
					password: '',
				},
				rules: {
					username: [
			            { required: true, message: '请输入用户名', trigger: 'blur' },
			        ],
					password: [
						{ required: true, message: '请输入密码', trigger: 'blur' }
					],
				},
				showLogin: false,
			}
		},
		mounted(){
			this.showLogin = true;
M
maguohua 已提交
51 52 53 54 55 56
			if (!this.adminInfo.id) {
    			this.getAdminData()
    		}
		},
		computed: {
			...mapState(['adminInfo']),
M
maguohua 已提交
57 58
		},
		methods: {
M
maguohua 已提交
59
			...mapActions(['getAdminData']),
M
maguohua 已提交
60 61
			async submitForm(formName) {
				this.$refs[formName].validate(async (valid) => {
M
maguohua 已提交
62
					if (valid) {
M
maguohua 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75
						const res = await login({user_name: this.loginForm.username, password: this.loginForm.password})
						if (res.status == 1) {
							this.$message({
		                        type: 'success',
		                        message: '登录成功'
		                    });
							this.$router.push('manage')
						}else{
							this.$message({
		                        type: 'error',
		                        message: res.message
		                    });
						}
M
maguohua 已提交
76 77 78 79 80 81 82 83 84 85
					} else {
						this.$notify.error({
							title: '错误',
							message: '请输入正确的用户名密码',
							offset: 100
						});
						return false;
					}
				});
			},
M
maguohua 已提交
86 87 88 89 90 91 92 93 94 95 96
		},
		watch: {
			adminInfo: function (newValue){
				if (newValue.id) {
					this.$message({
                        type: 'success',
                        message: '检测到您之前登录过,将自动登录'
                    });
					this.$router.push('manage')
				}
			}
M
maguohua 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
		}
	}
</script>

<style lang="less" scoped>
	@import '../style/mixin';
	.login_page{
		background-color: #324057;
	}
	.manage_tip{
		position: absolute;
		width: 100%;
		top: -100px;
		left: 0;
		p{
			font-size: 34px;
			color: #fff;
		}
	}
	.form_contianer{
		.wh(320px, 210px);
		.ctp(320px, 210px);
		padding: 25px;
		border-radius: 5px;
		text-align: center;
		background-color: #fff;
		.submit_btn{
			width: 100%;
			font-size: 16px;
		}
	}
M
maguohua 已提交
128 129
	.tip{
		font-size: 12px;
M
maguohua 已提交
130
		color: red;
M
maguohua 已提交
131
	}
M
maguohua 已提交
132 133 134 135 136 137 138 139
	.form-fade-enter-active, .form-fade-leave-active {
	  	transition: all 1s;
	}
	.form-fade-enter, .form-fade-leave-active {
	  	transform: translate3d(0, -50px, 0);
	  	opacity: 0;
	}
</style>