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

Skip to content

Commit ce52744

Browse files
committed
注册激活;支持修改email
1 parent 536e9a6 commit ce52744

File tree

11 files changed

+127
-55
lines changed

11 files changed

+127
-55
lines changed

websites/code2/studygolang/src/http/controller/account.go

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (self AccountController) RegisterRoute(e *echo.Group) {
4646
// 保存uuid和email的对应关系(TODO:重启如何处理,有效期问题)
4747
var regActivateCodeMap = map[string]string{}
4848

49-
func (AccountController) Register(ctx echo.Context) error {
49+
func (self AccountController) Register(ctx echo.Context) error {
5050
if _, ok := ctx.Get("user").(*model.Me); ok {
5151
return ctx.Redirect(http.StatusSeeOther, "/")
5252
}
@@ -91,18 +91,8 @@ func (AccountController) Register(ctx echo.Context) error {
9191
return render(ctx, registerTpl, data)
9292
}
9393

94-
var (
95-
uuid string
96-
email = ctx.FormValue("email")
97-
)
98-
for {
99-
uuid = guuid.NewV4().String()
100-
if _, ok := regActivateCodeMap[uuid]; !ok {
101-
regActivateCodeMap[uuid] = email
102-
break
103-
}
104-
logger.Errorln("GenUUID 冲突....")
105-
}
94+
email := ctx.FormValue("email")
95+
uuid := self.genUUID(email)
10696
var emailUrl string
10797
if strings.HasSuffix(email, "@gmail.com") {
10898
emailUrl = "http://mail.google.com"
@@ -124,15 +114,37 @@ func (AccountController) Register(ctx echo.Context) error {
124114
return render(ctx, registerTpl, data)
125115
}
126116

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+
127130
// SendActivateEmail 发送注册激活邮件
128-
func (AccountController) SendActivateEmail(ctx echo.Context) error {
131+
func (self AccountController) SendActivateEmail(ctx echo.Context) error {
129132
uuid := ctx.FormValue("uuid")
130-
email, ok := regActivateCodeMap[uuid]
131-
if !ok {
132-
return fail(ctx, 1, "非法请求")
133-
}
133+
if uuid != "" {
134+
email, ok := regActivateCodeMap[uuid]
135+
if !ok {
136+
return fail(ctx, 1, "非法请求")
137+
}
134138

135-
go logic.DefaultEmail.SendActivateMail(email, uuid)
139+
go logic.DefaultEmail.SendActivateMail(email, uuid)
140+
} else {
141+
user, ok := ctx.Get("user").(*model.Me)
142+
if !ok {
143+
return fail(ctx, 1, "非法请求")
144+
}
145+
146+
go logic.DefaultEmail.SendActivateMail(user.Email, self.genUUID(user.Email))
147+
}
136148

137149
return success(ctx, nil)
138150
}
@@ -226,23 +238,28 @@ func (AccountController) Login(ctx echo.Context) error {
226238
}
227239

228240
// Edit 用户编辑个人信息
229-
func (AccountController) Edit(ctx echo.Context) error {
241+
func (self AccountController) Edit(ctx echo.Context) error {
230242
me := ctx.Get("user").(*model.Me)
231-
user := logic.DefaultUser.FindOne(ctx, "uid", me.Uid)
232243

233-
if Request(ctx).Method != "POST" {
244+
if ctx.Request().Method() != "POST" {
245+
user := logic.DefaultUser.FindOne(ctx, "uid", me.Uid)
234246
return render(ctx, "user/edit.html", map[string]interface{}{
235247
"user": user,
236248
"default_avatars": logic.DefaultAvatars,
237249
})
238250
}
239251

240252
// 更新信息
241-
errMsg, err := logic.DefaultUser.Update(ctx, me.Uid, Request(ctx).PostForm)
253+
errMsg, err := logic.DefaultUser.Update(ctx, me, ctx.Request().FormParams())
242254
if err != nil {
243255
return fail(ctx, 1, errMsg)
244256
}
245257

258+
email := ctx.FormValue("email")
259+
if me.Email != email {
260+
go logic.DefaultEmail.SendActivateMail(email, self.genUUID(email))
261+
}
262+
246263
return success(ctx, nil)
247264
}
248265

websites/code2/studygolang/src/http/middleware/http_error.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ func HTTPError() echo.MiddlewareFunc {
2929
return ctx.String(http.StatusOK, `{"ok":0,"error":"接口不存在"}`)
3030
}
3131
return Render(ctx, "404.html", nil)
32+
case http.StatusForbidden:
33+
if util.IsAjax(ctx) {
34+
return ctx.String(http.StatusOK, `{"ok":0,"error":"没有权限访问"}`)
35+
}
36+
return Render(ctx, "403.html", map[string]interface{}{"msg": he.Message})
3237
case http.StatusInternalServerError:
3338
if util.IsAjax(ctx) {
3439
return ctx.String(http.StatusOK, `{"ok":0,"error":"接口服务器错误"}`)

websites/code2/studygolang/src/http/middleware/login.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"model"
1313
"net/http"
1414
"net/url"
15+
"strings"
1516
"util"
1617

1718
. "http"
@@ -48,8 +49,8 @@ func AutoLogin() echo.MiddlewareFunc {
4849
func NeedLogin() echo.MiddlewareFunc {
4950
return func(next echo.Handler) echo.Handler {
5051
return echo.HandlerFunc(func(ctx echo.Context) error {
51-
_, ok := ctx.Get("user").(*model.Me)
52-
if !ok {
52+
user, ok := ctx.Get("user").(*model.Me)
53+
if !ok || user.Status != model.UserStatusAudit {
5354
method := ctx.Request().Method()
5455
if util.IsAjax(ctx) {
5556
return ctx.JSON(http.StatusForbidden, `{"ok":0,"error":"403 Forbidden"}`)
@@ -58,12 +59,19 @@ func NeedLogin() echo.MiddlewareFunc {
5859
return ctx.HTML(http.StatusForbidden, `403 Forbidden`)
5960
}
6061

61-
reqURL := ctx.Request().URL()
62-
uri := reqURL.Path()
63-
if reqURL.QueryString() != "" {
64-
uri += "?" + reqURL.QueryString()
62+
if !ok {
63+
reqURL := ctx.Request().URL()
64+
uri := reqURL.Path()
65+
if reqURL.QueryString() != "" {
66+
uri += "?" + reqURL.QueryString()
67+
}
68+
return ctx.Redirect(http.StatusSeeOther, "/account/login?redirect_uri="+url.QueryEscape(uri))
69+
} else {
70+
// 未激活可以查看账号信息
71+
if !strings.HasPrefix(ctx.Path(), "/account") {
72+
return echo.NewHTTPError(http.StatusForbidden, `您的邮箱未激活,<a href="/account/edit">去激活</a>`)
73+
}
6574
}
66-
return ctx.Redirect(http.StatusSeeOther, "/account/login?redirect_uri="+url.QueryEscape(uri))
6775
}
6876
}
6977

websites/code2/studygolang/src/logic/email.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ func (self EmailLogic) EmailNotice() {
159159
}
160160

161161
for _, user := range users {
162+
if lastUid < user.Uid {
163+
lastUid = user.Uid
164+
}
165+
162166
if user.Unsubscribe == 1 {
163167
logger.Infoln("user unsubscribe", user)
164168
continue
@@ -180,10 +184,6 @@ func (self EmailLogic) EmailNotice() {
180184

181185
self.SendMail("每周精选", content, []string{user.Email})
182186

183-
if lastUid < user.Uid {
184-
lastUid = user.Uid
185-
}
186-
187187
// 控制发信速度
188188
time.Sleep(30 * time.Second)
189189
}

websites/code2/studygolang/src/logic/user.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (self UserLogic) CreateUser(ctx context.Context, form url.Values) (errMsg s
147147
}
148148

149149
// Update 更新用户信息
150-
func (self UserLogic) Update(ctx context.Context, uid int, form url.Values) (errMsg string, err error) {
150+
func (self UserLogic) Update(ctx context.Context, me *model.Me, form url.Values) (errMsg string, err error) {
151151
objLog := GetLogger(ctx)
152152

153153
if form.Get("open") != "1" {
@@ -163,15 +163,20 @@ func (self UserLogic) Update(ctx context.Context, uid int, form url.Values) (err
163163
}
164164

165165
cols := "name,open,city,company,github,weibo,website,monlog,introduce"
166-
_, err = MasterDB.Id(uid).Cols(cols).Update(user)
166+
// 变更了邮箱
167+
if user.Email != me.Email {
168+
cols += ",email,status"
169+
user.Status = model.UserStatusNoAudit
170+
}
171+
_, err = MasterDB.Id(me.Uid).Cols(cols).Update(user)
167172
if err != nil {
168-
objLog.Errorf("更新用户 【%d】 信息失败:%s", uid, err)
173+
objLog.Errorf("更新用户 【%d】 信息失败:%s", me.Uid, err)
169174
errMsg = "对不起,服务器内部错误,请稍后再试!"
170175
return
171176
}
172177

173178
// 修改用户资料,活跃度+1
174-
go self.IncrUserWeight("uid", uid, 1)
179+
go self.IncrUserWeight("uid", me.Uid, 1)
175180

176181
return
177182
}
@@ -283,13 +288,13 @@ func (self UserLogic) FindCurrentUser(ctx context.Context, username interface{})
283288
objLog := GetLogger(ctx)
284289

285290
user := &model.User{}
286-
_, err := MasterDB.Where("username=? AND status=?", username, model.UserStatusAudit).Get(user)
291+
_, err := MasterDB.Where("username=? AND status<=?", username, model.UserStatusAudit).Get(user)
287292
if err != nil {
288293
objLog.Errorf("获取用户 %q 信息失败:%s", username, err)
289294
return &model.Me{}
290295
}
291296
if user.Uid == 0 {
292-
logger.Infof("用户 %q 不存在!", username)
297+
logger.Infof("用户 %q 不存在或状态不正常!", username)
293298
return &model.Me{}
294299
}
295300

@@ -356,16 +361,15 @@ func (self UserLogic) Login(ctx context.Context, username, passwd string) (*mode
356361
return nil, ErrUsername
357362
}
358363

359-
// 检验用户是否审核通过,暂时只有审核通过的才能登录
364+
// 检验用户状态是否正常(未激活的可以登录,但不能发布信息)
360365
user := &model.User{}
361366
MasterDB.Id(userLogin.Uid).Get(user)
362-
if user.Status != model.UserStatusAudit {
367+
if user.Status > model.UserStatusAudit {
363368
objLog.Infof("用户 %q 的状态非审核通过, 用户的状态值:%d", username, user.Status)
364369
var errMap = map[int]error{
365-
model.UserStatusNoAudit: errors.New("您的账号未激活,请到注册邮件中进行激活操作!"),
366-
model.UserStatusRefuse: errors.New("您的账号审核拒绝"),
367-
model.UserStatusFreeze: errors.New("您的账号因为非法发布信息已被冻结,请联系管理员!"),
368-
model.UserStatusOutage: errors.New("您的账号因为非法发布信息已被停号,请联系管理员!"),
370+
model.UserStatusRefuse: errors.New("您的账号审核拒绝"),
371+
model.UserStatusFreeze: errors.New("您的账号因为非法发布信息已被冻结,请联系管理员!"),
372+
model.UserStatusOutage: errors.New("您的账号因为非法发布信息已被停号,请联系管理员!"),
369373
}
370374
return nil, errMap[user.Status]
371375
}

websites/code2/studygolang/src/vendor/manifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@
215215
{
216216
"importpath": "github.com/polaris1119/nosql",
217217
"repository": "https://github.com/polaris1119/nosql",
218-
"revision": "0988e14cd44db80aab69b14f7591d06e55976501",
218+
"revision": "8446d0dc2d499f1edffe4b3274476c57905705c8",
219219
"branch": "master"
220220
},
221221
{

websites/code2/studygolang/static/js/user.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,30 @@
7777

7878
user.edit(this);
7979
});
80+
81+
$('#active_email').on('click', function(evt){
82+
evt.preventDefault();
83+
84+
$.ajax({
85+
type:"post",
86+
url: "/account/send_activate_email",
87+
dataType: 'json',
88+
success: function(data){
89+
if(data.ok){
90+
comTip("激活邮件已发到您邮箱,请查收!");
91+
}else{
92+
comTip(data.error);
93+
}
94+
},
95+
error:function(xmlReq, textStatus, errorThrown){
96+
if (xmlReq.status == 403) {
97+
comTip("没有操作权限");
98+
}
99+
}
100+
});
101+
102+
return false;
103+
});
80104

81105
$('#avatar-tab a').click(function (evt) {
82106
evt.preventDefault();

websites/code2/studygolang/template/403.html

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@
22
{{define "seo"}}<meta name="keywords" content="Go语言,Golang,Go社区,Go中文社区,Golang中文社区,Go语言社区,Go语言学习,学习Go语言,Go语言学习园地,Golang 中国,Golang中国,Golang China, Go语言论坛">
33
<meta name="description" content="Go语言中文网,中国 Golang 社区,Go语言学习园地,致力于构建完善的 Golang 中文社区,Go语言爱好者的学习家园。分享 Go 语言知识,交流使用经验">{{end}}
44
{{define "content"}}
5-
<div id="main" class="container-fluid">
6-
<div class="content">
7-
<h2>对不起,您没有操作权限</h2>
8-
<p>将在<span id="jumpTo">3</span>秒后跳转到<a href="/">首页</a></p>
5+
<div class="row box_white">
6+
<div class="col-md-12">
7+
<h2>对不起,您没有操作权限</h2>
8+
<p>
9+
{{if .msg}}
10+
{{noescape .msg}}
11+
{{else}}
12+
将在<span id="jumpTo">3</span>秒后跳转到<a href="/">首页</a></p>
13+
{{end}}
914
</div>
1015
</div>
1116
{{end}}
1217
{{define "js"}}
1318
<script type="text/javascript">
1419
function countDown(secs, surl){
1520
var jumpTo = document.getElementById('jumpTo');
21+
if (jumpTo == null) {
22+
return;
23+
}
1624
jumpTo.innerHTML=secs;
1725
if(--secs>0){
1826
setTimeout("countDown("+secs+",'"+surl+"')",1000);

websites/code2/studygolang/template/register.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<div class="col-sm-4">
3636
<input class="form-control {required:true,email:true}" type="email" id="email" name="email" value="{{.email}}" placeholder="请输入Email">
3737
</div>
38-
<span class="help-block">保存以后就不可以改了哦</span>
38+
<span class="help-block">可以在个人资料设置中更改</span>
3939
</div>
4040
<div class="form-group form-group-sm">
4141
<label class="col-sm-3 control-label" for="passwd"><abbr>*</abbr>密码</label>

websites/code2/studygolang/template/user/activate.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
{{if .error}}
1616
<h2>激活账号出错了!</h2>
1717
{{.error}}
18+
<p>如果您确定已经注册了,可以登录,在 个人资料设置 中再次发送激活邮件</p>
1819
{{else}}
1920
<div style="padding:30px 30px 50px 30px;">
2021
<div style="color:#339502;font-size:22px;line-height: 2.5;">恭喜您激活成功!</div>

websites/code2/studygolang/template/user/edit.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@
2121
<p class="form-control-static">{{.user.Username}}</p>
2222
</div>
2323
</div>
24-
<div class="form-group">
24+
<div class="form-group form-group-sm">
2525
<label class="col-sm-3 control-label" for="email">Email</label>
2626
<div class="col-sm-6">
27-
<p class="form-control-static">{{.user.Email}}</p>
27+
<input class="form-control {required:true,email:true}" type="email" id="email" name="email" placeholder="邮箱" value="{{.user.Email}}">
2828
<label>
29+
{{if ne .user.Status 1}}
30+
邮箱未激活,<a href="#" id="active_email">发激活邮件</a>
31+
{{else}}
2932
<input type="checkbox" name="open" value="1" {{if .user.Open}}checked{{end}}> 公开Email
33+
{{end}}
3034
</label>
3135
</div>
36+
<span class="help-block">更改邮箱需要重新激活</span>
3237
</div>
3338
<div class="form-group form-group-sm">
3439
<label class="col-sm-3 control-label" for="name">名字</label>
@@ -227,7 +232,7 @@ <h4 class="title">头像</h4>
227232
<script type="text/javascript" src="/static/js/libs/marked.min.js"></script>
228233
<script type="text/javascript" src="/static/js/libs/highlight.min.js"></script>
229234
<script type="text/javascript" src="/static/js/libs/plupload.full.min.js"></script>
230-
<script type="text/javascript" src="/static/js/user.js?v=1.0"></script>
235+
<script type="text/javascript" src="/static/js/user.js?v=1.1"></script>
231236
<script type="text/javascript">
232237
// 需要加载的侧边栏
233238
SG.SIDE_BARS = [

0 commit comments

Comments
 (0)