officialaccount.go 3.0 KB

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