customer.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. accessToken, err = r.ctx.GetAccessToken()
  35. if err != nil {
  36. return
  37. }
  38. data, err = util.PostJSON(fmt.Sprintf(customerBatchGetAddr, accessToken), options)
  39. if err != nil {
  40. return
  41. }
  42. if err = json.Unmarshal(data, &info); err != nil {
  43. return
  44. }
  45. if info.ErrCode != 0 {
  46. return info, NewSDKErr(info.ErrCode, info.ErrMsg)
  47. }
  48. return info, nil
  49. }