Esempio n. 1
0
func TopicVoteHandler(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)
	}

	tid, _ := strconv.Atoi(vars["tid"])

	client := strings.Split(req.RemoteAddr, ":")

	logger.Traceln(tid, uid, client[0])
	if service.InsertVote(tid, uid, client[0]) == false {
		fmt.Fprint(rw, `{"errno": 1, "error":"已经投过票"}`)
		return
	}

	valInt, err := strconv.Atoi(vars["val"])
	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)
	}

	like, hate, err := service.FindTopicPopular(vars["tid"])
	logger.Traceln(like)
	logger.Traceln(hate)
	if err != nil {
		// TODO:
	}

	fmt.Fprint(rw, `{"errno": 0, "like":`+strconv.Itoa(like)+`, "hate":`+strconv.Itoa(hate)+`}`)
}
Esempio n. 2
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})
}