Vue登錄頁面
阿新 • • 發佈:2018-07-03
登錄失敗 電話 gettime 登錄 ons box ger 復選框 pan
技術選型:vue+element-ui+axios+webpack
1.頁面的基本結構
- <div class="login-wrap">
- <div class="ms-title">登錄頁面</div>
- <div class="ms-login">
- <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
- <el-form-item prop="username
- <el-input v-model="ruleForm.username" placeholder="用戶名"></el-input>
- </el-form-item>
- <el-form-item prop="password">
- <el-input type="password" placeholder="密碼" v-model="ruleForm.password" @keyup.enter.native="submitForm(‘ruleForm‘)
- </el-form-item>
- <!-- `checked` 為 true 或 false -->
- <el-checkbox v-model="checked">記住密碼</el-checkbox>
- <br>
- <br>
- <div class="login-btn">
- <el-button type="primary
- </div>
- </el-form>
- </div>
- </div>
2.用mockjs模擬後端接口
- import Mock from ‘mockjs‘; //es6語法引入mock模塊
- export default Mock.mock(‘http://test.advance.ai/login‘, { //輸出數據
- ‘username‘: ‘17610603706‘,
- ‘password‘: ‘123456‘
- //還可以自定義其他數據
- });
3.在created生命周期函數裏邊請求後端數據
- //data和method已經初始化,DOM還沒有渲染出來,適合請求數據
- created(){
- //請求模擬的後端地址,返回用戶名和密碼
- axios.get(‘http://test.advance.ai/login‘)
- .then(res => {
- if(res){
- console.log(res.data.username);
- this.loginName=res.data.username;
- this.loginPassword=res.data.password;
- }
- })
- },
表單驗證,手機號要寫正則表達式驗證
- rules: {
- username: [{
- required: true,
- trigger: ‘blur‘,
- validator: validPhone //手機號校驗規則
- }],
- password: [{
- required: true,
- message: ‘請輸入密碼‘,
- trigger: ‘blur‘
- }]
- }
手機號正則表達式驗證,validPhone變量
- //校驗手機號
- var validPhone=(rule, value,callback)=>{
- if (!value){
- callback(new Error(‘請輸入電話號碼‘))
- }else if (!isvalidPhone(value)){
- callback(new Error(‘請輸入正確的11位手機號碼‘))
- }else {
- callback()
- }
- }
- function isvalidPhone(str) {
- //驗證手機號的正則表達式
- const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
- return reg.test(str)
- }
4.登錄事件,登錄之前先驗證表單
- submitForm(formName) {
- const self = this;
- self.$refs[formName].validate((valid) => {
- if (valid) {
- //判斷復選框是否被勾選 勾選則調用配置cookie方法
- if (self.checked == true) {
- console.log("checked == true");
- //傳入賬號名,密碼,和保存天數3個參數
- self.setCookie(self.ruleForm.username, self.ruleForm.password, 7);
- }else {
- console.log("清空Cookie");
- //清空Cookie
- self.clearCookie();
- }
- if(self.loginName==self.ruleForm.username&&self.loginPassword==self.ruleForm.password){
- console.log("登陸成功");
- alert("登錄成功");
- }else{
- alert("登錄失敗,請重新登錄");
- }
- } else {
- console.log(‘error submit!!‘);
- return false;
- }
- });
- },
5.設置和獲取cookie
- //設置cookie
- setCookie(c_name, c_pwd, exdays) {
- var exdate = new Date(); //獲取時間
- exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天數
- //字符串拼接cookie
- window.document.cookie = "userName" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString();
- window.document.cookie = "userPwd" + "=" + c_pwd + ";path=/;expires=" + exdate.toGMTString();
- },
- //讀取cookie
- getCookie: function() {
- if (document.cookie.length > 0) {
- var arr = document.cookie.split(‘; ‘); //這裏顯示的格式需要切割一下
- for (var i = 0; i < arr.length; i++) {
- var arr2 = arr[i].split(‘=‘); //再次切割
- //判斷查找相對應的值
- if (arr2[0] == ‘userName‘) {
- this.ruleForm.username = arr2[1]; //保存到保存數據的地方
- } else if (arr2[0] == ‘userPwd‘) {
- this.ruleForm.password = arr2[1];
- }
- }
- }
- },
Vue登錄頁面