wx_app.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package wx_app
  2. import (
  3. "eta/eta_mini_api/utils"
  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. )
  11. func GetWxApp() (miniprogram *miniprogram.MiniProgram) {
  12. wc := wechat.NewWechat()
  13. memory := cache.NewMemory()
  14. // memory := cache.NewRedis(utils.Redis)
  15. cfg := &config.Config{
  16. AppID: utils.WX_MINI_APPID,
  17. AppSecret: utils.WX_MINI_APP_SECRET,
  18. Cache: memory,
  19. }
  20. miniprogram = wc.GetMiniProgram(cfg)
  21. return
  22. }
  23. // GetSession 获取用户详情
  24. func GetSession(code string) (userInfo auth.ResCode2Session, err error) {
  25. wechatClient := GetWxApp()
  26. authClient := wechatClient.GetAuth()
  27. userInfo, err = authClient.Code2Session(code)
  28. return
  29. }
  30. // 获取解密信息 GetDecryptInfo
  31. func GetDecryptInfo(sessionKey, encryptedData, iv string) (decryptData *encryptor.PlainData, err error) {
  32. wechatClient := GetWxApp()
  33. encryptorClient := wechatClient.GetEncryptor()
  34. decryptData, err = encryptorClient.Decrypt(sessionKey, encryptedData, iv)
  35. return
  36. }
  37. // // MsgSecCheck 检查一段文本是否含有违法违规内容。
  38. // func MsgSecCheck(openid string, content string) (result security.Result, err error) {
  39. // wechatClient := GetWxApp()
  40. // myMiniprogram := security.NewMyMiniprogram(wechatClient)
  41. // bodyContent := &security.BodyContent{
  42. // Version: 2,
  43. // Content: content,
  44. // Openid: openid,
  45. // Scene: 2,
  46. // }
  47. // return myMiniprogram.MsgSecCheckWithResult(bodyContent)
  48. // }