添加联系人置顶和取消置顶功能

This commit is contained in:
eatmoreapple 2021-08-01 01:24:16 +08:00
parent a54493fc61
commit 775580616f
4 changed files with 62 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package openwechat
import (
"testing"
"time"
)
func TestLogin(t *testing.T) {
@ -85,3 +86,27 @@ func TestGroups(t *testing.T) {
}
t.Log(groups)
}
func TestPinUser(t *testing.T) {
bot := DefaultBot(Desktop)
if err := bot.Login(); err != nil {
t.Error(err)
return
}
user, err := bot.GetCurrentUser()
if err != nil {
t.Error(err)
return
}
friends, err := user.Friends()
if err != nil {
t.Error(err)
return
}
if friends.Count() > 0 {
f := friends.First()
f.Pin()
time.Sleep(time.Second * 5)
f.UnPin()
}
}

View File

@ -336,6 +336,15 @@ func (c *Caller) WebWxStatusAsRead(request *BaseRequest, info *LoginInfo, msg *M
return parseBaseResponseError(resp)
}
// WebWxRelationPin 将联系人是否置顶
func (c *Caller) WebWxRelationPin(request *BaseRequest, user *User, op uint8) error {
resp, err := c.Client.WebWxRelationPin(request, op, user)
if err != nil {
return err
}
return parseBaseResponseError(resp)
}
// 处理响应返回的结果是否正常
func parseBaseResponseError(resp *http.Response) error {
defer resp.Body.Close()

View File

@ -674,3 +674,19 @@ func (c *Client) WebWxStatusAsRead(request *BaseRequest, info *LoginInfo, msg *M
req.Header.Add("Content-Type", jsonContentType)
return c.Do(req)
}
// WebWxRelationPin 联系人置顶接口
func (c *Client) WebWxRelationPin(request *BaseRequest, op uint8, user *User) (*http.Response, error) {
path, _ := url.Parse(c.domain.BaseHost() + webwxoplog)
content := map[string]interface{}{
"BaseRequest": request,
"CmdId": 3,
"OP": op,
"RemarkName": user.RemarkName,
"UserName": user.UserName,
}
body, _ := ToBuffer(content)
req, _ := http.NewRequest(http.MethodPost, path.String(), body)
req.Header.Add("Content-Type", jsonContentType)
return c.Do(req)
}

12
user.go
View File

@ -110,6 +110,18 @@ func (u *User) IsMP() bool {
return u.VerifyFlag == 8 || u.VerifyFlag == 24 || u.VerifyFlag == 136
}
// Pin 将联系人置顶
func (u *User) Pin() error {
req := u.Self.Bot.storage.Request
return u.Self.Bot.Caller.WebWxRelationPin(req, u, 1)
}
// UnPin 将联系人取消置顶
func (u *User) UnPin() error {
req := u.Self.Bot.storage.Request
return u.Self.Bot.Caller.WebWxRelationPin(req, u, 0)
}
// Self 自己,当前登录用户对象
type Self struct {
*User