1. 程式人生 > >beego + ajax做賬號是否被註冊,賬號密碼是否匹配

beego + ajax做賬號是否被註冊,賬號密碼是否匹配

話不多說上程式碼
註冊的js程式碼:

<html>
<head>
 <meta charset="UTF-8">
 <title>歡迎你,請先登陸!</title>
 <script type="text/javascript" src="../static/js/register.js"></script>
 <script type="text/javascript" src="../static/js/jquery-1.4.4.min.js"></script>
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
 <link rel="stylesheet" href="../static/css/lx.css">
</head>
<body>
    <form  method="POST" action="/register" onsubmit="return fnRegister()">
<div class="box" id="container" style="width: 400px" >
 <div id="header" style="background-color: aquamarine">
  <h2 >註冊</h2>
 </div>
 <div id="content">
  <p >賬號:
   <input id="username" type="text" name="username" placeholder="請輸入10位學號">
  </p>
  <p >密碼:
   <input id="password" type="password" name="password" placeholder="請輸入密碼">
  </p>
  <p >重複密碼:
        <input id="rpassword" type="password" name="rpassword" placeholder="請輸入密碼">
  </p>
  <p >姓名:
        <input id="name" type="text" name="name" placeholder="姓名">
  </p>
  <p >年齡:
  <input id="age" type="number" name="age" placeholder="年齡">
  </p>
  <button type="submit" class="btn btn-info">註冊</button>
 </div>
</div>
    </form>
</body>
<script type="text/javascript">
    
    $("#username").blur(function(){//blur表示失去焦點時觸發
        
        //取使用者名稱
        var uid = $("#username").val()
        //調ajax
        $.ajax({
            url:"/registerajax",//處理資料的頁面
            data:{username:uid},
           // contentType: "application/json; charset=utf-8",
            type:"POST",
            dataType:"json",
            success: function(data){
                 var re = JSON.parse(data)
                 if (re.data == "false")
                 {
                     alert("此使用者名稱已被註冊!")
                 }
                }
            
            });
        
        })
</script>
</html>

登入的js程式碼:

<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>歡迎你,請先登陸!</title>
 <script type="text/javascript" src="../static/js/lx.js"></script>
 <script type="text/javascript" src="../static/js/jquery-1.4.4.min.js"></script>
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
 <link rel="stylesheet" href="../static/css/lx.css">
 
</head>
<body>
<div class="box">
 <h2>登陸</h2>
 <div class="input_box">
  <input id="uname" type="text" name="user" placeholder="請輸入使用者名稱">
 </div>
 <div class="input_box">
  <input id="upass" type="password" name="psw" placeholder="請輸入密碼">
 </div>
 <div id="error_box"><br></div>
 <div class="input_box">
  <button  id="btn" type="button" class="btn btn-primary" >登陸</button>    
  <a href="../register"><input type="button" class="btn btn-info" name="regist" value="註冊"></a>
 </div>

</div>
</body>
<script type="text/javascript">
        $("#btn").click(function(){
        var uid = $("#uname").val()
        var upsw = $("#upass").val()
        //調ajax
        $.ajax({
            url:"/loginajax",
            data:{uname:uid,upass:upsw},
           // contentType: "application/json; charset=utf-8",
            type:"POST",
            dataType:"json",
            success: function(data){
                 var re = JSON.parse(data)
                 if(re.data == "null"){
                     alert("此賬號不存在!")
                 }
                 if(re.data == "false"){
                     alert("賬號與密碼不匹配!")
                 }
                 if(re.data == "true"){
                    alert("登入成功")
                 //window.location.href="/home"
                 }
                }
            });        
        })

</script>
</html>

下面是go程式碼:

package controllers

import (
	"fmt"
	"go_dev/MS/go/models"

	_ "github.com/go-sql-driver/mysql"

	"github.com/astaxie/beego/logs"

	"github.com/astaxie/beego"
)

type RegisterAjaxController struct {
	beego.Controller
}

type LoginAjaxController struct {
	beego.Controller
}
//處理註冊驗證的部分
func (this *RegisterAjaxController) Post() {
	this.TplName = "model_layout/regist.html"
	logs.Warn("registerajax ------post")
	un := this.GetString("username")//這裡的username對應ajax裡data:{username:uid}
	stu, _ := models.GetInfoById(un)
	fmt.Println(stu)
	var request string
	if stu != nil {
		request = "{\"data\":\"false\"}"
	} else {
		request = "{\"data\":\"true\"}"
	}
	this.Data["json"] = &request
	this.ServeJSON()
}
 //處理登入驗證的部分
func (this *LoginAjaxController) Post() {
	this.TplName = "model_layout/regist.html"
	logs.Warn("loginajax ------post")
	un := this.GetString("uname")
	psw := this.GetString("upass")
	stu, _ := models.GetInfoById(un)//用的sqlx做的查詢
	fmt.Println(stu)
	var request string
	if stu == nil {
		request = "{\"data\":\"null\"}"
	}
	if stu != nil && stu[0].PassWord != psw {
		request = "{\"data\":\"false\"}"
	}
	if stu != nil && stu[0].PassWord == psw {
		request = "{\"data\":\"true\"}"
	}

	this.Data["json"] = &request
	this.ServeJSON()
}

本人也是初學,希望大家能相互學習