// 最新评论 // 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)+`}`) }
// 用户个人首页 // URI: /user/{username} func UserHomeHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) username := vars["username"] // 获取用户信息 user := service.FindUserByUsername(username) if user == nil { util.Redirect(rw, req, "/users") return } topics := service.FindRecentTopics(user.Uid, "5") resources := service.FindUserRecentResources(user.Uid) resourceCats := make(map[int]string) for _, resource := range resources { resourceCats[resource.Catid] = service.GetCategoryName(resource.Catid) } projects := service.FindUserRecentProjects(user.Username) comments := service.FindRecentComments(user.Uid, -1, "5") // 设置模板数据 filter.SetData(req, map[string]interface{}{"activeUsers": "active", "topics": topics, "resources": resources, "resource_cats": resourceCats, "projects": projects, "comments": comments, "user": user}) req.Form.Set(filter.CONTENT_TPL_KEY, "/template/user/profile.html") }
// 用户个人首页 // URI: /user/{username} func UserHomeHandler(rw http.ResponseWriter, req *http.Request) { req.Form.Set(filter.CONTENT_TPL_KEY, "/template/user/profile.html") vars := mux.Vars(req) username := vars["username"] // 获取用户信息 user := service.FindUserByUsername(username) if user != nil { topics := service.FindRecentTopics(user.Uid) comments := service.FindRecentComments(user.Uid, model.TYPE_TOPIC) replies := service.FindRecentReplies(comments) // 设置模板数据 filter.SetData(req, map[string]interface{}{"activeUsers": "active", "topics": topics, "replies": replies, "user": user}) } }