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

Skip to content

Commit e8062f0

Browse files
committed
Merge branch 'app'
2 parents 6469368 + 89ffab8 commit e8062f0

File tree

20 files changed

+823
-35
lines changed

20 files changed

+823
-35
lines changed

src/http/controller/account.go

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package controller
88

99
import (
1010
"html/template"
11+
. "http/internal/helper"
1112
"http/middleware"
1213
"logic"
1314
"model"
@@ -43,9 +44,6 @@ func (self AccountController) RegisterRoute(g *echo.Group) {
4344
g.Get("/account/logout", self.Logout, middleware.NeedLogin())
4445
}
4546

46-
// 保存uuid和email的对应关系(TODO:重启如何处理,有效期问题)
47-
var regActivateCodeMap = map[string]string{}
48-
4947
func (self AccountController) Register(ctx echo.Context) error {
5048
if _, ok := ctx.Get("user").(*model.Me); ok {
5149
return ctx.Redirect(http.StatusSeeOther, "/")
@@ -92,7 +90,8 @@ func (self AccountController) Register(ctx echo.Context) error {
9290
}
9391

9492
email := ctx.FormValue("email")
95-
uuid := self.genUUID(email)
93+
94+
uuid := RegActivateCode.GenUUID(email)
9695
var emailUrl string
9796
if strings.HasSuffix(email, "@gmail.com") {
9897
emailUrl = "http://mail.google.com"
@@ -114,24 +113,11 @@ func (self AccountController) Register(ctx echo.Context) error {
114113
return render(ctx, registerTpl, data)
115114
}
116115

117-
func (AccountController) genUUID(email string) string {
118-
var uuid string
119-
for {
120-
uuid = guuid.NewV4().String()
121-
if _, ok := regActivateCodeMap[uuid]; !ok {
122-
regActivateCodeMap[uuid] = email
123-
break
124-
}
125-
logger.Errorln("GenUUID 冲突....")
126-
}
127-
return uuid
128-
}
129-
130116
// SendActivateEmail 发送注册激活邮件
131117
func (self AccountController) SendActivateEmail(ctx echo.Context) error {
132118
uuid := ctx.FormValue("uuid")
133119
if uuid != "" {
134-
email, ok := regActivateCodeMap[uuid]
120+
email, ok := RegActivateCode.GetEmail(uuid)
135121
if !ok {
136122
return fail(ctx, 1, "非法请求")
137123
}
@@ -143,7 +129,7 @@ func (self AccountController) SendActivateEmail(ctx echo.Context) error {
143129
return fail(ctx, 1, "非法请求")
144130
}
145131

146-
go logic.DefaultEmail.SendActivateMail(user.Email, self.genUUID(user.Email))
132+
go logic.DefaultEmail.SendActivateMail(user.Email, RegActivateCode.GenUUID(user.Email))
147133
}
148134

149135
return success(ctx, nil)
@@ -165,14 +151,14 @@ func (AccountController) Activate(ctx echo.Context) error {
165151
uuid := values.Get("uuid")
166152
timestamp := goutils.MustInt64(values.Get("timestamp"))
167153
sign := values.Get("sign")
168-
email, ok := regActivateCodeMap[uuid]
154+
email, ok := RegActivateCode.GetEmail(uuid)
169155
if !ok {
170156
data["error"] = "非法请求!"
171157
return render(ctx, contentTpl, data)
172158
}
173159

174160
if timestamp < time.Now().Add(-4*time.Hour).Unix() {
175-
delete(regActivateCodeMap, uuid)
161+
RegActivateCode.DelUUID(uuid)
176162
// TODO:可以再次发激活邮件?
177163
data["error"] = "链接已过期"
178164
return render(ctx, contentTpl, data)
@@ -184,7 +170,7 @@ func (AccountController) Activate(ctx echo.Context) error {
184170
return render(ctx, contentTpl, data)
185171
}
186172

187-
delete(regActivateCodeMap, uuid)
173+
RegActivateCode.DelUUID(uuid)
188174

189175
// 自动登录
190176
SetCookie(ctx, user.Username)
@@ -257,7 +243,7 @@ func (self AccountController) Edit(ctx echo.Context) error {
257243

258244
email := ctx.FormValue("email")
259245
if me.Email != email {
260-
go logic.DefaultEmail.SendActivateMail(email, self.genUUID(email))
246+
go logic.DefaultEmail.SendActivateMail(email, RegActivateCode.GenUUID(email))
261247
}
262248

263249
return success(ctx, nil)

src/http/controller/app/article.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright 2017 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 app
8+
9+
import (
10+
"logic"
11+
"model"
12+
13+
. "http"
14+
15+
"github.com/labstack/echo"
16+
"github.com/polaris1119/goutils"
17+
)
18+
19+
type ArticleController struct{}
20+
21+
// 注册路由
22+
func (this *ArticleController) RegisterRoute(g *echo.Group) {
23+
g.Get("/articles", this.ReadList)
24+
g.Get("/article/detail", this.Detail)
25+
}
26+
27+
// ReadList 网友文章列表页
28+
func (ArticleController) ReadList(ctx echo.Context) error {
29+
limit := 20
30+
31+
lastId := goutils.MustInt(ctx.QueryParam("base"))
32+
articles := logic.DefaultArticle.FindBy(ctx, limit+5, lastId)
33+
if articles == nil {
34+
return fail(ctx, "获取失败")
35+
}
36+
37+
articleList := make([]map[string]interface{}, 0, len(articles))
38+
for _, article := range articles {
39+
if lastId > 0 {
40+
if article.Top == 1 {
41+
continue
42+
}
43+
}
44+
articleList = append(articleList, map[string]interface{}{
45+
"id": article.Id,
46+
"name": article.Name,
47+
"title": article.Title,
48+
"pub_date": article.PubDate,
49+
"tags": article.Tags,
50+
"viewnum": article.Viewnum,
51+
"cmtnum": article.Cmtnum,
52+
"likenum": article.Likenum,
53+
"top": article.Top,
54+
"author": article.AuthorTxt,
55+
})
56+
}
57+
58+
hasMore := false
59+
if len(articleList) > limit {
60+
hasMore = true
61+
articleList = articleList[:limit]
62+
}
63+
64+
data := map[string]interface{}{
65+
"has_more": hasMore,
66+
"articles": articleList,
67+
}
68+
69+
return success(ctx, data)
70+
}
71+
72+
// Detail 文章详细页
73+
func (ArticleController) Detail(ctx echo.Context) error {
74+
article, prevNext, err := logic.DefaultArticle.FindByIdAndPreNext(ctx, goutils.MustInt(ctx.QueryParam("id")))
75+
if err != nil {
76+
return fail(ctx, err.Error())
77+
}
78+
79+
if article == nil || article.Id == 0 || article.Status == model.ArticleStatusOffline {
80+
return success(ctx, map[string]interface{}{"article": map[string]interface{}{"id": 0}})
81+
}
82+
83+
logic.Views.Incr(Request(ctx), model.TypeArticle, article.Id)
84+
85+
// 为了阅读数即时看到
86+
article.Viewnum++
87+
88+
article.Txt = ""
89+
data := map[string]interface{}{
90+
"article": article,
91+
}
92+
93+
// TODO: 暂时不用
94+
_ = prevNext
95+
return success(ctx, data)
96+
}

src/http/controller/app/base.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2016 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 app
8+
9+
import (
10+
"encoding/json"
11+
"logic"
12+
"net/http"
13+
14+
"github.com/labstack/echo"
15+
"github.com/polaris1119/logger"
16+
"github.com/polaris1119/nosql"
17+
18+
. "http"
19+
)
20+
21+
func getLogger(ctx echo.Context) *logger.Logger {
22+
return logic.GetLogger(ctx)
23+
}
24+
25+
func success(ctx echo.Context, data interface{}) error {
26+
result := map[string]interface{}{
27+
"code": 0,
28+
"msg": "操作成功",
29+
"data": data,
30+
}
31+
32+
b, err := json.Marshal(result)
33+
if err != nil {
34+
return err
35+
}
36+
37+
go func(b []byte) {
38+
if cacheKey := ctx.Get(nosql.CacheKey); cacheKey != nil {
39+
nosql.DefaultLRUCache.CompressAndAdd(cacheKey, b, nosql.NewCacheData())
40+
}
41+
}(b)
42+
43+
AccessControl(ctx)
44+
45+
if ctx.Response().Committed() {
46+
getLogger(ctx).Flush()
47+
return nil
48+
}
49+
50+
return ctx.JSONBlob(http.StatusOK, b)
51+
}
52+
53+
func fail(ctx echo.Context, msg string, codes ...int) error {
54+
AccessControl(ctx)
55+
56+
if ctx.Response().Committed() {
57+
getLogger(ctx).Flush()
58+
return nil
59+
}
60+
61+
code := 1
62+
if len(codes) > 0 {
63+
code = codes[0]
64+
}
65+
result := map[string]interface{}{
66+
"code": code,
67+
"msg": msg,
68+
}
69+
70+
getLogger(ctx).Errorln("operate fail:", result)
71+
72+
return ctx.JSON(http.StatusOK, result)
73+
}

src/http/controller/app/doc.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright 2014 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+
// app Go 语言中文网 APP 接口
8+
package app

src/http/controller/app/project.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2017 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 app
8+
9+
import (
10+
"logic"
11+
12+
"github.com/labstack/echo"
13+
"github.com/polaris1119/goutils"
14+
15+
. "http"
16+
"model"
17+
)
18+
19+
type ProjectController struct{}
20+
21+
// 注册路由
22+
func (self ProjectController) RegisterRoute(g *echo.Group) {
23+
g.GET("/projects", self.ReadList)
24+
g.GET("/project/detail", self.Detail)
25+
}
26+
27+
// ReadList 开源项目列表页
28+
func (ProjectController) ReadList(ctx echo.Context) error {
29+
limit := 20
30+
31+
lastId := goutils.MustInt(ctx.QueryParam("base"))
32+
projects := logic.DefaultProject.FindBy(ctx, limit+5, lastId)
33+
if projects == nil {
34+
return fail(ctx, "获取失败")
35+
}
36+
37+
projectList := make([]map[string]interface{}, 0, len(projects))
38+
for _, project := range projects {
39+
if lastId > 0 {
40+
// TODO: 推荐?
41+
// if project.Top == 1 {
42+
// continue
43+
// }
44+
}
45+
projectList = append(projectList, map[string]interface{}{
46+
"id": project.Id,
47+
"name": project.Name,
48+
"category": project.Category,
49+
"logo": project.Logo,
50+
"tags": project.Tags,
51+
"viewnum": project.Viewnum,
52+
"cmtnum": project.Cmtnum,
53+
"likenum": project.Likenum,
54+
"author": project.Author,
55+
"ctime": project.Ctime,
56+
})
57+
}
58+
59+
hasMore := false
60+
if len(projectList) > limit {
61+
hasMore = true
62+
projectList = projectList[:limit]
63+
}
64+
65+
data := map[string]interface{}{
66+
"has_more": hasMore,
67+
"projects": projectList,
68+
}
69+
70+
return success(ctx, data)
71+
}
72+
73+
// Detail 项目详情
74+
func (ProjectController) Detail(ctx echo.Context) error {
75+
id := goutils.MustInt(ctx.QueryParam("id"))
76+
project := logic.DefaultProject.FindOne(ctx, id)
77+
if project == nil || project.Id == 0 {
78+
return fail(ctx, "获取失败或已下线")
79+
}
80+
81+
logic.Views.Incr(Request(ctx), model.TypeProject, project.Id)
82+
83+
// 为了阅读数即时看到
84+
project.Viewnum++
85+
86+
return success(ctx, map[string]interface{}{"project": project})
87+
}

0 commit comments

Comments
 (0)