From 3e3c623ebc3fe2bc194800f634b3be9876764f0b Mon Sep 17 00:00:00 2001 From: richboo111 Date: Thu, 1 Dec 2022 11:22:46 +0800 Subject: [PATCH] wxapp login --- business/auth2/auth2.go | 22 +++++++++++++++++++- business/auth2/authprovider/weixin/weixin.go | 12 +++++++++++ business/model/const.go | 7 +++++-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/business/auth2/auth2.go b/business/auth2/auth2.go index 19000baa2..05e8603e2 100644 --- a/business/auth2/auth2.go +++ b/business/auth2/auth2.go @@ -5,6 +5,7 @@ import ( "encoding/base64" "encoding/json" "errors" + "git.rosy.net.cn/jx-callback/globals" "regexp" "strings" "time" @@ -36,6 +37,8 @@ const ( AuthTypePassword = "localpass" AuthTypeEmail = "email" AuthTypeMobile = "mobile" + AuthTypeWXApp = "weixinapp" //微信小程序 + AuthTypeWXMini = "weixinmini" //微信小程序 ) const ( @@ -280,8 +283,9 @@ func LoginInternal(ctx *Context, authType, authID, authIDType, authSecret string userProvider.UpdateLastLogin(user.GetID(), authType, ctx.GetRealRemoteIP()) } //如果是小程序 - if authType == "weixinmini" || authType == "weixinapp" { + if authType == AuthTypeWXMini { // || authType == AuthTypeWXApp 此处AuthTypeWXApp没用上 appID := strings.Split(authSecret, ",")[0] + globals.SugarLogger.Debugf("authSecret=%s,appID=%s", authSecret, appID) // 菜市商家 // // // 京西商城 if appID == model.JXC4BusinessAppId || appID == "wx2d6949f724b2541d" || appID == "wx18111a41fd17f24f" || appID == model.JXC4AppId { //菜市或者果园 if authInfo.AuthBindInfo.UserID != "" { @@ -300,6 +304,22 @@ func LoginInternal(ctx *Context, authType, authID, authIDType, authSecret string } } } + //微信APP端登录 + if authType == AuthTypeWXApp { + appID := strings.Split(authSecret, ",")[0] + if appID == model.JXC4ClientAppID && authInfo.AuthBindInfo.UserID != "" { + binds, err := dao.GetUserBindAuthInfo(dao.GetDB(), authInfo.AuthBindInfo.UserID, 0, nil, "", "", []string{model.JXC4ClientAppID}) + if err != nil { + return authInfo, err + } + if len(binds) == 0 { + authInfo.IsExistOpenID = false + } else { + authInfo.IsExistOpenID = true + } + authInfo.AppID = model.JXC4ClientAppID + } + } } } else { diff --git a/business/auth2/authprovider/weixin/weixin.go b/business/auth2/authprovider/weixin/weixin.go index fbbf15173..443e8d09e 100644 --- a/business/auth2/authprovider/weixin/weixin.go +++ b/business/auth2/authprovider/weixin/weixin.go @@ -70,6 +70,18 @@ func (a *Auther) VerifySecret(id, secret string) (authBindEx *auth2.AuthBindEx, } else { err = ErrStateIsWrong } + } else if a.authType == AuthTypeWxApp { + state := id + code := jsCode + if state == "" { + token, err2 := a.getAPI().SNSRetrieveToken(code) + if err = err2; err == nil { + openID = token.OpenID + accessToken = token.AccessToken + } + } else { + err = ErrStateIsWrong + } } else { openID = id accessToken = secret diff --git a/business/model/const.go b/business/model/const.go index 1ae19706c..0a17dd19a 100644 --- a/business/model/const.go +++ b/business/model/const.go @@ -533,7 +533,10 @@ func IsWaybillFinalStatus(status int) bool { } const ( - JXC4AppId = "wx4b5930c13f8b1170" // 京西菜市appId - JXC4BusinessAppId = "wx08a5c2a8581414ff" // 京西商家appId + JXC4AppId = "wx4b5930c13f8b1170" // 京西菜市appId type=weixinmini + JXC4BusinessAppId = "wx08a5c2a8581414ff" // 京西商家appId type=weixinmini JXC4TiktokAppId = "tta6a1d01c399f264201" // 抖音appID + + JXC4ClientAppID = "wxf3657c94aa01a3f0" //京西菜市客户端APP ID type="weixinapp" + )