external_user.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package externalcontact
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/silenceper/wechat/v2/util"
  6. )
  7. const (
  8. // FetchExternalContactUserListURL 获取客户列表
  9. FetchExternalContactUserListURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list"
  10. // FetchExternalContactUserDetailURL 获取客户详情
  11. FetchExternalContactUserDetailURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get"
  12. // FetchBatchExternalContactUserDetailURL 批量获取客户详情
  13. FetchBatchExternalContactUserDetailURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/batch/get_by_user"
  14. // UpdateUserRemarkURL 更新客户备注信息
  15. UpdateUserRemarkURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/remark"
  16. )
  17. // ExternalUserListResponse 外部联系人列表响应
  18. type ExternalUserListResponse struct {
  19. util.CommonError
  20. ExternalUserID []string `json:"external_userid"`
  21. }
  22. // GetExternalUserList 获取客户列表
  23. // @see https://developer.work.weixin.qq.com/document/path/92113
  24. func (r *Client) GetExternalUserList(userID string) ([]string, error) {
  25. accessToken, err := r.GetAccessToken()
  26. if err != nil {
  27. return nil, err
  28. }
  29. var response []byte
  30. response, err = util.HTTPGet(fmt.Sprintf("%s?access_token=%v&userid=%v", FetchExternalContactUserListURL, accessToken, userID))
  31. if err != nil {
  32. return nil, err
  33. }
  34. var result ExternalUserListResponse
  35. err = util.DecodeWithError(response, &result, "GetExternalUserList")
  36. if err != nil {
  37. return nil, err
  38. }
  39. return result.ExternalUserID, nil
  40. }
  41. // ExternalUserDetailResponse 外部联系人详情响应
  42. type ExternalUserDetailResponse struct {
  43. util.CommonError
  44. ExternalContact ExternalUser `json:"external_contact"`
  45. FollowUser []FollowUser `json:"follow_user"`
  46. NextCursor string `json:"next_cursor"`
  47. }
  48. // ExternalUser 外部联系人
  49. type ExternalUser struct {
  50. ExternalUserID string `json:"external_userid"`
  51. Name string `json:"name"`
  52. Avatar string `json:"avatar"`
  53. Type int64 `json:"type"`
  54. Gender int64 `json:"gender"`
  55. UnionID string `json:"unionid"`
  56. Position string `json:"position"`
  57. CorpName string `json:"corp_name"`
  58. CorpFullName string `json:"corp_full_name"`
  59. ExternalProfile string `json:"external_profile"`
  60. }
  61. // FollowUser 跟进用户(指企业内部用户)
  62. type FollowUser struct {
  63. UserID string `json:"userid"`
  64. Remark string `json:"remark"`
  65. Description string `json:"description"`
  66. CreateTime string `json:"create_time"`
  67. Tags []Tag `json:"tags"`
  68. RemarkCorpName string `json:"remark_corp_name"`
  69. RemarkMobiles []string `json:"remark_mobiles"`
  70. OperUserID string `json:"oper_userid"`
  71. AddWay int64 `json:"add_way"`
  72. WeChatChannels WechatChannel `json:"wechat_channels"`
  73. State string `json:"state"`
  74. }
  75. // Tag 已绑定在外部联系人的标签
  76. type Tag struct {
  77. GroupName string `json:"group_name"`
  78. TagName string `json:"tag_name"`
  79. Type int64 `json:"type"`
  80. TagID string `json:"tag_id"`
  81. }
  82. // WechatChannel 视频号添加的场景
  83. type WechatChannel struct {
  84. NickName string `json:"nickname"`
  85. Source string `json:"source"`
  86. }
  87. // GetExternalUserDetail 获取外部联系人详情
  88. // @see https://developer.work.weixin.qq.com/document/path/92114
  89. func (r *Client) GetExternalUserDetail(externalUserID string, nextCursor ...string) (*ExternalUserDetailResponse, error) {
  90. accessToken, err := r.GetAccessToken()
  91. if err != nil {
  92. return nil, err
  93. }
  94. var response []byte
  95. var cursor string
  96. if len(nextCursor) > 0 {
  97. cursor = nextCursor[0]
  98. }
  99. response, err = util.HTTPGet(fmt.Sprintf("%s?access_token=%v&external_userid=%v&cursor=%v", FetchExternalContactUserDetailURL, accessToken, externalUserID, cursor))
  100. if err != nil {
  101. return nil, err
  102. }
  103. result := &ExternalUserDetailResponse{}
  104. err = util.DecodeWithError(response, result, "get_external_user_detail")
  105. if err != nil {
  106. return nil, err
  107. }
  108. return result, nil
  109. }
  110. // BatchGetExternalUserDetailsRequest 批量获取外部联系人详情请求
  111. type BatchGetExternalUserDetailsRequest struct {
  112. UserIDList []string `json:"userid_list"`
  113. Cursor string `json:"cursor"`
  114. }
  115. // ExternalUserDetailListResponse 批量获取外部联系人详情响应
  116. type ExternalUserDetailListResponse struct {
  117. util.CommonError
  118. ExternalContactList []ExternalUserForBatch `json:"external_contact_list"`
  119. }
  120. // ExternalUserForBatch 批量获取外部联系人客户列表
  121. type ExternalUserForBatch struct {
  122. ExternalContact ExternalContact `json:"external_contact"`
  123. FollowInfo FollowInfo `json:"follow_info"`
  124. }
  125. // ExternalContact 批量获取外部联系人用户信息
  126. type ExternalContact struct {
  127. ExternalUserID string `json:"external_userid"`
  128. Name string `json:"name"`
  129. Position string `json:"position"`
  130. Avatar string `json:"avatar"`
  131. CorpName string `json:"corp_name"`
  132. CorpFullName string `json:"corp_full_name"`
  133. Type int64 `json:"type"`
  134. Gender int64 `json:"gender"`
  135. UnionID string `json:"unionid"`
  136. ExternalProfile string `json:"external_profile"`
  137. }
  138. // FollowInfo 批量获取外部联系人跟进人信息
  139. type FollowInfo struct {
  140. UserID string `json:"userid"`
  141. Remark string `json:"remark"`
  142. Description string `json:"description"`
  143. CreateTime int `json:"create_time"`
  144. TagID []string `json:"tag_id"`
  145. RemarkCorpName string `json:"remark_corp_name"`
  146. RemarkMobiles []string `json:"remark_mobiles"`
  147. OperUserID string `json:"oper_userid"`
  148. AddWay int64 `json:"add_way"`
  149. WeChatChannels WechatChannel `json:"wechat_channels"`
  150. }
  151. // BatchGetExternalUserDetails 批量获取外部联系人详情
  152. // @see https://developer.work.weixin.qq.com/document/path/92994
  153. func (r *Client) BatchGetExternalUserDetails(request BatchGetExternalUserDetailsRequest) ([]ExternalUserForBatch, error) {
  154. accessToken, err := r.GetAccessToken()
  155. if err != nil {
  156. return nil, err
  157. }
  158. var response []byte
  159. jsonData, err := json.Marshal(request)
  160. if err != nil {
  161. return nil, err
  162. }
  163. response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", FetchBatchExternalContactUserDetailURL, accessToken), string(jsonData))
  164. if err != nil {
  165. return nil, err
  166. }
  167. var result ExternalUserDetailListResponse
  168. err = util.DecodeWithError(response, &result, "BatchGetExternalUserDetails")
  169. if err != nil {
  170. return nil, err
  171. }
  172. return result.ExternalContactList, nil
  173. }
  174. // UpdateUserRemarkRequest 修改客户备注信息请求体
  175. type UpdateUserRemarkRequest struct {
  176. UserID string `json:"userid"`
  177. ExternalUserID string `json:"external_userid"`
  178. Remark string `json:"remark"`
  179. Description string `json:"description"`
  180. RemarkCompany string `json:"remark_company"`
  181. RemarkMobiles []string `json:"remark_mobiles"`
  182. RemarkPicMediaID string `json:"remark_pic_mediaid"`
  183. }
  184. // UpdateUserRemark 修改客户备注信息
  185. // @see https://developer.work.weixin.qq.com/document/path/92115
  186. func (r *Client) UpdateUserRemark(request UpdateUserRemarkRequest) error {
  187. accessToken, err := r.GetAccessToken()
  188. if err != nil {
  189. return err
  190. }
  191. var response []byte
  192. jsonData, err := json.Marshal(request)
  193. if err != nil {
  194. return err
  195. }
  196. response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", UpdateUserRemarkURL, accessToken), string(jsonData))
  197. if err != nil {
  198. return err
  199. }
  200. return util.DecodeWithCommonError(response, "UpdateUserRemark")
  201. }