wx_app.go 1.8 KB

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