diff --git a/models/wechat_bind.go b/models/wechat_bind.go index ab2d1e06d..b89a68cd9 100644 --- a/models/wechat_bind.go +++ b/models/wechat_bind.go @@ -47,7 +47,7 @@ func BindWechatOpenId(userId int64, wechatOpenId string) error { return sess.Commit() } -func UnbindWechatOpenId(userId int64) error { +func UnbindWechatOpenId(userId int64, oldWechatOpenID string) error { sess := x.NewSession() defer sess.Close() if err := sess.Begin(); err != nil { @@ -55,7 +55,7 @@ func UnbindWechatOpenId(userId int64) error { } param := &User{WechatOpenId: ""} - n, err := x.Where("ID = ?", userId).Update(param) + n, err := x.Where("ID = ? AND wechat_open_id =?", userId, oldWechatOpenID).Update(param) if err != nil { log.Error("update wechat_open_id failed,e=%v", err) return err @@ -64,10 +64,10 @@ func UnbindWechatOpenId(userId int64) error { log.Error("update wechat_open_id failed,user not exist,userId=%d", userId) return nil } - //todo 是否记录原有微信openId logParam := &WechatBindLog{ - UserID: userId, - Action: int(WECHAT_UNBIND), + UserID: userId, + WechatOpenId: oldWechatOpenID, + Action: int(WECHAT_UNBIND), } sess.Insert(logParam) return sess.Commit() diff --git a/modules/auth/wechat/bind.go b/modules/auth/wechat/bind.go index e060faf06..54937e4e1 100644 --- a/modules/auth/wechat/bind.go +++ b/modules/auth/wechat/bind.go @@ -20,6 +20,6 @@ func BindWechat(userId int64, wechatOpenId string) error { return models.BindWechatOpenId(userId, wechatOpenId) } -func UnbindWechat(userId int64) error { - return models.UnbindWechatOpenId(userId) +func UnbindWechat(userId int64, oldWechatOpenId string) error { + return models.UnbindWechatOpenId(userId, oldWechatOpenId) } diff --git a/routers/authentication/wechat.go b/routers/authentication/wechat.go index dacda1180..0139bb09f 100644 --- a/routers/authentication/wechat.go +++ b/routers/authentication/wechat.go @@ -40,7 +40,7 @@ func GetQRCode4Bind(ctx *context.Context) { }) } -// GetQRCode4Bind get QR code for wechat binding +// GetBindStatus func GetBindStatus(ctx *context.Context) { sceneStr := ctx.Query("sceneStr") val, _ := redis_client.Get(redis_key.WechatBindingUserIdKey(sceneStr)) @@ -65,6 +65,15 @@ func GetBindStatus(ctx *context.Context) { }) } +// UnbindWechat +func UnbindWechat(ctx *context.Context) { + wechat.UnbindWechat(ctx.User.ID, ctx.User.WechatOpenId) + ctx.JSON(200, map[string]interface{}{ + "code": "00", + "msg": "success", + }) +} + func createQRCode4Bind(userId int64) (*QRCodeResponse, error) { sceneStr := gouuid.NewV4().String() r := wechat.GetWechatQRCode4Bind(sceneStr) diff --git a/routers/routes/routes.go b/routers/routes/routes.go index e5d5bae3b..7fc5fa671 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -396,8 +396,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/authentication/wechat", func() { m.Get("/qrCode4Bind", authentication.GetQRCode4Bind) m.Get("/bindStatus", authentication.GetBindStatus) - // TODO manage redirection - m.Post("/authorize", bindIgnErr(auth.AuthorizationForm{}), user.AuthorizeOAuth) + m.Post("/unbind", authentication.UnbindWechat) }, reqSignIn) m.Group("/user/settings", func() {