wx_app.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. }
  35. // GetSession 获取用户详情
  36. func GetSession(code string) (userInfo auth.ResCode2Session, err error) {
  37. wechatClient := GetWxApp()
  38. authClient := wechatClient.GetAuth()
  39. userInfo, err = authClient.Code2Session(code)
  40. return
  41. }
  42. // GetSession 获取用户详情
  43. func GetUserInfo(code string) (userInfo auth.ResCode2Session, err error) {
  44. wechatClient := GetWxApp()
  45. authClient := wechatClient.GetAuth()
  46. fmt.Println("code:", code)
  47. userInfo, err = authClient.Code2Session(code)
  48. return
  49. }
  50. //func GetCode() {
  51. // codeParms := qrcode.QRCoder{
  52. // Page: "",
  53. // Path: "",
  54. // Width: 0,
  55. // Scene: "",
  56. // AutoColor: false,
  57. // LineColor: qrcode.Color{},
  58. // IsHyaline: false,
  59. // }
  60. // //wxApp := GetWxApp()
  61. // //qrCode := wxApp.GetQRCode()
  62. // //qrCode.
  63. // //qrCode.GetWXACodeUnlimit()
  64. //}