officialaccount.go 7.1 KB

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