Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8392f10

Browse files
committed
实现资源菜单
1 parent a0f2971 commit 8392f10

31 files changed

+1244
-87
lines changed

websites/code/studygolang/src/controller/admin/topic.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,21 @@ import (
1010
"filter"
1111
"model"
1212
"net/http"
13+
"service"
14+
"strconv"
1315
)
1416

1517
// 所有帖子(分页)
1618
func TopicsHandler(rw http.ResponseWriter, req *http.Request) {
17-
user, _ := filter.CurrentUser(req)
18-
// 设置内容模板
19-
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/admin/users.html")
20-
filter.SetData(req, map[string]interface{}{"user": user})
19+
page, _ := strconv.Atoi(req.FormValue("p"))
20+
if page == 0 {
21+
page = 1
22+
}
23+
topics, _ := service.FindTopics(page, 0, "", "ctime DESC")
24+
// pageHtml := service.GetPageHtml(page, total)
25+
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/admin/topics.html")
26+
// 设置模板数据
27+
filter.SetData(req, map[string]interface{}{"topics": topics})
2128
}
2229

2330
// 所有节点(分页)

websites/code/studygolang/src/controller/index.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ func IndexHandler(rw http.ResponseWriter, req *http.Request) {
1818
// 获取最新帖子
1919
newTopics, _ := service.FindTopics(1, 10, "", "ctime DESC")
2020
// 获取热门帖子
21-
hotTopics := service.FindHotTopics()
21+
//hotTopics := service.FindHotTopics()
2222
// 获得最新博文
2323
articles := service.FindNewBlogs()
24+
// 获得最新资源
25+
resources := service.FindRecentResources()
2426
// 设置内容模板
2527
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/index.html")
2628
// 设置模板数据
27-
filter.SetData(req, map[string]interface{}{"news": newTopics, "hots": hotTopics, "articles": articles, "nodes": nodes})
29+
filter.SetData(req, map[string]interface{}{"news": newTopics, "resources": resources, "articles": articles, "nodes": nodes})
2830
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2013 The StudyGolang Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
// http://studygolang.com
5+
// Author:polaris [email protected]
6+
7+
package controller
8+
9+
import (
10+
"filter"
11+
"fmt"
12+
"github.com/studygolang/mux"
13+
"model"
14+
"net/http"
15+
"service"
16+
"util"
17+
)
18+
19+
// 在需要评论且要回调的地方注册评论对象
20+
func init() {
21+
// 注册评论对象
22+
service.RegisterCommentObject("resource", service.ResourceComment{})
23+
}
24+
25+
// 资源索引页
26+
// uri: /resources
27+
func ResIndexHandler(rw http.ResponseWriter, req *http.Request) {
28+
util.Redirect(rw, req, "/resources/cat/1")
29+
}
30+
31+
// 某个分类的资源列表
32+
// uri: /resources/cat/{catid:[0-9]+}
33+
func CatResourcesHandler(rw http.ResponseWriter, req *http.Request) {
34+
vars := mux.Vars(req)
35+
catid := vars["catid"]
36+
resources := service.FindResourcesByCatid(catid)
37+
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/resources/index.html")
38+
filter.SetData(req, map[string]interface{}{"activeResources": "active", "resources": resources, "categories": model.AllCategory, "curCatid": catid})
39+
}
40+
41+
// 某个资源详细页
42+
// uri: /resources/{id:[0-9]+}
43+
func ResourceDetailHandler(rw http.ResponseWriter, req *http.Request) {
44+
vars := mux.Vars(req)
45+
resource, comments := service.FindResource(vars["id"])
46+
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/resources/detail.html")
47+
filter.SetData(req, map[string]interface{}{"activeResources": "active", "resource": resource, "comments": comments})
48+
}
49+
50+
// 发布新资源
51+
// uri: /resources/new{json:(|.json)}
52+
func NewResourceHandler(rw http.ResponseWriter, req *http.Request) {
53+
vars := mux.Vars(req)
54+
title := req.FormValue("title")
55+
if title == "" || req.Method != "POST" || vars["json"] == "" {
56+
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/resources/new.html")
57+
filter.SetData(req, map[string]interface{}{"activeResources": "active", "categories": model.AllCategory})
58+
return
59+
}
60+
errMsg := ""
61+
resForm := req.FormValue("form")
62+
if resForm == model.LinkForm {
63+
if req.FormValue("url") == "" {
64+
errMsg = "url不能为空"
65+
}
66+
} else {
67+
if req.FormValue("content") == "" {
68+
errMsg = "内容不能为空"
69+
}
70+
}
71+
if errMsg != "" {
72+
fmt.Fprint(rw, `{"errno": 1, "error":"`+errMsg+`"}`)
73+
return
74+
}
75+
user, _ := filter.CurrentUser(req)
76+
// 入库
77+
ok := service.PublishResource(user["uid"].(int), req.Form)
78+
if !ok {
79+
fmt.Fprint(rw, `{"errno": 1, "error":"服务器内部错误,请稍候再试!"}`)
80+
return
81+
}
82+
fmt.Fprint(rw, `{"errno": 0, "data":""}`)
83+
}

websites/code/studygolang/src/controller/topic.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ func NodesHandler(rw http.ResponseWriter, req *http.Request) {
7171
// uri: /topics/{tid:[0-9]+}
7272
func TopicDetailHandler(rw http.ResponseWriter, req *http.Request) {
7373
vars := mux.Vars(req)
74-
topic, replies, err := service.FindTopicByTid(vars["tid"])
75-
if err != nil {
76-
// TODO:
77-
}
7874
uid := 0
7975
user, ok := filter.CurrentUser(req)
8076
if ok {
@@ -83,6 +79,10 @@ func TopicDetailHandler(rw http.ResponseWriter, req *http.Request) {
8379
// TODO:刷屏暂时不处理
8480
// 增加浏览量
8581
service.IncrTopicView(vars["tid"], uid)
82+
topic, replies, err := service.FindTopicByTid(vars["tid"])
83+
if err != nil {
84+
// TODO:
85+
}
8686
// 设置内容模板
8787
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/detail.html")
8888
// 设置模板数据

websites/code/studygolang/src/filter/rules.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var rules = map[string]map[string]map[string]map[string]string{
4848
// 发新帖
4949
"/topics/new.json": {
5050
"nid": {
51-
"int": {"range": "0,", "error": "请选择节点"},
51+
"int": {"range": "1,", "error": "请选择节点:%d"},
5252
},
5353
"title": {
5454
"require": {"error": "标题不能为空"},
@@ -80,4 +80,14 @@ var rules = map[string]map[string]map[string]map[string]string{
8080
"length": {"range": "2,", "error": "内容长度必不能少于%d个字符"},
8181
},
8282
},
83+
// 发资源
84+
"/resources/new.json": {
85+
"title": {
86+
"require": {"error": "标题不能为空"},
87+
"length": {"range": "3,", "error": "标题长度必不能少于%d个字符"},
88+
},
89+
"catid": {
90+
"int": {"range": "1,", "error": "请选择类别:%d"},
91+
},
92+
},
8393
}

websites/code/studygolang/src/model/data.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@ var (
2323
nodeRWMutex sync.RWMutex
2424
// 节点信息
2525
AllNode []map[string]interface{}
26+
27+
catRWMutex sync.RWMutex
28+
// 资源分类
29+
AllCategory []*ResourceCat
2630
)
2731

2832
func init() {
2933
UpdateAllRole()
3034
UpdateAllNode()
35+
UpdateAllCategory()
3136
}
3237

3338
// 更新 AllRole 数据
@@ -116,3 +121,25 @@ func GetNodesName(nids []int) map[int]string {
116121
}
117122
return nodes
118123
}
124+
125+
// 更新 AllCategory 数据
126+
func UpdateAllCategory() {
127+
var err error
128+
AllCategory, err = NewResourceCat().FindAll()
129+
if err != nil {
130+
logger.Errorln("获取资源分类数据失败:", err)
131+
return
132+
}
133+
}
134+
135+
// 获得分类名
136+
func GetCategoryName(catid int) string {
137+
catRWMutex.RLock()
138+
defer catRWMutex.RUnlock()
139+
for _, cat := range AllCategory {
140+
if cat.Catid == catid {
141+
return cat.Name
142+
}
143+
}
144+
return ""
145+
}

0 commit comments

Comments
 (0)