login.vue 3.1 KB
Newer Older
Y
yyt 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 128
<template>
	<view class="content">
		<image class="logo" src="/static/login/login.png"></image>
		<view class="text-area">
			<el-form :model="ruleForm" :rules="rules1" ref="ruleForm" class="demo-ruleForm">
				<el-form-item prop="phone">
					<el-input type="text" placeholder="请输入手机号" required="required" v-model="ruleForm.phone"
						prefix-icon="el-icon-user-solid"></el-input>
				</el-form-item>
				<el-form-item prop="password">
					<el-input type="password" placeholder="请输入密码" prefix-icon="el-icon-lock" v-model="ruleForm.password"
						@keyup.enter.native="toSubmitForm('ruleForm')"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="login">登录</el-button>
				</el-form-item>
			</el-form>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'login',
		components: {},
		data() {
			var loginValidatePass = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请输入密码'))
				} else {
					callback()
				}
			}
			var loginValidateUser = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('手机号不能为空'))
				} else {
					const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
					// eslint-disable-next-line no-useless-escape
					const reg2 = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
					if ((reg.test(value) || reg2.test(value))) {
						callback()
					} else {
						callback(new Error('请输入正确的手机号'))
					}
				}
			}
			return {
				ruleForm: {
					phone: '',
					password: ''
				},
				rules: {
					phone: [{
						required: true,
						validator: loginValidateUser,
						trigger: 'blur'
					}],
					password: [{
						required: true,
						validator: loginValidatePass,
						trigger: 'blur'
					}]
				}
			}
		},
		methods: {
			isRightPhone(value) {
				const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
				// eslint-disable-next-line no-useless-escape
				const reg2 = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
				return value !== '' && (reg.test(value) || reg2.test(value))
			},
			loginCheck() {
				return this.isRightPhone(this.ruleForm.phone) && this.ruleForm.password.length >= 6
			},
			login() {
				if (!this.loginCheck()) {
					return
				}
				axios.get('/login', {
					params: {
						phonenumber: this.ruleForm.phone,
						password: this.ruleForm.password
					}
				}).then(response => {
					if (response.data.code === '400') {
						this.$message({
							message: '用户名或密码错误',
							type: 'error'
						})
					} else if (response.data.code === '200') {
						this.$globalVar.userId = response.data.userId
						this.$message({
							message: '登录成功',
							type: 'success'
						})
						console.log(this.$globalVar.userId)
						this.$router.push('/homepage')
					}
				})
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 480rpx;
		width: 500rpx;
		margin-top: 100rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}
</style>