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

Skip to content

Commit 42d2b9b

Browse files
committed
和认证微信公众号 polarisxu 绑定
1 parent d1e38bc commit 42d2b9b

File tree

9 files changed

+522
-18
lines changed

9 files changed

+522
-18
lines changed

.air.conf

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Config file for [Air](https://github.com/cosmtrek/air) in TOML format
2+
3+
# Working directory
4+
# . or absolute path, please note that the directories following must be under root
5+
root = "."
6+
# Optional! If `watch_dir` is empty, use `root`.
7+
watch_dir = ""
8+
tmp_dir = "tmp"
9+
10+
[build]
11+
# Just plain old shell command. You could use `make` as well.
12+
cmd = "make build"
13+
# Binary file yields from `cmd`.
14+
bin = "bin/studygolang"
15+
# Customize binary.
16+
# full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
17+
# This log file places in your tmp_dir.
18+
log = "air_errors.log"
19+
# Watch these filename extensions.
20+
include_ext = ["go", "tpl", "tmpl", "html"]
21+
# Ignore these filename extensions or directories.
22+
exclude_dir = ["log", "tmp", "vendor", "node_modules", "template", "static", "docs", "bin", "sitemap", "data", "config", "pid"]
23+
# There's no necessary to trigger build each time file changes if it's too frequency.
24+
delay = 1000 # ms
25+
26+
[log]
27+
# Show log time
28+
time = false
29+
30+
[color]
31+
# Customize each part's color. If no color found, use the raw app log.
32+
main = "magenta"
33+
watcher = "cyan"
34+
build = "yellow"
35+
runner = "green"
36+
37+
[misc]
38+
# Delete tmp directory on exit
39+
clean_on_exit = true

http/controller/account.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func (self AccountController) RegisterRoute(g *echo.Group) {
3737
g.Any("/account/register", self.Register)
3838
g.POST("/account/send_activate_email", self.SendActivateEmail)
3939
g.GET("/account/activate", self.Activate)
40+
g.POST("/account/wechat_active", self.WechatActive)
4041
g.Any("/account/login", self.Login)
4142
g.Any("/account/edit", self.Edit, middleware.NeedLogin())
4243
g.POST("/account/change_avatar", self.ChangeAvatar, middleware.NeedLogin())
@@ -103,6 +104,10 @@ func (self AccountController) Register(ctx echo.Context) error {
103104
return render(ctx, registerTpl, data)
104105
}
105106

107+
// 不验证邮箱,注册完成直接登录
108+
// 自动登录
109+
SetLoginCookie(ctx, username)
110+
106111
email := ctx.FormValue("email")
107112

108113
uuid := RegActivateCode.GenUUID(email)
@@ -122,6 +127,7 @@ func (self AccountController) Register(ctx echo.Context) error {
122127
我们已经发送一封邮件到 ` + email + `,请您根据提示信息完成邮箱验证.<br><br>
123128
<a href="` + emailUrl + `" target="_blank"><button type="button" class="btn btn-success">立即验证</button></a>&nbsp;&nbsp;<button type="button" class="btn btn-link" data-uuid="` + uuid + `" id="resend_email">未收到?再发一次</button>
124129
</div>`),
130+
"username": username,
125131
}
126132

127133
isHttps := CheckIsHttps(ctx)
@@ -131,10 +137,6 @@ func (self AccountController) Register(ctx echo.Context) error {
131137
return render(ctx, registerTpl, data)
132138
}
133139

134-
// 不验证邮箱,注册完成直接登录
135-
// 自动登录
136-
SetLoginCookie(ctx, username)
137-
138140
return ctx.Redirect(http.StatusSeeOther, "/balance")
139141
}
140142

@@ -168,7 +170,19 @@ func (AccountController) Activate(ctx echo.Context) error {
168170

169171
data := map[string]interface{}{}
170172

171-
param := goutils.Base64Decode(ctx.QueryParam("param"))
173+
param := ctx.QueryParam("param")
174+
if param == "" {
175+
me, ok := ctx.Get("user").(*model.Me)
176+
if ok {
177+
data["me"] = me
178+
return render(ctx, contentTpl, data)
179+
}
180+
181+
data["error"] = "非法请求!"
182+
return render(ctx, contentTpl, data)
183+
}
184+
185+
param = goutils.Base64Decode(param)
172186
values, err := url.ParseQuery(param)
173187
if err != nil {
174188
data["error"] = err.Error()
@@ -206,6 +220,25 @@ func (AccountController) Activate(ctx echo.Context) error {
206220
return ctx.Redirect(http.StatusSeeOther, "/balance")
207221
}
208222

223+
func (AccountController) WechatActive(ctx echo.Context) error {
224+
captcha := ctx.FormValue("captcha")
225+
if captcha == "" {
226+
return fail(ctx, 1, "验证码是不能空")
227+
}
228+
229+
echoCtx := context.EchoContext(ctx)
230+
me, ok := ctx.Get("user").(*model.Me)
231+
if !ok {
232+
return fail(ctx, 1, "必须先登录")
233+
}
234+
err := logic.DefaultWechat.CheckCaptchaAndActivate(echoCtx, me, captcha)
235+
if err != nil {
236+
return fail(ctx, 2, "验证码错误,请确认获取了或没填错!")
237+
}
238+
239+
return success(ctx, nil)
240+
}
241+
209242
// Login 登录
210243
func (AccountController) Login(ctx echo.Context) error {
211244
if _, ok := ctx.Get("user").(*model.Me); ok {

http/controller/wechat.go

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

99
import (
10+
"fmt"
1011
"io/ioutil"
1112
"net/http"
1213

1314
"github.com/studygolang/studygolang/context"
1415
"github.com/studygolang/studygolang/logic"
16+
"github.com/studygolang/studygolang/model"
1517

1618
echo "github.com/labstack/echo/v4"
1719
)
@@ -21,6 +23,7 @@ type WechatController struct{}
2123
// 注册路由
2224
func (self WechatController) RegisterRoute(g *echo.Group) {
2325
g.Any("/wechat/autoreply", self.AutoReply)
26+
g.POST("/wechat/bind", self.Bind)
2427
}
2528

2629
func (self WechatController) AutoReply(ctx echo.Context) error {
@@ -45,3 +48,23 @@ func (self WechatController) AutoReply(ctx echo.Context) error {
4548

4649
return ctx.XML(http.StatusOK, wechatReply)
4750
}
51+
52+
func (self WechatController) Bind(ctx echo.Context) error {
53+
captcha := ctx.FormValue("captcha")
54+
if captcha == "" {
55+
return fail(ctx, 1, "验证码是不能空")
56+
}
57+
58+
echoCtx := context.EchoContext(ctx)
59+
me, ok := ctx.Get("user").(*model.Me)
60+
if !ok {
61+
return fail(ctx, 1, "必须先登录")
62+
}
63+
err := logic.DefaultWechat.CheckCaptchaAndBind(echoCtx, me, captcha)
64+
if err != nil {
65+
fmt.Println("controller====", err)
66+
return fail(ctx, 2, "验证码错误,请确认获取了或没填错!")
67+
}
68+
69+
return success(ctx, nil)
70+
}

0 commit comments

Comments
 (0)