Example #1
0
// 网站统计信息
// uri: /websites/stat.json
func StatHandler(rw http.ResponseWriter, req *http.Request) {
	articleTotal := service.ArticlesTotal()
	projectTotal := service.ProjectsTotal()
	topicTotal := service.TopicsTotal()
	cmtTotal := service.CommentsTotal(-1)
	resourceTotal := service.ResourcesTotal()
	userTotal := service.CountUsers()

	data := map[string]int{
		"article":  articleTotal,
		"project":  projectTotal,
		"topic":    topicTotal,
		"resource": resourceTotal,
		"comment":  cmtTotal,
		"user":     userTotal,
	}

	buf, err := json.Marshal(data)
	if err != nil {
		logger.Errorln("[StatHandler] json.marshal error:", err)
		fmt.Fprint(rw, `{"ok": 0, "error":"解析json出错"}`)
		return
	}
	fmt.Fprint(rw, `{"ok": 1, "data":`+string(buf)+`}`)
}
Example #2
0
// 网站统计信息
func StatHandler(rw http.ResponseWriter, req *http.Request) {
	topicTotal := service.TopicsTotal()
	replyTotal := service.CommentsTotal(model.TYPE_TOPIC)
	resourceTotal := service.ResourcesTotal()
	userTotal := service.CountUsers()
	fmt.Fprint(rw, `{"errno": 0, "topic":`+strconv.Itoa(topicTotal)+`,"resource":`+strconv.Itoa(resourceTotal)+`,"reply":`+strconv.Itoa(replyTotal)+`,"user":`+strconv.Itoa(userTotal)+`}`)
}
Example #3
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})
}
Example #4
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})
}