// 按Tag展示文章列表 func (a Article) Tags(res web.Resource) { a.Layout = "index.html" a.LayoutData = make(map[string]interface{}) a.LayoutData["BlogTitle"] = web.Conf.Get("Blog", "name") data := make(map[string]interface{}) tags := model.NewClassifyModelList("tags") //defer category.Close() id := res.URLValue("id") if tags == nil { a.Render(res, nil) return } else { data["tags"] = tags } if id == "" { // 这里如果ID为空则默认显示第一个分类的信息 id = tags[0].Id.Hex() } // 获得对应ID的文章列表 con := make(map[string]interface{}) con["tags"] = id data["list"] = model.NewArticleModelList(con, 10) a.Render(res, data) }
// zan或者like func (a Article) ZanLike(res web.Resource) { id := res.URLValue("id") name := res.URLValue("type") article := model.NewArticleModel(id) article.PlusOne(name) a.Redirect(res, "/Article/Show/"+id) }
// 前台文章展示 func (a Article) Show(res web.Resource) { a.Layout = "index.html" a.LayoutData = make(map[string]interface{}) a.LayoutData["BlogTitle"] = web.Conf.Get("Blog", "name") id := res.URLValue("id") if id == "" { a.Redirect(res, "/") return } article := model.NewArticleModel(id) if article == nil { a.ShowMessage(res, "您要查看的文章不存在", "/", 5) } else { data := make(map[string]interface{}) data["article"] = article data["comments"] = model.NewCommentModelList(id, 10) a.Render(res, data) } }
// 文章评论 func (a Article) AddComment(res web.Resource) { // 解析表单数据 res.R.ParseForm() comment := model.CommentModel{} id := res.URLValue("id") if v, ok := res.R.Form["name"]; ok { comment.Name = v[0] } if v, ok := res.R.Form["email"]; ok { comment.Email = v[0] } if v, ok := res.R.Form["content"]; ok { comment.Content = v[0] } comment.IP = res.R.RemoteAddr comment.AddArticleId(id) // 将数据插入到数据库中 if ok := comment.Insert(); ok { a.ShowMessage(res, "感谢<b>"+comment.Name+"</b>, 您已评论成功,点击链接返回原文章", "/Article/Show/id/"+id, 5) } else { a.ShowMessage(res, "抱歉<b>"+comment.Name+"</b>, 您评论失败,点击链接返回原文章", "/Article/Show/id/"+id, 5) } }