client.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package dingtalk
  2. import (
  3. "dashoo.cn/opms_libary/plugin/dingtalk/bridge"
  4. "dashoo.cn/opms_libary/plugin/dingtalk/calendar"
  5. "dashoo.cn/opms_libary/plugin/dingtalk/contact"
  6. "dashoo.cn/opms_libary/plugin/dingtalk/context"
  7. "dashoo.cn/opms_libary/plugin/dingtalk/jsapi"
  8. "dashoo.cn/opms_libary/plugin/dingtalk/message"
  9. "dashoo.cn/opms_libary/plugin/dingtalk/message/corpconversation"
  10. "dashoo.cn/opms_libary/plugin/dingtalk/storage"
  11. "dashoo.cn/opms_libary/plugin/dingtalk/workflow"
  12. "github.com/gogf/gf/frame/g"
  13. "github.com/gogf/gf/os/gcache"
  14. "sync"
  15. )
  16. var Client *ClientImpl
  17. var memCache *gcache.Cache
  18. // ClientImpl struct
  19. type ClientImpl struct {
  20. Context *context.Context
  21. }
  22. func init() {
  23. Client = NewClient()
  24. }
  25. func NewClient() *ClientImpl {
  26. var config = context.Config{
  27. CorpId: g.Config().GetString("dingtalk.corp-id"), //
  28. AgentId: g.Config().GetString("dingtalk.agent-id"), //
  29. AppKey: g.Config().GetString("dingtalk.app-key"), //
  30. AppSecret: g.Config().GetString("dingtalk.app-secret"), //
  31. AESKey: g.Config().GetString("dingtalk.aes-key"), //
  32. Token: g.Config().GetString("dingtalk.token"), //
  33. }
  34. return newClient(config)
  35. }
  36. func newClient(cfg context.Config) *ClientImpl {
  37. context := new(context.Context)
  38. initContext(&cfg, context)
  39. return &ClientImpl{context}
  40. }
  41. func initContext(cfg *context.Config, context *context.Context) {
  42. if cfg.Cache == nil {
  43. if memCache == nil {
  44. memCache = gcache.New()
  45. }
  46. cfg.Cache = memCache
  47. }
  48. context.Config = cfg
  49. context.SetAccessTokenLock(new(sync.RWMutex))
  50. }
  51. // GetAccessToken 获取access_token
  52. func (c *ClientImpl) GetAccessToken() (string, error) {
  53. return c.Context.GetAccessToken()
  54. }
  55. // GetJsapi 获取Jsapi
  56. func (c *ClientImpl) GetJsapi() *jsapi.Jsapi {
  57. return jsapi.NewJsapi(c.Context)
  58. }
  59. // GetContact 通讯录
  60. func (c *ClientImpl) GetContact() *contact.Contact {
  61. return contact.NewContact(c.Context)
  62. }
  63. // GetContact 通讯录
  64. func (c *ClientImpl) GetCorpConversation() *corpconversation.CorpConversation {
  65. return corpconversation.NewCorpConversation(c.Context)
  66. }
  67. // GetWorkflow OA审批
  68. func (c *ClientImpl) GetWorkflow() *workflow.Workflow {
  69. return workflow.NewWorkflow(c.Context)
  70. }
  71. // GetCalendar 日程
  72. func (c *ClientImpl) GetCalendar() *calendar.Calendar {
  73. return calendar.NewCalendar(c.Context)
  74. }
  75. // GetStorage 获取Jsapi
  76. func (c *ClientImpl) GetStorage() *storage.Storage {
  77. return storage.NewStorage(c.Context)
  78. }
  79. // GetDingTalkHandler 消息管理
  80. func (c *ClientImpl) GetDingTalkHandler(msg *message.SubsMessage) *bridge.DingTalkHandler {
  81. c.Context.SubsMessage = msg
  82. return bridge.NewDingTalkHandler(c.Context)
  83. }