wx_app.go 2.4 KB

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