93 lines
2.2 KiB
Go
93 lines
2.2 KiB
Go
package controllers
|
|
|
|
import (
|
|
"errors"
|
|
"liteblog/models"
|
|
"liteblog/syserrors"
|
|
"log"
|
|
"strings"
|
|
)
|
|
|
|
type UserController struct {
|
|
BaseController
|
|
}
|
|
|
|
// 用户个人中心
|
|
// @router /user [get]
|
|
func (c *UserController) User() {
|
|
c.TplName = "user.html"
|
|
}
|
|
|
|
// 注册页面
|
|
// @router /reg [get]
|
|
func (c *UserController) Reg() {
|
|
c.TplName = "reg.html"
|
|
}
|
|
|
|
// 登陆接口
|
|
// @router /signin [post]
|
|
func (c *UserController) SignIn() {
|
|
log.Print("正在进行登陆操作")
|
|
// 判断邮箱不能为空
|
|
email := c.GetMustString("email", "邮箱不能为空")
|
|
// 判断密码不能为空
|
|
password := c.GetMustString("password", "密码不能为空")
|
|
|
|
var (
|
|
user models.User
|
|
err error
|
|
)
|
|
|
|
if user, err = models.QueryUserByEmailAndPassword(email, password); err != nil {
|
|
c.Abort500(syserrors.NewError("邮箱或密码不对", err))
|
|
}
|
|
// 将user保存到session
|
|
c.SetSession(SESSION_USER_KEY, user)
|
|
c.JSONOk("登陆成功", "/")
|
|
}
|
|
|
|
// 注册接口
|
|
// @router /signup [post]
|
|
func (c *UserController) SignUp() {
|
|
log.Print("正在进行注册操作")
|
|
// 判断昵称不能为空
|
|
name := c.GetMustString("name", "昵称不能为空")
|
|
// 判断邮箱不能为空
|
|
email := c.GetMustString("email", "邮箱不能为空")
|
|
// 判断密码不能为空
|
|
password := c.GetMustString("password", "密码不能为空")
|
|
// 判断确认密码不能为空
|
|
passwordConfirmation := c.GetMustString("password_confirmation", "确认密码不能为空")
|
|
|
|
if strings.Compare(password, passwordConfirmation) != 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("注册成功", "/user")
|
|
}
|
|
|
|
// 退出登陆接口
|
|
// @router /signout [get]
|
|
func (c *UserController) SignOut() {
|
|
c.MustLogin()
|
|
c.DelSession(SESSION_USER_KEY)
|
|
c.Redirect("/", 302)
|
|
} |