商城项目开发全流程之前端实现
in Projects with 0 comment

商城项目开发全流程之前端实现

in Projects with 0 comment

6 书城项目前端实现

6.1 创建bookStore项目

6.1.1 创建项目

在这里插入图片描述

6.1.2 导入项目

说明: 将课前资料中的内容导入到bookstore中
在这里插入图片描述
效果如下:
在这里插入图片描述

6.2 完成登录校验

6.2.1 业务说明

业务说明: 当用户点击登录按钮时,检查用户名和密码是否有效
在这里插入图片描述

6.2.2 登录页面校验实现

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>尚硅谷会员登录页面</title>
    <link type="text/css" rel="stylesheet" href="../../static/css/style.css" />
    <!--1. 导入vue.js文件-->
		<script src="../../static/script/vue.js"></script>
  </head>
  <body>
		
		<div id="app">
			<div id="login_header">
				<a href="../../index.html">
					<img class="logo_img" alt="" src="../../static/img/logo.gif" />
				</a>
			</div>

			<div class="login_banner">
				<div id="l_content">
					<span class="login_word">欢迎登录</span>
				</div>

				<div id="content">
					<div class="login_form">
						<div class="login_box">
							<div class="tit">
								<h1>尚硅谷会员</h1>
							</div>
							<div class="msg_cont">
								<b></b>
								<span class="errorMsg" v-text="msg"></span>
							</div>
							<div class="form">
								<form action="login_success.html" >
									<label>用户名称:</label>
									<input
										class="itxt"
										type="text"
										placeholder="请输入用户名"
										autocomplete="off"
										tabindex="1"
										name="username"
										id="username"
										v-model="username"
									/>
									<br />
									<br />
									<label>用户密码:</label>
									<input
										class="itxt"
										type="password"
										placeholder="请输入密码"
										autocomplete="off"
										tabindex="1"
										name="password"
										id="password"
										v-model="password"
									/>
									<br />
									<br />
									<input type="submit" value="登录" id="sub_btn" @click="login"/>
								</form>
								<div class="tit">
									<a href="regist.html">立即注册</a>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div id="bottom">
				<span>
					尚硅谷书城.Copyright &copy;2015
				</span>
			</div>
		</div>
		<script>
			
			 const app = new Vue({
				 el: "#app",
				 data: {
					 username: '',
					 password: '',
					 msg: '请输入用户名和密码'
				 },
				 methods: {
					 login(){
						 if(this.username === ''){
							 this.msg = "请输入用户名"
							 //阻止事件提交
							 event.preventDefault()
						 }
						 if(this.password === ''){
							 this.msg = "请输入密码"
							 //阻止事件提交
							 event.preventDefault()
						 }
					 }
				 }
			 })
				
		</script>
  </body>
</html>

6.3 完成用户注册校验

6.3.1 校验案例说明

在这里插入图片描述

