diff --git a/business/auth2/authprovider/weixin/weixin.go b/business/auth2/authprovider/weixin/weixin.go index 8caf13752..fbf9df42e 100644 --- a/business/auth2/authprovider/weixin/weixin.go +++ b/business/auth2/authprovider/weixin/weixin.go @@ -12,9 +12,9 @@ import ( ) const ( - AuthTypeWeixin = "wxqrcode" // 微信扫码 - AuthTypeMP = "weixinsns" // 公众号 - AuthTypeMini = "weixinmini" // 小程序 + AuthTypeWeixin = "wxqrcode" // 微信扫码 + AuthTypeMP = "weixinsns" // 公众号 + AuthTypeWXNative = "wxnative" // 微信APP ) type Auther struct { @@ -23,8 +23,9 @@ type Auther struct { } var ( - AutherObjWX *Auther - AutherObjMP *Auther + AutherObjWX *Auther + AutherObjMP *Auther + AutherObjNative *Auther ) var ( @@ -41,25 +42,42 @@ func init() { authType: AuthTypeMP, } auth2.RegisterAuther(AuthTypeMP, AutherObjMP) + + AutherObjNative = &Auther{ + authType: AuthTypeWXNative, + } + auth2.RegisterAuther(AuthTypeWXNative, AutherObjNative) } -func (a *Auther) VerifySecret(state, code string) (authBindEx *auth2.AuthBindEx, err error) { - globals.SugarLogger.Debugf("weixin VerifySecret code:%s", code) - if state == "" { - token, err2 := a.getAPI().SNSRetrieveToken(code) - if err = err2; err == nil { - wxUserinfo, err2 := a.getAPI().SNSGetUserInfo(token.AccessToken, token.OpenID) +func (a *Auther) VerifySecret(id, secret string) (authBindEx *auth2.AuthBindEx, err error) { + globals.SugarLogger.Debugf("weixin VerifySecret id:%s secret:%s", secret, secret) + var openID, accessToken string + if a.authType != AuthTypeWXNative { + state := id + code := secret + if state == "" { + token, err2 := a.getAPI().SNSRetrieveToken(code) if err = err2; err == nil { - if authBindEx, err = a.UnionFindAuthBind(a.authType, []string{AuthTypeWeixin, AuthTypeMP, AuthTypeMini}, wxUserinfo.OpenID, wxUserinfo.UnionID, wxUserinfo); err == nil { - authBindEx.UserHint = &auth2.UserBasic{ - Name: wxUserinfo.NickName, - Avatar: wxUserinfo.HeadImgURL, - } + openID = token.OpenID + accessToken = token.AccessToken + } + } else { + err = ErrStateIsWrong + } + } else { + openID = id + accessToken = secret + } + if err == nil { + wxUserinfo, err2 := a.getAPI().SNSGetUserInfo(accessToken, openID) + if err = err2; err == nil { + if authBindEx, err = a.UnionFindAuthBind(a.authType, []string{AuthTypeWeixin, AuthTypeMP, AuthTypeMini, AuthTypeWXNative}, wxUserinfo.OpenID, wxUserinfo.UnionID, wxUserinfo); err == nil { + authBindEx.UserHint = &auth2.UserBasic{ + Name: wxUserinfo.NickName, + Avatar: wxUserinfo.HeadImgURL, } } } - } else { - err = ErrStateIsWrong } return authBindEx, err } diff --git a/business/auth2/authprovider/weixin/weixin_mini.go b/business/auth2/authprovider/weixin/weixin_mini.go index 82e37b2ca..5737e6233 100644 --- a/business/auth2/authprovider/weixin/weixin_mini.go +++ b/business/auth2/authprovider/weixin/weixin_mini.go @@ -13,6 +13,10 @@ import ( "git.rosy.net.cn/jx-callback/globals/api" ) +const ( + AuthTypeMini = "weixinmini" // 小程序 +) + type MiniAuther struct { authprovider.DefAuther } @@ -37,7 +41,7 @@ func (a *MiniAuther) VerifySecret(dummy, jsCode string) (authBindEx *auth2.AuthB if err == nil { sessionKey := sessionInfo.SessionKey sessionInfo.SessionKey = "" - if authBindEx, err = a.UnionFindAuthBind(AuthTypeMini, []string{AuthTypeWeixin, AuthTypeMP, AuthTypeMini}, sessionInfo.OpenID, sessionInfo.UnionID, sessionInfo); err == nil { + if authBindEx, err = a.UnionFindAuthBind(AuthTypeMini, []string{AuthTypeWeixin, AuthTypeMP, AuthTypeMini, AuthTypeWXNative}, sessionInfo.OpenID, sessionInfo.UnionID, sessionInfo); err == nil { authBindEx.UserData = sessionKey } } diff --git a/controllers/auth2.go b/controllers/auth2.go index d4c0160d9..7bd189d9d 100644 --- a/controllers/auth2.go +++ b/controllers/auth2.go @@ -79,7 +79,7 @@ func (c *Auth2Controller) SendVerifyCode() { // @Title 登录接口 // @Description 登录接口(微信与公众号登录不能直接调用此接口) -// @Param authType formData string true "登录类型,当前支持[localpass:本地账号密码,mobile:手机短信,wxqrcode:微信登录,weixinsns:微信公众号,weixinmini;小程序,ddstaff:钉钉企业,ddqrcode:钉钉扫码,alipaycode:支付宝小程序]" +// @Param authType formData string true "登录类型,当前支持[localpass:本地账号密码,mobile:手机短信,wxqrcode:微信登录,weixinsns:微信公众号,weixinmini;小程序,wxnative:微信APP,ddstaff:钉钉企业,ddqrcode:钉钉扫码,alipaycode:支付宝小程序]" // @Param authSecret formData string true "不同登录类型的登录秘密,如果是localpass登录类型,是md5后的值(空串不要md5)" // @Param authID formData string false "登录ID,登录类型为localpass时依赖于authIDType,其它为相应登录类型的id" // @Param authIDType formData string false "只有在登录类型为localpass时,才有意义,分别为:userid2:用户名,email,mobile"