[ADD]注册
This commit is contained in:
parent
2907760207
commit
32629777fa
|
@ -11,7 +11,7 @@
|
||||||
*.out
|
*.out
|
||||||
*.DS_Store
|
*.DS_Store
|
||||||
conf/database.conf
|
conf/database.conf
|
||||||
|
data/*
|
||||||
.idea
|
.idea
|
||||||
lastupdate.tmp
|
lastupdate.tmp
|
||||||
liteblog
|
liteblog
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"liteblog/models"
|
"liteblog/models"
|
||||||
"liteblog/syserrors"
|
"liteblog/syserrors"
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserController struct {
|
type UserController struct {
|
||||||
|
@ -16,9 +18,16 @@ func (c *UserController) Login() {
|
||||||
c.TplName = "login.html"
|
c.TplName = "login.html"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 注册页面
|
||||||
|
// @router /reg [get]
|
||||||
|
func (c *UserController) Reg() {
|
||||||
|
c.TplName = "reg.html"
|
||||||
|
}
|
||||||
|
|
||||||
|
// 登陆接口
|
||||||
// @router /signin [post]
|
// @router /signin [post]
|
||||||
func (c *UserController) SignIn() {
|
func (c *UserController) SignIn() {
|
||||||
log.Print("======")
|
log.Print("正在进行登陆操作")
|
||||||
// 判断邮箱不能为空
|
// 判断邮箱不能为空
|
||||||
email := c.GetMustString("email", "邮箱不能为空")
|
email := c.GetMustString("email", "邮箱不能为空")
|
||||||
// 判断密码不能为空
|
// 判断密码不能为空
|
||||||
|
@ -36,3 +45,41 @@ func (c *UserController) SignIn() {
|
||||||
c.SetSession(SESSION_USER_KEY, user)
|
c.SetSession(SESSION_USER_KEY, user)
|
||||||
c.JSONOk("登陆成功", "/")
|
c.JSONOk("登陆成功", "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 注册接口
|
||||||
|
// @router /signup [post]
|
||||||
|
func (c *UserController) SignUp() {
|
||||||
|
log.Print("正在进行注册操作")
|
||||||
|
// 判断昵称不能为空
|
||||||
|
name := c.GetMustString("name", "昵称不能为空")
|
||||||
|
// 判断邮箱不能为空
|
||||||
|
email := c.GetMustString("email", "邮箱不能为空")
|
||||||
|
// 判断密码不能为空
|
||||||
|
password := c.GetMustString("password", "密码不能为空")
|
||||||
|
// 判断确认密码不能为空
|
||||||
|
password_confirmation := c.GetMustString("password_confirmation", "确认密码不能为空")
|
||||||
|
|
||||||
|
if strings.Compare(password, password_confirmation) != 0 {
|
||||||
|
c.Abort500(errors.New("密码与确认密码不一致"))
|
||||||
|
}
|
||||||
|
|
||||||
|
if u, err := models.QueryUserByName(name); err == nil && u.ID != 0 {
|
||||||
|
c.Abort500(syserrors.NewError("用户昵称已经存在", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
if u, err := models.QueryUserByEmail(email); err == nil && u.ID != 0 {
|
||||||
|
c.Abort500(syserrors.NewError("用户邮箱已经存在", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开始保存用户
|
||||||
|
if err := models.SaveUser(&models.User{
|
||||||
|
Name: name,
|
||||||
|
Email: email,
|
||||||
|
Pwd: password,
|
||||||
|
Avatar: "",
|
||||||
|
Role: 1,
|
||||||
|
}); err != nil {
|
||||||
|
c.Abort500(syserrors.NewError("用户注册失败", err))
|
||||||
|
}
|
||||||
|
c.JSONOk("注册成功", "/login")
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
|
@ -17,3 +17,17 @@ func QueryUserByEmailAndPassword(email, password string) (user User, err error)
|
||||||
e := db.Model(&User{}).First(&user, "email = ? and pwd = ?", email, password).Error
|
e := db.Model(&User{}).First(&user, "email = ? and pwd = ?", email, password).Error
|
||||||
return user, e
|
return user, e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func QueryUserByName(name string) (user User, err error) {
|
||||||
|
e := db.Model(&User{}).First(&user, "name = ?", name).Error
|
||||||
|
return user, e
|
||||||
|
}
|
||||||
|
|
||||||
|
func QueryUserByEmail(email string) (user User, err error) {
|
||||||
|
e := db.Model(&User{}).First(&user, "email = ?", email).Error
|
||||||
|
return user, e
|
||||||
|
}
|
||||||
|
|
||||||
|
func SaveUser(user *User) error {
|
||||||
|
return db.Create(user).Error
|
||||||
|
}
|
|
@ -39,7 +39,7 @@
|
||||||
<div class="layui-form-item">
|
<div class="layui-form-item">
|
||||||
<label class="layui-form-label">确认密码</label>
|
<label class="layui-form-label">确认密码</label>
|
||||||
<div class="layui-input-inline">
|
<div class="layui-input-inline">
|
||||||
<input type="password2" name="password2" required lay-verify="required"
|
<input type="password" name="password_confirmation" required lay-verify="required"
|
||||||
placeholder="请输入密码"
|
placeholder="请输入密码"
|
||||||
autocomplete="off" class="layui-input">
|
autocomplete="off" class="layui-input">
|
||||||
</div>
|
</div>
|
||||||
|
@ -57,5 +57,28 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{{ template "shares/footer.html". }}
|
{{ template "shares/footer.html". }}
|
||||||
|
<script>
|
||||||
|
layui.use(['form', 'jquery', 'layer', 'sysn'], function () {
|
||||||
|
var form = layui.form,
|
||||||
|
sysn = layui.sysn,
|
||||||
|
$ = layui.jquery,
|
||||||
|
layer = layui.layer;
|
||||||
|
//监听提交
|
||||||
|
form.on('submit(reg)', function (formData) {
|
||||||
|
sysn.post("/signup", formData.field)
|
||||||
|
// .setTimeout(5000)
|
||||||
|
.success(function (data) {
|
||||||
|
layer.msg(data.msg);
|
||||||
|
if (data.action) {
|
||||||
|
setTimeout(function () {
|
||||||
|
window.location.href = data.action;
|
||||||
|
}, 300)
|
||||||
|
}
|
||||||
|
}).run();
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
Reference in New Issue