wx_app.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package wx_app
  2. import (
  3. wechat "github.com/silenceper/wechat/v2"
  4. "github.com/silenceper/wechat/v2/cache"
  5. "github.com/silenceper/wechat/v2/miniprogram"
  6. "github.com/silenceper/wechat/v2/miniprogram/auth"
  7. "github.com/silenceper/wechat/v2/miniprogram/config"
  8. "github.com/silenceper/wechat/v2/miniprogram/encryptor"
  9. )
  10. func GetWxApp() (miniprogram *miniprogram.MiniProgram) {
  11. wc := wechat.NewWechat()
  12. memory := cache.NewMemory()
  13. //memory := cache.NewRedis(global.Redis)
  14. // var WxId = `gh_75abb562a946`
  15. var WxAppId = `wxb059c872d79b9967` // 投研小程序
  16. var WxAppSecret = `1737c73e9f69a21de1a345b8f0800258`
  17. cfg := &config.Config{
  18. // AppID: utils.WX_APPID,
  19. // AppSecret: utils.WX_APP_SECRET,
  20. AppID: WxAppId,
  21. AppSecret: WxAppSecret,
  22. Cache: memory,
  23. }
  24. miniprogram = wc.GetMiniProgram(cfg)
  25. return
  26. }
  27. // GetSession 获取用户详情
  28. func GetSession(code string) (userInfo auth.ResCode2Session, err error) {
  29. wechatClient := GetWxApp()
  30. authClient := wechatClient.GetAuth()
  31. userInfo, err = authClient.Code2Session(code)
  32. return
  33. }
  34. // 获取解密信息 GetDecryptInfo
  35. func GetDecryptInfo(sessionKey, encryptedData, iv string) (decryptData *encryptor.PlainData, err error) {
  36. wechatClient := GetWxApp()
  37. encryptorClient := wechatClient.GetEncryptor()
  38. decryptData, err = encryptorClient.Decrypt(sessionKey, encryptedData, iv)
  39. return
  40. }
  41. // // MsgSecCheck 检查一段文本是否含有违法违规内容。
  42. // func MsgSecCheck(openid string, content string) (result security.Result, err error) {
  43. // wechatClient := GetWxApp()
  44. // myMiniprogram := security.NewMyMiniprogram(wechatClient)
  45. // bodyContent := &security.BodyContent{
  46. // Version: 2,
  47. // Content: content,
  48. // Openid: openid,
  49. // Scene: 2,
  50. // }
  51. // return myMiniprogram.MsgSecCheckWithResult(bodyContent)
  52. // }