officialaccount.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package officialaccount
  2. import (
  3. "net/http"
  4. "github.com/silenceper/wechat/v2/officialaccount/draft"
  5. "github.com/silenceper/wechat/v2/officialaccount/freepublish"
  6. "github.com/silenceper/wechat/v2/officialaccount/ocr"
  7. "github.com/silenceper/wechat/v2/officialaccount/datacube"
  8. "github.com/silenceper/wechat/v2/credential"
  9. "github.com/silenceper/wechat/v2/officialaccount/basic"
  10. "github.com/silenceper/wechat/v2/officialaccount/broadcast"
  11. "github.com/silenceper/wechat/v2/officialaccount/config"
  12. "github.com/silenceper/wechat/v2/officialaccount/context"
  13. "github.com/silenceper/wechat/v2/officialaccount/device"
  14. "github.com/silenceper/wechat/v2/officialaccount/js"
  15. "github.com/silenceper/wechat/v2/officialaccount/material"
  16. "github.com/silenceper/wechat/v2/officialaccount/menu"
  17. "github.com/silenceper/wechat/v2/officialaccount/message"
  18. "github.com/silenceper/wechat/v2/officialaccount/oauth"
  19. "github.com/silenceper/wechat/v2/officialaccount/server"
  20. "github.com/silenceper/wechat/v2/officialaccount/user"
  21. )
  22. // OfficialAccount 微信公众号相关API
  23. type OfficialAccount struct {
  24. ctx *context.Context
  25. }
  26. // NewOfficialAccount 实例化公众号API
  27. func NewOfficialAccount(cfg *config.Config) *OfficialAccount {
  28. defaultAkHandle := credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, credential.CacheKeyOfficialAccountPrefix, cfg.Cache)
  29. ctx := &context.Context{
  30. Config: cfg,
  31. AccessTokenHandle: defaultAkHandle,
  32. }
  33. return &OfficialAccount{ctx: ctx}
  34. }
  35. // SetAccessTokenHandle 自定义access_token获取方式
  36. func (officialAccount *OfficialAccount) SetAccessTokenHandle(accessTokenHandle credential.AccessTokenHandle) {
  37. officialAccount.ctx.AccessTokenHandle = accessTokenHandle
  38. }
  39. // GetContext get Context
  40. func (officialAccount *OfficialAccount) GetContext() *context.Context {
  41. return officialAccount.ctx
  42. }
  43. // GetBasic qr/url 相关配置
  44. func (officialAccount *OfficialAccount) GetBasic() *basic.Basic {
  45. return basic.NewBasic(officialAccount.ctx)
  46. }
  47. // GetMenu 菜单管理接口
  48. func (officialAccount *OfficialAccount) GetMenu() *menu.Menu {
  49. return menu.NewMenu(officialAccount.ctx)
  50. }
  51. // GetServer 消息管理:接收事件,被动回复消息管理
  52. func (officialAccount *OfficialAccount) GetServer(req *http.Request, writer http.ResponseWriter) *server.Server {
  53. srv := server.NewServer(officialAccount.ctx)
  54. srv.Request = req
  55. srv.Writer = writer
  56. return srv
  57. }
  58. // GetAccessToken 获取access_token
  59. func (officialAccount *OfficialAccount) GetAccessToken() (string, error) {
  60. return officialAccount.ctx.GetAccessToken()
  61. }
  62. // GetOauth oauth2网页授权
  63. func (officialAccount *OfficialAccount) GetOauth() *oauth.Oauth {
  64. return oauth.NewOauth(officialAccount.ctx)
  65. }
  66. // GetMaterial 素材管理
  67. func (officialAccount *OfficialAccount) GetMaterial() *material.Material {
  68. return material.NewMaterial(officialAccount.ctx)
  69. }
  70. // GetDraft 草稿箱
  71. func (officialAccount *OfficialAccount) GetDraft() *draft.Draft {
  72. return draft.NewDraft(officialAccount.ctx)
  73. }
  74. // GetFreePublish 发布能力
  75. func (officialAccount *OfficialAccount) GetFreePublish() *freepublish.FreePublish {
  76. return freepublish.NewFreePublish(officialAccount.ctx)
  77. }
  78. // GetJs js-sdk配置
  79. func (officialAccount *OfficialAccount) GetJs() *js.Js {
  80. return js.NewJs(officialAccount.ctx)
  81. }
  82. // GetUser 用户管理接口
  83. func (officialAccount *OfficialAccount) GetUser() *user.User {
  84. return user.NewUser(officialAccount.ctx)
  85. }
  86. // GetTemplate 模板消息接口
  87. func (officialAccount *OfficialAccount) GetTemplate() *message.Template {
  88. return message.NewTemplate(officialAccount.ctx)
  89. }
  90. // GetCustomerMessageManager 客服消息接口
  91. func (officialAccount *OfficialAccount) GetCustomerMessageManager() *message.Manager {
  92. return message.NewMessageManager(officialAccount.ctx)
  93. }
  94. // GetDevice 获取智能设备的实例
  95. func (officialAccount *OfficialAccount) GetDevice() *device.Device {
  96. return device.NewDevice(officialAccount.ctx)
  97. }
  98. // GetBroadcast 群发消息
  99. // TODO 待完善
  100. func (officialAccount *OfficialAccount) GetBroadcast() *broadcast.Broadcast {
  101. return broadcast.NewBroadcast(officialAccount.ctx)
  102. }
  103. // GetDataCube 数据统计
  104. func (officialAccount *OfficialAccount) GetDataCube() *datacube.DataCube {
  105. return datacube.NewCube(officialAccount.ctx)
  106. }
  107. // GetOCR OCR接口
  108. func (officialAccount *OfficialAccount) GetOCR() *ocr.OCR {
  109. return ocr.NewOCR(officialAccount.ctx)
  110. }
  111. // GetSubscribe 公众号订阅消息
  112. func (officialAccount *OfficialAccount) GetSubscribe() *message.Subscribe {
  113. return message.NewSubscribe(officialAccount.ctx)
  114. }