feat: wecom tag create items
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package auth
|
||||
|
||||
type Client struct {
|
||||
CorpId string
|
||||
CorpSecret string
|
||||
}
|
||||
|
||||
type tokenResponse struct {
|
||||
ErrCode int `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
AccessToken string `json:"access_token"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
}
|
||||
|
||||
func NewClient(corpId, corpSecret string) *Client {
|
||||
return &Client{
|
||||
CorpId: corpId,
|
||||
CorpSecret: corpSecret,
|
||||
}
|
||||
}
|
||||
@@ -14,25 +14,6 @@ import (
|
||||
"lone-services/pkg/utils"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
CorpId string
|
||||
CorpSecret string
|
||||
}
|
||||
|
||||
type tokenResponse struct {
|
||||
ErrCode int `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
AccessToken string `json:"access_token"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
}
|
||||
|
||||
func NewClient(corpId, corpSecret string) *Client {
|
||||
return &Client{
|
||||
CorpId: corpId,
|
||||
CorpSecret: corpSecret,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) AccessToken(ctx context.Context) (string, error) {
|
||||
secretMd5 := md5.Sum([]byte(c.CorpSecret))
|
||||
key := fmt.Sprintf("wecom:access_token:%s:%s", c.CorpId, hex.EncodeToString(secretMd5[:])[:8])
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package tags
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type AddTagUsersRequest struct {
|
||||
TagId int `json:"tagid"`
|
||||
UserIds []string `json:"userlist"`
|
||||
}
|
||||
|
||||
type AddTagUsersResponse struct {
|
||||
ErrCode int `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
}
|
||||
|
||||
func (c *Client) Add(ctx context.Context, params AddTagUsersRequest) error {
|
||||
|
||||
token, err := c.auth.AccessToken(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
url := fmt.Sprintf(
|
||||
"https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers?access_token=%s",
|
||||
token,
|
||||
)
|
||||
|
||||
body, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodPost,
|
||||
url,
|
||||
bytes.NewBuffer(body),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
result := AddTagUsersResponse{}
|
||||
|
||||
err = json.NewDecoder(resp.Body).Decode(&result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if result.ErrCode != 0 {
|
||||
return errors.New(result.ErrMsg)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package tags
|
||||
|
||||
import "lone-services/pkg/wecom/auth"
|
||||
|
||||
type Client struct {
|
||||
auth *auth.Client
|
||||
}
|
||||
|
||||
func NewClient(authClient *auth.Client) *Client {
|
||||
return &Client{auth: authClient}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package tags
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (c *Client) Create(ctx context.Context, reqData CreateRequest) (*Response, error) {
|
||||
|
||||
token, err := c.auth.AccessToken(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url := fmt.Sprintf(
|
||||
"https://qyapi.weixin.qq.com/cgi-bin/tag/create?access_token=%s",
|
||||
token,
|
||||
)
|
||||
|
||||
body, err := json.Marshal(reqData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodPost,
|
||||
url,
|
||||
bytes.NewBuffer(body),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var result Response
|
||||
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if result.ErrCode != 0 {
|
||||
return nil, errors.New(result.ErrMsg)
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package tags
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (c *Client) Delete(ctx context.Context, tagId string) error {
|
||||
|
||||
token, err := c.auth.AccessToken(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
url := fmt.Sprintf(
|
||||
"https://qyapi.weixin.qq.com/cgi-bin/tag/delete?access_token=%s&tagid=%s",
|
||||
token,
|
||||
tagId,
|
||||
)
|
||||
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
url,
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var result Response
|
||||
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if result.ErrCode != 0 {
|
||||
return errors.New(result.ErrMsg)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package tags
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (c *Client) List(ctx context.Context) ([]Tags, error) {
|
||||
|
||||
token, err := c.auth.AccessToken(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url := fmt.Sprintf(
|
||||
"https://qyapi.weixin.qq.com/cgi-bin/tag/list?access_token=%s",
|
||||
token,
|
||||
)
|
||||
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
url,
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var result Response
|
||||
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if result.ErrCode != 0 {
|
||||
return nil, errors.New(result.ErrMsg)
|
||||
}
|
||||
|
||||
return result.TagList, nil
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package tags
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type RemoveTagUsersRequest struct {
|
||||
TagId int `json:"tagid"`
|
||||
UserIds []string `json:"userlist"`
|
||||
}
|
||||
|
||||
type RemoveTagUsersResponse struct {
|
||||
ErrCode int `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
}
|
||||
|
||||
func (c *Client) Remove(ctx context.Context, params RemoveTagUsersRequest) error {
|
||||
|
||||
token, err := c.auth.AccessToken(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
url := fmt.Sprintf(
|
||||
"https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers?access_token=%s",
|
||||
token,
|
||||
)
|
||||
|
||||
body, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodPost,
|
||||
url,
|
||||
bytes.NewBuffer(body),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
result := RemoveTagUsersResponse{}
|
||||
|
||||
err = json.NewDecoder(resp.Body).Decode(&result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if result.ErrCode != 0 {
|
||||
return errors.New(result.ErrMsg)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package tags
|
||||
|
||||
type CreateRequest struct {
|
||||
TagName string `json:"tagname"`
|
||||
TagID int `json:"tagid,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package tags
|
||||
|
||||
type Response struct {
|
||||
ErrCode int `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
TagID int64 `json:"tagid,omitempty"`
|
||||
TagList []Tags `json:"taglist,omitempty"`
|
||||
}
|
||||
|
||||
type Tags struct {
|
||||
TagID int64 `json:"tagid"`
|
||||
TagName string `json:"tagname"`
|
||||
}
|
||||
Reference in New Issue
Block a user