client.go 2.4 KB

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