码农笔录博客源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
3.5 KiB

6 years ago
package controllers
import (
"github.com/astaxie/beego"
"beeblog/service"
"beeblog/models"
"strings"
"crypto/md5"
"time"
"strconv"
"encoding/hex"
)
6 years ago
type UserController struct {
beego.Controller
}
func (u *UserController) LoginPage() {
6 years ago
u.TplName = "login.html"
6 years ago
}
func (u *UserController) RegistPage() {
u.TplName = "regist.html"
}
6 years ago
6 years ago
func (this *UserController) PersonBlog() {
uid := this.GetSession("userid")
if uid == nil {
this.Redirect("/login", 302)
return
}
size := 15
num, _ := this.GetInt("num")
if num <= 0 {
num = 1
}
flag, _ := this.GetInt("flag")
page, err := service.MeBlogs(num, size, flag, uid.(int64))
if err != nil {
if uid == nil {
this.Redirect("/500", 302)
return
}
}
user, uerr := service.GetUser(uid.(int64))
if uerr != nil {
if uid == nil {
this.Redirect("/500", 302)
return
}
}
this.Data["Page"] = page
this.Data["Flag"] = 0
this.Data["User"] = user
this.TplName = "ublogs.html"
6 years ago
}
func (this *UserController) Login() {
username := this.GetString("username")
userpwd := this.GetString("userpwd")
if username == "" {
6 years ago
this.Data["json"] = models.ReurnError(1, "用户名为空")
this.ServeJSON()
6 years ago
return
}
if len(username) < 4 {
6 years ago
this.Data["json"] = models.ReurnError(1, "用户名最低4位")
this.ServeJSON()
6 years ago
return
}
if userpwd == "" {
6 years ago
this.Data["json"] = models.ReurnError(1, "密码为空")
this.ServeJSON()
6 years ago
return
}
if len(userpwd) < 6 {
6 years ago
this.Data["json"] = models.ReurnError(1, "密码最低6位")
this.ServeJSON()
6 years ago
return
}
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("")
this.SetSession("userid", user.Id)
6 years ago
this.SetSession("nickname", user.NickName)
} else {
6 years ago
this.Data["json"] = models.ReurnError(1, "用户名或密码错误")
}
} else {
6 years ago
this.Data["json"] = models.ReurnError(1, "用户名不存在")
}
this.ServeJSON()
6 years ago
return
}
func (this *UserController) Regist() {
username := this.GetString("username")
userpwd := this.GetString("userpwd")
username = strings.Replace(username, " ", "", -1)
userpwd = strings.Replace(userpwd, " ", "", -1)
if username == "" {
6 years ago
this.Data["json"] = models.ReurnError(1, "用户名为空")
this.ServeJSON()
6 years ago
return
}
if len(username) < 4 {
6 years ago
this.Data["json"] = models.ReurnError(1, "用户名最低4位")
this.ServeJSON()
6 years ago
return
}
if userpwd == "" {
6 years ago
this.Data["json"] = models.ReurnError(1, "密码为空")
this.ServeJSON()
6 years ago
return
}
if len(userpwd) < 6 {
6 years ago
this.Data["json"] = models.ReurnError(1, "密码最低6位")
this.ServeJSON()
6 years ago
return
}
user, _ := service.FindByUserName(username)
if user != nil {
6 years ago
this.Data["json"] = models.ReurnError(1, "用户已经存在")
this.ServeJSON()
6 years ago
return
}
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 years ago
user = &models.User{UserName: username, NickName: username, UserPwd: userpwd, Salt: salt}
err := service.SaveUser(user)
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
} else {
6 years ago
this.Data["json"] = models.ReurnError(1, "注册失败")
}
this.ServeJSON()
6 years ago
return
}
6 years ago
func (this *UserController) Logout() {
this.DelSession("userid")
this.DelSession("nickname")
6 years ago
this.Redirect("/", 302)
6 years ago
return
6 years ago
}