customer.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package kf
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/silenceper/wechat/v2/util"
  6. )
  7. const (
  8. customerBatchGetAddr = "https://qyapi.weixin.qq.com/cgi-bin/kf/customer/batchget?access_token=%s"
  9. )
  10. // CustomerBatchGetOptions 客户基本信息获取请求参数
  11. type CustomerBatchGetOptions struct {
  12. ExternalUserIDList []string `json:"external_userid_list"` // external_userid 列表
  13. }
  14. // CustomerSchema 微信客户基本资料
  15. type CustomerSchema struct {
  16. ExternalUserID string `json:"external_userid"` // 微信客户的 external_userid
  17. NickName string `json:"nickname"` // 微信昵称
  18. Avatar string `json:"avatar"` // 微信头像。第三方不可获取
  19. Gender int `json:"gender"` // 性别
  20. UnionID string `json:"unionid"` // unionid,需要绑定微信开发者帐号才能获取到,查看绑定方法:https://open.work.weixin.qq.com/kf/doc/92512/93143/94769#%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E5%BE%AE%E4%BF%A1%E5%AE%A2%E6%88%B7%E7%9A%84unionid
  21. }
  22. // CustomerBatchGetSchema 获取客户基本信息响应内容
  23. type CustomerBatchGetSchema struct {
  24. util.CommonError
  25. CustomerList []CustomerSchema `json:"customer_list"` // 微信客户信息列表
  26. InvalidExternalUserID []string `json:"invalid_external_userid"` // 无效的微信客户 ID
  27. }
  28. // CustomerBatchGet 客户基本信息获取
  29. func (r *Client) CustomerBatchGet(options CustomerBatchGetOptions) (info CustomerBatchGetSchema, err error) {
  30. var (
  31. accessToken string
  32. data []byte
  33. )
  34. if accessToken, err = r.ctx.GetAccessToken(); err != nil {
  35. return
  36. }
  37. if data, err = util.PostJSON(fmt.Sprintf(customerBatchGetAddr, accessToken), options); err != nil {
  38. return
  39. }
  40. if err = json.Unmarshal(data, &info); err != nil {
  41. return
  42. }
  43. if info.ErrCode != 0 {
  44. return info, NewSDKErr(info.ErrCode, info.ErrMsg)
  45. }
  46. return info, nil
  47. }