// 最新资源 // uri: /resources/recent.json func RecentResourceHandler(rw http.ResponseWriter, req *http.Request) { limit := req.FormValue("limit") if limit == "" { limit = "10" } recentResources := service.FindResources("0", limit) buf, err := json.Marshal(recentResources) if err != nil { logger.Errorln("[RecentResourceHandler] json.marshal error:", err) fmt.Fprint(rw, `{"ok": 0, "error":"解析json出错"}`) return } fmt.Fprint(rw, `{"ok": 1, "data":`+string(buf)+`}`) }
// 首页 func IndexHandler(rw http.ResponseWriter, req *http.Request) { // nodes := service.GenNodes() num := 10 topicsList := make([]map[string]interface{}, num) // 置顶的topic topTopics, _ := service.FindTopics(1, num, "top=1", "ctime DESC") if len(topTopics) < num { // 获取最新帖子 newTopics, _ := service.FindTopics(1, num-len(topTopics), "top=0", "ctime DESC") topicsList = append(topTopics, newTopics...) } // 获取热门帖子 //hotTopics := service.FindHotTopics() // 获得最新博文 // blogs := service.FindNewBlogs() recentArticles := service.FindArticles("0", "10") // 获取当前用户喜欢对象信息 var likeFlags map[int]int if len(recentArticles) > 0 { user, ok := filter.CurrentUser(req) if ok { uid := user["uid"].(int) likeFlags, _ = service.FindUserLikeObjects(uid, model.TYPE_ARTICLE, recentArticles[0].Id, recentArticles[len(recentArticles)-1].Id) } } // Golang 资源 resources := service.FindResources("0", "10") /* start, end := 0, len(resources) if n := end - 10; n > 0 { start = rand.Intn(n) end = start + 10 } */ // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/index.html") // 设置模板数据 filter.SetData(req, map[string]interface{}{"topics": topicsList, "articles": recentArticles, "likeflags": likeFlags, "resources": resources}) }