From 9680512f0ab474b1fe2c8ec3826b26cd321d9c08 Mon Sep 17 00:00:00 2001 From: coward Date: Fri, 5 Jan 2024 11:54:25 +0800 Subject: [PATCH] :tada: --- .gitignore | 24 ++++++ README.md | 10 +++ client.go | 194 +++++++++++++++++++++++++++++++++++++++++++++++++ client_test.go | 36 +++++++++ go.mod | 8 ++ go.sum | 43 +++++++++++ request.go | 26 +++++++ response.go | 41 +++++++++++ 8 files changed, 382 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 client.go create mode 100644 client_test.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 request.go create mode 100644 response.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..af7e0ba --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +### Go template +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ +.idea + +# Go workspace file +go.work + diff --git a/README.md b/README.md new file mode 100644 index 0000000..39971dc --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# wx_helper_api_sdk + +## install: + ```go + go get gitea.mrx.ltd/go-pkg/wx_helper_sdk + ``` +## use +```go + 没啥好用的自己看client_test.go文件,大同小异,有些没写到测试用例里边,自己搞定 +``` \ No newline at end of file diff --git a/client.go b/client.go new file mode 100644 index 0000000..f805dec --- /dev/null +++ b/client.go @@ -0,0 +1,194 @@ +package wx_helper_sdk + +import ( + "encoding/json" + "fmt" + "github.com/go-resty/resty/v2" + "log" + "strings" +) + +type WxHelper struct { + Host string + Port uint + tmpResult CommonResponse + client *resty.Client +} + +func NewWxHelper(host string, port uint) WxHelper { + hc := resty.New() + return WxHelper{ + host, + port, + CommonResponse{}, + hc, + } +} + +// GetAddr +// @description: 获取请求地址 +// @receiver w +// @param uri +// @return string +func (w *WxHelper) GetAddr(uri string) string { + if !strings.HasPrefix(w.Host, "http") && !strings.HasPrefix(w.Host, "https://") { + w.Host = fmt.Sprintf("http://%s", w.Host) + } + + return fmt.Sprintf("%s:%d%s", w.Host, w.Port, uri) +} + +// SetHook +// @description: 设置回调 +// @receiver w +// @param req +// @return err +func (w *WxHelper) SetHook(param *SetHook) (err error) { + req := w.client.R() + req.Header.Set("Content-Type", "application/json") + req.SetBody(param) + _, err = req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/hookSyncMsg")) + if err != nil { + return + } + + if w.tmpResult.Code != 0 { + return fmt.Errorf("设置回调失败: %v", w.tmpResult.Msg) + } + return +} + +// UnHook +// @description: 取消回调设置 +// @receiver w +// @return err +func (w *WxHelper) UnHook() (err error) { + req := w.client.R() + _, err = req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/unhookSyncMsg")) + if err != nil { + return + } + + if w.tmpResult.Code != 0 { + return fmt.Errorf("取消回调设置失败: %v", w.tmpResult.Msg) + } + + return +} + +// CheckUserIsLogin +// @description: 校验用户是否已经登陆 +// @receiver *WxHelper +// @return bool +func (w *WxHelper) CheckUserIsLogin() bool { + req := w.client.R() + _, err := req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/checkLogin")) + if err != nil { + log.Fatalf("获取用户登陆状态失败: %v", err.Error()) + return false + } + + if w.tmpResult.Code != 1 { + return false + } + return true +} + +// GetCurrentLoginUserInfo +// @description: 获取当前登陆用户信息 +// @receiver w +// @return response +// @return err +func (w *WxHelper) GetCurrentLoginUserInfo() (response UserInfo, err error) { + req := w.client.R() + _, err = req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/userInfo")) + if err != nil { + log.Fatalf("获取用户登陆状态失败: %v", err.Error()) + return + } + + if w.tmpResult.Code != 1 { + return + } + + data, _ := json.Marshal(w.tmpResult.Data) + _ = json.Unmarshal(data, &response) + return +} + +// GetContactList +// @description: 获取好友列表 +// @receiver w +// @param isShowTool 是否展示一些工具聊天,什么文件助手呀,订阅号等等没用的东西 +// @return friends +// @return err +func (w *WxHelper) GetContactList(isShowTool bool) (friends []Friend, err error) { + req := w.client.R() + _, err = req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/getContactList")) + if err != nil { + log.Fatalf("获取好友列表失败: %v", err.Error()) + return + } + + if w.tmpResult.Code != 1 { + return + } + data, _ := json.Marshal(w.tmpResult.Data) + _ = json.Unmarshal(data, &friends) + + // 如果不需要展示那些工具号,直接剔除 + if !isShowTool { + var onlyFriends []Friend + for _, v := range friends { + // 固定开头 + if strings.HasPrefix(v.Wxid, "wxid") { + onlyFriends = append(onlyFriends, v) + } + } + friends = onlyFriends + } + + return +} + +// SendAtText +// @description: 群内圈人文本消息发送 +// @receiver w +// @param param +// @return err +func (w *WxHelper) SendAtText(param *SendAtText) (err error) { + req := w.client.R() + req.Header.Set("Content-Type", "application/json") + req.SetBody(param) + _, err = req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/sendAtText")) + if err != nil { + return fmt.Errorf("发送群内圈人消息失败: %v", err.Error()) + } + + if w.tmpResult.Code <= 0 { + return fmt.Errorf("发送群内圈人消息失败: %v", w.tmpResult.Msg) + } + + return +} + +// SendText +// @description: 发送文本信息(私聊) +// @receiver w +// @param param +// @return err +func (w *WxHelper) SendText(param *SendText) (err error) { + req := w.client.R() + req.Header.Set("Content-Type", "application/json") + req.SetBody(param) + _, err = req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/sendAtText")) + if err != nil { + return fmt.Errorf("发送私聊文本信息失败: %v", err.Error()) + } + + if w.tmpResult.Code == 0 { + return fmt.Errorf("发送私聊文本信息失败: %v", w.tmpResult.Msg) + } + + return +} diff --git a/client_test.go b/client_test.go new file mode 100644 index 0000000..32270f0 --- /dev/null +++ b/client_test.go @@ -0,0 +1,36 @@ +package wx_helper_sdk + +import ( + "fmt" + "testing" +) + +var wxh WxHelper + +func init() { + wxh = NewWxHelper("", 0) +} + +func TestClientCheckUserIsLogin(t *testing.T) { + ok := wxh.CheckUserIsLogin() + fmt.Println(ok) +} + +func TestClientGetCurrentLoginUser(t *testing.T) { + info, err := wxh.GetCurrentLoginUserInfo() + if err != nil { + t.Logf("failed: %v", err.Error()) + return + } + + fmt.Printf("%+v\n", info) +} + +func TestClientGetContactList(t *testing.T) { + list, err := wxh.GetContactList(false) + if err != nil { + return + } + + fmt.Printf("%+v", list) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7b2ff0f --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module gitea.mrx.ltd/go-pkg/wx_helper_sdk + +go 1.21 + +require ( + github.com/go-resty/resty/v2 v2.11.0 // indirect + golang.org/x/net v0.17.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..cf1df02 --- /dev/null +++ b/go.sum @@ -0,0 +1,43 @@ +github.com/go-resty/resty/v2 v2.11.0 h1:i7jMfNOJYMp69lq7qozJP+bjgzfAzeOhuGlyDrqxT/8= +github.com/go-resty/resty/v2 v2.11.0/go.mod h1:iiP/OpA0CkcL3IGt1O0+/SIItFUbkkyw5BGXiVdTu+A= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= +golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/request.go b/request.go new file mode 100644 index 0000000..5be38f9 --- /dev/null +++ b/request.go @@ -0,0 +1,26 @@ +package wx_helper_sdk + +// SetHook +// @description: 设置回调 +type SetHook struct { + Port string `json:"port"` + Ip string `json:"ip"` + Url string `json:"url"` + Timeout string `json:"timeout"` + EnableHttp string `json:"enableHttp"` +} + +// SendAtText +// @description: 发送群里圈人文本信息 +type SendAtText struct { + Wxids string `json:"wxids"` + ChatRoomId string `json:"chatRoomId"` + Msg string `json:"msg"` +} + +// SendText +// @description: 发送文本信息(私聊) +type SendText struct { + Wxid string `json:"wxid"` + Msg string `json:"msg"` +} diff --git a/response.go b/response.go new file mode 100644 index 0000000..112a6a4 --- /dev/null +++ b/response.go @@ -0,0 +1,41 @@ +package wx_helper_sdk + +// CommonResponse +// @description: 公共返回 +type CommonResponse struct { + Code int `json:"code"` + Msg string `json:"msg"` + Data any `json:"data"` +} + +// UserInfo +// @description: 当前用户信息 +type UserInfo struct { + Account string `json:"account"` + City string `json:"city"` + Country string `json:"country"` + CurrentDataPath string `json:"currentDataPath"` + DataSavePath string `json:"dataSavePath"` + DbKey string `json:"dbKey"` + HeadImage string `json:"headImage"` + Mobile string `json:"mobile"` + Name string `json:"name"` + Province string `json:"province"` + Signature string `json:"signature"` + Wxid string `json:"wxid"` +} + +// Friend +// @description: 朋友信息 +type Friend struct { + CustomAccount string `json:"customAccount"` + EncryptName string `json:"encryptName"` + Nickname string `json:"nickname"` + Pinyin string `json:"pinyin"` + PinyinAll string `json:"pinyinAll"` + Reserved1 int `json:"reserved1"` + Reserved2 int `json:"reserved2"` + Type int `json:"type"` + VerifyFlag int `json:"verifyFlag"` + Wxid string `json:"wxid"` +}