Add go http client
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package midware
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/opentdp/wechat-rest/args"
|
||||
)
|
||||
|
||||
func AuthGuard(c *gin.Context) {
|
||||
|
||||
token := ""
|
||||
|
||||
authcode := c.GetHeader("Authorization")
|
||||
parts := strings.SplitN(authcode, " ", 2)
|
||||
if len(parts) == 2 && parts[0] == "Bearer" {
|
||||
token = parts[1]
|
||||
}
|
||||
|
||||
if token != args.Httpd.Token {
|
||||
c.Set("Error", gin.H{"Code": 401, "Message": "未授权的操作"})
|
||||
c.Set("ExitCode", 401)
|
||||
c.Abort()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package midware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 获取错误代码
|
||||
|
||||
func exitCode(c *gin.Context, code int) int {
|
||||
|
||||
if code := c.GetInt("ExitCode"); code > 100 {
|
||||
return code
|
||||
}
|
||||
|
||||
return code
|
||||
|
||||
}
|
||||
|
||||
// 创建错误实例
|
||||
|
||||
func newError(data any) error {
|
||||
|
||||
if err, ok := data.(error); ok {
|
||||
return err
|
||||
}
|
||||
|
||||
if err, ok := data.(string); ok {
|
||||
return errors.New(err)
|
||||
}
|
||||
|
||||
return errors.New("未知错误")
|
||||
|
||||
}
|
||||
|
||||
// 构造错误信息
|
||||
|
||||
func newErrorMessage(data any) gin.H {
|
||||
|
||||
if err, ok := data.(error); ok {
|
||||
return gin.H{"Error": gin.H{"Message": err.Error()}}
|
||||
}
|
||||
|
||||
if err, ok := data.(string); ok {
|
||||
return gin.H{"Error": gin.H{"Message": err}}
|
||||
}
|
||||
|
||||
return gin.H{"Error": data}
|
||||
|
||||
}
|
||||
|
||||
// 构造结构数据
|
||||
|
||||
func newPayload(data any, msg, token string) gin.H {
|
||||
|
||||
payload := gin.H{"Payload": data}
|
||||
|
||||
if msg != "" {
|
||||
payload["Message"] = msg
|
||||
}
|
||||
|
||||
if token != "" {
|
||||
payload["Token"] = token
|
||||
}
|
||||
|
||||
return payload
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package midware
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func OutputHandle(c *gin.Context) {
|
||||
|
||||
c.Next()
|
||||
|
||||
// 输出错误信息
|
||||
|
||||
if err, exists := c.Get("Error"); exists {
|
||||
c.AbortWithStatusJSON(exitCode(c, 400), newErrorMessage(err))
|
||||
return
|
||||
}
|
||||
|
||||
// 输出请求结果
|
||||
|
||||
msg := c.GetString("Message")
|
||||
|
||||
if res, exists := c.Get("Payload"); exists || msg != "" {
|
||||
data := newPayload(res, msg, c.GetString("JwtToken"))
|
||||
c.AbortWithStatusJSON(exitCode(c, 200), data)
|
||||
return
|
||||
}
|
||||
|
||||
// 输出HTML内容
|
||||
|
||||
if htm := c.GetString("HTML"); htm != "" {
|
||||
c.Header("Content-Type", "text/html; charset=utf-8")
|
||||
c.String(200, htm)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 捕获异常返回
|
||||
|
||||
c.AbortWithStatusJSON(500, newErrorMessage("内部错误"))
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package httpd
|
||||
|
||||
import (
|
||||
"github.com/opentdp/go-helper/httpd"
|
||||
|
||||
"github.com/opentdp/wechat-rest/args"
|
||||
"github.com/opentdp/wechat-rest/httpd/midware"
|
||||
"github.com/opentdp/wechat-rest/httpd/wcfrest"
|
||||
)
|
||||
|
||||
// @title Wechat Rest API
|
||||
// @version v0.4.2
|
||||
// @description 基于 WeChatFerry RPC 实现的电脑版微信 REST-API,使用 Go 语言编写,无第三方运行时依赖。基于 HTTP 提供操作接口,轻松对接任意编程语言。
|
||||
// @contact.name WeChatRest
|
||||
// @contact.url https://github.com/opentdp/wechat-rest
|
||||
// @license.name Apache 2.0
|
||||
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
// @BasePath /api
|
||||
|
||||
func Server() {
|
||||
|
||||
httpd.Engine(args.Debug)
|
||||
|
||||
api := httpd.Group("/api")
|
||||
api.Use(midware.OutputHandle, midware.AuthGuard)
|
||||
|
||||
// 注册 WCF
|
||||
wcfrest.Route(api)
|
||||
|
||||
// 前端文件路由
|
||||
httpd.StaticEmbed("/", "public", args.Efs)
|
||||
|
||||
// 启动 HTTP 服务
|
||||
httpd.Server(args.Httpd.Address)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,581 @@
|
||||
package wcfrest
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/opentdp/go-helper/logman"
|
||||
"github.com/opentdp/go-helper/request"
|
||||
"github.com/opentdp/go-helper/strutil"
|
||||
|
||||
"github.com/opentdp/wechat-rest/args"
|
||||
"github.com/opentdp/wechat-rest/wcferry"
|
||||
)
|
||||
|
||||
var wc *wcferry.Client
|
||||
|
||||
func initService() {
|
||||
|
||||
parts := strings.Split(args.Wcf.Address, ":")
|
||||
|
||||
wc = &wcferry.Client{
|
||||
ListenAddr: parts[0],
|
||||
ListenPort: strutil.ToInt(parts[1]),
|
||||
SdkLibrary: args.Wcf.SdkLibrary,
|
||||
WeChatAuto: args.Wcf.WeChatAuto,
|
||||
}
|
||||
|
||||
logman.Info("wcf starting ...")
|
||||
if err := wc.Connect(); err != nil {
|
||||
logman.Fatal("failed to start wcf", "error", err)
|
||||
}
|
||||
|
||||
// 打印收到的消息
|
||||
if args.Wcf.MsgPrint {
|
||||
wc.EnrollReceiver(true, wcferry.MsgPrinter)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// @Summary 检查登录状态
|
||||
// @Produce json
|
||||
// @Success 200 {object} bool
|
||||
// @Router /is_login [get]
|
||||
func isLogin(c *gin.Context) {
|
||||
|
||||
c.Set("Payload", wc.CmdClient.IsLogin())
|
||||
|
||||
}
|
||||
|
||||
// @Summary 获取登录账号wxid
|
||||
// @Produce json
|
||||
// @Success 200 {object} string
|
||||
// @Router /self_wxid [get]
|
||||
func getSelfWxid(c *gin.Context) {
|
||||
|
||||
c.Set("Payload", wc.CmdClient.GetSelfWxid())
|
||||
|
||||
}
|
||||
|
||||
// @Summary 获取登录账号个人信息
|
||||
// @Produce json
|
||||
// @Success 200 {object} wcferry.UserInfo
|
||||
// @Router /user_info [get]
|
||||
func getUserInfo(c *gin.Context) {
|
||||
|
||||
c.Set("Payload", wc.CmdClient.GetUserInfo())
|
||||
|
||||
}
|
||||
|
||||
// @Summary 获取完整通讯录
|
||||
// @Produce json
|
||||
// @Success 200 {object} []wcferry.RpcContact
|
||||
// @Router /contacts [get]
|
||||
func getContacts(c *gin.Context) {
|
||||
|
||||
c.Set("Payload", wc.CmdClient.GetContacts())
|
||||
|
||||
}
|
||||
|
||||
// @Summary 获取好友列表
|
||||
// @Produce json
|
||||
// @Success 200 {object} []wcferry.RpcContact
|
||||
// @Router /friends [get]
|
||||
func getFriends(c *gin.Context) {
|
||||
|
||||
c.Set("Payload", wc.CmdClient.GetFriends())
|
||||
|
||||
}
|
||||
|
||||
// @Summary 根据wxid获取个人信息
|
||||
// @Produce json
|
||||
// @Param wxid path string true "wxid"
|
||||
// @Success 200 {object} wcferry.RpcContact
|
||||
// @Router /user_info/{wxid} [get]
|
||||
func getUserInfoByWxid(c *gin.Context) {
|
||||
|
||||
wxid := c.Param("wxid")
|
||||
c.Set("Payload", wc.CmdClient.GetInfoByWxid(wxid))
|
||||
|
||||
}
|
||||
|
||||
// @Summary 获取数据库列表
|
||||
// @Produce json
|
||||
// @Success 200 {object} []string
|
||||
// @Router /db_names [get]
|
||||
func getDbNames(c *gin.Context) {
|
||||
|
||||
c.Set("Payload", wc.CmdClient.GetDbNames())
|
||||
|
||||
}
|
||||
|
||||
// @Summary 获取数据库表列表
|
||||
// @Produce json
|
||||
// @Param db path string true "数据库名"
|
||||
// @Success 200 {object} []wcferry.DbTable
|
||||
// @Router /db_tables/{db} [get]
|
||||
func getDbTables(c *gin.Context) {
|
||||
|
||||
db := c.Param("db")
|
||||
c.Set("Payload", wc.CmdClient.GetDbTables(db))
|
||||
|
||||
}
|
||||
|
||||
// @Summary 执行数据库查询
|
||||
// @Produce json
|
||||
// @Param body body DbSqlQueryRequest true "数据库查询请求参数"
|
||||
// @Success 200 {object} map[string]any
|
||||
// @Router /db_query_sql [post]
|
||||
func dbSqlQuery(c *gin.Context) {
|
||||
|
||||
var req DbSqlQueryRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Set("Error", err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("Payload", wc.CmdClient.DbSqlQueryMap(req.Db, req.Sql))
|
||||
|
||||
}
|
||||
|
||||
// @Summary 获取所有消息类型
|
||||
// @Produce json
|
||||
// @Success 200 {object} map[int32]string
|
||||
// @Router /msg_types [get]
|
||||
func getMsgTypes(c *gin.Context) {
|
||||
|
||||
c.Set("Payload", wc.CmdClient.GetMsgTypes())
|
||||
|
||||
}
|
||||
|
||||
// @Summary 刷新朋友圈
|
||||
// @Produce json
|
||||
// @Param id path int true "朋友圈id"
|
||||
// @Success 200 {object} RespPayload
|
||||
// @Router /refresh_pyq/{id} [get]
|
||||
func refreshPyq(c *gin.Context) {
|
||||
|
||||
id := c.Param("id")
|
||||
pyqid := uint64(strutil.ToUint(id))
|
||||
|
||||
status := wc.CmdClient.RefreshPyq(pyqid)
|
||||
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: status == 1,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// @Summary 获取群列表
|
||||
// @Produce json
|
||||
// @Success 200 {object} []wcferry.RpcContact
|
||||
// @Router /chatrooms [get]
|
||||
func getChatRooms(c *gin.Context) {
|
||||
|
||||
c.Set("Payload", wc.CmdClient.GetChatRooms())
|
||||
|
||||
}
|
||||
|
||||
// @Summary 获取群成员列表
|
||||
// @Produce json
|
||||
// @Param roomid path string true "群id"
|
||||
// @Success 200 {object} []wcferry.RpcContact
|
||||
// @Router /chatroom_members/{roomid} [get]
|
||||
func getChatRoomMembers(c *gin.Context) {
|
||||
|
||||
roomid := c.Param("roomid")
|
||||
c.Set("Payload", wc.CmdClient.GetChatRoomMembers(roomid))
|
||||
|
||||
}
|
||||
|
||||
// @Summary 获取群成员昵称
|
||||
// @Produce json
|
||||
// @Param wxid path string true "wxid"
|
||||
// @Param roomid path string true "群id"
|
||||
// @Success 200 {object} string
|
||||
// @Router /alias_in_chatroom/{wxid}/{roomid} [get]
|
||||
func getAliasInChatRoom(c *gin.Context) {
|
||||
|
||||
wxid := c.Param("wxid")
|
||||
roomid := c.Param("roomid")
|
||||
c.Set("Payload", wc.CmdClient.GetAliasInChatRoom(wxid, roomid))
|
||||
|
||||
}
|
||||
|
||||
// @Summary 邀请群成员
|
||||
// @Produce json
|
||||
// @Param body body wcferry.MemberMgmt true "增删群成员请求参数"
|
||||
// @Success 200 {object} RespPayload
|
||||
// @Router /invite_chatroom_members [post]
|
||||
func inviteChatroomMembers(c *gin.Context) {
|
||||
|
||||
var req wcferry.MemberMgmt
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Set("Error", err)
|
||||
return
|
||||
}
|
||||
|
||||
status := wc.CmdClient.InviteChatroomMembers(req.Roomid, req.Wxids)
|
||||
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: status == 1,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// @Summary 添加群成员
|
||||
// @Produce json
|
||||
// @Param body body wcferry.MemberMgmt true "增删群成员请求参数"
|
||||
// @Success 200 {object} RespPayload
|
||||
// @Router /add_chatroom_members [post]
|
||||
func addChatRoomMembers(c *gin.Context) {
|
||||
|
||||
var req wcferry.MemberMgmt
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Set("Error", err)
|
||||
return
|
||||
}
|
||||
|
||||
status := wc.CmdClient.AddChatRoomMembers(req.Roomid, req.Wxids)
|
||||
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: status == 1,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// @Summary 删除群成员
|
||||
// @Produce json
|
||||
// @Param body body wcferry.MemberMgmt true "增删群成员请求参数"
|
||||
// @Success 200 {object} RespPayload
|
||||
// @Router /del_chatroom_members [post]
|
||||
func delChatRoomMembers(c *gin.Context) {
|
||||
|
||||
var req wcferry.MemberMgmt
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Set("Error", err)
|
||||
return
|
||||
}
|
||||
|
||||
status := wc.CmdClient.DelChatRoomMembers(req.Roomid, req.Wxids)
|
||||
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: status == 1,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// @Summary 撤回消息
|
||||
// @Produce json
|
||||
// @Param msgid path int true "消息id"
|
||||
// @Success 200 {object} RespPayload
|
||||
// @Router /revoke_msg/{msgid} [get]
|
||||
func revokeMsg(c *gin.Context) {
|
||||
|
||||
id := c.Param("msgid")
|
||||
msgid := uint64(strutil.ToUint(id))
|
||||
|
||||
status := wc.CmdClient.RevokeMsg(msgid)
|
||||
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: status == 1,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// @Summary 发送文本消息
|
||||
// @Produce json
|
||||
// @Param body body wcferry.TextMsg true "文本消息请求参数"
|
||||
// @Success 200 {object} RespPayload
|
||||
// @Router /send_txt [post]
|
||||
func sendTxt(c *gin.Context) {
|
||||
|
||||
var req wcferry.TextMsg
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Set("Error", err)
|
||||
return
|
||||
}
|
||||
|
||||
status := wc.CmdClient.SendTxt(req.Msg, req.Receiver, req.Aters)
|
||||
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: status == 0,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// @Summary 发送图片消息
|
||||
// @Produce json
|
||||
// @Param body body wcferry.PathMsg true "图片消息请求参数"
|
||||
// @Success 200 {object} RespPayload
|
||||
// @Router /send_img [post]
|
||||
func sendImg(c *gin.Context) {
|
||||
|
||||
var req wcferry.PathMsg
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Set("Error", err)
|
||||
return
|
||||
}
|
||||
|
||||
status := wc.CmdClient.SendImg(req.Path, req.Receiver)
|
||||
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: status == 0,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// @Summary 发送文件消息
|
||||
// @Produce json
|
||||
// @Param body body wcferry.PathMsg true "文件消息请求参数"
|
||||
// @Success 200 {object} RespPayload
|
||||
// @Router /send_file [post]
|
||||
func sendFile(c *gin.Context) {
|
||||
|
||||
var req wcferry.PathMsg
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Set("Error", err)
|
||||
return
|
||||
}
|
||||
|
||||
status := wc.CmdClient.SendFile(req.Path, req.Receiver)
|
||||
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: status == 0,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// @Summary 发送卡片消息
|
||||
// @Produce json
|
||||
// @Param body body wcferry.RichText true "卡片消息请求参数"
|
||||
// @Success 200 {object} RespPayload
|
||||
// @Router /send_rich_text [post]
|
||||
func sendRichText(c *gin.Context) {
|
||||
|
||||
var req wcferry.RichText
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Set("Error", err)
|
||||
return
|
||||
}
|
||||
|
||||
status := wc.CmdClient.SendRichText(req.Name, req.Account, req.Title, req.Digest, req.Url, req.Thumburl, req.Receiver)
|
||||
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: status == 0,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// @Summary 拍一拍群友
|
||||
// @Produce json
|
||||
// @Param body body wcferry.PatMsg true "拍一拍请求参数"
|
||||
// @Success 200 {object} RespPayload
|
||||
// @Router /send_pat_msg [post]
|
||||
func sendPatMsg(c *gin.Context) {
|
||||
|
||||
var req wcferry.PatMsg
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Set("Error", err)
|
||||
return
|
||||
}
|
||||
|
||||
status := wc.CmdClient.SendPatMsg(req.Roomid, req.Wxid)
|
||||
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: status == 1,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// @Summary 获取语音消息
|
||||
// @Produce json
|
||||
// @Param body body GetAudioMsgRequest true "语音消息请求参数"
|
||||
// @Success 200 {object} RespPayload
|
||||
// @Router /get_audio_msg [post]
|
||||
func getAudioMsg(c *gin.Context) {
|
||||
|
||||
var req GetAudioMsgRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Set("Error", err)
|
||||
return
|
||||
}
|
||||
|
||||
if req.Timeout > 0 {
|
||||
resp, err := wc.CmdClient.GetAudioMsgTimeout(req.Msgid, req.Dir, req.Timeout)
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: resp != "",
|
||||
Result: resp,
|
||||
Error: err,
|
||||
})
|
||||
} else {
|
||||
resp := wc.CmdClient.GetAudioMsg(req.Msgid, req.Dir)
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: resp != "",
|
||||
Result: resp,
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// @Summary 获取OCR识别结果
|
||||
// @Produce json
|
||||
// @Param body body GetOcrRequest true "文本请求参数"
|
||||
// @Success 200 {object} RespPayload
|
||||
// @Router /get_ocr_result [post]
|
||||
func getOcrResult(c *gin.Context) {
|
||||
|
||||
var req GetOcrRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Set("Error", err)
|
||||
return
|
||||
}
|
||||
|
||||
if req.Timeout > 0 {
|
||||
resp, err := wc.CmdClient.GetOcrResultTimeout(req.Extra, req.Timeout)
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: resp != "",
|
||||
Result: resp,
|
||||
Error: err,
|
||||
})
|
||||
} else {
|
||||
resp, stat := wc.CmdClient.GetOcrResult(req.Extra)
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: stat == 0,
|
||||
Result: resp,
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// @Summary 下载图片
|
||||
// @Produce json
|
||||
// @Param body body DownloadImageRequest true "下载图片参数"
|
||||
// @Success 200 {object} RespPayload
|
||||
// @Router /download_image [post]
|
||||
func downloadImage(c *gin.Context) {
|
||||
|
||||
var req DownloadImageRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Set("Error", err)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := wc.CmdClient.DownloadImage(req.Msgid, req.Extra, req.Dir, req.Timeout)
|
||||
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: resp != "",
|
||||
Result: resp,
|
||||
Error: err,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// @Summary 下载附件
|
||||
// @Produce json
|
||||
// @Param body body DownloadAttachRequest true "下载附件参数"
|
||||
// @Success 200 {object} RespPayload
|
||||
// @Router /download_attach [post]
|
||||
func downloadAttach(c *gin.Context) {
|
||||
|
||||
var req DownloadAttachRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Set("Error", err)
|
||||
return
|
||||
}
|
||||
|
||||
status := wc.CmdClient.DownloadAttach(req.Msgid, req.Thumb, req.Extra)
|
||||
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: status == 0,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// @Summary 接受好友请求
|
||||
// @Produce json
|
||||
// @Param body body wcferry.Verification true "接受好友请求参数"
|
||||
// @Success 200 {object} RespPayload
|
||||
// @Router /accept_new_friend [post]
|
||||
func acceptNewFriend(c *gin.Context) {
|
||||
|
||||
var req wcferry.Verification
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Set("Error", err)
|
||||
return
|
||||
}
|
||||
|
||||
status := wc.CmdClient.AcceptNewFriend(req.V3, req.V4, req.Scene)
|
||||
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: status == 1,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// @Summary 接受转账
|
||||
// @Produce json
|
||||
// @Param body body wcferry.Transfer true "接受转账请求参数"
|
||||
// @Success 200 {object} RespPayload
|
||||
// @Router /receive_transfer [post]
|
||||
func receiveTransfer(c *gin.Context) {
|
||||
|
||||
var req wcferry.Transfer
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Set("Error", err)
|
||||
return
|
||||
}
|
||||
|
||||
status := wc.CmdClient.ReceiveTransfer(req.Wxid, req.Tfid, req.Taid)
|
||||
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: status == 1,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// @Summary 开启消息转发
|
||||
// @Produce json
|
||||
// @Param body body ForwardMsgRequest true "消息转发请求参数"
|
||||
// @Success 200 {object} RespPayload
|
||||
// @Router /enable_forward_msg [post]
|
||||
func enableForwardMsg(c *gin.Context) {
|
||||
|
||||
var req ForwardMsgRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Set("Error", err)
|
||||
return
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(req.Url, "http") {
|
||||
c.Set("Error", "url must start with http(s)://")
|
||||
return
|
||||
}
|
||||
|
||||
err := wc.EnrollReceiver(true, func(msg *wcferry.WxMsg) {
|
||||
logman.Info("forward msg", "url", req.Url, "Id", msg.Id)
|
||||
request.JsonPost(req.Url, msg, request.H{})
|
||||
})
|
||||
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: err == nil,
|
||||
Error: err,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// @Summary 关闭消息转发
|
||||
// @Produce json
|
||||
// @Param body body ForwardMsgRequest true "消息转发请求参数"
|
||||
// @Success 200 {object} RespPayload
|
||||
// @Router /disable_forward_msg [post]
|
||||
func disableForwardMsg(c *gin.Context) {
|
||||
|
||||
err := wc.DisableReceiver()
|
||||
|
||||
c.Set("Payload", RespPayload{
|
||||
Success: err == nil,
|
||||
Error: err,
|
||||
})
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package wcfrest
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Route(rg *gin.RouterGroup) {
|
||||
|
||||
initService()
|
||||
|
||||
rg.GET("is_login", isLogin)
|
||||
rg.GET("self_wxid", getSelfWxid)
|
||||
rg.GET("user_info", getUserInfo)
|
||||
rg.GET("contacts", getContacts)
|
||||
rg.GET("friends", getFriends)
|
||||
rg.GET("user_info/:wxid", getUserInfoByWxid)
|
||||
|
||||
rg.GET("db_names", getDbNames)
|
||||
rg.GET("db_tables/:db", getDbTables)
|
||||
rg.POST("db_query_sql", dbSqlQuery)
|
||||
|
||||
rg.GET("msg_types", getMsgTypes)
|
||||
rg.GET("refresh_pyq/:id", refreshPyq)
|
||||
|
||||
rg.GET("chatrooms", getChatRooms)
|
||||
rg.GET("chatroom_members/:roomid", getChatRoomMembers)
|
||||
rg.GET("alias_in_chatroom/:wxid/:roomid", getAliasInChatRoom)
|
||||
rg.POST("invite_chatroom_members", inviteChatroomMembers)
|
||||
rg.POST("add_chatroom_members", addChatRoomMembers)
|
||||
rg.POST("del_chatroom_members", delChatRoomMembers)
|
||||
|
||||
rg.GET("revoke_msg/:msgid", revokeMsg)
|
||||
rg.POST("send_txt", sendTxt)
|
||||
rg.POST("send_img", sendImg)
|
||||
rg.POST("send_file", sendFile)
|
||||
rg.POST("send_rich_text", sendRichText)
|
||||
rg.POST("send_pat_msg", sendPatMsg)
|
||||
rg.POST("get_audio_msg", getAudioMsg)
|
||||
rg.POST("get_ocr_result", getOcrResult)
|
||||
rg.POST("download_image", downloadImage)
|
||||
rg.POST("download_attach", downloadAttach)
|
||||
|
||||
rg.POST("accept_new_friend", acceptNewFriend)
|
||||
rg.POST("receive_transfer", receiveTransfer)
|
||||
|
||||
rg.POST("enable_forward_msg", enableForwardMsg)
|
||||
rg.POST("disable_forward_msg", disableForwardMsg)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package wcfrest
|
||||
|
||||
// 执行结果
|
||||
type RespPayload struct {
|
||||
Success bool `json:"success,omitempty"`
|
||||
Result string `json:"result,omitempty"`
|
||||
Error error `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// 数据库查询参数
|
||||
type DbSqlQueryRequest struct {
|
||||
Db string `json:"db"`
|
||||
Sql string `json:"sql"`
|
||||
}
|
||||
|
||||
// 消息转发参数
|
||||
type ForwardMsgRequest struct {
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
// 获取音频消息参数
|
||||
type GetAudioMsgRequest struct {
|
||||
Msgid uint64 `json:"msgid"`
|
||||
Dir string `json:"path"`
|
||||
Timeout int `json:"timeout"`
|
||||
}
|
||||
|
||||
// 获取OCR识别参数
|
||||
type GetOcrRequest struct {
|
||||
Extra string `json:"extra"`
|
||||
Timeout int `json:"timeout"`
|
||||
}
|
||||
|
||||
// 下载图片参数
|
||||
type DownloadImageRequest struct {
|
||||
Msgid uint64 `json:"msgid"`
|
||||
Extra string `json:"extra"`
|
||||
Dir string `json:"dir"`
|
||||
Timeout int `json:"timeout"`
|
||||
}
|
||||
|
||||
// 下载附件参数
|
||||
type DownloadAttachRequest struct {
|
||||
Msgid uint64 `json:"msgid"`
|
||||
Thumb string `json:"thumb"`
|
||||
Extra string `json:"extra"`
|
||||
}
|
||||
Reference in New Issue
Block a user