1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package services
- import (
- "eta_gn/eta_api/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"
- )
- var (
- WxId string //微信原始ID
- WxAppId string
- WxAppSecret string
- WxPlatform int //用户来源,需要入库,用来保存该用户来自哪个平台,默认是:1
- EnvVersion string // 小程序版本, release-正式版; trial-体验版; develop-开发版
- )
- func GetWxApp() (miniprogram *miniprogram.MiniProgram) {
- wc := wechat.NewWechat()
- memory := cache.NewMemory()
- cfg := &config.Config{
- AppID: utils.WxYbAppId,
- AppSecret: utils.WxYbAppSecret,
- Cache: memory,
- }
- miniprogram = wc.GetMiniProgram(cfg)
- return
- }
- func GetSession(code string) (userInfo auth.ResCode2Session, err error) {
- wechatClient := GetWxApp()
- authClient := wechatClient.GetAuth()
- userInfo, err = authClient.Code2Session(code)
- return
- }
- func GetUserInfo(code string) (userInfo auth.ResCode2Session, err error) {
- wechatClient := GetWxApp()
- authClient := wechatClient.GetAuth()
- fmt.Println("code:", code)
- userInfo, err = authClient.Code2Session(code)
- return
- }
- func GetDecryptInfo(sessionKey, encryptedData, iv string) (decryptData *encryptor.PlainData, err error) {
- wechatClient := GetWxApp()
- encryptorClient := wechatClient.GetEncryptor()
- decryptData, err = encryptorClient.Decrypt(sessionKey, encryptedData, iv)
- return
- }
- func GetSunCode(page, scene string) (resp []byte, err error) {
- 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)
- }
|