officialaccount.go 7.3 KB

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