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/{tid:[0-9]+} func TopicDetailHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) topic, replies, err := service.FindTopicByTid(vars["tid"]) if err != nil { util.Redirect(rw, req, "/topics") return } likeFlag := 0 hadCollect := 0 user, ok := filter.CurrentUser(req) if ok { uid := user["uid"].(int) tid := topic["tid"].(int) likeFlag = service.HadLike(uid, tid, model.TYPE_TOPIC) hadCollect = service.HadFavorite(uid, tid, model.TYPE_TOPIC) } service.Views.Incr(req, model.TYPE_TOPIC, util.MustInt(vars["tid"])) // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/detail.html,/template/common/comment.html") // 设置模板数据 filter.SetData(req, map[string]interface{}{"activeTopics": "active", "topic": topic, "replies": replies, "likeflag": likeFlag, "hadcollect": hadCollect}) }
// 社区帖子详细页 // uri: /topics/{tid:[0-9]+} func TopicDetailHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) uid := 0 user, ok := filter.CurrentUser(req) if ok { uid = user["uid"].(int) } // TODO:刷屏暂时不处理 topic, replies, err := service.FindTopicByTid(vars["tid"]) if err != nil || topic == nil || topic["tid"] == nil { logger.Traceln("------") logger.Traceln(vars["tid"]) i, _ := strconv.Atoi(vars["tid"]) total := service.TopicsTotal() if i >= total { i = 0 } if i <= 0 { i = total - 1 } for ; i <= total; i++ { logger.Traceln(i) topic, replies, err = service.FindTopicByTid(strconv.Itoa(i)) if err == nil && topic != nil && topic["tid"] != nil { break } } } logger.Traceln("------end..........") if err != nil || topic == nil || topic["tid"] == nil { NotFoundHandler(rw, req) return } // 增加浏览量 service.IncrTopicView(vars["tid"], uid) topic["prev_tid"] = topic["tid"].(int) - 1 topic["next_tid"] = topic["tid"].(int) + 1 // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/detail.html") // 设置模板数据 filter.SetData(req, map[string]interface{}{"activeTopics": "active", "topic": topic, "replies": replies}) }
// 社区帖子详细页 // uri: /topics/{tid:[0-9]+} func TopicDetailHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) topic, replies, err := service.FindTopicByTid(vars["tid"]) if err != nil { // TODO: } uid := 0 user, ok := filter.CurrentUser(req) if ok { uid = user["uid"].(int) } // TODO:刷屏暂时不处理 // 增加浏览量 service.IncrTopicView(vars["tid"], uid) // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/detail.html") // 设置模板数据 filter.SetData(req, map[string]interface{}{"activeTopics": "active", "topic": topic, "replies": replies}) }
// 社区帖子详细页 // uri: /topics/{tid:[0-9]+} func InspectTopicHandler(rw http.ResponseWriter, req *http.Request) { logger.Traceln(req.RemoteAddr) vars := mux.Vars(req) uid, _ := strconv.Atoi(Config["auid"]) user, ok := filter.CurrentUser(req) if ok { uid = user["uid"].(int) } logger.Traceln("------------") logger.Traceln(vars["tid"]) logger.Traceln(vars["val"]) logger.Traceln("------------") total := service.TopicsTotal() if vars["tid"] == "" { vars["tid"] = strconv.Itoa(randInt(1, total)) } else { tid, _ := strconv.Atoi(vars["tid"]) client := strings.Split(req.RemoteAddr, ":") logger.Traceln(tid, uid, client[0]) if service.InsertVote(tid, uid, client[0]) == true { valInt, err := strconv.Atoi(vars["val"]) logger.Traceln(valInt) if err != nil { fmt.Fprint(rw, `{"errno": 1, "error": "invalid request data"}`) return } if valInt == 0 { service.IncrTopicHate(vars["tid"], uid) } else { // 增加like量 service.IncrTopicLike(vars["tid"], uid) } } } // TODO:刷屏暂时不处理 i, _ := strconv.Atoi(vars["tid"]) i++ if i >= total { i = 0 } else if i <= 0 { i = total - 1 } topic, _, err := service.FindTopicByTid(strconv.Itoa(i)) if err != nil || topic == nil || topic["tid"] == nil { for ; i <= total; i++ { logger.Traceln(i) topic, _, err = service.FindTopicByTid(strconv.Itoa(i)) if err == nil && topic != nil && topic["tid"] != nil { break } } } logger.Traceln("------end..........") if err != nil || topic == nil || topic["tid"] == nil { NotFoundHandler(rw, req) return } // 增加浏览量 service.IncrTopicView(vars["tid"], uid) // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/inspect.html") // 设置模板数据 filter.SetData(req, map[string]interface{}{"activeTopics": "active", "topic": topic}) }