package weixin import ( "encoding/base64" "errors" "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/auth2" "git.rosy.net.cn/jx-callback/business/auth2/authprovider" "git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/business/model/dao" "git.rosy.net.cn/jx-callback/globals" "git.rosy.net.cn/jx-callback/globals/api" ) type MiniAuther struct { authprovider.DefAuther } var ( ErrAuthTypeShouldBeMini = errors.New("当前操作要求是小程序登录方式") ) var ( AutherObjMini *MiniAuther ) func init() { AutherObjMini = new(MiniAuther) auth2.RegisterAuther(AuthTypeMini, AutherObjMini) } func (a *MiniAuther) VerifySecret(dummy, jsCode string) (authBind *model.AuthBind, err error) { globals.SugarLogger.Debugf("weixin mini VerifySecret jsCode:%s", jsCode) sessionInfo, err := api.WeixinMiniAPI.SNSCode2Session(jsCode) if err == nil { db := dao.GetDB() if authBind, err = dao.GetAuthBind(db, "", AuthTypeMP, sessionInfo.OpenID); dao.IsNoRowsError(err) { var authBindList []*model.AuthBind sessionKey := sessionInfo.SessionKey sessionInfo.SessionKey = "" if sessionInfo.UnionID != "" { if authBindList, err = dao.GetAuthBindsByAuthID2(db, sessionInfo.UnionID, []string{AuthTypeWeixin, AuthTypeMP, AuthTypeMini}); err == nil && len(authBindList) > 0 { authBind = authBindList[0] authBind.Type = AuthTypeMP authBind.AuthID = sessionInfo.OpenID authBind.DetailData = string(utils.MustMarshal(sessionInfo)) authBind.UserData = sessionKey err = a.AddAuthBind(authBind, "admin") } } if err == nil && len(authBindList) == 0 { authBind = &model.AuthBind{ Type: AuthTypeMP, AuthID: sessionInfo.OpenID, AuthID2: sessionInfo.UnionID, DetailData: string(utils.MustMarshal(sessionInfo)), UserData: sessionKey, } } } } return authBind, err } // 特殊接口 func (a *MiniAuther) DecryptData(authInfo *auth2.AuthInfo, encryptedData, iv string) (decryptedDataBase64 string, err error) { globals.SugarLogger.Debugf("weixin mini DecryptData encryptedData:%s, iv:%s", encryptedData, iv) if authInfo.AuthBindInfo.Type != AuthTypeMini { return "", ErrAuthTypeShouldBeMini } sessionKey := authInfo.AuthBindInfo.UserData.(string) decryptedData, err := api.WeixinMiniAPI.SNSDecodeMiniProgramData(encryptedData, sessionKey, iv) if err != nil { return "", err } return base64.StdEncoding.EncodeToString(decryptedData), nil }