注冊頁面
先從vue頁面開始,下面是舉例的vue頁面截圖
從上面的截圖我們可以看到一些vue的指令:v-model和v-on(縮寫成@)
v-model是表單指令:就是獲取屬性值,在這里就是這些輸入框的值,后台也可以通過request.data.get('mobile') 獲取到填寫的手機號碼
v-on是事件指令:v-on:事件名="事件函數名(參數可有可無)"
然后在vue頁面的script中寫一些js的操作進行校驗,在vue頁面中統一用methods實例成員實現事件函數
事件名函數()調用:checkMobile(),一個事件函數名類似於一個后台的接口。下面的兩個if就是填寫的手機號在前台做一些判斷,然后還有后台的額一些驗證,都在這個事件名函數里面
前后台交互:$axios
1.url是后台的接口,method有get和post請求兩種,如果是get請求,下面就是params,如果是post請求,下面就是data
2.獲取后台發過來的數據也有兩種寫法
//第一種
.then(function (response){
后台返回的數據request.data
}).catch(function (error){
錯誤信息
})
//第二種 .then(response =>{ 后台返回數據request.data }).catch(error =>{ 報錯信息 })

<template> <div class="box"> <img src="@/assets/img/Loginbg.jpg" alt=""> <div class="register"> <div class="register_box"> <div class="register-title">注冊路飛學城</div> <div class="inp"> <input v-model="mobile" @blur="checkMobile" type="text" placeholder="手機號碼" class="user"> <!-- v-on:blur='事件函數名'簡寫 blur是失去焦點事件 --> <input v-model="password" type="password" placeholder="用戶密碼" class="user"> <div class="sms"> <input v-model="sms" type="text" placeholder="輸入驗證碼" class="user"> <span class="sms_btn" @click="send_sms">{{sms_interval_tips}}</span> </div> <div id="geetest"></div> <button class="register_btn" @click="registerMobile">注冊</button> <p class="go_login">已有賬號 <router-link to="/login">直接登錄</router-link> </p> </div> </div> </div> </div> </template> <script> export default { name: 'Register', data() { return { sms: "", mobile: "", password: "", is_send: false, // 是否在60s內發送了短信 sms_interval_tips: "獲取驗證碼", } }, //鈎子函數 created() { }, methods: { //事件函數名加() 調用 checkMobile() { //下面兩個if都是進行一些前端的校驗 //設置了 v-model就是設置當前的屬性值 if (this.mobile.length < 1) { return false; } // 手機號碼格式是否正確 //js的正則表達式要寫在/ /里面 if (!/^1[3-9]\d{9}$/.test(this.mobile)) { this.$message({ message: "對不起!手機號碼格式有誤!" }); return false; } // 驗證手機號碼是否已經注冊了 // this.$axios.get(this.$settings.Host+"/users/mobile/"+this.mobile+"/"); this.$axios({ url: this.$settings.base_url + 'user/mobile/', // 后台路徑 method: 'get', params: { mobile: this.mobile } }).then(response => { let data = response.data; window.console.log(data); if (data.status != 0) { this.$message({ message: "對不起!手機號碼已經被注冊!" }); return false; } else { this.$message({ message: "期待您加入我們!" }); } }).catch(error => { let data = error.response.data; this.$message({ message: data.message }) }) }, send_sms() { // 發送短信 if (!/^1[3-9]\d{9}$/.test(this.mobile)) { this.$message({ message: "對不起!手機號碼格式有誤!" }); return false; } // 判斷是否在60s內發送過短信 if (this.is_send) { this.$message({ message: "對不起,不能頻繁發送短信驗證!" }); return false; } // 請求發送短信 this.$axios({ url: this.$settings.Host + 'user/sms/', method: 'get', params: { mobile: this.mobile } }).then(response => { this.$message({ message: response.data.result, }); // 修改短信的發送狀態 this.is_send = true; // 設置間隔時間60s let sms_interval_time = 60; // 設置短信發送間隔倒計時,.60s后把is_send改成false let timer = setInterval(() => { if (sms_interval_time <= 1) { clearInterval(timer); this.sms_interval_tips = "獲取驗證碼"; this.is_send = false; // 重新回復點擊發送功能的條件 } else { sms_interval_time -= 1; this.sms_interval_tips = `${sms_interval_time}秒后再次獲取`; } }, 1000); }).catch(error => { this.$message({ message: error.response.data.result, }) }); }, registerMobile() { // 注冊信息提交 if (!/^1[3-9]\d{9}$/.test(this.mobile)) { this.$message({ message: "對不起!手機號碼格式有誤!" }); return false; } if (this.sms.length < 1) { this.$message({ message: "短信驗證碼不能為空!" }); return false; } if (this.password.length < 6 || this.password.length > 16) { this.$message({ message: "對不起,密碼長度必須在6-16個字符之間!" }); return false; } this.$axios({ url: this.$settings.Host + 'user/register/mobile/', method: 'post', data: { mobile: this.mobile, password: this.password, sms: this.sms } }).then(response => { let _this = this; let status = response.data.status; let msg = response.data.msg; _this.$message({ message: msg, onClose() { if (status == 0) { // 保存登錄狀態 sessionStorage.user_name = response.data.user.username; // sessionStorage.user_mobile = response.data.user.mobile; // 跳轉到用戶中心 // _this.$router.push('/user'); } } }); }).catch(error => { this.$message({ message: error.response.data.result }); }) } }, }; </script> <style scoped> .box { width: 100%; height: 100%; position: relative; overflow: hidden; } .box img { width: 100%; min-height: 100%; } .box .register { position: absolute; width: 500px; height: 400px; left: 0; margin: auto; right: 0; bottom: 0; top: -238px; } .register .register-title { width: 100%; font-size: 24px; text-align: center; padding-top: 30px; padding-bottom: 30px; color: #4a4a4a; letter-spacing: .39px; } .register-title img { width: 190px; height: auto; } .register-title p { font-size: 18px; color: #fff; letter-spacing: .29px; padding-top: 10px; padding-bottom: 50px; } .register_box { width: 400px; height: auto; background: #fff; box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .5); border-radius: 4px; margin: 0 auto; padding-bottom: 40px; } .register_box .title { font-size: 20px; color: #9b9b9b; letter-spacing: .32px; border-bottom: 1px solid #e6e6e6; display: flex; justify-content: space-around; padding: 50px 60px 0 60px; margin-bottom: 20px; cursor: pointer; } .register_box .title span:nth-of-type(1) { color: #4a4a4a; border-bottom: 2px solid #84cc39; } .inp { width: 350px; margin: 0 auto; } .inp input { outline: 0; width: 100%; height: 45px; border-radius: 4px; border: 1px solid #d9d9d9; text-indent: 20px; font-size: 14px; background: #fff !important; } .inp input.user { margin-bottom: 16px; } .inp .rember { display: flex; justify-content: space-between; align-items: center; position: relative; margin-top: 10px; } .inp .rember p:first-of-type { font-size: 12px; color: #4a4a4a; letter-spacing: .19px; margin-left: 22px; display: -ms-flexbox; display: flex; -ms-flex-align: center; align-items: center; /*position: relative;*/ } .inp .rember p:nth-of-type(2) { font-size: 14px; color: #9b9b9b; letter-spacing: .19px; cursor: pointer; } .inp .rember input { outline: 0; width: 30px; height: 45px; border-radius: 4px; border: 1px solid #d9d9d9; text-indent: 20px; font-size: 14px; background: #fff !important; } .inp .rember p span { display: inline-block; font-size: 12px; width: 100px; /*position: absolute;*/ /*left: 20px;*/ } #geetest { margin-top: 20px; } .register_btn { width: 100%; height: 45px; background: #84cc39; border-radius: 5px; font-size: 16px; color: #fff; letter-spacing: .26px; margin-top: 30px; } .inp .go_login { text-align: center; font-size: 14px; color: #9b9b9b; letter-spacing: .26px; padding-top: 20px; } .inp .go_login a { color: #84cc39; cursor: pointer; } .sms { position: relative; } .sms .sms_btn { position: absolute; top: -12px; right: 0; bottom: 0; margin: auto; width: 130px; text-align: center; height: 24px; color: #ff7000; cursor: pointer; border-left: 1px solid #999; } </style>