wx_app.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package services
  2. import (
  3. "eta/eta_mobile/utils"
  4. "fmt"
  5. wechat "github.com/silenceper/wechat/v2"
  6. "github.com/silenceper/wechat/v2/cache"
  7. "github.com/silenceper/wechat/v2/miniprogram"
  8. "github.com/silenceper/wechat/v2/miniprogram/auth"
  9. "github.com/silenceper/wechat/v2/miniprogram/config"
  10. "github.com/silenceper/wechat/v2/miniprogram/encryptor"
  11. "github.com/silenceper/wechat/v2/miniprogram/qrcode"
  12. )
  13. func GetWxApp() (miniprogram *miniprogram.MiniProgram) {
  14. wc := wechat.NewWechat()
  15. memory := cache.NewMemory()
  16. //memory := cache.NewRedis(global.Redis)
  17. cfg := &config.Config{
  18. AppID: utils.WxYbAppId,
  19. AppSecret: utils.WxYbAppSecret,
  20. Cache: memory,
  21. }
  22. miniprogram = wc.GetMiniProgram(cfg)
  23. return
  24. }
  25. // GetSession 获取用户详情
  26. func GetSession(code string) (userInfo auth.ResCode2Session, err error) {
  27. wechatClient := GetWxApp()
  28. authClient := wechatClient.GetAuth()
  29. userInfo, err = authClient.Code2Session(code)
  30. return
  31. }
  32. // GetSession 获取用户详情
  33. func GetUserInfo(code string) (userInfo auth.ResCode2Session, err error) {
  34. wechatClient := GetWxApp()
  35. authClient := wechatClient.GetAuth()
  36. fmt.Println("code:", code)
  37. userInfo, err = authClient.Code2Session(code)
  38. return
  39. }
  40. // 获取解密信息 GetDecryptInfo
  41. func GetDecryptInfo(sessionKey, encryptedData, iv string) (decryptData *encryptor.PlainData, err error) {
  42. wechatClient := GetWxApp()
  43. encryptorClient := wechatClient.GetEncryptor()
  44. decryptData, err = encryptorClient.Decrypt(sessionKey, encryptedData, iv)
  45. return
  46. }
  47. // GetSunCode 获取太阳码
  48. func GetSunCode(page, scene string) (resp []byte, err error) {
  49. // 此处因初始化顺序问题不放在init中
  50. env := "trial"
  51. if utils.RunMode == "release" {
  52. env = "release"
  53. }
  54. codePars := qrcode.QRCoder{
  55. Page: page,
  56. Scene: scene,
  57. EnvVersion: env,
  58. }
  59. wechatClient := GetWxApp()
  60. qr := wechatClient.GetQRCode()
  61. return qr.GetWXACodeUnlimit(codePars)
  62. }