Platform login page implementation (1)

Insert image description here


1. Implement user name, password, login button, and remember user form

<template>

	<LoginBackVue>
		<div class="login_box">
			<!-- 显示logo的地方 -->
			<div class="log"></div>
			<!-- 登录的表单 -->
				<el-form>
					<el-form-item>
						<el-input prefix-icon="User" placeholder="请输入用户名"/>
					</el-form-item>

					<el-form-item>
						<el-input prefix-icon="Lock" placeholder="请输入密码"/>
					</el-form-item>

					<el-form-item label="记住用户">
						<el-switch />
					</el-form-item>
		
					<el-form-item>
						<el-button color="#00608c" style="width: 100%" @click="onSubmit">登录</el-button>
					</el-form-item>
				</el-form>
			</div>
	</LoginBackVue>
</template>

<script>
import LoginBackVue from "@/components/LoginBack.vue";

export default{
    
    
	components:{
    
    
		LoginBackVue
	}
}

</script>

<style scoped>
	/* 设置登录框的宽高和位置居中 */
	.login_box{
    
    
		width: 500px;
		height:350px;
		margin: calc((100vh - 350px)/2) auto;
		text-align: center;
		color: white;
	}
</style>

1. The global css code is defined in asserts/css/global.css


/* --------------------登录页面--------------------- */

/* 修改element-plus 输入框表单的样式*/
.login_box .el-input__wrapper{
    
    
    background: none;
    box-shadow: none;
    border-bottom: solid 1px #00608c;
    border-radius: 0;
}
/* 修改输入框图表样式 */
.login_box .el-input__prefix{
    
    
    color: #fff;
    font-size: 20px;
}

Insert image description here
Import into main.js and take effect globally
Insert image description here

2. Two-way binding of user name, password, and remembered user

<template>

	<LoginBackVue>
		<div class="login_box">
			<!-- 显示logo的地方 -->
			<div class="log"></div>
			<!-- 登录的表单 -->
				<el-form>
					<el-form-item>
						<el-input v-model="loginForm.username" prefix-icon="User" placeholder="请输入用户名"/>
					</el-form-item>

					<el-form-item>
						<el-input type="password" v-model="loginForm.password" prefix-icon="Lock" placeholder="请输入密码"/>
					</el-form-item>

					<el-form-item label="记住用户">
						<el-switch  v-model="loginForm.status" />
					</el-form-item>
		
					<el-form-item>
						<el-button color="#00608c" style="width: 100%" @click="onSubmit">登录</el-button>
					</el-form-item>
				</el-form>
			</div>
	</LoginBackVue>
</template>

<script>
import LoginBackVue from "@/components/LoginBack.vue";

export default{
    
    
	data(){
    
    
		return{
    
    
			loginForm:{
    
    
				username:"",
				password:"",
				status:false
			}
		}
	},
	components:{
    
    
		LoginBackVue
	}
}

</script>

<style scoped>
	/* 设置登录框的宽高和位置居中 */
	.login_box{
    
    
		width: 500px;
		height:350px;
		margin: calc((100vh - 350px)/2) auto;
		text-align: center;
		color: white;
	}



</style>

Insert image description here

3. If there is no user, click the registration function to realize

<template>

	<LoginBackVue>
		<div class="login_box">
			<!-- 显示logo的地方 -->
			<div class="log"></div>
			<!-- 登录的表单 -->
				<el-form>
					<el-form-item>
						<el-input v-model="loginForm.username" prefix-icon="User" placeholder="请输入用户名"/>
					</el-form-item>

					<el-form-item>
						<el-input type="password" v-model="loginForm.password" prefix-icon="Lock" placeholder="请输入密码"/>
					</el-form-item>

					<el-form-item label="记住用户">
						<el-switch  v-model="loginForm.status" />
					</el-form-item>
		
					<el-form-item>
						<el-button color="#00608c" style="width: 100%" @click="onSubmit">登录</el-button>
					</el-form-item>
				</el-form>
				<div class="isregister">没有账号?
					<span @click="register">点击注册</span>
				</div>
			</div>
	</LoginBackVue>
</template>

<script>
import LoginBackVue from "@/components/LoginBack.vue";

