officialaccount.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. basic *basic.Basic
  26. menu *menu.Menu
  27. oauth *oauth.Oauth
  28. material *material.Material
  29. draft *draft.Draft
  30. freepublish *freepublish.FreePublish
  31. js *js.Js
  32. user *user.User
  33. templateMsg *message.Template
  34. managerMsg *message.Manager
  35. device *device.Device
  36. broadcast *broadcast.Broadcast
  37. datacube *datacube.DataCube
  38. ocr *ocr.OCR
  39. subscribeMsg *message.Subscribe
  40. }
  41. // NewOfficialAccount 实例化公众号API
  42. func NewOfficialAccount(cfg *config.Config) *OfficialAccount {
  43. defaultAkHandle := credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, credential.CacheKeyOfficialAccountPrefix, cfg.Cache)
  44. ctx := &context.Context{
  45. Config: cfg,
  46. AccessTokenHandle: defaultAkHandle,
  47. }
  48. return &OfficialAccount{ctx: ctx}
  49. }
  50. // SetAccessTokenHandle 自定义access_token获取方式
  51. func (officialAccount *OfficialAccount) SetAccessTokenHandle(accessTokenHandle credential.AccessTokenHandle) {
  52. officialAccount.ctx.AccessTokenHandle = accessTokenHandle
  53. }
  54. // GetContext get Context
  55. func (officialAccount *OfficialAccount) GetContext() *context.Context {
  56. return officialAccount.ctx
  57. }
  58. // GetBasic qr/url 相关配置
  59. func (officialAccount *OfficialAccount) GetBasic() *basic.Basic {
  60. if officialAccount.basic == nil {
  61. officialAccount.basic = basic.NewBasic(officialAccount.ctx)
  62. }
  63. return officialAccount.basic
  64. }
  65. // GetMenu 菜单管理接口
  66. func (officialAccount *OfficialAccount) GetMenu() *menu.Menu {
  67. if officialAccount.menu == nil {
  68. officialAccount.menu = menu.NewMenu(officialAccount.ctx)
  69. }
  70. return officialAccount.menu
  71. }
  72. // GetServer 消息管理:接收事件,被动回复消息管理
  73. func (officialAccount *OfficialAccount) GetServer(req *http.Request, writer http.ResponseWriter) *server.Server {
  74. srv := server.NewServer(officialAccount.ctx)
  75. srv.Request = req
  76. srv.Writer = writer
  77. return srv
  78. }
  79. // GetAccessToken 获取access_token
  80. func (officialAccount *OfficialAccount) GetAccessToken() (string, error) {
  81. return officialAccount.ctx.GetAccessToken()
  82. }
  83. // GetOauth oauth2网页授权
  84. func (officialAccount *OfficialAccount) GetOauth() *oauth.Oauth {
  85. if officialAccount.oauth == nil {
  86. officialAccount.oauth = oauth.NewOauth(officialAccount.ctx)
  87. }
  88. return officialAccount.oauth
  89. }
  90. // GetMaterial 素材管理
  91. func (officialAccount *OfficialAccount) GetMaterial() *material.Material {
  92. if officialAccount.material == nil {
  93. officialAccount.material = material.NewMaterial(officialAccount.ctx)
  94. }
  95. return officialAccount.material
  96. }
  97. // GetDraft 草稿箱
  98. func (officialAccount *OfficialAccount) GetDraft() *draft.Draft {
  99. if officialAccount.draft == nil {
  100. officialAccount.draft = draft.NewDraft(officialAccount.ctx)
  101. }
  102. return officialAccount.draft
  103. }
  104. // GetFreePublish 发布能力
  105. func (officialAccount *OfficialAccount) GetFreePublish() *freepublish.FreePublish {
  106. if officialAccount.freepublish == nil {
  107. officialAccount.freepublish = freepublish.NewFreePublish(officialAccount.ctx)
  108. }
  109. return officialAccount.freepublish
  110. }
  111. // GetJs js-sdk配置
  112. func (officialAccount *OfficialAccount) GetJs() *js.Js {
  113. if officialAccount.js == nil {
  114. officialAccount.js = js.NewJs(officialAccount.ctx)
  115. }
  116. return officialAccount.js
  117. }
  118. // GetUser 用户管理接口
  119. func (officialAccount *OfficialAccount) GetUser() *user.User {
  120. if officialAccount.user == nil {
  121. officialAccount.user = user.NewUser(officialAccount.ctx)
  122. }
  123. return officialAccount.user
  124. }
  125. // GetTemplate 模板消息接口
  126. func (officialAccount *OfficialAccount) GetTemplate() *message.Template {
  127. if officialAccount.templateMsg == nil {
  128. officialAccount.templateMsg = message.NewTemplate(officialAccount.ctx)
  129. }
  130. return officialAccount.templateMsg
  131. }
  132. // GetCustomerMessageManager 客服消息接口
  133. func (officialAccount *OfficialAccount) GetCustomerMessageManager() *message.Manager {
  134. if officialAccount.managerMsg == nil {
  135. officialAccount.managerMsg = message.NewMessageManager(officialAccount.ctx)
  136. }
  137. return officialAccount.managerMsg
  138. }
  139. // GetDevice 获取智能设备的实例
  140. func (officialAccount *OfficialAccount) GetDevice() *device.Device {
  141. if officialAccount.device == nil {
  142. officialAccount.device = device.NewDevice(officialAccount.ctx)
  143. }
  144. return officialAccount.device
  145. }
  146. // GetBroadcast 群发消息
  147. // TODO 待完善
  148. func (officialAccount *OfficialAccount) GetBroadcast() *broadcast.Broadcast {
  149. if officialAccount.broadcast == nil {
  150. officialAccount.broadcast = broadcast.NewBroadcast(officialAccount.ctx)
  151. }
  152. return officialAccount.broadcast
  153. }
  154. // GetDataCube 数据统计
  155. func (officialAccount *OfficialAccount) GetDataCube() *datacube.DataCube {
  156. if officialAccount.datacube == nil {
  157. officialAccount.datacube = datacube.NewCube(officialAccount.ctx)
  158. }
  159. return officialAccount.datacube
  160. }
  161. // GetOCR OCR接口
  162. func (officialAccount *OfficialAccount) GetOCR() *ocr.OCR {
  163. if officialAccount.ocr == nil {
  164. officialAccount.ocr = ocr.NewOCR(officialAccount.ctx)
  165. }
  166. return officialAccount.ocr
  167. }
  168. // GetSubscribe 公众号订阅消息
  169. func (officialAccount *OfficialAccount) GetSubscribe() *message.Subscribe {
  170. if officialAccount.subscribeMsg == nil {
  171. officialAccount.subscribeMsg = message.NewSubscribe(officialAccount.ctx)
  172. }
  173. return officialAccount.subscribeMsg
  174. }