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

Skip to content

Commit bc75141

Browse files
committed
注册加验证码
1 parent 18f14ed commit bc75141

File tree

7 files changed

+47
-10
lines changed

7 files changed

+47
-10
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
package controller
88

99
import (
10+
"fmt"
11+
"html/template"
12+
"net/http"
13+
"strings"
14+
1015
"config"
1116
"filter"
12-
"fmt"
17+
"github.com/dchest/captcha"
1318
"github.com/gorilla/sessions"
1419
"github.com/studygolang/mux"
15-
"html/template"
1620
"logger"
17-
"net/http"
1821
"service"
19-
"strings"
2022
"util"
2123
)
2224

@@ -27,25 +29,32 @@ func RegisterHandler(rw http.ResponseWriter, req *http.Request) {
2729
username := req.PostFormValue("username")
2830
// 请求注册页面
2931
if username == "" || req.Method != "POST" || vars["json"] == "" {
32+
filter.SetData(req, map[string]interface{}{"captchaId": captcha.NewLen(4)})
3033
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/register.html")
3134
return
3235
}
3336

37+
// 校验验证码
38+
if !captcha.VerifyString(req.PostFormValue("captchaid"), req.PostFormValue("captchaSolution")) {
39+
fmt.Fprint(rw, `{"errno": 1, "error":"验证码错误"}`)
40+
return
41+
}
42+
3443
// 入库
3544
errMsg, err := service.CreateUser(req.Form)
3645
if err != nil {
3746
// bugfix:http://studygolang.com/topics/255
3847
if errMsg == "" {
3948
errMsg = err.Error()
4049
}
41-
fmt.Fprint(rw, `{"errno": 1, "error":"`, errMsg, `"}`)
50+
fmt.Fprint(rw, `{"errno": 2, "error":"`, errMsg, `"}`)
4251
return
4352
}
4453

4554
// 注册成功,自动为其登录
4655
setCookie(rw, req, req.PostFormValue("username"))
4756
// 发送欢迎邮件
48-
go sendWelcomeMail([]string{req.PostFormValue("email")})
57+
// go sendWelcomeMail([]string{req.PostFormValue("email")})
4958
fmt.Fprint(rw, `{"errno": 0, "error":""}`)
5059
}
5160

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ package main
99
import (
1010
. "config"
1111
"controller"
12+
"github.com/dchest/captcha"
1213
"go.net/websocket"
14+
"process"
15+
1316
"log"
1417
"math/rand"
1518
"net/http"
1619
"path/filepath"
17-
"process"
1820
"runtime"
1921
"time"
2022
)
@@ -30,6 +32,9 @@ func main() {
3032
// 服务静态文件
3133
http.Handle("/static/", http.FileServer(http.Dir(ROOT)))
3234

35+
// 验证码
36+
http.Handle("/captcha/", captcha.Server(100, 40))
37+
3338
go ServeWebSocket()
3439

3540
go ServeBackGround()

websites/code/studygolang/template/admin/reading/modify.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ <h1 class="pagetitle">修改文章</h1>
2222
<label for="rtype">晨读类型</label>
2323
<span class="field">
2424
<select id="rtype" name="rtype" class="uniformselect">
25-
<option value="0"{{if and .reading (eq .reading.Rtype 0)}} selected{{end}}>Go晨读</option>
26-
<option value="1"{{if and .reading (eq .reading.Rtype 1)}} selected{{end}}>综合晨读</option>
25+
<option value="0"{{if .reading }}{{if eq .reading.Rtype 0}} selected{{end}}{{end}}>Go晨读</option>
26+
<option value="1"{{if .reading}}{{if eq .reading.Rtype 1}} selected{{end}}{{end}}>综合晨读</option>
2727
</select>
2828
</span>
2929
</p>

websites/code/studygolang/template/readings/list.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@
5555
</ul>
5656
</div>
5757
<div class="col-lg-3 col-md-4 col-sm-5">
58+
<div class="row box_white sidebar">
59+
<div class="top">
60+
<h3 class="title"><i class="glyphicon glyphicon-qrcode"></i>&nbsp;&nbsp;关注我们(微信/微博)</h3>
61+
</div>
62+
<div class="sb-content">
63+
<div>
64+
<ul class="list-unstyled">
65+
<li><img src="http://studygolang.qiniudn.com/wx_studygolang.jpg?imageView2/2/w/286" alt="关注微信"></li>
66+
<li><iframe width="100%" height="550" class="share_self" frameborder="0" scrolling="no" src="http://widget.weibo.com/weiboshow/index.php?language=&width=0&height=550&fansRow=1&ptype=1&speed=0&skin=1&isTitle=1&noborder=1&isWeibo=1&isFans=0&uid=3169236923&verifier=d1997605&dpc=1"></iframe></li>
67+
</ul>
68+
</div>
69+
</div>
70+
</div>
71+
5872
<div class="row box_white sidebar">
5973
<div class="top">
6074
<h3 class="title"><i class="glyphicon glyphicon-list"></i>&nbsp;<a href="/articles" target="_blank" title="点击更多">最新博文</a></h3>

websites/code/studygolang/template/register.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<div class="control-group email required"><label class="email required control-label" for="email"><abbr title="required">*</abbr> Email</label><div class="controls"><input class="string email required span5" id="email" name="email" placeholder="[email protected]" size="50" type="email" value=""><p class="help-block">保存以后就不可以改了哦!</p></div></div>
1717
<div class="control-group password optional"><label class="password optional control-label" for="passwd">密码</label><div class="controls"><input class="password optional" id="passwd" name="passwd" size="50" placeholder="请填写密码" type="password"></div></div>
1818
<div class="control-group password optional"><label class="password optional control-label" for="pass2">确认密码</label><div class="controls"><input class="password optional" id="pass2" name="pass2" size="50" type="password" placeholder="请再次输入密码" ></div></div>
19+
<div class="control-group optional"><label class="optional control-label">验证码</label><div class="controls"><input type="text" id="captchaSolution" name="captchaSolution" /><img id="captcha_img" src="/captcha/{{.captchaId}}.png" alt="验证码" title="看不清,点击" /><input type="hidden" name="captchaid" value="{{.captchaId}}"></div></div>
1920
<div class="form-actions">
2021
<input class="btn btn-primary" data-disable-with="正在提交" name="commit" type="submit" value="提交注册信息">
2122
</div>
@@ -50,7 +51,13 @@ <h2>已经有帐号了?</h2>
5051
});
5152
return false;
5253
});
53-
54+
55+
$('#captcha_img').on('click', function(evt){
56+
evt.preventDefault();
57+
58+
var origSrc = $(this).attr("src");
59+
$(this).attr("src", origSrc+"?reload=" + (new Date()).getTime());
60+
});
5461
})
5562
</script>
5663
{{end}}

websites/code/thirdparty/getpkg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ go get -u github.com/gorilla/sessions
1616
go get -u github.com/robfig/cron
1717
go get -u github.com/PuerkitoBio/goquery
1818
go get -u github.com/qiniu/api
19+
go get -u github.com/dchest/captcha
1920

2021
hg clone https://code.google.com/p/go.net src/go.net
2122
go install go.net/websocket

websites/code/thirdparty/getpkg.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ go get -u github.com/gorilla/sessions
1717
go get -u github.com/robfig/cron
1818
go get -u github.com/PuerkitoBio/goquery
1919
go get -u github.com/qiniu/api
20+
go get -u github.com/dchest/captcha
2021
hg clone https://code.google.com/p/go.net src/go.net
2122
go install go.net/websocket
2223

0 commit comments

Comments
 (0)