export default{
    
    
	data(){
    
    
		return{
    
    
			loginForm:{
    
    
				username:"",
				password:"",
				status:false
			}
		}
	},
	methods:{
    
    
		register(){
    
    
			this.$message({
    
    
				type:'warning',
				message:"平台未开放注册功能"
			})
		}
	},
	components:{
    
    
		LoginBackVue
	}
}

</script>

<style scoped>
	/* 设置登录框的宽高和位置居中 */
	.login_box{
    
    
		width: 500px;
		height:350px;
		margin: calc((100vh - 350px)/2) auto;
		text-align: center;
		color: white;
	}
	.isregister{
    
    
		width: 100%;
		text-align: left;
		
	}
	.isregister span{
    
    
		color: turquoise;
		cursor: pointer;
	}



</style>

4. Enter the user name, password, and click the login button to log in.

1. Form input box, bind the entered data
2. Click the login button, send a request to complete the login

<template>

	<LoginBackVue>
		<div class="login_box">
			<!-- 显示logo的地方 -->
			<div class="log"></div>
			<!-- 登录的表单 -->
				<el-form>
					<el-form-item>
						<el-input v-model="loginForm.username" prefix-icon="User" placeholder="请输入用户名"/>
					</el-form-item>

					<el-form-item>
						<el-input type="password" v-model="loginForm.password" prefix-icon="Lock" placeholder="请输入密码"/>
					</el-form-item>

					<el-form-item label="记住用户">
						<el-switch  v-model="loginForm.status" />
					</el-form-item>
		
					<el-form-item>
						<el-button color="#00608c" style="width: 100%" @click="submitLogin">登录</el-button>
					</el-form-item>
				</el-form>
				<div class="isregister">没有账号?
					<span @click="register">点击注册</span>
				</div>
			</div>
	</LoginBackVue>
</template>

<script>
import LoginBackVue from "@/components/LoginBack.vue";

export default{
    
    
	data(){
    
    
		return{
    
    
			loginForm:{
    
    
				username:"",
				password:"",
				status:false
			}
		}
	},
	methods:{
    
    
		// 点击登录的方法
		async submitLogin(){
    
    
			const response=await this.$api.login(this.loginForm)
			if (response.status===200){
    
    
				// 提示登录成功
				this.$message({
    
    
					type:"success",
					message:"登录成功"
				})
				// 页面跳转(后面加)
				this.$router.push({
    
    "name":"home"})

			}
		},
		// 点击注册
		register(){
    
    
			this.$message({
    
    
				type:'warning',
				message:"平台未开放注册功能"
			})
		}
	},
	components:{
    
    
		LoginBackVue
	}
}

</script>

<style scoped>
	/* 设置登录框的宽高和位置居中 */
	.login_box{
    
    
		width: 500px;
		height:350px;
		margin: calc((100vh - 350px)/2) auto;
		text-align: center;
		color: white;
	}
	.isregister{
    
    
		width: 100%;
		text-align: left;
		
	}
	.isregister span{
    
    
		color: turquoise;
		cursor: pointer;
	}



</style>

Insert image description here

5. Implement form item verification

Steps:
1. Bind the rules attribute on the el-form tag and specify the verification rule object.
Insert image description here
2. Define the binding verification rule in data
Insert image description here
. 3. Specify the verification field in the el-form-item tag.
Insert image description here
Insert image description here

<template>

	<LoginBackVue>
		<div class="login_box">
			<!-- 显示logo的地方 -->
			<div class="log"></div>
			<!-- 登录的表单 -->
				<el-form :rules="loginRules" :model="loginForm">
					<el-form-item prop="username">
						<el-input v-model="loginForm.username" prefix-icon="User" placeholder="请输入用户名"/>
					</el-form-item>

					<el-form-item prop="password">
						<el-input type="password" v-model="loginForm.password" prefix-icon="Lock" placeholder="请输入密码"/>
					</el-form-item>

					<el-form-item label="记住用户">
						<el-switch  v-model="loginForm.status" />
					</el-form-item>
		
					<el-form-item>
						<el-button color="#00608c" style="width: 100%" @click="submitLogin">登录</el-button>
					</el-form-item>
				</el-form>
				<div class="isregister">没有账号?
					<span @click="register">点击注册</span>
				</div>
			</div>
	</LoginBackVue>
