github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/mp/user/tag/tag.go (about)

     1  package tag
     2  
     3  import (
     4  	"github.com/chanxuehong/wechat/mp/core"
     5  )
     6  
     7  type Tag struct {
     8  	Id        int    `json:"id"`    // tag id
     9  	Name      string `json:"name"`  // tag name
    10  	UserCount int    `json:"count"` // Tag内用户数量
    11  }
    12  
    13  // 获取用户列表返回的数据结构
    14  type GetResult struct {
    15  	Count int `json:"count"` // 拉取的OPENID个数, 最大值为10000
    16  
    17  	Data struct {
    18  		OpenIdList []string `json:"openid,omitempty"`
    19  	} `json:"data"` // 列表数据, OPENID的列表
    20  
    21  	// 拉取列表的最后一个用户的OPENID, 如果 next_openid == "" 则表示没有了用户数据
    22  	NextOpenId string `json:"next_openid"`
    23  }
    24  
    25  func Create(clt *core.Client, name string) (tag *Tag, err error) {
    26  	var incompleteURL = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token="
    27  	var request struct {
    28  		Tag struct {
    29  			Name string `json:"name"`
    30  		} `json:"tag"`
    31  	}
    32  	request.Tag.Name = name
    33  	var result struct {
    34  		core.Error
    35  		Tag Tag `json:"tag"`
    36  	}
    37  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
    38  		return
    39  	}
    40  	if result.ErrCode != core.ErrCodeOK {
    41  		err = &result.Error
    42  		return
    43  	}
    44  	result.Tag.UserCount = 0
    45  	tag = &result.Tag
    46  	return
    47  }
    48  
    49  // List 查询所有Tag.
    50  func List(clt *core.Client) (tags []Tag, err error) {
    51  	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token="
    52  
    53  	var result struct {
    54  		core.Error
    55  		Tags []Tag `json:"tags"`
    56  	}
    57  	if err = clt.GetJSON(incompleteURL, &result); err != nil {
    58  		return
    59  	}
    60  	if result.ErrCode != core.ErrCodeOK {
    61  		err = &result.Error
    62  		return
    63  	}
    64  	tags = result.Tags
    65  	return
    66  }
    67  
    68  // Update 修改Tag名.
    69  func Update(clt *core.Client, tagId int, name string) (err error) {
    70  	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/tags/update?access_token="
    71  
    72  	var request struct {
    73  		Tag struct {
    74  			Id   int    `json:"id"`
    75  			Name string `json:"name"`
    76  		} `json:"tag"`
    77  	}
    78  	request.Tag.Id = tagId
    79  	request.Tag.Name = name
    80  
    81  	var result core.Error
    82  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
    83  		return
    84  	}
    85  	if result.ErrCode != core.ErrCodeOK {
    86  		err = &result
    87  		return
    88  	}
    89  	return
    90  }
    91  
    92  // TagGet 根据TagId获取用户列表.
    93  //
    94  //	NOTE: 每次最多能获取 10000 个用户, 可以多次指定 nextOpenId 来获取以满足需求, 如果 nextOpenId == "" 则表示从头获取
    95  func TagGet(clt *core.Client, tagId int, nextOpenId string) (rslt *GetResult, err error) {
    96  	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token="
    97  
    98  	var request = struct {
    99  		Id     int    `json:"tagid"`
   100  		OpenId string `json:"next_openid"`
   101  	}{
   102  		Id:     tagId,
   103  		OpenId: nextOpenId,
   104  	}
   105  	var result struct {
   106  		core.Error
   107  		GetResult
   108  	}
   109  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
   110  		return
   111  	}
   112  	if result.ErrCode != core.ErrCodeOK {
   113  		err = &result.Error
   114  		return
   115  	}
   116  	rslt = &result.GetResult
   117  	return
   118  }
   119  
   120  // Delete 删除Tag.
   121  func Delete(clt *core.Client, tagId int) (err error) {
   122  	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/tags/delete?access_token="
   123  
   124  	var request struct {
   125  		Tag struct {
   126  			Id int `json:"id"`
   127  		} `json:"tag"`
   128  	}
   129  	request.Tag.Id = tagId
   130  
   131  	var result core.Error
   132  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
   133  		return
   134  	}
   135  	if result.ErrCode != core.ErrCodeOK {
   136  		err = &result
   137  		return
   138  	}
   139  	return
   140  }
   141  
   142  // BatchTag 批量打标签.
   143  func BatchTag(clt *core.Client, openIdList []string, tagId int) (err error) {
   144  	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token="
   145  
   146  	if len(openIdList) <= 0 {
   147  		return
   148  	}
   149  
   150  	var request = struct {
   151  		OpenIdList []string `json:"openid_list,omitempty"`
   152  		TagId      int      `json:"tagid"`
   153  	}{
   154  		OpenIdList: openIdList,
   155  		TagId:      tagId,
   156  	}
   157  	var result core.Error
   158  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
   159  		return
   160  	}
   161  	if result.ErrCode != core.ErrCodeOK {
   162  		err = &result
   163  		return
   164  	}
   165  	return
   166  }
   167  
   168  // BatchUntag 批量取消标签.
   169  func BatchUntag(clt *core.Client, openIdList []string, tagId int) (err error) {
   170  	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token="
   171  
   172  	if len(openIdList) <= 0 {
   173  		return
   174  	}
   175  
   176  	var request = struct {
   177  		OpenIdList []string `json:"openid_list,omitempty"`
   178  		TagId      int      `json:"tagid"`
   179  	}{
   180  		OpenIdList: openIdList,
   181  		TagId:      tagId,
   182  	}
   183  	var result core.Error
   184  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
   185  		return
   186  	}
   187  	if result.ErrCode != core.ErrCodeOK {
   188  		err = &result
   189  		return
   190  	}
   191  	return
   192  }