department.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package addresslist
  2. import (
  3. "fmt"
  4. "github.com/silenceper/wechat/v2/util"
  5. )
  6. const (
  7. // departmentCreateURL 创建部门
  8. departmentCreateURL = "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=%s"
  9. // departmentSimpleListURL 获取子部门 ID 列表
  10. departmentSimpleListURL = "https://qyapi.weixin.qq.com/cgi-bin/department/simplelist?access_token=%s&id=%d"
  11. // departmentListURL 获取部门列表
  12. departmentListURL = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=%s"
  13. departmentListByIDURL = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=%s&id=%d"
  14. // departmentGetURL 获取单个部门详情 https://qyapi.weixin.qq.com/cgi-bin/department/get?access_token=ACCESS_TOKEN&id=ID
  15. departmentGetURL = "https://qyapi.weixin.qq.com/cgi-bin/department/get?access_token=%s&id=%d"
  16. )
  17. type (
  18. // DepartmentCreateRequest 创建部门数据请求
  19. DepartmentCreateRequest struct {
  20. Name string `json:"name"`
  21. NameEn string `json:"name_en,omitempty"`
  22. ParentID int `json:"parentid"`
  23. Order int `json:"order,omitempty"`
  24. ID int `json:"id,omitempty"`
  25. }
  26. // DepartmentCreateResponse 创建部门数据响应
  27. DepartmentCreateResponse struct {
  28. util.CommonError
  29. ID int `json:"id"`
  30. }
  31. // DepartmentSimpleListResponse 获取子部门 ID 列表响应
  32. DepartmentSimpleListResponse struct {
  33. util.CommonError
  34. DepartmentID []*DepartmentID `json:"department_id"`
  35. }
  36. // DepartmentID 子部门 ID
  37. DepartmentID struct {
  38. ID int `json:"id"`
  39. ParentID int `json:"parentid"`
  40. Order int `json:"order"`
  41. }
  42. // DepartmentListResponse 获取部门列表响应
  43. DepartmentListResponse struct {
  44. util.CommonError
  45. Department []*Department `json:"department"`
  46. }
  47. // Department 部门列表数据
  48. Department struct {
  49. ID int `json:"id"` // 创建的部门 id
  50. Name string `json:"name"` // 部门名称
  51. NameEn string `json:"name_en"` // 英文名称
  52. DepartmentLeader []string `json:"department_leader"` // 部门负责人的 UserID
  53. ParentID int `json:"parentid"` // 父部门 id。根部门为 1
  54. Order int `json:"order"` // 在父部门中的次序值。order 值大的排序靠前
  55. }
  56. // DepartmentGetResponse 获取单个部门详情
  57. DepartmentGetResponse struct {
  58. util.CommonError
  59. Department Department `json:"department"`
  60. }
  61. )
  62. // DepartmentCreate 创建部门
  63. // see https://developer.work.weixin.qq.com/document/path/90205
  64. func (r *Client) DepartmentCreate(req *DepartmentCreateRequest) (*DepartmentCreateResponse, error) {
  65. var (
  66. accessToken string
  67. err error
  68. )
  69. if accessToken, err = r.GetAccessToken(); err != nil {
  70. return nil, err
  71. }
  72. var response []byte
  73. if response, err = util.PostJSON(fmt.Sprintf(departmentCreateURL, accessToken), req); err != nil {
  74. return nil, err
  75. }
  76. result := &DepartmentCreateResponse{}
  77. err = util.DecodeWithError(response, result, "DepartmentCreate")
  78. return result, err
  79. }
  80. // DepartmentSimpleList 获取子部门 ID 列表
  81. // see https://developer.work.weixin.qq.com/document/path/95350
  82. func (r *Client) DepartmentSimpleList(departmentID int) ([]*DepartmentID, error) {
  83. var (
  84. accessToken string
  85. err error
  86. )
  87. if accessToken, err = r.GetAccessToken(); err != nil {
  88. return nil, err
  89. }
  90. var response []byte
  91. if response, err = util.HTTPGet(fmt.Sprintf(departmentSimpleListURL, accessToken, departmentID)); err != nil {
  92. return nil, err
  93. }
  94. result := &DepartmentSimpleListResponse{}
  95. err = util.DecodeWithError(response, result, "DepartmentSimpleList")
  96. return result.DepartmentID, err
  97. }
  98. // DepartmentList 获取部门列表
  99. // @desc https://developer.work.weixin.qq.com/document/path/90208
  100. func (r *Client) DepartmentList() ([]*Department, error) {
  101. return r.DepartmentListByID(0)
  102. }
  103. // DepartmentListByID 获取部门列表
  104. //
  105. // departmentID 部门 id。获取指定部门及其下的子部门(以及子部门的子部门等等,递归)
  106. //
  107. // @desc https://developer.work.weixin.qq.com/document/path/90208
  108. func (r *Client) DepartmentListByID(departmentID int) ([]*Department, error) {
  109. var formatURL string
  110. // 获取 accessToken
  111. accessToken, err := r.GetAccessToken()
  112. if err != nil {
  113. return nil, err
  114. }
  115. if departmentID > 0 {
  116. formatURL = fmt.Sprintf(departmentListByIDURL, accessToken, departmentID)
  117. } else {
  118. formatURL = fmt.Sprintf(departmentListURL, accessToken)
  119. }
  120. // 发起 http 请求
  121. response, err := util.HTTPGet(formatURL)
  122. if err != nil {
  123. return nil, err
  124. }
  125. // 按照结构体解析返回值
  126. result := &DepartmentListResponse{}
  127. err = util.DecodeWithError(response, result, "DepartmentList")
  128. // 返回数据
  129. return result.Department, err
  130. }
  131. // DepartmentGet 获取单个部门详情
  132. // see https://developer.work.weixin.qq.com/document/path/95351
  133. func (r *Client) DepartmentGet(departmentID int) (*Department, error) {
  134. var (
  135. accessToken string
  136. err error
  137. )
  138. if accessToken, err = r.GetAccessToken(); err != nil {
  139. return nil, err
  140. }
  141. var response []byte
  142. if response, err = util.HTTPGet(fmt.Sprintf(departmentGetURL, accessToken, departmentID)); err != nil {
  143. return nil, err
  144. }
  145. result := &DepartmentGetResponse{}
  146. err = util.DecodeWithError(response, result, "DepartmentGet")
  147. return &result.Department, err
  148. }