</template>

<script>
import LoginBackVue from "@/components/LoginBack.vue";

export default{
    
    
	data(){
    
    
		return{
    
    
			// 登录输入的数据
			loginForm:{
    
    
				username:"",
				password:"",
				status:false
			},
			// 登录表单的校验规则
			loginRules:{
    
    
				// username的校验规则
				username:[{
    
    
					required:true,
					message:"用户名不能为空",
					trigger:'blur'
			}],
				// password的校验规则
				password:[{
    
    
					required:true,
					message:"密码不能为空",
					trigger:'blur'
				},
				{
    
    
					min:4,
					max:18,
					message:"密码长度需要在6-18位之间",
					trigger:'blur'
				}]
			}
		}
	},
	methods:{
    
    
		// 点击登录的方法
		async submitLogin(){
    
    
			const response=await this.$api.login(this.loginForm)
			if (response.status===200){
    
    
				// 提示登录成功
				this.$message({
    
    
					type:"success",
					message:"登录成功"
				})
				// 页面跳转(后面加)
				this.$router.push({
    
    "name":"home"})

			}else{
    
    
				this.$message({
    
    
					type:"error",
					message:response.data
				})
			}
		},
		// 点击注册
		register(){
    
    
			this.$message({
    
    
				type:'warning',
				message:"平台未开放注册功能"
			})
		}
	},
	components:{
    
    
		LoginBackVue
	}
}

</script>

<style scoped>
	/* 设置登录框的宽高和位置居中 */
	.login_box{
    
    
		width: 500px;
		height:350px;
		margin: calc((100vh - 350px)/2) auto;
		text-align: center;
		color: white;
	}
	.isregister{
    
    
		width: 100%;
		text-align: left;
		
	}
	.isregister span{
    
    
		color: turquoise;
		cursor: pointer;
	}



</style>

6. Submit form pre-verification

vue——》Global attributes $refs={}
Check whether there is a ref attribute in the page. If so, the ref attribute will be added to $refs={}$
refs={ loginRef: element (component) }

1. Bind a method defined in methods through the ref attribute in the el-form tag.

<el-form :model="loginForm" :rules="loginRules" ref="loginRef">

2. Define a ref in method

clickLogin(){
    
    
	// 对登录的表单数据进行验证
	this.$refs['loginRef'].validate((res)=>{
    
    
		if(res){
    
    
			this.submitLogin()
		}
	})
},

3. Pre-verify the form

methods:{
    
    
	clickLogin(){
    
    
		// 对登录的表单数据进行验证
		this.$refs['loginRef'].validate((res)=>{
    
    
			if(res){
    
    
				this.submitLogin()
			}
		})
	},
	
	// 发送请求到后端
	async submitLogin(){
    
    
		const response=await this.$api.login(this.loginForm)
		if(response.status===200){
    
    
			// 提示登录成功
			this.$message({
    
    
				type:'success',
				message:'登录成功'
			})
			// 页面跳转(后面加)
			this.$router.push({
    
    name:'index'})
			
		}else{
    
    
			this.$message({
    
    
				type:'error',
				message:response.data
			})
	}
	},
	register(){
    
    
		this.$message({
    
    
			type:'warning',
			message:'平台暂时未开放注册功能'
		})
	}
},

7. Remember account number and password

1. Save token

window.sessionStorage.setItem('token',response.data.token)

2. Save username

window.sessionStorage.setItem('username',response.data.username)

3. Determine whether the user's account and password need to be remembered, and save the account and password in localStorage.

Pay special attention
to the need to convert this.loginForm into json format data

if(this.loginForm.status){
    
    
	// 将账号密码转换为json字符串
	const uinfo=JSON.stringify(this.loginForm)
	// 进行编码(加密)
	const u=window.btoa(unescape(encodeURIComponent(uinfo)))
	// 存储到localStorage
	window.localStorage.setItem('uinfo:',u)
}

4. Read the account password

created(){
    
    
		const uinfo=window.localStorage.getItem('uinfo')
		// 判断是否有账号密码
		if(uinfo){
    
    
			// 进行解密
			// const u= decodeURIComponent(escape(window.atob(uinfo)))
			// 转化位js对象,保存到loginForm
			this.loginForm =JSON.parse(uinfo)
		}
	}

