码农笔录博客源码
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

321 行
8.1 KiB

6 年前
package controllers
import (
"beeblog/models"
"beeblog/service"
"crypto/md5"
"encoding/hex"
beego "github.com/beego/beego/v2/server/web"
"strconv"
"strings"
"time"
)
6 年前
type UserController struct {
beego.Controller
}
func (u *UserController) LoginPage() {
6 年前
u.TplName = "login.html"
6 年前
}
func (u *UserController) RegistPage() {
u.TplName = "regist.html"
}
6 年前
func (this *UserController) UserInfo() {
5 年前
userService := service.UserService{}
blogService := service.BlogService{}
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
5 年前
user, err := userService.GetUser(id)
if err != nil {
5 年前
this.Redirect("/404", 302)
return
}
size := 15
num, _ := this.GetInt("num")
if num <= 0 {
num = 1
}
flag, _ := this.GetInt("flag")
5 年前
if page, err := blogService.MeBlogs(num, size, flag, id); err == nil {
this.Data["Page"] = page
}
this.Data["User"] = user
6 年前
this.Data["UserId"] = this.GetSession("userid")
this.Data["HeadImg"] = this.GetSession("headimg")
this.Data["NickName"] = this.GetSession("nickname")
this.Data["IsLogin"] = this.GetSession("nickname") != nil
6 年前
this.Data["IsDDesc"] = true
this.TplName = "user.html"
return
}
func (this *UserController) PersonBlog() {
5 年前
userService := service.UserService{}
blogService := service.BlogService{}
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")
5 年前
page, err := blogService.MeBlogs(num, size, flag, uid.(int64))
if err != nil {
5 年前
this.Redirect("/404", 302)
return
}
5 年前
user, uerr := userService.GetUser(uid.(int64))
if uerr != nil {
5 年前
this.Redirect("/404", 302)
return
}
6 年前
this.Data["UserId"] = this.GetSession("userid")
this.Data["HeadImg"] = this.GetSession("headimg")
this.Data["NickName"] = this.GetSession("nickname")
this.Data["IsLogin"] = this.GetSession("nickname") != nil
this.Data["Page"] = page
this.Data["IsMeBlog"] = true
this.Data["Flag"] = 0
this.Data["User"] = user
this.TplName = "ublogs.html"
}
func (this *UserController) PersonNote() {
5 年前
userService := service.UserService{}
noteService := service.NoteService{}
uid := this.GetSession("userid")
if uid == nil {
this.Redirect("/login", 302)
return
}
5 年前
notColl, err := noteService.GetNoteColl(uid.(int64))
if err == nil {
if len(notColl) > 0 {
for i := 0; i < len(notColl); i++ {
5 年前
count, _ := noteService.CountNote(notColl[i].Id)
notColl[i].Count = count
}
}
} else {
notColl = make([]*models.NoteColl, 0)
}
if err != nil {
5 年前
this.Redirect("/404", 302)
return
}
5 年前
user, uerr := userService.GetUser(uid.(int64))
if uerr != nil {
if uid == nil {
5 年前
this.Redirect("/404", 302)
return
}
}
6 年前
this.Data["UserId"] = this.GetSession("userid")
this.Data["HeadImg"] = this.GetSession("headimg")
this.Data["NickName"] = this.GetSession("nickname")
this.Data["IsLogin"] = this.GetSession("nickname") != nil
this.Data["Note"] = notColl
this.Data["IsMeNote"] = true
this.Data["User"] = user
this.TplName = "unote.html"
}
func (this *UserController) PersonLike() {
5 年前
userService := service.UserService{}
likeService := service.LikeService{}
uid := this.GetSession("userid")
if uid == nil {
this.Redirect("/login", 302)
return
}
size := 15
num, _ := this.GetInt("num")
if num <= 0 {
num = 1
}
5 年前
page, err := likeService.MeLikes(num, size, uid.(int64))
if err != nil {
5 年前
this.Redirect("/404", 302)
return
}
5 年前
user, uerr := userService.GetUser(uid.(int64))
if uerr != nil {
if uid == nil {
5 年前
this.Redirect("/404", 302)
return
}
}
6 年前
this.Data["UserId"] = this.GetSession("userid")
this.Data["HeadImg"] = this.GetSession("headimg")
this.Data["NickName"] = this.GetSession("nickname")
this.Data["IsLogin"] = this.GetSession("nickname") != nil
this.Data["Page"] = page
this.Data["IsMeLike"] = true
this.Data["User"] = user
this.TplName = "ulike.html"
}
func (this *UserController) PersonInfo() {
5 年前
userService := service.UserService{}
uid := this.GetSession("userid")
if uid == nil {
this.Redirect("/login", 302)
return
}
5 年前
user, err := userService.GetUser(uid.(int64))
if err != nil {
5 年前
this.Redirect("/404", 302)
return
}
this.Data["IsLogin"] = this.GetSession("nickname") != nil
6 年前
this.Data["UserId"] = this.GetSession("userid")
this.Data["HeadImg"] = this.GetSession("headimg")
this.Data["NickName"] = this.GetSession("nickname")
this.Data["IsMeInfo"] = true
this.Data["User"] = user
this.TplName = "uinfo.html"
}
func (this *UserController) Edit() {
5 年前
userService := service.UserService{}
uid := this.GetSession("userid")
if uid == nil {
models.ReurnError(401, "")
this.ServeJSON()
return
}
5 年前
user, err := userService.GetUser(uid.(int64))
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
this.ServeJSON()
return
}
birthday := this.GetString("birthday")
if birthday != "" {
birthday += " 00:00:00"
if localTime, errt := time.ParseInLocation("2006-01-02 15:04:05", birthday, time.Local); errt == nil {
user.Birthday = localTime
}
}
user.NickName = this.GetString("nickName")
user.Email = this.GetString("email")
user.Mobile = this.GetString("mobile")
user.QQ = this.GetString("qqnum")
user.Sex, _ = this.GetInt("catory")
user.DescInfo = this.GetString("mdesc")
5 年前
if _, err := userService.EditUser(user); err != nil {
this.Data["json"] = models.ReurnError(500, "")
} else {
this.Data["json"] = models.ReurnSuccess("")
}
this.ServeJSON()
return
6 年前
}
func (this *UserController) Login() {
5 年前
userService := service.UserService{}
username := this.GetString("username")
userpwd := this.GetString("userpwd")
if username == "" {
this.Data["json"] = models.ReurnError(1, "用户名为空")
this.ServeJSON()
return
}
if len(username) < 4 {
this.Data["json"] = models.ReurnError(1, "用户名最低4位")
this.ServeJSON()
return
}
if userpwd == "" {
this.Data["json"] = models.ReurnError(1, "密码为空")
this.ServeJSON()
return
}
if len(userpwd) < 6 {
this.Data["json"] = models.ReurnError(1, "密码最低6位")
this.ServeJSON()
return
}
5 年前
user, error := userService.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 年前
this.SetSession("nickname", user.NickName)
6 年前
this.SetSession("headimg", user.Headimg)
} else {
this.Data["json"] = models.ReurnError(1, "用户名或密码错误")
}
} else {
this.Data["json"] = models.ReurnError(1, "用户名不存在")
}
this.ServeJSON()
return
}
func (this *UserController) Regist() {
5 年前
userService := service.UserService{}
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, "用户名为空")
this.ServeJSON()
return
}
if len(username) < 4 {
this.Data["json"] = models.ReurnError(1, "用户名最低4位")
this.ServeJSON()
return
}
if userpwd == "" {
this.Data["json"] = models.ReurnError(1, "密码为空")
this.ServeJSON()
return
}
if len(userpwd) < 6 {
this.Data["json"] = models.ReurnError(1, "密码最低6位")
this.ServeJSON()
return
}
5 年前
user, _ := userService.FindByUserName(username)
if user != nil {
this.Data["json"] = models.ReurnError(1, "用户已经存在")
this.ServeJSON()
return
}
h := md5.New()
h.Write([]byte(strconv.FormatInt(time.Now().Unix(), 10) + "aiprose:nelson"))
salt := hex.EncodeToString(h.Sum(nil))
h = md5.New()
h.Write([]byte(userpwd + salt))
userpwd = hex.EncodeToString(h.Sum(nil))
user = &models.User{UserName: username, NickName: username, UserPwd: userpwd, Salt: salt}
5 年前
user.Headimg = "https://oss.aiprose.com/aiprose/timg.jpg"
err := userService.SaveUser(user)
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
} else {
this.Data["json"] = models.ReurnError(1, "注册失败")
}
this.ServeJSON()
return
}
6 年前
func (this *UserController) Logout() {
this.DelSession("userid")
this.DelSession("nickname")
this.Redirect("/", 302)
6 年前
return
}