init.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package hook
  2. import (
  3. "github.com/gorilla/websocket"
  4. "github.com/imroc/req/v3"
  5. "net/url"
  6. "time"
  7. )
  8. type Hook struct {
  9. ServerIp string
  10. AdminPort string
  11. WxPort string
  12. Client *req.Client
  13. WorkPhoneUrl string
  14. Ctype uint64
  15. }
  16. func NewHook(serverIp string, adminPort string, WxPort string) *Hook {
  17. client := req.C().DevMode()
  18. client.SetCommonRetryCount(2).
  19. SetCommonRetryBackoffInterval(1*time.Second, 5*time.Second).
  20. SetCommonRetryFixedInterval(2 * time.Second).SetTimeout(30 * time.Second)
  21. workPhoneUrl := url.URL{Scheme: "ws", Host: "chat.gkscrm.com:13088"}
  22. return &Hook{
  23. ServerIp: serverIp,
  24. AdminPort: adminPort,
  25. WxPort: WxPort,
  26. Client: req.C().DevMode(),
  27. WorkPhoneUrl: workPhoneUrl.String(),
  28. Ctype: 1,
  29. }
  30. }
  31. // NewWecomHook 企微客户端初始化
  32. func NewWecomHook(serverIp string, adminPort string, WxPort string) *Hook {
  33. client := req.C().DevMode()
  34. client.SetCommonRetryCount(2).
  35. SetCommonRetryBackoffInterval(1*time.Second, 5*time.Second).
  36. SetCommonRetryFixedInterval(2 * time.Second).SetTimeout(30 * time.Second)
  37. wecomUrl := url.URL{Scheme: "ws", Host: "wecom.gkscrm.com:15088"}
  38. return &Hook{
  39. ServerIp: serverIp,
  40. AdminPort: adminPort,
  41. WxPort: WxPort,
  42. Client: req.C().DevMode(),
  43. WorkPhoneUrl: wecomUrl.String(),
  44. Ctype: 3,
  45. }
  46. }
  47. func (h *Hook) setServerIp(ip string) *Hook {
  48. h.ServerIp = ip
  49. return h
  50. }
  51. func (h *Hook) setAdminPort(port string) *Hook {
  52. h.AdminPort = port
  53. return h
  54. }
  55. func (h *Hook) setWxPort(port string) *Hook {
  56. h.WxPort = port
  57. return h
  58. }
  59. func (h *Hook) connWorkPhone() (*websocket.Conn, error) {
  60. dialer := websocket.DefaultDialer
  61. conn, _, err := dialer.Dial(h.WorkPhoneUrl, nil)
  62. if err != nil {
  63. return nil, err
  64. }
  65. return conn, nil
  66. }