// 修改文章 func (this *ArticleController) Edit() { // 显示修改文章的页面 if this.Ctx.Input.Method() == "GET" { id := this.Ctx.Input.Param(":id") intid, err := strconv.ParseInt(id, 10, 64) if err != nil { this.Ctx.WriteString(err.Error()) return } this.Data["Categories"], err = models.GetAllCategories() if err != nil { beego.Error(err) } this.Data["Article"], err = models.GetArticle(intid) if err != nil { this.Abort("404") } this.Data["Title"] = "管理后台 - 修改文章" this.Layout = "manager/layout.html" this.TplNames = "manager/article_edit.html" this.LayoutSections = make(map[string]string) this.LayoutSections["HtmlHead"] = "manager/article_edit_heade.html" return } // 处理修改文章请求 id, err := this.GetInt64("id") if err != nil { this.Data["json"] = map[string]string{"code": "error", "info": err.Error()} this.ServeJson() return } title := this.GetString("title") category := this.GetString("category") content := this.GetString("content") if title == "" || category == "" || content == "" { this.Data["json"] = map[string]string{"code": "error", "info": "必填选项不能为空!"} this.ServeJson() return } err = models.EditArticle(id, title, category, content) if err != nil { this.Data["json"] = map[string]string{"code": "error", "info": err.Error()} } else { this.Data["json"] = map[string]string{"code": "success", "info": "文章修改完成!"} } this.ServeJson() return }
// 查看一篇文章 func (this *ArticleController) View() { id := this.Ctx.Input.Param(":id") intid, err := strconv.ParseInt(id, 10, 64) if err != nil { this.Abort("404") } this.Data["Article"], err = models.GetArticle(intid) if err != nil { this.Abort("404") } this.Data["Title"] = "管理后台 - 预览文章" this.Layout = "manager/layout.html" this.TplNames = "manager/article_view.html" this.LayoutSections = make(map[string]string) this.LayoutSections["HtmlHead"] = "manager/article_view_heade.html" return }
//查看一篇文章 func (this *IndexController) Posts() { id := this.Ctx.Input.Param(":id") intid, err := strconv.ParseInt(id, 10, 64) if err != nil { this.Abort("404") } this.Data["Article"], err = models.GetArticle(intid) if err != nil { this.Abort("404") } this.Data["Categories"], err = models.GetAllCategories() if err != nil { this.Abort("404") } this.Layout = "index/layout.html" this.TplNames = "index/view.html" this.LayoutSections = make(map[string]string) this.LayoutSections["HtmlHead"] = "index/view_heade.html" return }