func ModifyRuleHandler(rw http.ResponseWriter, req *http.Request) { var data = make(map[string]interface{}) if req.PostFormValue("submit") == "1" { user, _ := filter.CurrentUser(req) errMsg, err := service.ModifyTopic(user, req.PostForm) if err != nil { data["ok"] = 0 data["error"] = errMsg } else { data["ok"] = 1 data["msg"] = "修改成功" } } else { topic, replies, err := service.FindTopicByTid(req.FormValue("tid")) if err != nil { rw.WriteHeader(http.StatusInternalServerError) return } // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/admin/topic/modify.html") data["topic"] = topic data["replies"] = replies data["nodes"] = service.GenNodes() } filter.SetData(req, data) }
// 修改主题 // uri: /topics/modify{json:(|.json)} func ModifyTopicHandler(rw http.ResponseWriter, req *http.Request) { tid := req.FormValue("tid") if tid == "" { util.Redirect(rw, req, "/topics") return } nodes := service.GenNodes() vars := mux.Vars(req) // 请求编辑主题页面 if req.Method != "POST" || vars["json"] == "" { topic := service.FindTopic(tid) req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/new.html") filter.SetData(req, map[string]interface{}{"nodes": nodes, "topic": topic, "activeTopics": "active"}) return } user, _ := filter.CurrentUser(req) err := service.PublishTopic(user, req.PostForm) if err != nil { if err == service.NotModifyAuthorityErr { rw.WriteHeader(http.StatusForbidden) return } fmt.Fprint(rw, `{"ok": 0, "error":"内部服务错误!"}`) return } fmt.Fprint(rw, `{"ok": 1, "data":""}`) }
// 社区帖子列表页 // uri: /topics{view:(|/popular|/no_reply|/last)} func TopicsHandler(rw http.ResponseWriter, req *http.Request) { nodes := service.GenNodes() // 设置内容模板 page, _ := strconv.Atoi(req.FormValue("p")) if page == 0 { page = 1 } vars := mux.Vars(req) order := "" where := "" view := "" switch vars["view"] { case "/no_reply": view = "no_reply" where = "lastreplyuid=0" case "/last": view = "last" order = "ctime DESC" } topics, total := service.FindTopics(page, 0, where, order) pageHtml := service.GetPageHtml(page, total, req.URL.Path) req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/list.html") // 设置模板数据 filter.SetData(req, map[string]interface{}{"activeTopics": "active", "topics": topics, "page": template.HTML(pageHtml), "nodes": nodes, "view": view}) }
// 新建帖子 // uri: /topics/new{json:(|.json)} func NewTopicHandler(rw http.ResponseWriter, req *http.Request) { nodes := service.GenNodes() vars := mux.Vars(req) title := req.PostFormValue("title") // 请求新建主题页面 if title == "" || req.Method != "POST" || vars["json"] == "" { req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/new.html") filter.SetData(req, map[string]interface{}{"nodes": nodes, "activeTopics": "active"}) return } user, _ := filter.CurrentUser(req) err := service.PublishTopic(user, req.PostForm) if err != nil { fmt.Fprint(rw, `{"ok": 0, "error":"内部服务错误!"}`) return } fmt.Fprint(rw, `{"ok": 1, "data":""}`) }