码农笔录博客源码
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

309 rindas
7.6 KiB

pirms 6 gadiem
package controllers
pirms 6 gadiem
import (
"github.com/astaxie/beego"
"beeblog/service"
"beeblog/models"
"strings"
"crypto/md5"
"time"
"strconv"
"encoding/hex"
)
pirms 6 gadiem
type UserController struct {
beego.Controller
}
func (u *UserController) LoginPage() {
pirms 6 gadiem
u.TplName = "login.html"
pirms 6 gadiem
}
pirms 6 gadiem
func (u *UserController) RegistPage() {
u.TplName = "regist.html"
}
pirms 6 gadiem
pirms 6 gadiem
func (this *UserController) UserInfo() {
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
user, err := service.GetUser(id)
if err != nil {
pirms 6 gadiem
this.Redirect("/404", 302)
pirms 6 gadiem
return
}
size := 15
num, _ := this.GetInt("num")
if num <= 0 {
num = 1
}
flag, _ := this.GetInt("flag")
if page, err := service.MeBlogs(num, size, flag, id); err == nil {
this.Data["Page"] = page
}
this.Data["User"] = user
pirms 6 gadiem
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
pirms 6 gadiem
this.Data["IsDDesc"] = true
pirms 6 gadiem
this.TplName = "user.html"
return
}
pirms 6 gadiem
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 {
pirms 6 gadiem
this.Redirect("/404", 302)
pirms 6 gadiem
return
pirms 6 gadiem
}
user, uerr := service.GetUser(uid.(int64))
if uerr != nil {
pirms 6 gadiem
this.Redirect("/404", 302)
pirms 6 gadiem
return
pirms 6 gadiem
}
pirms 6 gadiem
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
pirms 6 gadiem
this.Data["Page"] = page
this.Data["IsMeBlog"] = true
this.Data["Flag"] = 0
this.Data["User"] = user
this.TplName = "ublogs.html"
}
func (this *UserController) PersonNote() {
uid := this.GetSession("userid")
if uid == nil {
this.Redirect("/login", 302)
return
}
notColl, err := service.GetNoteColl(uid.(int64))
if err == nil {
if len(notColl) > 0 {
for i := 0; i < len(notColl); i++ {
count, _ := service.CountNote(notColl[i].Id)
notColl[i].Count = count
}
}
} else {
notColl = make([]*models.NoteColl, 0)
}
if err != nil {
pirms 6 gadiem
this.Redirect("/404", 302)
pirms 6 gadiem
return
}
user, uerr := service.GetUser(uid.(int64))
if uerr != nil {
if uid == nil {
pirms 6 gadiem
this.Redirect("/404", 302)
return
}
}
pirms 6 gadiem
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() {
uid := this.GetSession("userid")
if uid == nil {
this.Redirect("/login", 302)
return
}
size := 15
num, _ := this.GetInt("num")
if num <= 0 {
num = 1
}
pirms 6 gadiem
page, err := service.MeLikes(num, size, uid.(int64))
if err != nil {
pirms 6 gadiem
this.Redirect("/404", 302)
pirms 6 gadiem
return
}
user, uerr := service.GetUser(uid.(int64))
if uerr != nil {
if uid == nil {
pirms 6 gadiem
this.Redirect("/404", 302)
return
}
}
pirms 6 gadiem
this.Data["UserId"] = this.GetSession("userid")
this.Data["HeadImg"] = this.GetSession("headimg")
pirms 6 gadiem
this.Data["NickName"] = this.GetSession("nickname")
this.Data["IsLogin"] = this.GetSession("nickname") != nil
this.Data["Page"] = page
pirms 6 gadiem
this.Data["IsMeLike"] = true
pirms 6 gadiem
this.Data["User"] = user
pirms 6 gadiem
this.TplName = "ulike.html"
}
func (this *UserController) PersonInfo() {
uid := this.GetSession("userid")
if uid == nil {
this.Redirect("/login", 302)
return
}
user, err := service.GetUser(uid.(int64))
if err != nil {
pirms 6 gadiem
this.Redirect("/404", 302)
pirms 6 gadiem
return
}
this.Data["IsLogin"] = this.GetSession("nickname") != nil
pirms 6 gadiem
this.Data["UserId"] = this.GetSession("userid")
this.Data["HeadImg"] = this.GetSession("headimg")
this.Data["NickName"] = this.GetSession("nickname")
pirms 6 gadiem
this.Data["IsMeInfo"] = true
this.Data["User"] = user
this.TplName = "uinfo.html"
}
func (this *UserController) Edit() {
uid := this.GetSession("userid")
if uid == nil {
models.ReurnError(401, "")
this.ServeJSON()
return
}
user, err := service.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")
pirms 6 gadiem
if _, err := service.EditUser(user); err != nil {
pirms 6 gadiem
this.Data["json"] = models.ReurnError(500, "")
pirms 6 gadiem
} else {
pirms 6 gadiem
this.Data["json"] = models.ReurnSuccess("")
}
this.ServeJSON()
return
pirms 6 gadiem
}
pirms 6 gadiem
func (this *UserController) Login() {
username := this.GetString("username")
userpwd := this.GetString("userpwd")
if username == "" {
pirms 6 gadiem
this.Data["json"] = models.ReurnError(1, "用户名为空")
pirms 6 gadiem
this.ServeJSON()
pirms 6 gadiem
return
pirms 6 gadiem
}
if len(username) < 4 {
pirms 6 gadiem
this.Data["json"] = models.ReurnError(1, "用户名最低4位")
pirms 6 gadiem
this.ServeJSON()
pirms 6 gadiem
return
pirms 6 gadiem
}
if userpwd == "" {
pirms 6 gadiem
this.Data["json"] = models.ReurnError(1, "密码为空")
pirms 6 gadiem
this.ServeJSON()
pirms 6 gadiem
return
pirms 6 gadiem
}
if len(userpwd) < 6 {
pirms 6 gadiem
this.Data["json"] = models.ReurnError(1, "密码最低6位")
pirms 6 gadiem
this.ServeJSON()
pirms 6 gadiem
return
pirms 6 gadiem
}
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("")
pirms 6 gadiem
this.SetSession("userid", user.Id)
pirms 6 gadiem
this.SetSession("nickname", user.NickName)
pirms 6 gadiem
this.SetSession("headimg", user.Headimg)
pirms 6 gadiem
} else {
pirms 6 gadiem
this.Data["json"] = models.ReurnError(1, "用户名或密码错误")
pirms 6 gadiem
}
} else {
pirms 6 gadiem
this.Data["json"] = models.ReurnError(1, "用户名不存在")
pirms 6 gadiem
}
this.ServeJSON()
pirms 6 gadiem
return
pirms 6 gadiem
}
func (this *UserController) Regist() {
username := this.GetString("username")
userpwd := this.GetString("userpwd")
username = strings.Replace(username, " ", "", -1)
userpwd = strings.Replace(userpwd, " ", "", -1)
if username == "" {
pirms 6 gadiem
this.Data["json"] = models.ReurnError(1, "用户名为空")
pirms 6 gadiem
this.ServeJSON()
pirms 6 gadiem
return
pirms 6 gadiem
}
if len(username) < 4 {
pirms 6 gadiem
this.Data["json"] = models.ReurnError(1, "用户名最低4位")
pirms 6 gadiem
this.ServeJSON()
pirms 6 gadiem
return
pirms 6 gadiem
}
if userpwd == "" {
pirms 6 gadiem
this.Data["json"] = models.ReurnError(1, "密码为空")
pirms 6 gadiem
this.ServeJSON()
pirms 6 gadiem
return
pirms 6 gadiem
}
if len(userpwd) < 6 {
pirms 6 gadiem
this.Data["json"] = models.ReurnError(1, "密码最低6位")
pirms 6 gadiem
this.ServeJSON()
pirms 6 gadiem
return
pirms 6 gadiem
}
user, _ := service.FindByUserName(username)
if user != nil {
pirms 6 gadiem
this.Data["json"] = models.ReurnError(1, "用户已经存在")
pirms 6 gadiem
this.ServeJSON()
pirms 6 gadiem
return
pirms 6 gadiem
}
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))
pirms 6 gadiem
user = &models.User{UserName: username, NickName: username, UserPwd: userpwd, Salt: salt}
pirms 6 gadiem
user.Headimg = "https://www.aiprose.com/foss/timg.jpg"
pirms 6 gadiem
err := service.SaveUser(user)
if err == nil {
pirms 6 gadiem
this.Data["json"] = models.ReurnSuccess("")
pirms 6 gadiem
} else {
pirms 6 gadiem
this.Data["json"] = models.ReurnError(1, "注册失败")
pirms 6 gadiem
}
this.ServeJSON()
pirms 6 gadiem
return
pirms 6 gadiem
}
pirms 6 gadiem
func (this *UserController) Logout() {
this.DelSession("userid")
this.DelSession("nickname")
pirms 6 gadiem
this.Redirect("/", 302)
pirms 6 gadiem
return
pirms 6 gadiem
}