diff --git a/business/auth2/authprovider/weixin/weixin_mini.go b/business/auth2/authprovider/weixin/weixin_mini.go index 5737e6233..4c2d41db2 100644 --- a/business/auth2/authprovider/weixin/weixin_mini.go +++ b/business/auth2/authprovider/weixin/weixin_mini.go @@ -37,7 +37,8 @@ func init() { func (a *MiniAuther) VerifySecret(dummy, jsCode string) (authBindEx *auth2.AuthBindEx, err error) { globals.SugarLogger.Debugf("weixin mini VerifySecret jsCode:%s", jsCode) - sessionInfo, err := ProxySNSCode2Session(jsCode) + appID, jsCode := SplitJsCode(jsCode) + sessionInfo, err := getWxApp(appID).SNSCode2Session(jsCode) if err == nil { sessionKey := sessionInfo.SessionKey sessionInfo.SessionKey = "" @@ -52,8 +53,9 @@ func (a *MiniAuther) VerifySecret(dummy, jsCode string) (authBindEx *auth2.AuthB func (a *MiniAuther) DecryptData(authInfo *auth2.AuthInfo, jsCode, encryptedData, iv string) (decryptedDataBase64 string, err error) { globals.SugarLogger.Debugf("weixin mini DecryptData jsCode:%s, encryptedData:%s, iv:%s", jsCode, encryptedData, iv) var sessionKey string + appID, jsCode := SplitJsCode(jsCode) if jsCode != "" { - sessionInfo, err := ProxySNSCode2Session(jsCode) + sessionInfo, err := getWxApp(appID).SNSCode2Session(jsCode) if err == nil { if authBindEx, err := a.UnionFindAuthBind(AuthTypeMini, []string{AuthTypeMini}, sessionInfo.OpenID, "", nil); err == nil { if authBindEx.UserID != authInfo.GetID() { @@ -72,7 +74,8 @@ func (a *MiniAuther) DecryptData(authInfo *auth2.AuthInfo, jsCode, encryptedData } sessionKey = authInfo.AuthBindInfo.UserData.(string) } - decryptedData, err := ProxySNSDecodeMiniProgramData(encryptedData, sessionKey, iv) + // decryptedData, err := ProxySNSDecodeMiniProgramData(encryptedData, sessionKey, iv) + decryptedData, err := weixinapi.SNSDecodeMiniProgramData(encryptedData, sessionKey, iv) if err != nil { return "", err } @@ -84,21 +87,51 @@ func (a *MiniAuther) GetUserType() (userType int8) { return model.UserTypeStoreBoss } -func ProxySNSCode2Session(jsCode string) (sessionInfo *weixinapi.SessionInfo, err error) { - miniApi := api.WeixinMiniAPI - list := strings.Split(jsCode, ",") - if len(list) >= 2 && len(list[0]) == len("wx4b5930c13f8b1170") { - if list[0] == api.WeixinMiniAppID2 { - miniApi = api.WeixinMiniAPI2 - } - jsCode = strings.Join(list[1:], ",") +func getWxApp(appID string) (miniApi *weixinapi.API) { + miniApi = api.WeixinMiniAPI + if len(appID) > 0 && appID == api.WeixinMiniAppID2 { + miniApi = api.WeixinMiniAPI2 } - sessionInfo, err = miniApi.SNSCode2Session(jsCode) - return sessionInfo, err + return miniApi } -func ProxySNSDecodeMiniProgramData(encryptedData, sessionKey, iv string) (decryptedData []byte, err error) { - globals.SugarLogger.Debugf("ProxySNSDecodeMiniProgramData, encryptedData:%s, sessionKey:%s, iv:%s", encryptedData, sessionKey, iv) - decryptedData, err = api.WeixinMiniAPI.SNSDecodeMiniProgramData(encryptedData, sessionKey, iv) - return decryptedData, err +func SplitJsCode(jsCode string) (appID, realJsCode string) { + list := strings.Split(jsCode, ",") + if len(list) == 2 { + appID = list[0] + realJsCode = list[1] + } else if len(list) == 1 { + realJsCode = jsCode + } else { + globals.SugarLogger.Warnf("SplitJsCode abnormal jsCode:%s", jsCode) + } + return appID, realJsCode } + +func ComposeJsCode(appID, jsCode string) (composedCode string) { + composedCode = strings.Join([]string{ + appID, + jsCode, + }, ",") + return composedCode +} + +// func ProxySNSCode2Session(jsCode string) (sessionInfo *weixinapi.SessionInfo, err error) { +// miniApi := api.WeixinMiniAPI +// list := strings.Split(jsCode, ",") +// if len(list) >= 2 && len(list[0]) == len("wx4b5930c13f8b1170") { +// if list[0] == api.WeixinMiniAppID2 { +// miniApi = api.WeixinMiniAPI2 +// } +// miniApi = getWxApp(list[0]) +// jsCode = strings.Join(list[1:], ",") +// } +// sessionInfo, err = miniApi.SNSCode2Session(jsCode) +// return sessionInfo, err +// } + +// func ProxySNSDecodeMiniProgramData(encryptedData, sessionKey, iv string) (decryptedData []byte, err error) { +// globals.SugarLogger.Debugf("ProxySNSDecodeMiniProgramData, encryptedData:%s, sessionKey:%s, iv:%s", encryptedData, sessionKey, iv) +// decryptedData, err = api.WeixinMiniAPI.SNSDecodeMiniProgramData(encryptedData, sessionKey, iv) +// return decryptedData, err +// } diff --git a/controllers/auth2.go b/controllers/auth2.go index c9ce8311a..a09e18fea 100644 --- a/controllers/auth2.go +++ b/controllers/auth2.go @@ -18,19 +18,14 @@ import ( ) func GetComposedCode(c *beego.Controller, code string) (composedCode string) { - if code != "" { - composedCode = code - referer := c.Ctx.Request.Referer() - globals.SugarLogger.Debugf("GetComposedCode referer:%s", referer) - index := strings.Index(referer, "//") - if index > 0 { - list := strings.Split(referer[index+2:], "/") - if len(list) >= 2 { - composedCode = strings.Join([]string{ - list[1], - code, - }, ",") - } + composedCode = code + referer := c.Ctx.Request.Referer() + globals.SugarLogger.Debugf("GetComposedCode referer:%s", referer) + index := strings.Index(referer, "//") + if index > 0 { + list := strings.Split(referer[index+2:], "/") + if len(list) >= 2 { + composedCode = weixin.ComposeJsCode(list[1], code) } } return composedCode