From c9d8115c65298016f874f03a756b4da629bb8205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=A5=E6=B5=B7?= Date: Fri, 12 Jan 2024 09:02:19 +0800 Subject: [PATCH] Fix db_query field value to string --- clients/gohttp/config.yml | 2 +- clients/gohttp/go.mod | 4 +- clients/gohttp/go.sum | 12 +++--- clients/gohttp/httpd/README.md | 41 +++++++++++++++++++ clients/gohttp/httpd/server.go | 6 +-- clients/gohttp/httpd/wcfrest/controller.go | 26 ++++++------ .../httpd/wcfrest/{forward.go => receiver.go} | 28 ++++++------- clients/gohttp/httpd/wcfrest/router.go | 4 +- clients/gohttp/httpd/wcfrest/types.go | 2 +- clients/gohttp/public/swag/swagger.json | 38 ++++++++--------- 10 files changed, 102 insertions(+), 61 deletions(-) create mode 100644 clients/gohttp/httpd/README.md rename clients/gohttp/httpd/wcfrest/{forward.go => receiver.go} (52%) diff --git a/clients/gohttp/config.yml b/clients/gohttp/config.yml index 2b6049c..b5859ea 100644 --- a/clients/gohttp/config.yml +++ b/clients/gohttp/config.yml @@ -1,4 +1,4 @@ -# REST 接口 +# HTTP 接口 httpd: address: 127.0.0.1:7600 # Api 监听地址 token: "" # 使用 Token 验证请求 diff --git a/clients/gohttp/go.mod b/clients/gohttp/go.mod index 89a2c0c..38bfdbb 100644 --- a/clients/gohttp/go.mod +++ b/clients/gohttp/go.mod @@ -7,7 +7,7 @@ require ( github.com/knadh/koanf v1.5.0 github.com/knadh/koanf/v2 v2.0.1 github.com/opentdp/go-helper v0.5.5-0.20240109013403-7323088c3f39 - github.com/opentdp/wechat-rest v0.6.0 + github.com/opentdp/wechat-rest v0.8.1 ) require ( @@ -51,7 +51,7 @@ require ( golang.org/x/net v0.20.0 // indirect golang.org/x/sys v0.16.0 // indirect golang.org/x/text v0.14.0 // indirect - golang.org/x/tools v0.16.1 // indirect + golang.org/x/tools v0.17.0 // indirect google.golang.org/protobuf v1.32.0 // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/clients/gohttp/go.sum b/clients/gohttp/go.sum index 4a67b92..212ce9e 100644 --- a/clients/gohttp/go.sum +++ b/clients/gohttp/go.sum @@ -250,8 +250,8 @@ github.com/npillmayer/nestext v0.1.3/go.mod h1:h2lrijH8jpicr25dFY+oAJLyzlya6jhnu github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= github.com/opentdp/go-helper v0.5.5-0.20240109013403-7323088c3f39 h1:qIzCCWpIPhzZq9ZXevYuqBV3w4F/D8/SK+W6UjKhSf0= github.com/opentdp/go-helper v0.5.5-0.20240109013403-7323088c3f39/go.mod h1:9m+t/2x1CmjZ9YQP37+xECCO84eqMnsxnRE/7y37GjE= -github.com/opentdp/wechat-rest v0.6.0 h1:e7Co8Bne32gQ8MDGNyBzJ0dhIMhlrIwsDhOxSXI4j1Q= -github.com/opentdp/wechat-rest v0.6.0/go.mod h1:F3rutIf4PahHxPGuAm3wlM6d1S7nznaU/ZNrk0dMEDA= +github.com/opentdp/wechat-rest v0.8.1 h1:2M7BFYCotM3gJkGxJ3mOczmftByxqRNSj4Q2Ec96Dws= +github.com/opentdp/wechat-rest v0.8.1/go.mod h1:F3rutIf4PahHxPGuAm3wlM6d1S7nznaU/ZNrk0dMEDA= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= @@ -378,8 +378,8 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= -golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= +golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -437,8 +437,8 @@ golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapK golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= -golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA= -golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= +golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc= +golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/clients/gohttp/httpd/README.md b/clients/gohttp/httpd/README.md new file mode 100644 index 0000000..da652f9 --- /dev/null +++ b/clients/gohttp/httpd/README.md @@ -0,0 +1,41 @@ +# WeChat Rest Api + +基于 [wcferry](https://github.com/opentdp/wechat-rest/tree/master/wcferry) 实现的 HTTP 接口服务,已实现如下功能: + +- 检查登录状态 +- 获取登录账号 wxid +- 获取登录账号个人信息 +- 获取所有消息类型 +- 获取完整通讯录 +- 获取好友列表 +- 获取所有数据库 +- 获取数据库中所有表 +- 执行 SQL 查询 +- 发送文本消息(可 @) +- 发送图片 +- 发送文件 +- 发送卡片消息 +- 保存图片 +- 保存语音 +- 图片 OCR +- 接受好友申请 +- 接收转账 +- 刷新朋友圈 +- 添加群成员 +- 删除群成员 +- 获取群列表 +- 获取群成员列表 +- 获取群成员昵称 +- 邀请群成员 +- 拍一拍群友 +- 转发消息给好友 +- 转发收到的消息到URL + +## 生成 OpenApi 文档 + +```shell +go get github.com/swaggo/swag/cmd/swag +go install github.com/swaggo/swag/cmd/swag + +swag init --parseDependency -g httpd/server.go -o public/swag -ot json +``` diff --git a/clients/gohttp/httpd/server.go b/clients/gohttp/httpd/server.go index 2eb39eb..70dad6b 100644 --- a/clients/gohttp/httpd/server.go +++ b/clients/gohttp/httpd/server.go @@ -8,9 +8,9 @@ import ( "wechat-rest/httpd/wcfrest" ) -// @title Wechat Rest API -// @version v0.4.6 -// @description 基于 WeChatFerry RPC 实现的电脑版微信 REST API,使用 Go 语言编写,无第三方运行时依赖。基于 HTTP 提供操作接口,轻松对接任意编程语言。 +// @title WeChat Rest Api +// @version v0.5.0 +// @description 基于 WeChatFerry RPC 实现的微信接口,使用 Go 语言编写,无第三方运行时依赖,易于对接任意编程语言。 // @contact.name WeChatRest // @contact.url https://github.com/opentdp/wechat-rest // @license.name Apache 2.0 diff --git a/clients/gohttp/httpd/wcfrest/controller.go b/clients/gohttp/httpd/wcfrest/controller.go index e53080f..bff2b95 100644 --- a/clients/gohttp/httpd/wcfrest/controller.go +++ b/clients/gohttp/httpd/wcfrest/controller.go @@ -110,7 +110,7 @@ func (wc *Controller) dbSqlQuery(c *gin.Context) { return } - c.Set("Payload", wc.CmdClient.DbSqlQueryMap(req.Db, req.Sql)) + c.Set("Payload", wc.CmdClient.DbSqlQuery(req.Db, req.Sql)) } @@ -531,14 +531,14 @@ func (wc *Controller) receiveTransfer(c *gin.Context) { } -// @Summary 开启转发消息到URL +// @Summary 开启推送消息到URL // @Produce json -// @Param body body ForwardMsgRequest true "消息转发请求参数" +// @Param body body ReceiverRequest true "消息推送请求参数" // @Success 200 {object} RespPayload -// @Router /enable_forward_msg [post] -func (wc *Controller) enableForwardMsg(c *gin.Context) { +// @Router /enable_receiver [post] +func (wc *Controller) enabledReceiver(c *gin.Context) { - var req ForwardMsgRequest + var req ReceiverRequest if err := c.ShouldBindJSON(&req); err != nil { c.Set("Error", err) return @@ -549,7 +549,7 @@ func (wc *Controller) enableForwardMsg(c *gin.Context) { return } - err := wc.enableForwardToUrl(req.Url) + err := wc.enableUrlReceiver(req.Url) c.Set("Payload", RespPayload{ Success: err == nil, Error: err, @@ -557,20 +557,20 @@ func (wc *Controller) enableForwardMsg(c *gin.Context) { } -// @Summary 关闭转发消息到URL +// @Summary 关闭推送消息到URL // @Produce json -// @Param body body ForwardMsgRequest true "消息转发请求参数" +// @Param body body ReceiverRequest true "消息推送请求参数" // @Success 200 {object} RespPayload -// @Router /disable_forward_msg [post] -func (wc *Controller) disableForwardMsg(c *gin.Context) { +// @Router /disable_receiver [post] +func (wc *Controller) disableReceiver(c *gin.Context) { - var req ForwardMsgRequest + var req ReceiverRequest if err := c.ShouldBindJSON(&req); err != nil { c.Set("Error", err) return } - err := wc.disableForwardToUrl(req.Url) + err := wc.disableUrlReceiver(req.Url) c.Set("Payload", RespPayload{ Success: err == nil, Error: err, diff --git a/clients/gohttp/httpd/wcfrest/forward.go b/clients/gohttp/httpd/wcfrest/receiver.go similarity index 52% rename from clients/gohttp/httpd/wcfrest/forward.go rename to clients/gohttp/httpd/wcfrest/receiver.go index f51e06d..542d988 100644 --- a/clients/gohttp/httpd/wcfrest/forward.go +++ b/clients/gohttp/httpd/wcfrest/receiver.go @@ -9,15 +9,15 @@ import ( "github.com/opentdp/wechat-rest/wcferry" ) -var forwardToUrlStat = false -var forwardToUrlList = map[string]bool{} +var urlReceiverStat = false +var urlReceiverList = map[string]bool{} -func (wc *Controller) enableForwardToUrl(url string) error { +func (wc *Controller) enableUrlReceiver(url string) error { - if !forwardToUrlStat { + if !urlReceiverStat { err := wc.EnrollReceiver(true, func(msg *wcferry.WxMsg) { - ret := wcferry.WxMsgParser(msg) - for url := range forwardToUrlList { + ret := wcferry.ParseWxMsg(msg) + for url := range urlReceiverList { logman.Info("forward msg", "url", url, "Id", ret.Id) go request.JsonPost(url, ret, request.H{}) } @@ -27,30 +27,30 @@ func (wc *Controller) enableForwardToUrl(url string) error { } } - if _, ok := forwardToUrlList[url]; ok { + if _, ok := urlReceiverList[url]; ok { return errors.New("url already exists") } - forwardToUrlStat = true - forwardToUrlList[url] = true + urlReceiverStat = true + urlReceiverList[url] = true return nil } -func (wc *Controller) disableForwardToUrl(url string) error { +func (wc *Controller) disableUrlReceiver(url string) error { - if _, ok := forwardToUrlList[url]; !ok { + if _, ok := urlReceiverList[url]; !ok { return errors.New("url not exists") } - delete(forwardToUrlList, url) + delete(urlReceiverList, url) - if len(forwardToUrlList) == 0 { + if len(urlReceiverList) == 0 { if err := wc.DisableReceiver(false); err != nil { return err } - forwardToUrlStat = false + urlReceiverStat = false } return nil diff --git a/clients/gohttp/httpd/wcfrest/router.go b/clients/gohttp/httpd/wcfrest/router.go index c68a25d..3fc3e62 100644 --- a/clients/gohttp/httpd/wcfrest/router.go +++ b/clients/gohttp/httpd/wcfrest/router.go @@ -45,7 +45,7 @@ func Route(rg *gin.RouterGroup) { rg.POST("accept_new_friend", ctrl.acceptNewFriend) rg.POST("receive_transfer", ctrl.receiveTransfer) - rg.POST("enable_forward_msg", ctrl.enableForwardMsg) - rg.POST("disable_forward_msg", ctrl.disableForwardMsg) + rg.POST("enable_receiver", ctrl.enabledReceiver) + rg.POST("disable_receiver", ctrl.disableReceiver) } diff --git a/clients/gohttp/httpd/wcfrest/types.go b/clients/gohttp/httpd/wcfrest/types.go index e5d5989..79ef94b 100644 --- a/clients/gohttp/httpd/wcfrest/types.go +++ b/clients/gohttp/httpd/wcfrest/types.go @@ -14,7 +14,7 @@ type DbSqlQueryRequest struct { } // 消息转发参数 -type ForwardMsgRequest struct { +type ReceiverRequest struct { Url string `json:"url"` } diff --git a/clients/gohttp/public/swag/swagger.json b/clients/gohttp/public/swag/swagger.json index 46e13b5..865264c 100644 --- a/clients/gohttp/public/swag/swagger.json +++ b/clients/gohttp/public/swag/swagger.json @@ -1,8 +1,8 @@ { "swagger": "2.0", "info": { - "description": "基于 WeChatFerry RPC 实现的电脑版微信 REST API,使用 Go 语言编写,无第三方运行时依赖。基于 HTTP 提供操作接口,轻松对接任意编程语言。", - "title": "Wechat Rest API", + "description": "基于 WeChatFerry RPC 实现的微信接口,使用 Go 语言编写,无第三方运行时依赖,易于对接任意编程语言。", + "title": "WeChat Rest Api", "contact": { "name": "WeChatRest", "url": "https://github.com/opentdp/wechat-rest" @@ -11,7 +11,7 @@ "name": "Apache 2.0", "url": "http://www.apache.org/licenses/LICENSE-2.0.html" }, - "version": "v0.4.6" + "version": "v0.5.0" }, "basePath": "/api", "paths": { @@ -269,20 +269,20 @@ } } }, - "/disable_forward_msg": { + "/disable_receiver": { "post": { "produces": [ "application/json" ], - "summary": "关闭转发消息到URL", + "summary": "关闭推送消息到URL", "parameters": [ { - "description": "消息转发请求参数", + "description": "消息推送请求参数", "name": "body", "in": "body", "required": true, "schema": { - "$ref": "#/definitions/wcfrest.ForwardMsgRequest" + "$ref": "#/definitions/wcfrest.ReceiverRequest" } } ], @@ -350,20 +350,20 @@ } } }, - "/enable_forward_msg": { + "/enable_receiver": { "post": { "produces": [ "application/json" ], - "summary": "开启转发消息到URL", + "summary": "开启推送消息到URL", "parameters": [ { - "description": "消息转发请求参数", + "description": "消息推送请求参数", "name": "body", "in": "body", "required": true, "schema": { - "$ref": "#/definitions/wcfrest.ForwardMsgRequest" + "$ref": "#/definitions/wcfrest.ReceiverRequest" } } ], @@ -1059,14 +1059,6 @@ } } }, - "wcfrest.ForwardMsgRequest": { - "type": "object", - "properties": { - "url": { - "type": "string" - } - } - }, "wcfrest.GetAudioMsgRequest": { "type": "object", "properties": { @@ -1092,6 +1084,14 @@ } } }, + "wcfrest.ReceiverRequest": { + "type": "object", + "properties": { + "url": { + "type": "string" + } + } + }, "wcfrest.RespPayload": { "type": "object", "properties": {