officialaccount.go 3.7 KB

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