Esempio n. 1
0
// 最新评论
// uri: /comments/recent.json
func RecentCommentHandler(rw http.ResponseWriter, req *http.Request) {
	limit := req.FormValue("limit")
	if limit == "" {
		limit = "10"
	}
	recentComments := service.FindRecentComments(0, -1, limit)

	uids := util.Models2Intslice(recentComments, "Uid")
	users := service.GetUserInfos(uids)

	result := map[string]interface{}{
		"comments": recentComments,
	}

	// json encode 不支持 map[int]...
	for uid, user := range users {
		result[strconv.Itoa(uid)] = user
	}

	buf, err := json.Marshal(result)

	if err != nil {
		logger.Errorln("[RecentCommentHandler] json.marshal error:", err)
		fmt.Fprint(rw, `{"ok": 0, "error":"解析json出错"}`)
		return
	}
	fmt.Fprint(rw, `{"ok": 1, "data":`+string(buf)+`}`)
}
Esempio n. 2
0
// 获取某对象的评论信息
// uri: /object/comments.json
func ObjectCommentsHandler(rw http.ResponseWriter, req *http.Request) {
	objid := req.FormValue("objid")
	objtype := req.FormValue("objtype")

	commentList, err := service.FindObjectComments(objid, objtype)

	uids := util.Models2Intslice(commentList, "Uid")
	users := service.GetUserInfos(uids)

	result := map[string]interface{}{
		"comments": commentList,
	}

	// json encode 不支持 map[int]...
	for uid, user := range users {
		result[strconv.Itoa(uid)] = user
	}

	buf, err := json.Marshal(result)

	if err != nil {
		logger.Errorln("[RecentCommentHandler] json.marshal error:", err)
		fmt.Fprint(rw, `{"ok": 0, "error":"解析json出错"}`)
		return
	}
	fmt.Fprint(rw, `{"ok": 1, "data":`+string(buf)+`}`)
}