client.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/message"
  8. "dashoo.cn/opms_libary/plugin/dingtalk/workflow"
  9. "github.com/gogf/gf/os/gcache"
  10. "sync"
  11. )
  12. var Client *ClientImpl
  13. var memCache *gcache.Cache
  14. // ClientImpl struct
  15. type ClientImpl struct {
  16. Context *context.Context
  17. }
  18. func init() {
  19. Client = NewClient()
  20. }
  21. func NewClient() *ClientImpl {
  22. var config = context.Config{
  23. //微信公众平台,需要填写的信息
  24. AppKey: "dinguytykawticadfoht", //g.Config().GetString("dingtalk.app-key"), //"your app id",
  25. AppSecret: "zPlj4ZpITsUbeq2C0GrwJ78-e8knH_kIeyvznaNQacqtrSb9zbeZcOajgBKdolky", //g.Config().GetString("dingtalk.app-secret"), //"your app secret",
  26. AESKey: "oUjmeWea8Ow1jsdK4UHoDthy6EMQKq3RGbM2rEeTgnm",
  27. Token: "WaasHsYk8V3wqwN5xRGsCmiiRDB",
  28. }
  29. return newClient(config)
  30. }
  31. func newClient(cfg context.Config) *ClientImpl {
  32. context := new(context.Context)
  33. initContext(&cfg, context)
  34. return &ClientImpl{context}
  35. }
  36. func initContext(cfg *context.Config, context *context.Context) {
  37. if cfg.Cache == nil {
  38. if memCache == nil {
  39. memCache = gcache.New()
  40. }
  41. cfg.Cache = memCache
  42. }
  43. context.Config = cfg
  44. context.SetAccessTokenLock(new(sync.RWMutex))
  45. }
  46. //GetAccessToken 获取access_token
  47. func (c *ClientImpl) GetAccessToken() (string, error) {
  48. return c.Context.GetAccessToken()
  49. }
  50. //GetContact 通讯录
  51. func (c *ClientImpl) GetContact() *contact.Contact {
  52. return contact.NewContact(c.Context)
  53. }
  54. //GetWorkflow OA审批
  55. func (c *ClientImpl) GetWorkflow() *workflow.Workflow {
  56. return workflow.NewWorkflow(c.Context)
  57. }
  58. //GetCalendar 日程
  59. func (c *ClientImpl) GetCalendar() *calendar.Calendar {
  60. return calendar.NewCalendar(c.Context)
  61. }
  62. // GetDingTalkHandler 消息管理
  63. func (c *ClientImpl) GetDingTalkHandler(msg *message.SubsMessage) *bridge.DingTalkHandler {
  64. c.Context.SubsMessage = msg
  65. return bridge.NewDingTalkHandler(c.Context)
  66. }