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

Skip to content

Commit 34db035

Browse files
committed
资源改版
1 parent 6bfd33e commit 34db035

File tree

24 files changed

+920
-441
lines changed

24 files changed

+920
-441
lines changed

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

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
package controller
88

99
import (
10-
"filter"
1110
"fmt"
11+
"html/template"
12+
"net/http"
13+
"strconv"
14+
15+
"filter"
1216
"github.com/studygolang/mux"
1317
"model"
14-
"net/http"
1518
"service"
1619
"util"
1720
)
@@ -33,9 +36,17 @@ func ResIndexHandler(rw http.ResponseWriter, req *http.Request) {
3336
func CatResourcesHandler(rw http.ResponseWriter, req *http.Request) {
3437
vars := mux.Vars(req)
3538
catid := vars["catid"]
36-
resources := service.FindResourcesByCatid(catid)
39+
40+
page, _ := strconv.Atoi(req.FormValue("p"))
41+
if page == 0 {
42+
page = 1
43+
}
44+
45+
resources, total := service.FindResourcesByCatid(catid, page)
46+
pageHtml := service.GetPageHtml(page, total, req.URL.Path)
47+
3748
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/resources/index.html")
38-
filter.SetData(req, map[string]interface{}{"activeResources": "active", "resources": resources, "categories": service.AllCategory, "curCatid": catid})
49+
filter.SetData(req, map[string]interface{}{"activeResources": "active", "resources": resources, "categories": service.AllCategory, "page": template.HTML(pageHtml), "curCatid": util.MustInt(catid)})
3950
}
4051

4152
// 某个资源详细页
@@ -51,33 +62,67 @@ func ResourceDetailHandler(rw http.ResponseWriter, req *http.Request) {
5162
// uri: /resources/new{json:(|.json)}
5263
func NewResourceHandler(rw http.ResponseWriter, req *http.Request) {
5364
vars := mux.Vars(req)
54-
title := req.FormValue("title")
65+
title := req.PostFormValue("title")
66+
// 请求新建资源页面
5567
if title == "" || req.Method != "POST" || vars["json"] == "" {
5668
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/resources/new.html")
5769
filter.SetData(req, map[string]interface{}{"activeResources": "active", "categories": service.AllCategory})
5870
return
5971
}
72+
6073
errMsg := ""
61-
resForm := req.FormValue("form")
74+
resForm := req.PostFormValue("form")
6275
if resForm == model.LinkForm {
63-
if req.FormValue("url") == "" {
76+
if req.PostFormValue("url") == "" {
6477
errMsg = "url不能为空"
6578
}
6679
} else {
67-
if req.FormValue("content") == "" {
80+
if req.PostFormValue("content") == "" {
6881
errMsg = "内容不能为空"
6982
}
7083
}
7184
if errMsg != "" {
72-
fmt.Fprint(rw, `{"errno": 1, "error":"`+errMsg+`"}`)
85+
fmt.Fprint(rw, `{"ok": 0, "error":"`+errMsg+`"}`)
86+
return
87+
}
88+
89+
user, _ := filter.CurrentUser(req)
90+
err := service.PublishResource(user, req.PostForm)
91+
if err != nil {
92+
fmt.Fprint(rw, `{"ok": 0, "error":"内部服务错误,请稍候再试!"}`)
7393
return
7494
}
95+
96+
fmt.Fprint(rw, `{"ok": 1, "data":""}`)
97+
}
98+
99+
// 修改資源
100+
// uri: /resources/modify{json:(|.json)}
101+
func ModifyResourceHandler(rw http.ResponseWriter, req *http.Request) {
102+
id := req.FormValue("id")
103+
if id == "" {
104+
util.Redirect(rw, req, "/resources")
105+
return
106+
}
107+
108+
vars := mux.Vars(req)
109+
// 请求编辑資源页面
110+
if req.Method != "POST" || vars["json"] == "" {
111+
resource := service.FindResourceById(id)
112+
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/resources/new.html")
113+
filter.SetData(req, map[string]interface{}{"resource": resource, "activeResources": "active", "categories": service.AllCategory})
114+
return
115+
}
116+
75117
user, _ := filter.CurrentUser(req)
76-
// 入库
77-
ok := service.PublishResource(user["uid"].(int), req.Form)
78-
if !ok {
79-
fmt.Fprint(rw, `{"errno": 1, "error":"服务器内部错误,请稍候再试!"}`)
118+
err := service.PublishResource(user, req.PostForm)
119+
if err != nil {
120+
if err == service.NotModifyAuthorityErr {
121+
rw.WriteHeader(http.StatusForbidden)
122+
return
123+
}
124+
fmt.Fprint(rw, `{"ok": 0, "error":"内部服务错误!"}`)
80125
return
81126
}
82-
fmt.Fprint(rw, `{"errno": 0, "data":""}`)
127+
fmt.Fprint(rw, `{"ok": 1, "data":""}`)
83128
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func NewTopicHandler(rw http.ResponseWriter, req *http.Request) {
108108
fmt.Fprint(rw, `{"ok": 0, "error":"内部服务错误!"}`)
109109
return
110110
}
111+
111112
fmt.Fprint(rw, `{"ok": 1, "data":""}`)
112113
}
113114

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,7 @@ var funcMap = template.FuncMap{
4343
t, _ := time.Parse("2006-01-02 15:04:05", ctime)
4444
return t.Format(time.RFC3339) + "+08:00"
4545
},
46-
"substring": func(str string, length int, suffix string) string {
47-
if length >= len(str) {
48-
return str
49-
}
50-
utf8Str := util.NewString(str)
51-
if length > utf8Str.RuneCount() {
52-
return str
53-
}
54-
return utf8Str.Slice(0, length) + suffix
55-
},
46+
"substring": util.Substring,
5647
"add": func(nums ...interface{}) int {
5748
total := 0
5849
for _, num := range nums {
@@ -166,6 +157,19 @@ func (this *ViewFilter) PostFilter(rw http.ResponseWriter, req *http.Request) bo
166157
contentHtmls[i] = config.ROOT + strings.TrimSpace(contentHtml)
167158
}
168159

160+
if !this.isBackView {
161+
// TODO: 旧模板还未完成的页面
162+
if strings.HasPrefix(req.RequestURI, "/wiki") ||
163+
strings.HasPrefix(req.RequestURI, "/user") ||
164+
strings.HasPrefix(req.RequestURI, "/account") {
165+
this.commonHtmlFiles = []string{config.ROOT + "/template/common/base.html"}
166+
this.baseTplName = "base.html"
167+
} else {
168+
this.commonHtmlFiles = []string{config.ROOT + "/template/common/layout.html"}
169+
this.baseTplName = "layout.html"
170+
}
171+
}
172+
169173
// 为了使用自定义的模板函数,首先New一个以第一个模板文件名为模板名。
170174
// 这样,在ParseFiles时,新返回的*Template便还是原来的模板实例
171175
tpl, err := template.New(this.baseTplName).Funcs(funcMap).ParseFiles(append(this.commonHtmlFiles, contentHtmls...)...)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ const (
132132
MsgtypeWikiComment // 评论我的Wiki页
133133
MsgtypeProjectComment // 评论我的项目
134134

135-
MsgtypeAtMe = 10 // @提到我
135+
MsgtypeAtMe = 10 // 评论 @提到我
136+
MsgtypePublishAtMe = 11 // 发布时提到我
136137
)
137138

138139
// 短消息

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ func (this *Resource) FindAll(selectCol ...string) ([]*Resource, error) {
7575
return resourceList, nil
7676
}
7777

78+
// 为了支持连写
79+
func (this *Resource) Set(clause string, args ...interface{}) *Resource {
80+
this.Dao.Set(clause, args...)
81+
return this
82+
}
83+
7884
// 为了支持连写
7985
func (this *Resource) Where(condition string, args ...interface{}) *Resource {
8086
this.Dao.Where(condition, args...)
@@ -117,6 +123,7 @@ type ResourceEx struct {
117123
Id int `json:"id"`
118124
Viewnum int `json:"viewnum"`
119125
Cmtnum int `json:"cmtnum"`
126+
Likenum int `json:"likenum"`
120127
Mtime string `json:"mtime"`
121128

122129
// 数据库访问对象
@@ -186,15 +193,16 @@ func (this *ResourceEx) Order(order string) *ResourceEx {
186193
}
187194

188195
func (this *ResourceEx) prepareInsertData() {
189-
this.columns = []string{"id", "viewnum", "cmtnum"}
190-
this.colValues = []interface{}{this.Id, this.Viewnum, this.Cmtnum}
196+
this.columns = []string{"id", "viewnum", "cmtnum", "likenum"}
197+
this.colValues = []interface{}{this.Id, this.Viewnum, this.Cmtnum, this.Likenum}
191198
}
192199

193200
func (this *ResourceEx) colFieldMap() map[string]interface{} {
194201
return map[string]interface{}{
195202
"id": &this.Id,
196203
"viewnum": &this.Viewnum,
197204
"cmtnum": &this.Cmtnum,
205+
"likenum": &this.Likenum,
198206
"mtime": &this.Mtime,
199207
}
200208
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func initRouter() *mux.Router {
8787
router.HandleFunc("/resources/cat/{catid:[0-9]+}", CatResourcesHandler)
8888
router.HandleFunc("/resources/{id:[0-9]+}", ResourceDetailHandler)
8989
router.HandleFunc("/resources/new{json:(|.json)}", NewResourceHandler).AppendFilterChain(loginFilterChain)
90+
router.HandleFunc("/resources/modify{json:(|.json)}", ModifyResourceHandler).AppendFilterChain(loginFilterChain)
9091

9192
// 评论
9293
router.HandleFunc("/comment/{objid:[0-9]+}.json", CommentHandler).AppendFilterChain(loginFilterChain)

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,18 @@ func SendSysMsgAtUids(uids string, ext map[string]interface{}) bool {
107107
}
108108

109109
// 给被@的用户发系统消息
110+
// ext 中可以指定 msgtype,没有指定,默认为 MsgtypeAtMe
110111
func SendSysMsgAtUsernames(usernames string, ext map[string]interface{}) bool {
111112
if usernames == "" {
112113
return true
113114
}
114115
message := model.NewSystemMessage()
115-
message.Msgtype = model.MsgtypeAtMe
116+
if msgtype, ok := ext["msgtype"]; ok {
117+
message.Msgtype = msgtype.(int)
118+
delete(ext, "msgtype")
119+
} else {
120+
message.Msgtype = model.MsgtypeAtMe
121+
}
116122
message.SetExt(ext)
117123

118124
msg := NewMessage(WsMsgNotify, 1)
@@ -149,6 +155,7 @@ func SendSysMsgAtUsernames(usernames string, ext map[string]interface{}) bool {
149155
// model.MsgtypeTopicReply/MsgtypeResourceComment/MsgtypeWikiComment存放都为:
150156
// {"uid":xxx,"objid":xxx}
151157
// model.MsgtypeAtMe 为:{"uid":xxx,"cid":xxx,"objid":xxx,"objtype":xxx}
158+
// model.MsgtypePulishAtMe 为:{"uid":xxx,"objid":xxx,"objtype":xxx}
152159
func FindSysMsgsByUid(uid string) []map[string]interface{} {
153160
messages, err := model.NewSystemMessage().Where("to=" + uid).Order("ctime DESC").FindAll()
154161
if err != nil {
@@ -183,7 +190,7 @@ func FindSysMsgsByUid(uid string) []map[string]interface{} {
183190
resIds[objid] = objid
184191
case model.MsgtypeWikiComment:
185192
wikiIds[objid] = objid
186-
case model.MsgtypeAtMe:
193+
case model.MsgtypeAtMe, model.MsgtypePublishAtMe:
187194
objTypeFloat := ext["objtype"].(float64)
188195
switch int(objTypeFloat) {
189196
case model.TYPE_TOPIC:
@@ -275,6 +282,43 @@ func FindSysMsgsByUid(uid string) []map[string]interface{} {
275282
objUrl += "#commentForm"
276283
title += "项目:"
277284
}
285+
286+
case model.MsgtypePublishAtMe:
287+
title = "发布"
288+
switch int(ext["objtype"].(float64)) {
289+
case model.TYPE_TOPIC:
290+
topic := topicMap[objid]
291+
objTitle = topic.Title
292+
objUrl = "/topics/" + strconv.Itoa(topic.Tid)
293+
title += "主题"
294+
case model.TYPE_ARTICLE:
295+
article := articleMap[objid]
296+
objTitle = article.Title
297+
objUrl = "/articles/" + strconv.Itoa(article.Id)
298+
title += "博文"
299+
case model.TYPE_RESOURCE:
300+
resource := resourceMap[objid]
301+
objTitle = resource.Title
302+
objUrl = "/resources/" + strconv.Itoa(resource.Id)
303+
title += "资源"
304+
case model.TYPE_WIKI:
305+
wiki := wikiMap[objid]
306+
objTitle = wiki.Title
307+
objUrl = "/wiki/" + strconv.Itoa(wiki.Id)
308+
title += "wiki"
309+
case model.TYPE_PROJECT:
310+
project := projectMap[objid]
311+
objTitle = project.Category + project.Name
312+
objUrl = "/p/"
313+
if project.Uri != "" {
314+
objUrl += project.Uri
315+
} else {
316+
objUrl += strconv.Itoa(project.Id)
317+
}
318+
title += "项目"
319+
}
320+
321+
title += "时提到了你:"
278322
}
279323
tmpMap["objtitle"] = objTitle
280324
tmpMap["objurl"] = objUrl

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import (
1818
)
1919

2020
func PublishProject(user map[string]interface{}, form url.Values) (err error) {
21-
isModify := form.Get("id") != ""
21+
id := form.Get("id")
22+
isModify := id != ""
2223

2324
if !isModify && ProjectUriExists(form.Get("uri")) {
2425
err = errors.New("uri存在")
@@ -28,19 +29,29 @@ func PublishProject(user map[string]interface{}, form url.Values) (err error) {
2829
username := user["username"].(string)
2930

3031
project := model.NewOpenProject()
31-
util.ConvertAssign(project, form)
3232

3333
if isModify {
34-
isAdmin := user["isadmin"].(bool)
34+
err = project.Where("id=?", id).Find()
35+
if err != nil {
36+
logger.Errorln("Publish Project find error:", err)
37+
return
38+
}
39+
isAdmin := false
40+
if _, ok := user["isadmin"]; ok {
41+
isAdmin = user["isadmin"].(bool)
42+
}
3543
if project.Username != username && !isAdmin {
3644
err = NotModifyAuthorityErr
3745
return
3846
}
47+
3948
} else {
4049
project.Username = username
4150
project.Ctime = util.TimeNow()
4251
}
4352

53+
util.ConvertAssign(project, form)
54+
4455
project.Uri = strings.ToLower(project.Uri)
4556

4657
github := "github.com"

0 commit comments

Comments
 (0)