Skip to content

Commit

Permalink
[refactor] 重构绑定接口为统一入口
Browse files Browse the repository at this point in the history
  • Loading branch information
wutongci committed May 11, 2019
1 parent 41afabe commit 9733fc1
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 53 deletions.
31 changes: 29 additions & 2 deletions pkg/controllers/myaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,36 @@ func (c *MyAccountController) Verifymail() {
c.Resp(0, "success", "邮件发送成功!")
}

//解除绑定第三方应用
func (c *MyAccountController) Thirdbind() {
bindThirdDto := &dto.BindThirdDto{}
err := c.ParseAndValidateFirstErr(bindThirdDto)
if err != nil {
c.Fail(components.ErrInvalidParams, err.Error())
return
}
from := bindThirdDto.From
if from == 0 {
from = 1
}
user_id, err := strconv.Atoi(c.Uid)
if err != nil {
c.Fail(components.ErrInvalidUser, err.Error())
return
}
myAccountService := service.MyAccountService{} //switch case from 1 钉钉 2 微信 TODO
openid,err := myAccountService.BindByDingtalk(bindThirdDto.Code, user_id,from)
if err != nil {
c.Fail(components.ErrBindDingtalk, err.Error())
}
c.Resp(0, "success", map[string]interface{}{
"openid": openid,
})
}

//解除绑定第三方应用
func (c *MyAccountController) ThirdUnbind() {
UnBindDingtalkDto := &dto.UnBindDingtalkDto{}
UnBindDingtalkDto := &dto.UnBindThirdDto{}
err := c.ParseAndValidateFirstErr(UnBindDingtalkDto)
if err != nil {
c.Fail(components.ErrInvalidParams, err.Error())
Expand All @@ -166,7 +193,7 @@ func (c *MyAccountController) ThirdUnbind() {
if from == 0 {
from = 1
}
userService := service.UserService{}
userService := service.UserService{} //switch case from 1 钉钉 2 微信 TODO
errs := userService.UnBindUserDingtalk(from,user_id)
if errs != nil {
c.Fail(components.ErrUnBindDingtalk, errs.Error())
Expand Down
26 changes: 0 additions & 26 deletions pkg/controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,32 +114,6 @@ func (c *AccountController) DingtalkLogin() {
}
}

//绑定钉钉
func (c *UserController) DingtalkBind() {
dingtalkDto := &dto.LoginDingtalkDto{}
err := c.ParseAndValidateFirstErr(dingtalkDto)
if err != nil {
c.Fail(components.ErrInvalidParams, err.Error())
return
}
userService := service.UserService{}
user_id, err := strconv.Atoi(c.Uid)
if err != nil {
c.Fail(components.ErrInvalidUser, err.Error())
return
}
openid,err := userService.BindByDingtalk(dingtalkDto.Code, user_id)
if err != nil {
c.Fail(components.ErrBindDingtalk, err.Error())
}
c.Resp(0, "success", map[string]interface{}{
"openid": openid,
})
}




func (c *UserController) Add() {
userAddDto := &dto.UserAddDto{}
err := c.ParseAndValidateFirstErr(userAddDto)
Expand Down
8 changes: 7 additions & 1 deletion pkg/dto/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ type LoginDingtalkDto struct {
Code string `form:"code"`
}

type UnBindDingtalkDto struct {
type BindThirdDto struct {
From int `form:"from"`
Code string `form:"code"`
}


type UnBindThirdDto struct {
From int `form:"from"`
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func init() {
userController := &controllers.UserController{}
beego.Router("/user/login", accountController, "post:Login") //用户登录
beego.Router("/user/login-dingtalk", accountController, "post:DingtalkLogin") //第三方登陆
beego.Router("/user/bind-dingtalk", userController, "post:DingtalkBind") //绑定钉钉

beego.Router("/user/refresh-token", userController, "post:RefreshToken") //刷新令牌
beego.Router("/user/loginout", userController, "get:Logout") //用户退出登录
beego.Router("/user/findpasswd", accountController) //找回密码
Expand All @@ -33,7 +33,8 @@ func init() {
myAccountController := &controllers.MyAccountController{}
beego.Router("/account/security", myAccountController, "get:GetInfo") // 安全设置 -- 两步验证
beego.Router("/account/bindcode", myAccountController, "post:BindCode") //安全设置- 校验验证码
beego.Router("/account/unbind", myAccountController, "get:ThirdUnbind") //解除绑定第三方应用
beego.Router("/account/unbind", myAccountController, "post:ThirdUnbind") //解除绑定第三方应用
beego.Router("/account/bind", myAccountController, "post:Thirdbind") //绑定第三方应用
beego.Router("/account/third", myAccountController, "get:Third") // 第三方绑定账号列表
beego.Router("/account/verifymail", myAccountController, "post:Verifymail") // 发送邮件

Expand Down
23 changes: 23 additions & 0 deletions pkg/service/myaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base32"
"crypto/rand"
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
"github.com/bullteam/zeus/pkg/dao"
"github.com/bullteam/zeus/pkg/models"
Expand Down Expand Up @@ -48,4 +49,26 @@ func (s *MyAccountService) GetSecret(uid int) (userSecretQuery models.UserSecret
*/
func (s *MyAccountService) GetThirdList(user_id int) (oauthlist []orm.Params) {
return s.oauthdao.List(user_id)
}

//绑定第三方应用
func (s *MyAccountService) BindByDingtalk(code string, uid int,from int) (openid string,err error) {
Info,err := getUserInfo(code)
if err != nil {
return "",err
}
User,errs := s.oauthdao.GetUserByOpenId(Info.Openid, from)
if errs != nil || !utils.IsNilObject(User){
return "",nil
}
beego.Debug(Info)
userOAuth := models.UserOAuth{
From: from, // 1表示钉钉
User_id: uid,
Name: Info.Nick,
Openid: Info.Openid,
Unionid: Info.Unionid,
}
s.oauthdao.Create(userOAuth)
return Info.Openid,nil
}
22 changes: 0 additions & 22 deletions pkg/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/bullteam/zeus/pkg/dao"
"github.com/bullteam/zeus/pkg/dto"
"github.com/bullteam/zeus/pkg/models"
"github.com/bullteam/zeus/pkg/utils"
dingtalk "github.com/icepy/go-dingtalk/src"
"strconv"
)
Expand Down Expand Up @@ -160,27 +159,6 @@ func (us *UserService) SwitchDepartment(uids []string, did int) (int64, error) {
return us.dao.UpdateDepartment(euid, did)
}

//绑定钉钉
func (us *UserService) BindByDingtalk(code string, uid int) (openid string,err error) {
Info,err := getUserInfo(code)
if err != nil {
return "",err
}
User,errs := us.userOAuthDao.GetUserByOpenId(Info.Openid, 1)
if errs != nil || !utils.IsNilObject(User){
return "",nil
}
beego.Debug(Info)
userOAuth := models.UserOAuth{
From: 1, // 1表示钉钉
User_id: uid,
Name: Info.Nick,
Openid: Info.Openid,
Unionid: Info.Unionid,
}
us.userOAuthDao.Create(userOAuth)
return Info.Openid,nil
}

//钉钉登陆
func (us *UserService) LoginByDingtalk(code string) (user *models.UserOAuth ,err error) {
Expand Down

0 comments on commit 9733fc1

Please sign in to comment.