external_user.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. Limit int `json:"limit,omitempty"`
  115. }
  116. // ExternalUserDetailListResponse 批量获取外部联系人详情响应
  117. type ExternalUserDetailListResponse struct {
  118. util.CommonError
  119. ExternalContactList []ExternalUserForBatch `json:"external_contact_list"`
  120. }
  121. // ExternalUserForBatch 批量获取外部联系人客户列表
  122. type ExternalUserForBatch struct {
  123. ExternalContact ExternalContact `json:"external_contact"`
  124. FollowInfo FollowInfo `json:"follow_info"`
  125. }
  126. // ExternalContact 批量获取外部联系人用户信息
  127. type ExternalContact struct {
  128. ExternalUserID string `json:"external_userid"`
  129. Name string `json:"name"`
  130. Position string `json:"position"`
  131. Avatar string `json:"avatar"`
  132. CorpName string `json:"corp_name"`
  133. CorpFullName string `json:"corp_full_name"`
  134. Type int64 `json:"type"`
  135. Gender int64 `json:"gender"`
  136. UnionID string `json:"unionid"`
  137. ExternalProfile string `json:"external_profile"`
  138. }
  139. // FollowInfo 批量获取外部联系人跟进人信息
  140. type FollowInfo struct {
  141. UserID string `json:"userid"`
  142. Remark string `json:"remark"`
  143. Description string `json:"description"`
  144. CreateTime int `json:"create_time"`
  145. TagID []string `json:"tag_id"`
  146. RemarkCorpName string `json:"remark_corp_name"`
  147. RemarkMobiles []string `json:"remark_mobiles"`
  148. OperUserID string `json:"oper_userid"`
  149. AddWay int64 `json:"add_way"`
  150. WeChatChannels WechatChannel `json:"wechat_channels"`
  151. }
  152. // BatchGetExternalUserDetails 批量获取外部联系人详情
  153. // @see https://developer.work.weixin.qq.com/document/path/92994
  154. func (r *Client) BatchGetExternalUserDetails(request BatchGetExternalUserDetailsRequest) ([]ExternalUserForBatch, error) {
  155. accessToken, err := r.GetAccessToken()
  156. if err != nil {
  157. return nil, err
  158. }
  159. var response []byte
  160. jsonData, err := json.Marshal(request)
  161. if err != nil {
  162. return nil, err
  163. }
  164. response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", fetchBatchExternalContactUserDetailURL, accessToken), string(jsonData))
  165. if err != nil {
  166. return nil, err
  167. }
  168. var result ExternalUserDetailListResponse
  169. err = util.DecodeWithError(response, &result, "BatchGetExternalUserDetails")
  170. if err != nil {
  171. return nil, err
  172. }
  173. return result.ExternalContactList, nil
  174. }
  175. // UpdateUserRemarkRequest 修改客户备注信息请求体
  176. type UpdateUserRemarkRequest struct {
  177. UserID string `json:"userid"`
  178. ExternalUserID string `json:"external_userid"`
  179. Remark string `json:"remark"`
  180. Description string `json:"description"`
  181. RemarkCompany string `json:"remark_company"`
  182. RemarkMobiles []string `json:"remark_mobiles"`
  183. RemarkPicMediaID string `json:"remark_pic_mediaid"`
  184. }
  185. // UpdateUserRemark 修改客户备注信息
  186. // @see https://developer.work.weixin.qq.com/document/path/92115
  187. func (r *Client) UpdateUserRemark(request UpdateUserRemarkRequest) error {
  188. accessToken, err := r.GetAccessToken()
  189. if err != nil {
  190. return err
  191. }
  192. var response []byte
  193. jsonData, err := json.Marshal(request)
  194. if err != nil {
  195. return err
  196. }
  197. response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", updateUserRemarkURL, accessToken), string(jsonData))
  198. if err != nil {
  199. return err
  200. }
  201. return util.DecodeWithCommonError(response, "UpdateUserRemark")
  202. }