wx_app.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package wx_app
  2. import (
  3. "fmt"
  4. wechat "github.com/silenceper/wechat/v2"
  5. "github.com/silenceper/wechat/v2/cache"
  6. "github.com/silenceper/wechat/v2/miniprogram"
  7. "github.com/silenceper/wechat/v2/miniprogram/auth"
  8. "github.com/silenceper/wechat/v2/miniprogram/config"
  9. "github.com/silenceper/wechat/v2/miniprogram/encryptor"
  10. "github.com/silenceper/wechat/v2/miniprogram/qrcode"
  11. "hongze/hongze_yb/global"
  12. )
  13. //微信小程序配置信息
  14. var (
  15. WxId string //微信原始ID
  16. WxAppId string
  17. WxAppSecret string
  18. WxPlatform int //用户来源,需要入库,用来保存该用户来自哪个平台,默认是:1
  19. EnvVersion string // 小程序版本, release-正式版; trial-体验版; develop-开发版
  20. )
  21. func init() {
  22. WxAppId = `wxb059c872d79b9967`
  23. WxId = `gh_75abb562a946`
  24. WxAppSecret = `1737c73e9f69a21de1a345b8f0800258`
  25. WxPlatform = 6 //弘则研报来源
  26. EnvVersion = "trial"
  27. if global.CONFIG.Serve.RunMode == "release" {
  28. EnvVersion = "release"
  29. }
  30. }
  31. func GetWxApp() (miniprogram *miniprogram.MiniProgram) {
  32. wc := wechat.NewWechat()
  33. memory := cache.NewMemory()
  34. //memory := cache.NewRedis(global.Redis)
  35. cfg := &config.Config{
  36. AppID: WxAppId,
  37. AppSecret: WxAppSecret,
  38. Cache: memory,
  39. }
  40. miniprogram = wc.GetMiniProgram(cfg)
  41. return
  42. }
  43. // GetSession 获取用户详情
  44. func GetSession(code string) (userInfo auth.ResCode2Session, err error) {
  45. wechatClient := GetWxApp()
  46. authClient := wechatClient.GetAuth()
  47. userInfo, err = authClient.Code2Session(code)
  48. return
  49. }
  50. // GetSession 获取用户详情
  51. func GetUserInfo(code string) (userInfo auth.ResCode2Session, err error) {
  52. wechatClient := GetWxApp()
  53. authClient := wechatClient.GetAuth()
  54. fmt.Println("code:", code)
  55. userInfo, err = authClient.Code2Session(code)
  56. return
  57. }
  58. // 获取解密信息 GetDecryptInfo
  59. func GetDecryptInfo(sessionKey, encryptedData, iv string) (decryptData *encryptor.PlainData, err error) {
  60. wechatClient := GetWxApp()
  61. encryptorClient := wechatClient.GetEncryptor()
  62. decryptData, err = encryptorClient.Decrypt(sessionKey, encryptedData, iv)
  63. return
  64. }
  65. // GetSunCode 获取太阳码
  66. func GetSunCode(page, scene string) (resp []byte, err error) {
  67. codePars := qrcode.QRCoder{
  68. Page: page,
  69. Scene: scene,
  70. EnvVersion: EnvVersion,
  71. }
  72. wechatClient := GetWxApp()
  73. qr := wechatClient.GetQRCode()
  74. return qr.GetWXACodeUnlimit(codePars)
  75. }