officialaccount.go 6.5 KB

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