officialaccount.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package officialaccount
  2. import (
  3. "net/http"
  4. "github.com/silenceper/wechat/v2/credential"
  5. "github.com/silenceper/wechat/v2/officialaccount/basic"
  6. "github.com/silenceper/wechat/v2/officialaccount/broadcast"
  7. "github.com/silenceper/wechat/v2/officialaccount/config"
  8. "github.com/silenceper/wechat/v2/officialaccount/context"
  9. "github.com/silenceper/wechat/v2/officialaccount/device"
  10. "github.com/silenceper/wechat/v2/officialaccount/js"
  11. "github.com/silenceper/wechat/v2/officialaccount/material"
  12. "github.com/silenceper/wechat/v2/officialaccount/menu"
  13. "github.com/silenceper/wechat/v2/officialaccount/message"
  14. "github.com/silenceper/wechat/v2/officialaccount/oauth"
  15. "github.com/silenceper/wechat/v2/officialaccount/server"
  16. "github.com/silenceper/wechat/v2/officialaccount/user"
  17. )
  18. //OfficialAccount 微信公众号相关API
  19. type OfficialAccount struct {
  20. ctx *context.Context
  21. }
  22. //NewOfficialAccount 实例化公众号API
  23. func NewOfficialAccount(cfg *config.Config) *OfficialAccount {
  24. defaultAkHandle := credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, credential.CacheKeyOfficialAccountPrefix, cfg.Cache)
  25. ctx := &context.Context{
  26. Config: cfg,
  27. AccessTokenHandle: defaultAkHandle,
  28. }
  29. return &OfficialAccount{ctx: ctx}
  30. }
  31. //SetAccessTokenHandle 自定义access_token获取方式
  32. func (officialAccount *OfficialAccount) SetAccessTokenHandle(accessTokenHandle credential.AccessTokenHandle) {
  33. officialAccount.ctx.AccessTokenHandle = accessTokenHandle
  34. }
  35. // GetContext get Context
  36. func (officialAccount *OfficialAccount) GetContext() *context.Context {
  37. return officialAccount.ctx
  38. }
  39. // GetBasic qr/url 相关配置
  40. func (officialAccount *OfficialAccount) GetBasic() *basic.Basic {
  41. return basic.NewBasic(officialAccount.ctx)
  42. }
  43. // GetMenu 菜单管理接口
  44. func (officialAccount *OfficialAccount) GetMenu() *menu.Menu {
  45. return menu.NewMenu(officialAccount.ctx)
  46. }
  47. // GetServer 消息管理:接收事件,被动回复消息管理
  48. func (officialAccount *OfficialAccount) GetServer(req *http.Request, writer http.ResponseWriter) *server.Server {
  49. srv := server.NewServer(officialAccount.ctx)
  50. srv.Request = req
  51. srv.Writer = writer
  52. return srv
  53. }
  54. //GetAccessToken 获取access_token
  55. func (officialAccount *OfficialAccount) GetAccessToken() (string, error) {
  56. return officialAccount.ctx.GetAccessToken()
  57. }
  58. // GetOauth oauth2网页授权
  59. func (officialAccount *OfficialAccount) GetOauth() *oauth.Oauth {
  60. return oauth.NewOauth(officialAccount.ctx)
  61. }
  62. // GetMaterial 素材管理
  63. func (officialAccount *OfficialAccount) GetMaterial() *material.Material {
  64. return material.NewMaterial(officialAccount.ctx)
  65. }
  66. // GetJs js-sdk配置
  67. func (officialAccount *OfficialAccount) GetJs() *js.Js {
  68. return js.NewJs(officialAccount.ctx)
  69. }
  70. // GetUser 用户管理接口
  71. func (officialAccount *OfficialAccount) GetUser() *user.User {
  72. return user.NewUser(officialAccount.ctx)
  73. }
  74. // GetTemplate 模板消息接口
  75. func (officialAccount *OfficialAccount) GetTemplate() *message.Template {
  76. return message.NewTemplate(officialAccount.ctx)
  77. }
  78. // GetDevice 获取智能设备的实例
  79. func (officialAccount *OfficialAccount) GetDevice() *device.Device {
  80. return device.NewDevice(officialAccount.ctx)
  81. }
  82. //GetBroadcast 群发消息
  83. //TODO 待完善
  84. func (officialAccount *OfficialAccount) GetBroadcast() *broadcast.Broadcast {
  85. return broadcast.NewBroadcast(officialAccount.ctx)
  86. }