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

Skip to content

Commit 6bfd33e

Browse files
committed
改版:新增主题和编辑主题
1 parent e95252a commit 6bfd33e

File tree

24 files changed

+573
-308
lines changed

24 files changed

+573
-308
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ func ArticleDetailHandler(rw http.ResponseWriter, req *http.Request) {
123123

124124
service.Views.Incr(req, model.TYPE_ARTICLE, article.Id)
125125

126+
// 为了阅读数即时看到
127+
article.Viewnum++
128+
126129
// 设置内容模板
127130
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/articles/detail.html")
128131
// 设置模板数据

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ func ModifyProjectHandler(rw http.ResponseWriter, req *http.Request) {
139139
user, _ := filter.CurrentUser(req)
140140
err := service.PublishProject(user, req.PostForm)
141141
if err != nil {
142+
if err == service.NotModifyAuthorityErr {
143+
rw.WriteHeader(http.StatusForbidden)
144+
return
145+
}
142146
fmt.Fprint(rw, `{"ok": 0, "error":"内部服务错误!"}`)
143147
return
144148
}
@@ -166,6 +170,9 @@ func ProjectDetailHandler(rw http.ResponseWriter, req *http.Request) {
166170

167171
service.Views.Incr(req, model.TYPE_PROJECT, project.Id)
168172

173+
// 为了阅读数即时看到
174+
project.Cmtnum++
175+
169176
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/projects/detail.html")
170177
filter.SetData(req, map[string]interface{}{"activeProjects": "active", "project": project, "likeflag": likeFlag, "hadcollect": hadCollect})
171178
}

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

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,52 @@ func TopicDetailHandler(rw http.ResponseWriter, req *http.Request) {
9494
func NewTopicHandler(rw http.ResponseWriter, req *http.Request) {
9595
nodes := service.GenNodes()
9696
vars := mux.Vars(req)
97-
title := req.FormValue("title")
98-
// 请求新建帖子页面
97+
title := req.PostFormValue("title")
98+
// 请求新建主题页面
9999
if title == "" || req.Method != "POST" || vars["json"] == "" {
100100
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/new.html")
101-
filter.SetData(req, map[string]interface{}{"nodes": nodes})
101+
filter.SetData(req, map[string]interface{}{"nodes": nodes, "activeTopics": "active"})
102102
return
103103
}
104104

105105
user, _ := filter.CurrentUser(req)
106-
// 入库
107-
topic := model.NewTopic()
108-
topic.Uid = user["uid"].(int)
109-
topic.Nid = util.MustInt(req.FormValue("nid"))
110-
topic.Title = req.FormValue("title")
111-
topic.Content = req.FormValue("content")
112-
errMsg, err := service.PublishTopic(topic)
106+
err := service.PublishTopic(user, req.PostForm)
113107
if err != nil {
114-
fmt.Fprint(rw, `{"errno": 1, "error":"`, errMsg, `"}`)
108+
fmt.Fprint(rw, `{"ok": 0, "error":"内部服务错误!"}`)
115109
return
116110
}
117-
fmt.Fprint(rw, `{"errno": 0, "error":""}`)
111+
fmt.Fprint(rw, `{"ok": 1, "data":""}`)
112+
}
113+
114+
// 修改主题
115+
// uri: /topics/modify{json:(|.json)}
116+
func ModifyTopicHandler(rw http.ResponseWriter, req *http.Request) {
117+
tid := req.FormValue("tid")
118+
if tid == "" {
119+
util.Redirect(rw, req, "/topics")
120+
return
121+
}
122+
123+
nodes := service.GenNodes()
124+
125+
vars := mux.Vars(req)
126+
// 请求编辑主题页面
127+
if req.Method != "POST" || vars["json"] == "" {
128+
topic := service.FindTopic(tid)
129+
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/new.html")
130+
filter.SetData(req, map[string]interface{}{"nodes": nodes, "topic": topic, "activeTopics": "active"})
131+
return
132+
}
133+
134+
user, _ := filter.CurrentUser(req)
135+
err := service.PublishTopic(user, req.PostForm)
136+
if err != nil {
137+
if err == service.NotModifyAuthorityErr {
138+
rw.WriteHeader(http.StatusForbidden)
139+
return
140+
}
141+
fmt.Fprint(rw, `{"ok": 0, "error":"内部服务错误!"}`)
142+
return
143+
}
144+
fmt.Fprint(rw, `{"ok": 1, "data":""}`)
118145
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ var funcMap = template.FuncMap{
5353
}
5454
return utf8Str.Slice(0, length) + suffix
5555
},
56-
"add": func(nums ...int) int {
56+
"add": func(nums ...interface{}) int {
5757
total := 0
5858
for _, num := range nums {
59-
total += num
59+
if n, ok := num.(int); ok {
60+
total += n
61+
}
6062
}
6163
return total
6264
},

websites/code/studygolang/src/server/studygolang/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func initRouter() *mux.Router {
3737
router.HandleFunc("/topics{view:(|/popular|/no_reply|/last)}", TopicsHandler)
3838
router.HandleFunc("/topics/{tid:[0-9]+}", TopicDetailHandler)
3939
router.HandleFunc("/topics/new{json:(|.json)}", NewTopicHandler).AppendFilterChain(loginFilterChain)
40-
40+
router.HandleFunc("/topics/modify{json:(|.json)}", ModifyTopicHandler).AppendFilterChain(loginFilterChain)
4141
// 某个节点下的话题
4242
router.HandleFunc("/topics/node{nid:[0-9]+}", NodesHandler)
4343

websites/code/studygolang/src/service/project.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func PublishProject(user map[string]interface{}, form url.Values) (err error) {
3333
if isModify {
3434
isAdmin := user["isadmin"].(bool)
3535
if project.Username != username && !isAdmin {
36-
err = errors.New("没有修改权限")
36+
err = NotModifyAuthorityErr
3737
return
3838
}
3939
} else {
@@ -59,6 +59,15 @@ func PublishProject(user map[string]interface{}, form url.Values) (err error) {
5959
logger.Errorln("Publish Project error:", err)
6060
}
6161

62+
// 发布項目,活跃度+10
63+
if uid, ok := user["uid"].(int); ok {
64+
weight := 10
65+
if isModify {
66+
weight = 2
67+
}
68+
go IncUserWeight("uid="+strconv.Itoa(uid), weight)
69+
}
70+
6271
return
6372
}
6473

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

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,66 @@ package service
99
import (
1010
"errors"
1111
"html/template"
12-
"logger"
13-
"model"
1412
"net/url"
1513
"strconv"
1614
"strings"
17-
"time"
15+
16+
"logger"
17+
"model"
1818
"util"
1919
)
2020

21+
var NotModifyAuthorityErr = errors.New("没有修改权限")
22+
2123
// 发布帖子。入topics和topics_ex库
22-
func PublishTopic(topic *model.Topic) (errMsg string, err error) {
23-
topic.Ctime = time.Now().Format("2006-01-02 15:04:05")
24-
tid, err := topic.Insert()
25-
if err != nil {
26-
errMsg = "内部服务器错误"
27-
logger.Errorln(errMsg, ":", err)
28-
return
29-
}
24+
func PublishTopic(user map[string]interface{}, form url.Values) (err error) {
25+
uid := user["uid"].(int)
3026

31-
// 存扩展信息
32-
topicEx := model.NewTopicEx()
33-
topicEx.Tid = tid
34-
_, err = topicEx.Insert()
35-
if err != nil {
36-
errMsg = "内部服务器错误"
37-
logger.Errorln(errMsg, ":", err)
38-
return
39-
}
27+
if form.Get("tid") != "" {
28+
isAdmin := user["isadmin"].(bool)
29+
if topic.Uid != uid && !isAdmin {
30+
err = NotModifyAuthorityErr
31+
return
32+
}
33+
34+
_, err = ModifyTopic(user, form)
35+
if err != nil {
36+
logger.Errorln("Publish Topic error:", err)
37+
return
38+
}
39+
} else {
40+
41+
topic := model.NewTopic()
42+
util.ConvertAssign(topic, form)
43+
44+
topic.Uid = uid
45+
topic.Ctime = util.TimeNow()
4046

41-
// 发布帖子,活跃度+10
42-
go IncUserWeight("uid="+strconv.Itoa(topic.Uid), 10)
47+
var tid int
48+
tid, err = topic.Insert()
49+
50+
if err != nil {
51+
logger.Errorln("Publish Topic error:", err)
52+
return
53+
}
54+
55+
// 存扩展信息
56+
topicEx := model.NewTopicEx()
57+
topicEx.Tid = tid
58+
_, err = topicEx.Insert()
59+
if err != nil {
60+
logger.Errorln("Insert TopicEx error:", err)
61+
return
62+
}
63+
64+
// 发布主题,活跃度+10
65+
go IncUserWeight("uid="+strconv.Itoa(uid), 10)
66+
}
4367

4468
return
4569
}
4670

47-
// 修改帖子
71+
// 修改主题
4872
// user 修改人的(有可能是作者或管理员)
4973
func ModifyTopic(user map[string]interface{}, form url.Values) (errMsg string, err error) {
5074
uid := user["uid"].(int)
@@ -63,7 +87,7 @@ func ModifyTopic(user map[string]interface{}, form url.Values) (errMsg string, e
6387
}
6488

6589
username := user["username"].(string)
66-
// 修改帖子,活跃度+2
90+
// 修改主题,活跃度+2
6791
go IncUserWeight("username="+username, 2)
6892

6993
return
@@ -115,6 +139,17 @@ func FindTopicByTid(tid string) (topicMap map[string]interface{}, replies []map[
115139
return
116140
}
117141

142+
// 获取单个 Topic 信息(用于编辑)
143+
func FindTopic(tid string) *model.Topic {
144+
topic := model.NewTopic()
145+
err := topic.Where("tid=?", tid).Find()
146+
if err != nil {
147+
logger.Errorf("FindTopic [%s] error:%s\n", tid, err)
148+
}
149+
150+
return topic
151+
}
152+
118153
// 通过tid获得话题的所有者
119154
func getTopicOwner(tid int) int {
120155
// 帖子信息

websites/code/studygolang/static/css/main.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,6 @@ label.error {color:red;}
227227
.emoji {width:20px;height:20px;vertical-align: middle;}
228228

229229
.img-rounded {-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;}
230+
231+
/*form required flag*/
232+
.control-label abbr {color: #c00;}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
form .md-toolbar ul { margin-bottom:2px;}
2+
form .md-toolbar ul a { -moz-border-radius: 8px;-webkit-border-radius: 8px;border-radius: 8px;padding: 0 5px;line-height: 18px;font-size: 12px;margin-right: 6px;text-shadow: 0;color: #444;border: 1px solid #fff;}
3+
form .md-toolbar ul a:hover { text-decoration: none;}
4+
form .md-toolbar ul .cur a { background: #fff;border: 1px solid #ddd;color: #666;}
5+
form .md-toolbar .upload-img { cursor: pointer;}
6+
form .content-preview { margin-bottom: 5px; width: 100%;height: 200px;border: 1px solid #CCCCCC;border-radius: 3px 3px 3px 3px;-moz-border-radius: 3px 3px 3px 3px;display: none;padding: 4px;overflow: scroll; display: none; }

websites/code/studygolang/static/js/comment.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
$('.page-comment .content-preview').show();
4545
});
4646

47-
$('.markdown_tip').html(marked($('.markdown_tip').text()));
48-
4947
emojify.setConfig({
5048
// emojify_tag_type : 'span',
5149
only_crawl_id : null,
@@ -96,8 +94,18 @@
9694
$('.page-comment .words').removeClass('hide');
9795
}
9896

97+
// emoji 表情解析
9998
emojify.run($('.page-comment .words ul').get(0));
100-
99+
// twitter emoji 表情解析
100+
/*
101+
var result = twemoji.parse($('.page-comment .words ul').get(0), {
102+
callback: function(icon, options, variant) {
103+
return ''.concat(options.base, options.size, '/', icon, options.ext);
104+
},
105+
size: 16
106+
});
107+
*/
108+
101109
registerAtEvent();
102110
} else {
103111
comTip("评论加载失败");
@@ -240,7 +248,7 @@
240248
var objid = $('.page-comment').data('objid'),
241249
objtype = $('.page-comment').data('objtype');
242250

243-
var usernames = analyzeAt(content);
251+
var usernames = SG.analyzeAt(content);
244252

245253
$.ajax({
246254
type:"post",
@@ -264,8 +272,14 @@
264272
$('.page-comment .words ul').append(oneCmt);
265273
$('.page-comment .words').removeClass('hide');
266274

267-
// 解析表情
275+
// emoji 表情解析
268276
emojify.run($('.page-comment .words ul li:last').get(0));
277+
278+
// twitter emoji 表情解析
279+
/*
280+
twemoji.parse($('.page-comment .words ul li:last').get(0));
281+
*/
282+
269283
// 注册@
270284
registerAtEvent();
271285

@@ -303,20 +317,12 @@
303317
at: ":",
304318
data: window.emojis,
305319
tpl:"<li data-value='${key}'><img src='http://www.emoji-cheat-sheet.com/graphics/emojis/${name}.png' height='20' width='20' /> ${name}</li>"
306-
});
320+
})/*.atwho({
321+
at: "\\",
322+
data: window.twemojis,
323+
tpl:"<li data-value='${name}'><img src='https://twemoji.maxcdn.com/16x16/${key}.png' height='16' width='16' /> ${name}</li>"
324+
})*/;
307325
}
308326
}
309-
310-
// 分析 @ 的用户
311-
var analyzeAt = function(text) {
312-
var usernames = [];
313-
314-
String(text).replace(/[^@]*@([^\s@]{4,20})\s*/g, function (match, username) {
315-
usernames.push(username);
316-
});
317-
318-
return usernames;
319-
}
320-
321327
});
322328
}).call(this)

0 commit comments

Comments
 (0)