wx_app.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. )
  11. //微信小程序配置信息
  12. var (
  13. WxId string //微信原始ID
  14. WxAppId string
  15. WxAppSecret string
  16. WxPlatform int //用户来源,需要入库,用来保存该用户来自哪个平台,默认是:1
  17. )
  18. func init() {
  19. WxAppId = `wxb059c872d79b9967`
  20. WxId = `gh_75abb562a946`
  21. WxAppSecret = `1737c73e9f69a21de1a345b8f0800258`
  22. WxPlatform = 6 //弘则研报来源
  23. }
  24. func GetWxApp() (miniprogram *miniprogram.MiniProgram) {
  25. wc := wechat.NewWechat()
  26. memory := cache.NewMemory()
  27. //memory := cache.NewRedis(global.Redis)
  28. cfg := &config.Config{
  29. AppID: WxAppId,
  30. AppSecret: WxAppSecret,
  31. Cache: memory,
  32. }
  33. miniprogram = wc.GetMiniProgram(cfg)
  34. return
  35. }
  36. // GetSession 获取用户详情
  37. func GetSession(code string) (userInfo auth.ResCode2Session, err error) {
  38. wechatClient := GetWxApp()
  39. authClient := wechatClient.GetAuth()
  40. userInfo, err = authClient.Code2Session(code)
  41. return
  42. }
  43. // GetSession 获取用户详情
  44. func GetUserInfo(code string) (userInfo auth.ResCode2Session, err error) {
  45. wechatClient := GetWxApp()
  46. authClient := wechatClient.GetAuth()
  47. fmt.Println("code:", code)
  48. userInfo, err = authClient.Code2Session(code)
  49. return
  50. }
  51. // 获取解密信息 GetDecryptInfo
  52. func GetDecryptInfo(sessionKey, encryptedData, iv string) (decryptData *encryptor.PlainData, err error) {
  53. wechatClient := GetWxApp()
  54. encryptorClient := wechatClient.GetEncryptor()
  55. decryptData, err = encryptorClient.Decrypt(sessionKey, encryptedData, iv)
  56. return
  57. }
  58. //func GetCode() {
  59. // codeParms := qrcode.QRCoder{
  60. // Page: "",
  61. // Path: "",
  62. // Width: 0,
  63. // Scene: "",
  64. // AutoColor: false,
  65. // LineColor: qrcode.Color{},
  66. // IsHyaline: false,
  67. // }
  68. // //wxApp := GetWxApp()
  69. // //qrCode := wxApp.GetQRCode()
  70. // //qrCode.
  71. // //qrCode.GetWXACodeUnlimit()
  72. //}