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

     1  package user
     2  
     3  import (
     4  	"github.com/chanxuehong/wechat/mp/core"
     5  )
     6  
     7  // GroupId 查询用户所在分组.
     8  func GroupId(clt *core.Client, openId string) (groupId int64, err error) {
     9  	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/groups/getid?access_token="
    10  
    11  	var request = struct {
    12  		OpenId string `json:"openid"`
    13  	}{
    14  		OpenId: openId,
    15  	}
    16  	var result struct {
    17  		core.Error
    18  		GroupId int64 `json:"groupid"`
    19  	}
    20  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
    21  		return
    22  	}
    23  	if result.ErrCode != core.ErrCodeOK {
    24  		err = &result.Error
    25  		return
    26  	}
    27  	groupId = result.GroupId
    28  	return
    29  }
    30  
    31  // MoveToGroup 移动用户分组.
    32  func MoveToGroup(clt *core.Client, openId string, toGroupId int64) (err error) {
    33  	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token="
    34  
    35  	var request = struct {
    36  		OpenId    string `json:"openid"`
    37  		ToGroupId int64  `json:"to_groupid"`
    38  	}{
    39  		OpenId:    openId,
    40  		ToGroupId: toGroupId,
    41  	}
    42  	var result core.Error
    43  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
    44  		return
    45  	}
    46  	if result.ErrCode != core.ErrCodeOK {
    47  		err = &result
    48  		return
    49  	}
    50  	return
    51  }
    52  
    53  // BatchMoveToGroup 批量移动用户分组.
    54  func BatchMoveToGroup(clt *core.Client, openIdList []string, toGroupId int64) (err error) {
    55  	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/groups/members/batchupdate?access_token="
    56  
    57  	if len(openIdList) <= 0 {
    58  		return
    59  	}
    60  
    61  	var request = struct {
    62  		OpenIdList []string `json:"openid_list,omitempty"`
    63  		ToGroupId  int64    `json:"to_groupid"`
    64  	}{
    65  		OpenIdList: openIdList,
    66  		ToGroupId:  toGroupId,
    67  	}
    68  	var result core.Error
    69  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
    70  		return
    71  	}
    72  	if result.ErrCode != core.ErrCodeOK {
    73  		err = &result
    74  		return
    75  	}
    76  	return
    77  }