6.3.2 表单校验JS

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>尚硅谷会员注册页面</title>
    <link type="text/css" rel="stylesheet" href="../../static/css/style.css" />
    <link rel="stylesheet" href="../../static/css/register.css" />
		<script src="../../static/script/vue.js"></script>
    <style type="text/css">
      .login_form {
        height: 420px;
        margin-top: 25px;
      }
    </style>
  </head>
  <body>
		<div id="app">
			<div id="login_header">
				<a href="../../index.html">
					<img class="logo_img" alt="" src="../../static/img/logo.gif" />
				</a>
			</div>

			<div class="login_banner">
				<div class="register_form">
					<h1>注册尚硅谷会员</h1>
					<form action="regist_success.html">
						<div class="form-item">
							<div>
								<label>用户名称:</label>
								<input type="text" placeholder="请输入用户名" v-model="user.usermame" @blur="checkUsername"/>
							</div>
							<span class="errMess" v-text="msg.usernameErrorMsg"></span>
						</div>
						<div class="form-item">
							<div>
								<label>用户密码:</label>
								<input type="password" placeholder="请输入密码" v-model="user.password" @blur="checkPassword"/>
							</div>
							<span class="errMess" v-text="msg.passwordErrorMsg"></span>
						</div>
						<div class="form-item">
							<div>
								<label>确认密码:</label>
								<input type="password" placeholder="请输入确认密码" v-model="user.password2" @blur="checkPassword2"/>
							</div>
							<span class="errMess"v-text="msg.passwordErrorMsg2"></span>
						</div>
						<div class="form-item">
							<div>
								<label>用户邮箱:</label>
								<input type="text" placeholder="请输入邮箱" v-model="user.email" @blur="checkEmail"/>
							</div>
							<span class="errMess" v-text="msg.emailErrorMsg"></span>
						</div>
			 
						<div class="form-item">
							<div>
								<label>验证码:</label>
								<div class="verify">
									<input type="text" placeholder="" />
									<img src="../../static/img/code.bmp" alt="" height="50px"/>
								</div>
							</div>
							<span class="errMess" v-text="msg.codeMsg"></span>
						</div>
						<button class="btn" @click="regist">注册</button>
					</form>
				</div>
			</div>
			<div id="bottom">
				<span>
					尚硅谷书城.Copyright &copy;2015
				</span>
			</div>
		</div>
		
		<!-- 定义vue对象 -->
		<script type="text/javascript">
			
			const app = new Vue({
				el: "#app",
				data: {
					msg: {
						usernameErrorMsg: "用户名应为6~16位数字和字母组成",
						passwordErrorMsg: "密码的长度至少为8位",
						passwordErrorMsg2: "密码两次输入不一致",
						emailErrorMsg: "请输入正确的邮箱格式",
						codeMsg: "请输入正确的验证码"
					},
					user: {
						usermame: '',
						password: '',
						password2: '',
						email: ''
					},
					//定义全局表单提交标识符
					flagArray : [0,0,0,0]
				},
				methods: {
					checkUsername(){
						 //1.定义正则表达式语法
						 let usernameRege = /^[a-zA-Z0-9]{6,16}$/
						 let usernameFlag = usernameRege.test(this.user.usermame)
						 if(usernameFlag){
							 this.msg.usernameErrorMsg = "√"
							 this.flagArray[0] = 1
						 }else{
							 this.msg.usernameErrorMsg = "用户名应为6~16位数字和字母组成"
							 this.flagArray[0] = 0
						 }
					},
					checkPassword(){
						let passwordRege = /^.{8,}$/
						let passwordFlag = passwordRege.test(this.user.password)
						if(passwordFlag){
							this.msg.passwordErrorMsg = "√"
							this.flagArray[1] = 1
						}else{
							this.msg.passwordErrorMsg = "密码的长度至少为8位"
							this.flagArray[1] = 0
						}
					},
					checkPassword2(){
						if(this.user.password === this.user.password2){
							this.msg.passwordErrorMsg2 = "√"
							this.flagArray[2] = 1
						}else{
							this.msg.passwordErrorMsg2 = "密码两次输入不一致"
							this.flagArray[2] = 0
						}
					},
					checkEmail(){
						let emailRege = /^[a-zA-Z0-9_.-]+@([a-zA-Z0-9-]+[.]{1})+[a-zA-Z]+$/
						if(emailRege.test(this.user.email)){
							this.msg.emailErrorMsg = '√'
							this.flagArray[3] = 1
						}else{
							this.msg.emailErrorMsg = "请输入正确的邮箱格式"
							this.flagArray[3] = 0
						}
					},
					regist(){
						//再次校验密码是否相同即可.
						this.checkPassword2()
						let flagStr = this.flagArray.join(",")
						if(flagStr !== "1,1,1,1"){
							//应该阻止表单提交
							event.preventDefault()
							alert("表单校验不通过!!!")
						}else{
							alert("表单校验成功")
						}
						
					}
				}
			})
		
		</script>
  </body>
</html>