码农笔录博客源码
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

126 linhas
3.0 KiB

6 anos atrás
package controllers
6 anos atrás
import (
"github.com/astaxie/beego"
"beeblog/service"
"beeblog/models"
"strings"
"crypto/md5"
"time"
"strconv"
"encoding/hex"
6 anos atrás
"fmt"
6 anos atrás
)
6 anos atrás
type UserController struct {
beego.Controller
}
func (u *UserController) LoginPage() {
6 anos atrás
u.TplName = "login.html"
6 anos atrás
}
6 anos atrás
func (u *UserController) RegistPage() {
u.TplName = "regist.html"
}
6 anos atrás
6 anos atrás
func (u *UserController) PersonBlog() {
u.TplName = "ublogs.html"
}
6 anos atrás
func (this *UserController) Login() {
username := this.GetString("username")
userpwd := this.GetString("userpwd")
if username == "" {
this.Data["json"] = models.ReurnError(1,"用户名为空")
6 anos atrás
this.ServeJSON()
6 anos atrás
return
6 anos atrás
}
if len(username) < 4 {
this.Data["json"] = models.ReurnError(1,"用户名最低4位")
6 anos atrás
this.ServeJSON()
6 anos atrás
return
6 anos atrás
}
if userpwd == "" {
this.Data["json"] = models.ReurnError(1,"密码为空")
6 anos atrás
this.ServeJSON()
6 anos atrás
return
6 anos atrás
}
if len(userpwd) < 6 {
this.Data["json"] = models.ReurnError(1,"密码最低6位")
6 anos atrás
this.ServeJSON()
6 anos atrás
return
6 anos atrás
}
user, error := service.FindByUserName(username)
if error == nil && user != nil {
h := md5.New()
h.Write([]byte(userpwd + user.Salt))
userpwd = hex.EncodeToString(h.Sum(nil))
if userpwd == user.UserPwd {
this.Data["json"] = models.ReurnSuccess("")
6 anos atrás
this.SetSession("userid", user.Id)
6 anos atrás
this.SetSession("nickname", user.NickName)
6 anos atrás
fmt.Println(this.CruSession)
6 anos atrás
} else {
this.Data["json"] = models.ReurnError(1,"用户名或密码错误")
6 anos atrás
}
} else {
this.Data["json"] = models.ReurnError(1,"用户名不存在")
6 anos atrás
}
this.ServeJSON()
6 anos atrás
return
6 anos atrás
}
func (this *UserController) Regist() {
username := this.GetString("username")
userpwd := this.GetString("userpwd")
username = strings.Replace(username, " ", "", -1)
userpwd = strings.Replace(userpwd, " ", "", -1)
if username == "" {
this.Data["json"] = models.ReurnError(1,"用户名为空")
6 anos atrás
this.ServeJSON()
6 anos atrás
return
6 anos atrás
}
if len(username) < 4 {
this.Data["json"] = models.ReurnError(1,"用户名最低4位")
6 anos atrás
this.ServeJSON()
6 anos atrás
return
6 anos atrás
}
if userpwd == "" {
this.Data["json"] = models.ReurnError(1,"密码为空")
6 anos atrás
this.ServeJSON()
6 anos atrás
return
6 anos atrás
}
if len(userpwd) < 6 {
this.Data["json"] = models.ReurnError(1,"密码最低6位")
6 anos atrás
this.ServeJSON()
6 anos atrás
return
6 anos atrás
}
user, _ := service.FindByUserName(username)
if user != nil {
this.Data["json"] = models.ReurnError(1,"用户已经存在")
6 anos atrás
this.ServeJSON()
6 anos atrás
return
6 anos atrás
}
h := md5.New()
h.Write([]byte(strconv.FormatInt(time.Now().Unix(), 10) + beego.AppConfig.String("host")))
salt := hex.EncodeToString(h.Sum(nil))
h = md5.New()
h.Write([]byte(userpwd + salt))
userpwd = hex.EncodeToString(h.Sum(nil))
6 anos atrás
user = &models.User{UserName: username,NickName:username, UserPwd: userpwd, Salt: salt}
6 anos atrás
err := service.SaveUser(user)
if err == nil {
6 anos atrás
this.Data["json"] = models.ReurnSuccess("")
6 anos atrás
} else {
this.Data["json"] = models.ReurnError(1,"注册失败")
6 anos atrás
}
this.ServeJSON()
6 anos atrás
return
6 anos atrás
}
6 anos atrás
func (this *UserController) Logout() {
this.DelSession("userid")
this.DelSession("nickname")
this.Redirect("/",302)
return
}