wx_app.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. if global.CONFIG.Serve.RunMode == "release" {
  27. EnvVersion = "release"
  28. } else {
  29. EnvVersion = "trial"
  30. }
  31. }
  32. func GetWxApp() (miniprogram *miniprogram.MiniProgram) {
  33. wc := wechat.NewWechat()
  34. memory := cache.NewMemory()
  35. //memory := cache.NewRedis(global.Redis)
  36. cfg := &config.Config{
  37. AppID: WxAppId,
  38. AppSecret: WxAppSecret,
  39. Cache: memory,
  40. }
  41. miniprogram = wc.GetMiniProgram(cfg)
  42. return
  43. }
  44. // GetSession 获取用户详情
  45. func GetSession(code string) (userInfo auth.ResCode2Session, err error) {
  46. wechatClient := GetWxApp()
  47. authClient := wechatClient.GetAuth()
  48. userInfo, err = authClient.Code2Session(code)
  49. return
  50. }
  51. // GetSession 获取用户详情
  52. func GetUserInfo(code string) (userInfo auth.ResCode2Session, err error) {
  53. wechatClient := GetWxApp()
  54. authClient := wechatClient.GetAuth()
  55. fmt.Println("code:", code)
  56. userInfo, err = authClient.Code2Session(code)
  57. return
  58. }
  59. // 获取解密信息 GetDecryptInfo
  60. func GetDecryptInfo(sessionKey, encryptedData, iv string) (decryptData *encryptor.PlainData, err error) {
  61. wechatClient := GetWxApp()
  62. encryptorClient := wechatClient.GetEncryptor()
  63. decryptData, err = encryptorClient.Decrypt(sessionKey, encryptedData, iv)
  64. return
  65. }
  66. // GetSunCode 获取太阳码
  67. func GetSunCode(page, scene string) (resp []byte, err error) {
  68. codePars := qrcode.QRCoder{
  69. Page: page,
  70. Scene: scene,
  71. EnvVersion: EnvVersion,
  72. }
  73. wechatClient := GetWxApp()
  74. qr := wechatClient.GetQRCode()
  75. return qr.GetWXACodeUnlimit(codePars)
  76. }