Пример #1
0
// 社区帖子详细页
// 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})
}
Пример #2
0
// 社区帖子详细页
// 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})
}
Пример #3
0
// 社区帖子详细页
// 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})
}