client.go 1.9 KB

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