dingtalk_oa.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package xnet
  2. import (
  3. "encoding/json"
  4. "gcore/core"
  5. "log"
  6. "net/http"
  7. )
  8. type DevConf struct {
  9. AppKey string
  10. AppSecret string
  11. GetTokenUrl string
  12. GetDeptUserListUrl string
  13. Token string
  14. Params map[string]string
  15. Headers map[string]string
  16. Cookie []*http.Cookie
  17. DeptId int
  18. }
  19. // @title GetToken
  20. // @description 获取access token
  21. // @param
  22. // @return
  23. func (m *DevConf) GetToken() error {
  24. params := map[string]string{"appkey": m.AppKey, "appsecret": m.AppSecret}
  25. content, err := GetX(m.GetTokenUrl, params, m.Headers, m.Cookie)
  26. if err != nil {
  27. log.Printf("GetToken error : %v", err)
  28. }
  29. //fmt.Printf("%v\n", content)
  30. ret := map[string]interface{}{}
  31. err = json.Unmarshal([]byte(content), &ret)
  32. if err != nil {
  33. log.Printf("Unmarshal error : %v", err)
  34. return err
  35. }
  36. m.Token = ret["access_token"].(string)
  37. m.Params["access_token"] = ret["access_token"].(string)
  38. m.Headers["Content-Type"] = "application/json"
  39. return nil
  40. }
  41. // @title GetDeptUserList
  42. // @description 获取部门成员的姓名和userid
  43. // @param
  44. // @return
  45. func (m *DevConf) GetDeptUserList() (deptUserList []map[string]string, err error) {
  46. cursor := 0
  47. size := 100
  48. for {
  49. data := map[string]interface{}{
  50. "dept_id": m.DeptId,
  51. "cursor": cursor,
  52. "size": size,
  53. }
  54. content, err := PostJson(m.GetDeptUserListUrl, m.Params, m.Headers, data, m.Cookie)
  55. if err != nil {
  56. log.Printf("GetToken error : %v", err)
  57. continue
  58. }
  59. ret := map[string]interface{}{}
  60. err = json.Unmarshal([]byte(content), &ret)
  61. if err != nil {
  62. log.Printf("Unmarshal error : %v", err)
  63. continue
  64. }
  65. result := ret["result"]
  66. for _, i := range result.(map[string]interface{})["list"].([]interface{}) {
  67. tmpData := core.StringInterfaceToStringString(i.(map[string]interface{}))
  68. deptUserList = append(deptUserList, tmpData)
  69. }
  70. //当前请求返回数据量小于size,跳出循环
  71. if len(result.(map[string]interface{})["list"].([]interface{})) < size {
  72. break
  73. }
  74. data["curosr"] = result.(map[string]interface{})["next_cursor"].(int64)
  75. }
  76. return
  77. }
  78. // @title GetDeptAttendanceData
  79. // @description 30天内部门考勤数据
  80. // @param
  81. // @return
  82. func GetDeptAttendanceData() {
  83. }