wx_app.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. "hongze/hongze_yb/services/wx_app/security"
  13. )
  14. // 微信小程序配置信息
  15. var (
  16. WxId string //微信原始ID
  17. WxAppId string
  18. WxAppSecret string
  19. WxPlatform int //用户来源,需要入库,用来保存该用户来自哪个平台,默认是:1
  20. EnvVersion string // 小程序版本, release-正式版; trial-体验版; develop-开发版
  21. SendWxTemplateMsgUrl string
  22. )
  23. func init() {
  24. WxAppId = `wxb059c872d79b9967`
  25. WxId = `gh_75abb562a946`
  26. WxAppSecret = `1737c73e9f69a21de1a345b8f0800258`
  27. WxPlatform = 6 //弘则研报来源
  28. if global.CONFIG.Serve.RunMode == "release" {
  29. EnvVersion = "release"
  30. SendWxTemplateMsgUrl = "http://127.0.0.1:8086/v1/wechat/send_template_msg"
  31. } else {
  32. EnvVersion = "trial"
  33. SendWxTemplateMsgUrl = "http://127.0.0.1:8086/v1/wechat/send_template_msg"
  34. }
  35. }
  36. func GetWxApp() (miniprogram *miniprogram.MiniProgram) {
  37. wc := wechat.NewWechat()
  38. memory := cache.NewMemory()
  39. //memory := cache.NewRedis(global.Redis)
  40. cfg := &config.Config{
  41. AppID: WxAppId,
  42. AppSecret: WxAppSecret,
  43. Cache: memory,
  44. }
  45. miniprogram = wc.GetMiniProgram(cfg)
  46. return
  47. }
  48. // GetSession 获取用户详情
  49. func GetSession(code string) (userInfo auth.ResCode2Session, err error) {
  50. wechatClient := GetWxApp()
  51. authClient := wechatClient.GetAuth()
  52. userInfo, err = authClient.Code2Session(code)
  53. return
  54. }
  55. // GetSession 获取用户详情
  56. func GetUserInfo(code string) (userInfo auth.ResCode2Session, err error) {
  57. wechatClient := GetWxApp()
  58. authClient := wechatClient.GetAuth()
  59. fmt.Println("code:", code)
  60. userInfo, err = authClient.Code2Session(code)
  61. return
  62. }
  63. // 获取解密信息 GetDecryptInfo
  64. func GetDecryptInfo(sessionKey, encryptedData, iv string) (decryptData *encryptor.PlainData, err error) {
  65. wechatClient := GetWxApp()
  66. encryptorClient := wechatClient.GetEncryptor()
  67. decryptData, err = encryptorClient.Decrypt(sessionKey, encryptedData, iv)
  68. return
  69. }
  70. // GetSunCode 获取太阳码
  71. func GetSunCode(page, scene string) (resp []byte, err error) {
  72. codePars := qrcode.QRCoder{
  73. Page: page,
  74. Scene: scene,
  75. EnvVersion: EnvVersion,
  76. }
  77. wechatClient := GetWxApp()
  78. qr := wechatClient.GetQRCode()
  79. return qr.GetWXACodeUnlimit(codePars)
  80. }
  81. // MsgSecCheck 检查一段文本是否含有违法违规内容。
  82. func MsgSecCheck(openid string, content string) (result security.Result, err error) {
  83. wechatClient := GetWxApp()
  84. myMiniprogram := security.NewMyMiniprogram(wechatClient)
  85. bodyContent := &security.BodyContent{
  86. Version: 2,
  87. Content: content,
  88. Openid: openid,
  89. Scene: 2,
  90. }
  91. return myMiniprogram.MsgSecCheckWithResult(bodyContent)
  92. }
  93. // GetSunCode 获取太阳码
  94. func GetSunCodeV2(page string) (resp []byte, err error) {
  95. codePars := qrcode.QRCoder{
  96. Path: page,
  97. EnvVersion: EnvVersion,
  98. Width: 256,
  99. }
  100. wechatClient := GetWxApp()
  101. qr := wechatClient.GetQRCode()
  102. return qr.GetWXACode(codePars)
  103. }