码农笔录博客源码
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

147 строки
3.4 KiB

6 лет назад
package controllers
6 лет назад
import (
"github.com/astaxie/beego"
"strconv"
"beeblog/models"
"beeblog/service"
"fmt"
)
6 лет назад
type NoteController struct {
beego.Controller
}
6 лет назад
func (this *NoteController) Save() {
6 лет назад
pid, _ := this.GetInt64("pid")
6 лет назад
title := this.GetString("title")
6 лет назад
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401,"保存失败")
this.ServeJSON()
}
note := &models.Note{Title: title, Pid: pid, UserId: uid.(int64)}
6 лет назад
err := service.SaveNote(note)
if err == nil {
6 лет назад
this.Data["json"] = note
6 лет назад
} else {
6 лет назад
this.Data["json"] = models.ReurnError(500,"保存失败")
}
this.ServeJSON()
}
func (this *NoteController) Edit() {
idStr := this.Ctx.Input.Param(":id")
noteHtml := this.GetString("noteHtml")
id, _ := strconv.ParseInt(idStr, 10, 64)
6 лет назад
uid := this.GetSession("userid")
if uid == nil{
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
}
6 лет назад
note := &models.Note{Id:id}
err1 := service.GetNote(note)
if err1 != nil {
fmt.Print(err1)
this.Data["json"] = models.ReurnError(500,"保存失败")
6 лет назад
this.ServeJSON()
6 лет назад
}
if uid != note.UserId {
6 лет назад
this.Data["json"] = models.ReurnError(403,"")
this.ServeJSON()
6 лет назад
}
note.NoteHtml = noteHtml
6 лет назад
err := service.EditNote(note)
6 лет назад
if err == nil {
6 лет назад
this.Data["json"] = models.ReurnSuccess("")
6 лет назад
} else {
this.Data["json"] = models.ReurnError(500,"保存失败")
6 лет назад
}
this.ServeJSON()
}
func (this *NoteController) SaveNoteColl() {
title := this.GetString("title")
6 лет назад
uid := this.GetSession("userid")
if uid == nil{
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
}
note := &models.NoteColl{Title: title, UserId: uid.(int64)}
6 лет назад
err := service.SaveNoteColl(note)
if err == nil {
6 лет назад
this.Data["json"] = note
6 лет назад
} else {
6 лет назад
this.Data["json"] = models.ReurnError(500,"保存失败")
6 лет назад
}
this.ServeJSON()
}
func (this *NoteController) Get() {
6 лет назад
uid := this.GetSession("userid")
if uid == nil{
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
}
6 лет назад
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
note := &models.Note{Id: id}
err := service.GetNote(note)
if err == nil {
this.Data["json"] = note
}
6 лет назад
if note.UserId != uid.(int64) {
this.Data["json"] = models.ReurnError(403, "")
this.ServeJSON()
}
this.ServeJSON()
}
func (this *NoteController) Delete() {
uid := this.GetSession("userid")
if uid == nil{
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
}
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
note := &models.Note{Id: id}
err := service.GetNote(note)
if err != nil {
this.Data["json"] = models.ReurnError(500,"")
this.ServeJSON()
}
if note.UserId != uid.(int64) {
this.Data["json"] = models.ReurnError(403, "")
this.ServeJSON()
}
err = service.DelNote(note)
if err != nil {
this.Data["json"] = models.ReurnError(500,"")
this.ServeJSON()
}
this.Data["json"] = models.ReurnSuccess("")
6 лет назад
this.ServeJSON()
}
6 лет назад
func (this *NoteController) Note() {
uid := this.GetSession("userid")
fmt.Println("userid", uid)
if uid == nil {
this.Redirect("/login", 302)
}
noteColls, err := service.GetNoteColl(uid.(int64))
6 лет назад
if err == nil {
6 лет назад
if len(noteColls) > 0 {
for i := 0; i < len(noteColls); i++ {
notes, err1 := service.GetNoteByPid(noteColls[i].Id)
6 лет назад
if err1 == nil {
6 лет назад
noteColls[i].Notes = notes
}
}
}
} else {
noteColls = make([]*models.NoteColl, 0)
}
this.Data["NoteColls"] = noteColls
this.TplName = "note.html"
}