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

Skip to content

Commit 4086363

Browse files
committed
支持特定节点不在首页显示
1 parent 3fa7710 commit 4086363

File tree

15 files changed

+247
-36
lines changed

15 files changed

+247
-36
lines changed

config/db.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ CREATE TABLE IF NOT EXISTS `topics_node` (
5959
`ename` varchar(15) NOT NULL DEFAULT '' COMMENT '节点英文名,用于导航',
6060
`intro` varchar(127) NOT NULL DEFAULT '' COMMENT '节点简介',
6161
`seq` smallint unsigned NOT NULL DEFAULT 0 COMMENT '节点排序,小的在前',
62+
`show_index` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT '主题是否在首页显示',
6263
`ctime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
6364
PRIMARY KEY (`nid`),
6465
INDEX `idx_ename` (`ename`)

config/init.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ VALUES
5151
(38, '用户修改', 1, 12, '/admin/user/user/modify', '', '2015-07-14 13:53:53', '2015-07-14 13:53:53'),
5252
(39, '设置', 0, 0, '', '', '2017-05-21 16:03:00', '2017-05-21 16:03:59'),
5353
(40, '常规', 39, 0, '/admin/setting/genneral/modify', '', '2017-05-21 16:05:00', '2017-05-21 16:05:46'),
54-
(41, '导航', 39, 0, '/admin/setting/nav/modify', '', '2017-05-21 18:01:00', '2017-05-21 18:01:16');
54+
(41, '导航', 39, 0, '/admin/setting/nav/modify', '', '2017-05-21 18:01:00', '2017-05-21 18:01:16'),
55+
(42, '节点管理', 15, 0, '/admin/community/node/list', 'polaris', '2017-09-01 22:23:08', '2017-09-01 23:10:38'),
56+
(43, '编辑/新增节点', 15, 42, '/admin/community/node/modify', 'polaris', '2017-09-01 22:23:08', '2017-09-01 23:11:09');
57+
5558

5659
INSERT INTO `website_setting` (`id`, `name`, `domain`, `title_suffix`, `favicon`, `logo`, `start_year`, `blog_url`, `reading_menu`, `docs_menu`, `slogan`, `beian`, `friends_logo`, `footer_nav`, `project_df_logo`, `index_nav`, `created_at`, `updated_at`)
5760
VALUES

src/http/controller/topic.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (TopicController) Detail(ctx echo.Context) error {
171171

172172
// Create 新建主题
173173
func (TopicController) Create(ctx echo.Context) error {
174-
nid := goutils.MustInt(ctx.QueryParam("nid"))
174+
nid := goutils.MustInt(ctx.FormValue("nid"))
175175

176176
title := ctx.FormValue("title")
177177
// 请求新建主题页面
@@ -197,6 +197,10 @@ func (TopicController) Create(ctx echo.Context) error {
197197
return render(ctx, "topics/new.html", data)
198198
}
199199

200+
if nid == 0 {
201+
return fail(ctx, 1, "没有选择节点!")
202+
}
203+
200204
me := ctx.Get("user").(*model.Me)
201205
tid, err := logic.DefaultTopic.Publish(ctx, me, ctx.FormParams())
202206
if err != nil {
@@ -213,8 +217,6 @@ func (TopicController) Modify(ctx echo.Context) error {
213217
return ctx.Redirect(http.StatusSeeOther, "/topics")
214218
}
215219

216-
nodes := logic.GenNodes()
217-
218220
if ctx.Request().Method() != "POST" {
219221
topics := logic.DefaultTopic.FindByTids([]int{tid})
220222
if len(topics) == 0 {
@@ -223,12 +225,24 @@ func (TopicController) Modify(ctx echo.Context) error {
223225

224226
hotNodes := logic.DefaultTopic.FindHotNodes(ctx)
225227

226-
return render(ctx, "topics/new.html", map[string]interface{}{
227-
"nodes": nodes,
228+
data := map[string]interface{}{
228229
"topic": topics[0],
229230
"activeTopics": "active",
230231
"tab_list": hotNodes,
231-
})
232+
}
233+
234+
hadRecommend := false
235+
if len(logic.AllRecommendNodes) > 0 {
236+
hadRecommend = true
237+
238+
data["nodes"] = logic.DefaultNode.FindAll(ctx)
239+
} else {
240+
data["nodes"] = logic.GenNodes()
241+
}
242+
243+
data["had_recommend"] = hadRecommend
244+
245+
return render(ctx, "topics/new.html", data)
232246
}
233247

234248
me := ctx.Get("user").(*model.Me)

src/logic/data.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ func LoadNodes() error {
161161
nodeMap["name"] = node.Name
162162
nodeMap["ename"] = node.Ename
163163
nodeMap["intro"] = node.Intro
164+
nodeMap["show_index"] = node.ShowIndex
164165
nodeMap["ctime"] = node.Ctime
165166
AllNode[i] = nodeMap
166167
}
@@ -282,12 +283,13 @@ func GetNode(nid int) map[string]interface{} {
282283
if len(AllRecommendNodes) > 0 {
283284
node := DefaultNode.FindOne(nid)
284285
return map[string]interface{}{
285-
"ename": node.Ename,
286-
"pid": node.Parent,
287-
"name": node.Name,
288-
"nid": node.Nid,
289-
"logo": node.Logo,
290-
"intro": node.Intro,
286+
"ename": node.Ename,
287+
"pid": node.Parent,
288+
"name": node.Name,
289+
"nid": node.Nid,
290+
"logo": node.Logo,
291+
"intro": node.Intro,
292+
"show_index": node.ShowIndex,
291293
}
292294
}
293295

@@ -314,9 +316,10 @@ func GetNodesByNids(nids []int) map[int]*model.TopicNode {
314316
for _, node := range AllNode {
315317
if node["nid"].(int) == nid {
316318
nodes[nid] = &model.TopicNode{
317-
Nid: nid,
318-
Name: node["name"].(string),
319-
Ename: node["ename"].(string),
319+
Nid: nid,
320+
Name: node["name"].(string),
321+
Ename: node["ename"].(string),
322+
ShowIndex: node["show_index"].(bool),
320323
}
321324
}
322325
}

src/logic/feed.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"model"
1212
"strconv"
1313
"time"
14+
"util"
1415

1516
. "db"
1617

@@ -51,6 +52,7 @@ func (FeedLogic) fillOtherInfo(ctx context.Context, feeds []*model.Feed, filterT
5152
newFeeds := make([]*model.Feed, 0, len(feeds))
5253

5354
uidSet := set.New(set.NonThreadSafe)
55+
nidSet := set.New(set.NonThreadSafe)
5456
for _, feed := range feeds {
5557
if feed.State == model.FeedOffline {
5658
continue
@@ -69,7 +71,7 @@ func (FeedLogic) fillOtherInfo(ctx context.Context, feeds []*model.Feed, filterT
6971
uidSet.Add(feed.Lastreplyuid)
7072
}
7173
if feed.Objtype == model.TypeTopic {
72-
feed.Node = GetNode(feed.Nid)
74+
nidSet.Add(feed.Nid)
7375
} else if feed.Objtype == model.TypeResource {
7476
feed.Node = map[string]interface{}{
7577
"name": GetCategoryName(feed.Nid),
@@ -80,13 +82,21 @@ func (FeedLogic) fillOtherInfo(ctx context.Context, feeds []*model.Feed, filterT
8082
}
8183

8284
usersMap := DefaultUser.FindUserInfos(ctx, set.IntSlice(uidSet))
85+
nodesMap := GetNodesByNids(set.IntSlice(nidSet))
8386
for _, feed := range newFeeds {
8487
if _, ok := usersMap[feed.Uid]; ok {
8588
feed.User = usersMap[feed.Uid]
8689
}
8790
if _, ok := usersMap[feed.Lastreplyuid]; ok {
8891
feed.Lastreplyuser = usersMap[feed.Lastreplyuid]
8992
}
93+
94+
if feed.Objtype == model.TypeTopic {
95+
if _, ok := nodesMap[feed.Nid]; ok {
96+
feed.Node = map[string]interface{}{}
97+
util.Struct2Map(feed.Node, nodesMap[feed.Nid])
98+
}
99+
}
90100
}
91101

92102
return newFeeds
@@ -110,9 +120,16 @@ func (FeedLogic) updateComment(objid, objtype, uid int, cmttime time.Time) {
110120

111121
func (self FeedLogic) modifyTopicNode(tid, nid int) {
112122
go func() {
123+
change := map[string]interface{}{
124+
"nid": nid,
125+
}
126+
127+
node := &model.TopicNode{}
128+
_, err := MasterDB.Id(nid).Get(node)
129+
if err == nil && !node.ShowIndex {
130+
change["state"] = model.FeedOffline
131+
}
113132
MasterDB.Table(new(model.Feed)).Where("objid=? AND objtype=?", tid, model.TypeTopic).
114-
Update(map[string]interface{}{
115-
"nid": nid,
116-
})
133+
Update(change)
117134
}()
118135
}

src/logic/topic.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,29 +382,47 @@ func (TopicLogic) FindHotNodes(ctx context.Context) []map[string]interface{} {
382382

383383
objLog := GetLogger(ctx)
384384

385+
hotNum := 10
386+
385387
lastWeek := time.Now().Add(-7 * 24 * time.Hour).Format("2006-01-02 15:04:05")
386-
strSql := fmt.Sprintf("SELECT nid, COUNT(1) AS topicnum FROM topics WHERE ctime>='%s' GROUP BY nid ORDER BY topicnum DESC LIMIT 10", lastWeek)
388+
strSql := fmt.Sprintf("SELECT nid, COUNT(1) AS topicnum FROM topics WHERE ctime>='%s' GROUP BY nid ORDER BY topicnum DESC LIMIT 15", lastWeek)
387389
rows, err := MasterDB.DB().DB.Query(strSql)
388390
if err != nil {
389391
objLog.Errorln("TopicLogic FindHotNodes error:", err)
390392
return nil
391393
}
392-
nodes := make([]map[string]interface{}, 0, 10)
394+
395+
nids := make([]int, 0, hotNum)
393396
for rows.Next() {
394397
var nid, topicnum int
395398
err = rows.Scan(&nid, &topicnum)
396399
if err != nil {
397400
objLog.Errorln("rows.Scan error:", err)
398401
continue
399402
}
400-
nodeInfo := GetNode(nid)
403+
404+
nids = append(nids, nid)
405+
}
406+
407+
nodes := make([]map[string]interface{}, 0, hotNum)
408+
409+
topicNodes := GetNodesByNids(nids)
410+
for _, topicNode := range topicNodes {
411+
if !topicNode.ShowIndex {
412+
continue
413+
}
414+
401415
node := map[string]interface{}{
402-
"name": nodeInfo["name"].(string),
403-
"ename": nodeInfo["ename"].(string),
404-
"nid": nid,
416+
"name": topicNode.Name,
417+
"ename": topicNode.Ename,
418+
"nid": topicNode.Nid,
405419
}
406420
nodes = append(nodes, node)
421+
if len(nodes) == hotNum {
422+
break
423+
}
407424
}
425+
408426
hotNodesCache = nodes
409427
hotNodesBegin = time.Now()
410428

src/logic/topic_node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (self TopicNodeLogic) Modify(ctx context.Context, form url.Values) error {
9292

9393
change := make(map[string]interface{})
9494

95-
fields := []string{"parent", "logo", "name", "ename", "intro", "seq"}
95+
fields := []string{"parent", "logo", "name", "ename", "intro", "seq", "show_index"}
9696
for _, field := range fields {
9797
change[field] = form.Get(field)
9898
}

src/logic/user_rich.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ func (self UserRichLogic) IncrUserRich(user *model.User, typ, award int, desc st
125125
}
126126

127127
user.Balance += initialAward + award
128+
if user.Balance < 0 {
129+
user.Balance = 0
130+
}
128131
_, err = session.Where("uid=?", user.Uid).Cols("balance").Update(user)
129132
if err != nil {
130133
logger.Errorln("IncrUserRich update error:", err)

src/model/feed.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ func PublishFeed(object interface{}, objectExt interface{}) {
4242
var feed *Feed
4343
switch objdoc := object.(type) {
4444
case *Topic:
45+
node := &TopicNode{}
46+
_, err := db.MasterDB.Id(objdoc.Nid).Get(node)
47+
if err == nil && !node.ShowIndex {
48+
return
49+
}
50+
4551
cmtnum := 0
4652
if objectExt != nil {
4753
// 传递过来的是一个 *TopicEx 对象,类型是有的,即时值是 nil,这里也和 nil 是不等

src/model/mission.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const (
2323
MissionTypeWiki = 55
2424
MissionTypeProject = 56
2525
MissionTypeBook = 57
26+
27+
MissionTypeModify = 65
2628
// 被回复
2729
MissionTypeReplied = 70
2830
// 额外奖励

src/model/topic.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,15 @@ func (*TopicInfo) TableName() string {
8484

8585
// 社区主题节点信息
8686
type TopicNode struct {
87-
Nid int `json:"nid" xorm:"pk autoincr"`
88-
Parent int `json:"parent"`
89-
Logo string `json:"logo"`
90-
Name string `json:"name"`
91-
Ename string `json:"ename"`
92-
Seq int `json:"seq"`
93-
Intro string `json:"intro"`
94-
Ctime time.Time `json:"ctime" xorm:"<-"`
87+
Nid int `json:"nid" xorm:"pk autoincr"`
88+
Parent int `json:"parent"`
89+
Logo string `json:"logo"`
90+
Name string `json:"name"`
91+
Ename string `json:"ename"`
92+
Seq int `json:"seq"`
93+
Intro string `json:"intro"`
94+
ShowIndex bool `json:"show_index"`
95+
Ctime time.Time `json:"ctime" xorm:"<-"`
9596

9697
Level int `json:"-" xorm:"-"`
9798
}

template/admin/topic/node.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ <h1 class="pagetitle">主题节点</h1>
1818
<thead class="center">
1919
<tr>
2020
<td width="10%">节点名称</td>
21-
<td width="5%">Logo</td>
21+
<td width="4%">Logo</td>
2222
<td width="15%">简介</td>
23+
<td width="5%">是否首页显示</td>
2324
<td width="5%">排序</td>
2425
<td width="5%">操作</td>
2526
</tr>
@@ -35,6 +36,7 @@ <h1 class="pagetitle">主题节点</h1>
3536
</td>
3637
<td><img src="{{.Logo}}" width="40px"></td>
3738
<td style="text-align: left;">{{.Intro}}</td>
39+
<td>{{.ShowIndex}}</td>
3840
<td class="edit" style="padding: 0px;">
3941
<input type="text" size="8" class="seq" data-id="{{.Nid}}" style="line-height: 16px;height: 16px; font-size: 16p;" name="seq" value="{{.Seq}}" />
4042
</td>

template/admin/topic/node_modify.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ <h1 class="pagetitle">{{if .node.Nid}}修改{{else}}新增{{end}}主题节点</h
4949
</span>
5050
</p>
5151
</div>
52+
<div>
53+
<p>
54+
<label for="show_index">是否在首页显示</label>
55+
<span class="field">
56+
<select id="show_index" name="show_index" class="uniformselect">
57+
<option value="0" {{if not .node.ShowIndex}}selected{{end}}></option>
58+
<option value="1" {{if .node.ShowIndex}}selected{{end}}></option>
59+
</select>
60+
</span>
61+
</p>
62+
</div>
5263
<div>
5364
<p>
5465
<label for="parent">父节点</label>

template/topics/common_list.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<div class="box_white">
22
<dl class="topics dl-horizontal">
33
{{range .topics}}
4+
{{if or ($.node) (and (not $.node) .node.ShowIndex)}}
45
<div class="topic">
56
<dt class="avatar">
67
<a href="/user/{{.user.Username}}" title="{{.user.Username}}"><img alt="{{.user.Username}}" class="img-rounded" src="{{gravatar .user.Avatar .user.Email 48 $.is_https}}" width="48px" height="48px"></a>
@@ -37,6 +38,7 @@
3738
</div>
3839
</dd>
3940
</div>
41+
{{end}}
4042
{{else}}
4143
<p class="text-center">暂时没有任何主题</p>
4244
{{end}}

0 commit comments

Comments
 (0)