beego框架表单数据验证之注册登录功能开发示例代码
beego登录注册功能之前的文章有写过,今天主要就是用一下表单数据验证validation模块,
及改成异步处理方式来登录注册,具体细节就不记录了,我这里就简单的curd,
有兴趣的可以自行翻阅之前的文章
注册代码:
"github.com/astaxie/beego/validation"
//注册页模板
func (c *UserController) Reg() {
c.TplName = "register.html" //只有加了这句,前端才会跳转到注册页面
}
//注册用户 模型添加数据
func (c *UserController) Register() {
//1.拿到数据
name := c.GetString("name")
email := c.GetString("email")
password := c.GetString("password")
//2.对数据进行校验
valid := validation.Validation{}
//用户名不空
isName := valid.Required(name, "name")
if !isName.Ok {
c.ErrorJson(500, "用户名不合法", nil)
}
//密码 >6<12
isMinPassword := valid.MinSize(password, 6, "password")
if !isMinPassword.Ok {
c.ErrorJson(500, "密码值需大于6位", nil)
}
isMaxPassword := valid.MaxSize(password, 12, "password")
if !isMaxPassword.Ok {
c.ErrorJson(500, "密码值需小于12位", nil)
}
//验证邮箱格式
isEmail := valid.Email(email, "email")
if !isEmail.Ok {
c.ErrorJson(500, "邮箱不合法", nil)
}
//TO DO 队列验证邮箱真实性
//1.验证-验证码
isCpt := cpt.VerifyReq(c.Ctx.Request)
if !isCpt {
c.ErrorJson(500, "验证码不合法", nil)
}
//24小时制
timeObj := time.Now()
var str = timeObj.Format("2006-01-02 15:04:05")
user := models.Users{Name: name, Email: email, Password: models.Md5(password), Created_at: str}
//TO DO
//里面需在查一遍 做最后层的筛查 比如:数据是否存在|验证邮箱重复 等等
//插入
models.AddUser(&user)
fmt.Println(user)
c.SuccessJson("注册成功")
//c.Ctx.WriteString("注册成功!")
}
前端:
{{template "public/header.html" .}}
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="row clearfix"><div class="col-md-12 column"></div></div>
<div class="row clearfix">
<div class="col-md-12 column">
<div class="row clearfix">
<div class="col-md-4 column"></div>
<div class="col-md-4 column" style="border: 1px solid #eee;margin-top: 10%;padding-bottom: 50px">
<h3 style="padding: 5rem;">注册界面</h3>
<form class="form-horizontal" id="myForm" action="/register" method="post" role="form">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">账 号</label>
<div class="col-sm-10">
<input type="name" name="name" class="form-control" id="name" placeholder="请输入用户名" />
<span id="check_user" style="font-size: 8px;color: red;display: none;"></span>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">密 码</label>
<div class="col-sm-10"><input type="password" name="password" class="form-control" id="inputPassword3" placeholder="请输入密码" /></div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">邮 箱</label>
<div class="col-sm-10"><input type="email" name="email" class="form-control" id="inputEmail3" placeholder="请输入邮箱" /></div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">验 证 码</label>
<div class="col-sm-10">
<input type="text" name="captcha" class="form-control" placeholder="请输入验证码" />{{create_captcha}}
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10"><button type="submit" class="btn btn-default">注册</button></div>
</div>
</form>
</div>
<div class="col-md-4 column"></div>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-md-2 column"><dl></dl></div>
<div class="col-md-8 column"><dl></dl></div>
<div class="col-md-2 column"> </div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
// bind form using ajaxForm
$('#myForm').ajaxForm({
dataType: 'json',
// success: processJson
success: function(data){ console.log(data);
if (data.Code == 200) {
alert(data.Msg);
location.href = "/user";
}else{
$("#check_user").html(data.Msg);
$("#check_user").css("display","block");
};
}
});
});
</script>
{{template "public/footer.html" .}}
测试一下看看效果
登录代码:(这个我就简单的写写了,有兴趣的可以按注册代码改写)
//登录页模板
func (c *UserController) Login() {
//判断一下是否登录
c.TplName = "login.html"
}
//登录post 单条数据查询
func (c *UserController) Info() {
//获取get值
name := c.GetString("name")
password := c.GetString("password")
//2.判断数据是否合法
if name == "" || password == "" {
c.ErrorJson(500, "账号||密码不合法", nil)
}
//1.验证-验证码
isCpt := cpt.VerifyReq(c.Ctx.Request)
if !isCpt {
c.ErrorJson(500, "验证码不合法", nil)
}
//3.查询账号密码是否正确
o := orm.NewOrm()
user := models.Users{}
user.Name = name
user.Password = models.Md5(password)
err := o.Read(&user, "Name", "Password")
if err != nil {
c.ErrorJson(500, "账号密码错误", nil)
} else {
c.SetSession("username", name)
c.SetSession("userid", user.Id)
c.SetSession("useremail", user.Email)
}
c.SuccessJson("登录成功")
//4.跳转 改前端跳
//c.Ctx.Redirect(302, "/user")
}
前端代码也不贴了,跟注册一样,
如果你比较懒的话 登录注册也可以共用一个view的
相关文章