Exemplo n.º 1
0
// 最新博文
// uri: /articles/recent.json
func RecentArticleHandler(rw http.ResponseWriter, req *http.Request) {
	limit := req.FormValue("limit")
	if limit == "" {
		limit = "10"
	}

	recentArticles := service.FindArticles("0", limit)
	buf, err := json.Marshal(recentArticles)
	if err != nil {
		logger.Errorln("[RecentArticleHandler] json.marshal error:", err)
		fmt.Fprint(rw, `{"ok": 0, "error":"解析json出错"}`)
		return
	}
	fmt.Fprint(rw, `{"ok": 1, "data":`+string(buf)+`}`)
}
Exemplo n.º 2
0
// 首页
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})
}
Exemplo n.º 3
0
// 网友文章列表页
// uri: /articles
func ArticlesHandler(rw http.ResponseWriter, req *http.Request) {
	limit := 20

	lastId := req.FormValue("lastid")
	if lastId == "" {
		lastId = "0"
	}

	articles := service.FindArticles(lastId, "25")
	if articles == nil {
		// TODO:服务暂时不可用?
	}

	num := len(articles)

	if num == 0 {
		if lastId == "0" {
			util.Redirect(rw, req, "/")
		} else {
			util.Redirect(rw, req, "/articles")
		}

		return
	}

	var (
		hasPrev, hasNext bool
		prevId, nextId   int
	)

	if lastId != "0" {
		prevId, _ = strconv.Atoi(lastId)

		// 避免因为文章下线,导致判断错误(所以 > 5)
		if prevId-articles[0].Id > 5 {
			hasPrev = false
		} else {
			prevId += limit
			hasPrev = true
		}
	}

	if num > limit {
		hasNext = true
		articles = articles[:limit]
		nextId = articles[limit-1].Id
	} else {
		nextId = articles[num-1].Id
	}

	pageInfo := map[string]interface{}{
		"has_prev": hasPrev,
		"prev_id":  prevId,
		"has_next": hasNext,
		"next_id":  nextId,
	}

	// 获取当前用户喜欢对象信息
	user, ok := filter.CurrentUser(req)
	var likeFlags map[int]int
	if ok {
		uid := user["uid"].(int)
		likeFlags, _ = service.FindUserLikeObjects(uid, model.TYPE_ARTICLE, articles[0].Id, nextId)
	}

	req.Form.Set(filter.CONTENT_TPL_KEY, "/template/articles/list.html")
	// 设置模板数据
	filter.SetData(req, map[string]interface{}{"articles": articles, "activeArticles": "active", "page": pageInfo, "likeflags": likeFlags})
}