修理图片下载,增加消息中缺少的字段

This commit is contained in:
origin 2024-07-11 17:02:31 +08:00
parent 8b1156f72c
commit 5b75c57b53
7 changed files with 106 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
type Result struct {
@ -196,6 +197,7 @@ func SendIMG(c *gin.Context) {
var RequestData struct {
Path string `json:"path"`
Receiver string `json:"receiver"`
Suffix string `json:"suffix"`
}
if err := c.BindJSON(&RequestData); err != nil {
result.Code = 0
@ -205,6 +207,9 @@ func SendIMG(c *gin.Context) {
c.JSON(http.StatusOK, result)
return
}
if RequestData.Path[0:4] == "http" {
RequestData.Path, _ = DownloadFile(RequestData.Path, "file", RequestData.Suffix)
}
result.Code = 1
result.Message = "发送完成"
var data = WxClient.SendIMG(RequestData.Path, RequestData.Receiver)
@ -218,6 +223,7 @@ func SendFile(c *gin.Context) {
var RequestData struct {
Path string `json:"path"`
Receiver string `json:"receiver"`
Suffix string `json:"suffix"`
}
if err := c.BindJSON(&RequestData); err != nil {
result.Code = 0
@ -227,6 +233,9 @@ func SendFile(c *gin.Context) {
c.JSON(http.StatusOK, result)
return
}
if RequestData.Path[0:4] == "http" {
RequestData.Path, _ = DownloadFile(RequestData.Path, "file", RequestData.Suffix)
}
result.Code = 1
result.Message = "发送完成"
var data = WxClient.SendFile(RequestData.Path, RequestData.Receiver)
@ -444,7 +453,17 @@ func DownloadAttach(c *gin.Context) {
}
result.Code = 1
result.Message = "下载附件调用成功"
var data = WxClient.DownloadAttach(RequestData.Id, RequestData.Thumb, RequestData.Extra)
result.Data = data
WxClient.DownloadAttach(RequestData.Id, "", RequestData.Extra)
times := 1
path := ""
for times < 30 {
path = WxClient.DecryptImage(RequestData.Extra, RequestData.Thumb)
if path != "func:FUNC_DECRYPT_IMAGE str:\"\"" && path != "func:FUNC_DECRYPT_IMAGE str:\"\"" {
break
}
time.Sleep(time.Millisecond * 1000)
times += 1
}
result.Data = map[string]string{"path": path}
c.JSON(http.StatusOK, result)
}

View File

@ -10,8 +10,15 @@ extern int WxDestroySDK();
*/
import "C"
import (
"bytes"
"fmt"
"github.com/go-resty/resty/v2"
"github.com/google/uuid"
"go_wechatFerry/wcf"
"io"
"os"
"path/filepath"
"strings"
"time"
)
@ -19,6 +26,7 @@ var WxClient *wcf.Client
// Message 组装成一个结构体展示消息
type Message struct {
IsSelf bool `json:"is_self,omitempty"`
IsGroup bool `json:"is_group,omitempty"`
MessageId uint64 `json:"message_id,omitempty"`
Type uint32 `json:"type,omitempty"`
@ -27,12 +35,13 @@ type Message struct {
Content string `json:"content,omitempty"`
WxId string `json:"wx_id,omitempty"`
Sign string `json:"sign,omitempty"`
Thumb string `json:"thumb,omitempty"`
Extra string `json:"extra,omitempty"`
Xml string `json:"xml,omitempty"`
}
// WechatFerryInit 调用sdk.dll中的WxInitSdk 进行启动微信并注入
func WechatFerryInit() {
// 调试模式 端口
initSuccess := C.WxInitSDK(C.bool(false), C.int(10086))
if initSuccess == 0 {
@ -60,6 +69,13 @@ func WechatFerryInit() {
fmt.Println("初始化完成")
}
// MessageProcess 在这里可以继续写代码了
func MessageProcess(msg Message) {
// 方法都在WxClient中
//WxClient.SendTxt("测试","","")
fmt.Println(msg)
}
// ContactsInit 通讯录初始化
func ContactsInit() {
var contactsMap []map[string]string
@ -86,3 +102,44 @@ func ContactsInit() {
}
WxClient.ContactsMap = contactsMap
}
// DownloadFile 下载文件
func DownloadFile(url string, fileType string, suffix string) (string, error) {
fmt.Println(url)
// 发送HTTP请求获取文件
resp, err := resty.New().R().Get(url)
if err != nil {
return "", err
}
// 获取当前日期
currentTime := time.Now()
datePath := filepath.Join("./resource/static/"+fileType, currentTime.Format("2006-01-02"))
// 创建目录
if err := os.MkdirAll(datePath, os.ModePerm); err != nil {
return "", err
}
// 生成唯一的文件名
fileName := uuid.New().String() + "." + suffix
filePath := filepath.Join(datePath, fileName)
// 创建文件
file, err := os.Create(filePath)
if err != nil {
return "", err
}
defer file.Close()
// 将HTTP响应的Body复制到文件
_, err = io.Copy(file, bytes.NewBuffer(resp.Body()))
if err != nil {
return "", err
}
currentDir, err := os.Getwd()
if err != nil {
return "", err
}
filePath = currentDir + "/" + filePath
filePath = strings.Replace(filePath, "\\", "/", -1)
return filePath, nil
}

View File

@ -6,6 +6,7 @@ require (
github.com/danbai225/go-logs v0.3.2
github.com/gin-gonic/gin v1.10.0
github.com/go-resty/resty/v2 v2.13.1
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.0
go.nanomsg.org/mangos/v3 v3.4.2
google.golang.org/protobuf v1.34.2

View File

@ -36,6 +36,8 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=

View File

@ -103,12 +103,13 @@ func httpInit() {
// 下载附件
r.POST("/api/DownloadAttach", app.DownloadAttach)
r.Run(":8000")
r.Run("127.0.0.1:8000")
}
func OnMsg() {
err := app.WxClient.OnMSG(func(msg *wcf.WxMsg) {
var message app.Message
message.IsSelf = msg.IsSelf
message.IsGroup = msg.IsGroup
message.MessageId = msg.Id
message.Type = msg.Type
@ -117,7 +118,10 @@ func OnMsg() {
message.Content = msg.Content
message.Sign = msg.Sign
message.WxId = msg.Sender
message.Thumb = msg.Thumb
message.Extra = msg.Extra
message.Xml = msg.Xml
// 如果你设置了回调链接 那么他就是会传给你 如果你没设置 你可以在else中 添加你的代码 直接删掉 回调的判断即可
if app.WxClient.MessageCallbackUrl != "" {
var data = map[string]interface{}{
"code": 0,
@ -177,11 +181,23 @@ func OnMsg() {
}
}
} else {
// 植入我自己的功能接口
fmt.Println("请设置消息回调的url")
//fmt.Println("消息类型:", message.Type)
//fmt.Println("消息Thumb", message.Thumb) // 这个可以直接下载
//fmt.Println("消息Extra", message.Extra) // 这个要点一下才能下载(自行处理)
//fmt.Println("消息xml", message.Xml)
//if message.Type == 3 {
// resp, _ := resty.New().R().SetBody(map[string]interface{}{
// "id": message.MessageId,
// "thumb": "F:/c++/WeChatFerry/clients/go_wcf_http/", //下载到本地的哪里
// "extra": message.Thumb, // 看上面的
// }).Post("http://127.0.0.1:8001/api/DownloadAttach")
// fmt.Println(resp.String())
// fmt.Println(resp.Error())
// fmt.Println(resp.StatusCode())
//}
go app.MessageProcess(message)
}
})
fmt.Println(err)
if err != nil {
fmt.Println(err)
OnMsg()

File diff suppressed because one or more lines are too long

View File

@ -237,7 +237,7 @@ func (c *Client) RefreshPYQ() int32 {
}
// DecryptImage 解密图片 加密路径,解密路径
func (c *Client) DecryptImage(src, dst string) int32 {
func (c *Client) DecryptImage(src, dst string) string {
req := genFunReq(Functions_FUNC_DECRYPT_IMAGE)
q := Request_Dec{
Dec: &DecPath{Src: src, Dst: dst},
@ -251,7 +251,8 @@ func (c *Client) DecryptImage(src, dst string) int32 {
if err != nil {
logs.Err(err)
}
return recv.GetStatus()
return recv.String()
}
// AddChatRoomMembers 添加群成员