package services import ( "eta/eta_mobile/utils" "fmt" wechat "github.com/silenceper/wechat/v2" "github.com/silenceper/wechat/v2/cache" "github.com/silenceper/wechat/v2/miniprogram" "github.com/silenceper/wechat/v2/miniprogram/auth" "github.com/silenceper/wechat/v2/miniprogram/config" "github.com/silenceper/wechat/v2/miniprogram/encryptor" "github.com/silenceper/wechat/v2/miniprogram/qrcode" ) func GetWxApp() (miniprogram *miniprogram.MiniProgram) { wc := wechat.NewWechat() memory := cache.NewMemory() //memory := cache.NewRedis(global.Redis) cfg := &config.Config{ AppID: utils.WxYbAppId, AppSecret: utils.WxYbAppSecret, Cache: memory, } miniprogram = wc.GetMiniProgram(cfg) return } // GetSession 获取用户详情 func GetSession(code string) (userInfo auth.ResCode2Session, err error) { wechatClient := GetWxApp() authClient := wechatClient.GetAuth() userInfo, err = authClient.Code2Session(code) return } // GetSession 获取用户详情 func GetUserInfo(code string) (userInfo auth.ResCode2Session, err error) { wechatClient := GetWxApp() authClient := wechatClient.GetAuth() fmt.Println("code:", code) userInfo, err = authClient.Code2Session(code) return } // 获取解密信息 GetDecryptInfo func GetDecryptInfo(sessionKey, encryptedData, iv string) (decryptData *encryptor.PlainData, err error) { wechatClient := GetWxApp() encryptorClient := wechatClient.GetEncryptor() decryptData, err = encryptorClient.Decrypt(sessionKey, encryptedData, iv) return } // GetSunCode 获取太阳码 func GetSunCode(page, scene string) (resp []byte, err error) { // 此处因初始化顺序问题不放在init中 env := "trial" if utils.RunMode == "release" { env = "release" } codePars := qrcode.QRCoder{ Page: page, Scene: scene, EnvVersion: env, } wechatClient := GetWxApp() qr := wechatClient.GetQRCode() return qr.GetWXACodeUnlimit(codePars) }