// 首页 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}) }
// 社区帖子列表页 // uri: /topics{view:(|/popular|/no_reply|/last)} func TopicsHandler(rw http.ResponseWriter, req *http.Request) { nodes := service.GenNodes() // 设置内容模板 page, _ := strconv.Atoi(req.FormValue("p")) if page == 0 { page = 1 } vars := mux.Vars(req) order := "" where := "" view := "" switch vars["view"] { case "/no_reply": view = "no_reply" where = "lastreplyuid=0" case "/last": view = "last" order = "ctime DESC" } topics, total := service.FindTopics(page, 0, where, order) pageHtml := service.GetPageHtml(page, total, req.URL.Path) req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/list.html") // 设置模板数据 filter.SetData(req, map[string]interface{}{"activeTopics": "active", "topics": topics, "page": template.HTML(pageHtml), "nodes": nodes, "view": view}) }
// 所有帖子(分页) func TopicsHandler(rw http.ResponseWriter, req *http.Request) { page, _ := strconv.Atoi(req.FormValue("p")) if page == 0 { page = 1 } topics, _ := service.FindTopics(page, 0, "", "ctime DESC") // pageHtml := service.GetPageHtml(page, total, 10) req.Form.Set(filter.CONTENT_TPL_KEY, "/template/admin/topics.html") // 设置模板数据 filter.SetData(req, map[string]interface{}{"topics": topics}) }
// 首页 func IndexHandler(rw http.ResponseWriter, req *http.Request) { nodes := genNodes() // 获取最新帖子 newTopics, _ := service.FindTopics(1, 10, "", "ctime DESC") // 获取热门帖子 hotTopics := service.FindHotTopics() // 获得最新博文 articles := service.FindNewBlogs() // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/index.html") // 设置模板数据 filter.SetData(req, map[string]interface{}{"news": newTopics, "hots": hotTopics, "articles": articles, "nodes": nodes}) }
// 某节点下的帖子列表 // uri: /topics/node{nid:[0-9]+} func NodesHandler(rw http.ResponseWriter, req *http.Request) { page, _ := strconv.Atoi(req.FormValue("p")) if page == 0 { page = 1 } vars := mux.Vars(req) topics, total := service.FindTopics(page, 0, "nid="+vars["nid"]) pageHtml := service.GetPageHtml(page, total) // 当前节点信息 node := model.GetNode(util.MustInt(vars["nid"])) req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/node.html") // 设置模板数据 filter.SetData(req, map[string]interface{}{"activeTopics": "active", "topics": topics, "page": template.HTML(pageHtml), "total": total, "node": node}) }
// 社区帖子列表页 // uri: /topics{view:(|/popular|/no_reply|/last)} func TopicsHandler(rw http.ResponseWriter, req *http.Request, ismobile bool) { logger.Traceln("User-Agent") logger.Traceln(req.Header["User-Agent"][0]) nodes := genNodes() // 设置内容模板 page, _ := strconv.Atoi(req.FormValue("p")) if page == 0 { page = 1 } vars := mux.Vars(req) order := "" where := "" switch vars["view"] { case "/popular": where = "like>0" case "/last": order = "ctime DESC" } var PAGE_NUM int if ismobile { PAGE_NUM = 30 } else { PAGE_NUM = 10 } topics, total := service.FindTopics(page, PAGE_NUM, where, order) logger.Traceln(total) logger.Traceln(len(topics)) logger.Traceln(PAGE_NUM) pageHtml := service.GetPageHtml(page, total, PAGE_NUM) if ismobile { req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/list_mobile.html") } else { req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/list.html") } // 设置模板数据 switch vars["view"] { case "/popular": filter.SetData(req, map[string]interface{}{"popular": 1, "topics": topics, "page": template.HTML(pageHtml), "nodes": nodes}) case "/last": filter.SetData(req, map[string]interface{}{"last": 1, "topics": topics, "page": template.HTML(pageHtml), "nodes": nodes}) default: filter.SetData(req, map[string]interface{}{"active": 1, "topics": topics, "page": template.HTML(pageHtml), "nodes": nodes}) } }