5. If you do not need to remember the account and password, delete the information in localStorage.

window.localStorage.removeItem('uinfo')

Insert image description here

8. Written login page

<template>

	<LoginBackVue>
		<div class="login_box">
			<!-- 显示logo的地方 -->
			<div class="log"></div>
			<!-- 登录的表单 -->
				<el-form :rules="loginRules" :model="loginForm" ref="loginRef">
					<el-form-item prop="username">
						<el-input v-model="loginForm.username" prefix-icon="User" placeholder="请输入用户名"/>
					</el-form-item>

					<el-form-item prop="password">
						<el-input type="password" v-model="loginForm.password" prefix-icon="Lock" placeholder="请输入密码"/>
					</el-form-item>

					<el-form-item label="记住用户">
						<el-switch  v-model="loginForm.status" />
					</el-form-item>
		
					<el-form-item>
						<el-button color="#00608c" style="width: 100%" @click="clickLogin">登录</el-button>
					</el-form-item>
				</el-form>
				<div class="isregister">没有账号?
					<span @click="register">点击注册</span>
				</div>
			</div>
	</LoginBackVue>
</template>

<script>
import LoginBackVue from "@/components/LoginBack.vue";

export default{
    
    
	data(){
    
    
		return{
    
    
			// 登录输入的数据
			loginForm:{
    
    
				username:"",
				password:"",
				status:false
			},
			// 登录表单的校验规则
			loginRules:{
    
    
				// username的校验规则
				username:[{
    
    
					required:true,
					message:"用户名不能为空",
					trigger:'blur'
			}],
				// password的校验规则
				password:[{
    
    
					required:true,
					message:"密码不能为空",
					trigger:'blur'
				},
				{
    
    
					min:4,
					max:18,
					message:"密码长度需要在6-18位之间",
					trigger:'blur'
				}]
			}
		}
	},
	methods:{
    
    
		clickLogin(){
    
    
			// 对登录的表单数据进行校验
			this.$refs['loginRef'].validate((res) =>{
    
    
				if(res){
    
    
					this.submitLogin()
				}
			})
		},

		// 点击登录的方法
		async submitLogin(){
    
    

			const response=await this.$api.login(this.loginForm)
			if (response.status===200){
    
    
				// 保存token,用户名
				window.sessionStorage.setItem('token',response.data.token)
				window.sessionStorage.setItem('uname',response.data.username)
				// 判断是否需要记住用户名和密码
				if(this.loginForm.status){
    
    
					//将账号密码信息保存到localStorage中
					// 将账号密码转换为json字符串
					const uinfo=JSON.stringify(this.loginForm)
					// 存储到localStorage
					window.localStorage.setItem('uinfo',uinfo)
				}else{
    
    
					window.localStorage.removeItem('uinfo')
				}
				// 提示登录成功
				this.$message({
    
    
					type:"success",
					message:"登录成功"
				})
				// 页面跳转(后面加)
				this.$router.push({
    
    "name":"index"})

			}else{
    
    
				this.$message({
    
    
					type:"error",
					message:response.data
				})
			}
		},
		// 点击注册
		register(){
    
    
			this.$message({
    
    
				type:'warning',
				message:"平台未开放注册功能"
			})
		}
	},
	components:{
    
    
		LoginBackVue
	},


	created(){
    
    
		const uinfo=window.localStorage.getItem('uinfo')
		// 判断是否有账号密码
		if(uinfo){
    
    
			// 进行解密
			// const u= decodeURIComponent(escape(window.atob(uinfo)))
			// 转化位js对象,保存到loginForm
			this.loginForm =JSON.parse(uinfo)
		}
	}
}

</script>

<style scoped>
	/* 设置登录框的宽高和位置居中 */
	.login_box{
    
    
		width: 500px;
		height:350px;
		margin: calc((100vh - 350px)/2) auto;
		text-align: center;
		color: white;
	}
	.isregister{
    
    
		width: 100%;
		text-align: left;
		
	}
	.isregister span{
    
    
		color: turquoise;
		cursor: pointer;
	}



</style>

Guess you like

Origin blog.csdn.net/YZL40514131/article/details/127700789