github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/mp/user/group/group.go (about) 1 // 用户分组管理. 2 package group 3 4 import ( 5 "github.com/chanxuehong/wechat/mp/core" 6 ) 7 8 type Group struct { 9 Id int64 `json:"id"` // 分组id, 由微信分配 10 Name string `json:"name"` // 分组名字, UTF8编码 11 UserCount int `json:"count"` // 分组内用户数量 12 } 13 14 // Create 创建分组. 15 func Create(clt *core.Client, name string) (group *Group, err error) { 16 const incompleteURL = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token=" 17 18 var request struct { 19 Group struct { 20 Name string `json:"name"` 21 } `json:"group"` 22 } 23 request.Group.Name = name 24 25 var result struct { 26 core.Error 27 Group `json:"group"` 28 } 29 if err = clt.PostJSON(incompleteURL, &request, &result); err != nil { 30 return 31 } 32 if result.ErrCode != core.ErrCodeOK { 33 err = &result.Error 34 return 35 } 36 result.Group.UserCount = 0 37 group = &result.Group 38 return 39 } 40 41 // Delete 删除分组. 42 func Delete(clt *core.Client, groupId int64) (err error) { 43 const incompleteURL = "https://api.weixin.qq.com/cgi-bin/groups/delete?access_token=" 44 45 var request struct { 46 Group struct { 47 Id int64 `json:"id"` 48 } `json:"group"` 49 } 50 request.Group.Id = groupId 51 52 var result core.Error 53 if err = clt.PostJSON(incompleteURL, &request, &result); err != nil { 54 return 55 } 56 if result.ErrCode != core.ErrCodeOK { 57 err = &result 58 return 59 } 60 return 61 } 62 63 // Update 修改分组名. 64 func Update(clt *core.Client, groupId int64, name string) (err error) { 65 const incompleteURL = "https://api.weixin.qq.com/cgi-bin/groups/update?access_token=" 66 67 var request struct { 68 Group struct { 69 Id int64 `json:"id"` 70 Name string `json:"name"` 71 } `json:"group"` 72 } 73 request.Group.Id = groupId 74 request.Group.Name = name 75 76 var result core.Error 77 if err = clt.PostJSON(incompleteURL, &request, &result); err != nil { 78 return 79 } 80 if result.ErrCode != core.ErrCodeOK { 81 err = &result 82 return 83 } 84 return 85 } 86 87 // List 查询所有分组. 88 func List(clt *core.Client) (groups []Group, err error) { 89 const incompleteURL = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=" 90 91 var result struct { 92 core.Error 93 Groups []Group `json:"groups"` 94 } 95 if err = clt.GetJSON(incompleteURL, &result); err != nil { 96 return 97 } 98 if result.ErrCode != core.ErrCodeOK { 99 err = &result.Error 100 return 101 } 102 groups = result.Groups 103 return 104 }