支持登录类型wxnative

This commit is contained in:
gazebo
2020-01-17 15:21:26 +08:00
parent a27f261ca4
commit 0fffdfdd76
3 changed files with 42 additions and 20 deletions

View File

@@ -12,9 +12,9 @@ import (
) )
const ( const (
AuthTypeWeixin = "wxqrcode" // 微信扫码 AuthTypeWeixin = "wxqrcode" // 微信扫码
AuthTypeMP = "weixinsns" // 公众号 AuthTypeMP = "weixinsns" // 公众号
AuthTypeMini = "weixinmini" // 小程序 AuthTypeWXNative = "wxnative" // 微信APP
) )
type Auther struct { type Auther struct {
@@ -23,8 +23,9 @@ type Auther struct {
} }
var ( var (
AutherObjWX *Auther AutherObjWX *Auther
AutherObjMP *Auther AutherObjMP *Auther
AutherObjNative *Auther
) )
var ( var (
@@ -41,25 +42,42 @@ func init() {
authType: AuthTypeMP, authType: AuthTypeMP,
} }
auth2.RegisterAuther(AuthTypeMP, AutherObjMP) auth2.RegisterAuther(AuthTypeMP, AutherObjMP)
AutherObjNative = &Auther{
authType: AuthTypeWXNative,
}
auth2.RegisterAuther(AuthTypeWXNative, AutherObjNative)
} }
func (a *Auther) VerifySecret(state, code string) (authBindEx *auth2.AuthBindEx, err error) { func (a *Auther) VerifySecret(id, secret string) (authBindEx *auth2.AuthBindEx, err error) {
globals.SugarLogger.Debugf("weixin VerifySecret code:%s", code) globals.SugarLogger.Debugf("weixin VerifySecret id:%s secret:%s", secret, secret)
if state == "" { var openID, accessToken string
token, err2 := a.getAPI().SNSRetrieveToken(code) if a.authType != AuthTypeWXNative {
if err = err2; err == nil { state := id
wxUserinfo, err2 := a.getAPI().SNSGetUserInfo(token.AccessToken, token.OpenID) code := secret
if state == "" {
token, err2 := a.getAPI().SNSRetrieveToken(code)
if err = err2; err == nil { if err = err2; err == nil {
if authBindEx, err = a.UnionFindAuthBind(a.authType, []string{AuthTypeWeixin, AuthTypeMP, AuthTypeMini}, wxUserinfo.OpenID, wxUserinfo.UnionID, wxUserinfo); err == nil { openID = token.OpenID
authBindEx.UserHint = &auth2.UserBasic{ accessToken = token.AccessToken
Name: wxUserinfo.NickName, }
Avatar: wxUserinfo.HeadImgURL, } 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 return authBindEx, err
} }

View File

@@ -13,6 +13,10 @@ import (
"git.rosy.net.cn/jx-callback/globals/api" "git.rosy.net.cn/jx-callback/globals/api"
) )
const (
AuthTypeMini = "weixinmini" // 小程序
)
type MiniAuther struct { type MiniAuther struct {
authprovider.DefAuther authprovider.DefAuther
} }
@@ -37,7 +41,7 @@ func (a *MiniAuther) VerifySecret(dummy, jsCode string) (authBindEx *auth2.AuthB
if err == nil { if err == nil {
sessionKey := sessionInfo.SessionKey sessionKey := sessionInfo.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 authBindEx.UserData = sessionKey
} }
} }

View File

@@ -79,7 +79,7 @@ func (c *Auth2Controller) SendVerifyCode() {
// @Title 登录接口 // @Title 登录接口
// @Description 登录接口(微信与公众号登录不能直接调用此接口) // @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微信APPddstaff钉钉企业ddqrcode钉钉扫码alipaycode支付宝小程序]"
// @Param authSecret formData string true "不同登录类型的登录秘密如果是localpass登录类型是md5后的值空串不要md5" // @Param authSecret formData string true "不同登录类型的登录秘密如果是localpass登录类型是md5后的值空串不要md5"
// @Param authID formData string false "登录ID登录类型为localpass时依赖于authIDType其它为相应登录类型的id" // @Param authID formData string false "登录ID登录类型为localpass时依赖于authIDType其它为相应登录类型的id"
// @Param authIDType formData string false "只有在登录类型为localpass时才有意义分别为userid2用户名emailmobile" // @Param authIDType formData string false "只有在登录类型为localpass时才有意义分别为userid2用户名emailmobile"