officialaccount.go 6.9 KB

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