Browse Source

change msg

master
godo 9 months ago
parent
commit
81db2b2ec6
  1. 10
      frontend/src/components/localchat/ChatContent.vue
  2. 10
      frontend/src/stores/localchat.ts
  3. 41
      godo/localchat/filedown.go
  4. 8
      godo/localchat/filemsg.go

10
frontend/src/components/localchat/ChatContent.vue

@ -95,8 +95,8 @@ async function scroll({ scrollTop }: { scrollTop: number }) {
<span>对方发送文件</span> <span>对方发送文件</span>
</div> </div>
</template> </template>
<div class="file-content" v-for="el in item.content.fileList"> <div class="file-content">
<div class="file-name">{{ el }}</div> <div class="file-name" @click="sys.openFile(item.content.path)">查看文件</div>
</div> </div>
<template #footer> <template #footer>
<span v-if="item.content.status === 'apply'"> <span v-if="item.content.status === 'apply'">
@ -108,10 +108,10 @@ async function scroll({ scrollTop }: { scrollTop: number }) {
<span v-if="item.content.status === 'cannel'"> <span v-if="item.content.status === 'cannel'">
已拒绝 已拒绝
</span> </span>
<span v-if="item.content.status === 'accessing'"> <!-- <span v-if="item.content.status === 'accessing'">
接收中 接收中
</span> </span> -->
<span v-if="item.content.status === 'confirm'"> <span v-if="item.content.status === 'accessed'">
已接收 已接收
</span> </span>
</template> </template>

10
frontend/src/stores/localchat.ts

@ -430,6 +430,16 @@ export const useLocalChatStore = defineStore('localChatStore', () => {
//console.log(coms) //console.log(coms)
notifyError("确认失败!") notifyError("确认失败!")
} else { } else {
const res = await coms.json()
if(res.code === 0){
item.content.path = res.data.path
item.content.status = 'accessed'
//console.log(item)
await db.update('chatmsg', res.data.msg.msgId, toRaw(item))
await getMsgList()
}else{
notifyError(res.message)
}
// item.content.status = 'accessing' // item.content.status = 'accessing'
// //console.log(item) // //console.log(item)
// await db.update('chatmsg', item.id, toRaw(item)) // await db.update('chatmsg', item.id, toRaw(item))

41
godo/localchat/filedown.go

@ -38,38 +38,38 @@ import (
"time" "time"
) )
func downloadFiles(msg UdpMessage) error { func downloadFiles(msg UdpMessage) (string, error) {
postUrl := fmt.Sprintf("http://%s:56780/localchat/getfiles", msg.IP) postUrl := fmt.Sprintf("http://%s:56780/localchat/getfiles", msg.IP)
postData, err := json.Marshal(msg.Message) postData, err := json.Marshal(msg.Message)
if err != nil { if err != nil {
return fmt.Errorf("failed to marshal post data: %v", err) return "", fmt.Errorf("failed to marshal post data: %v", err)
} }
log.Printf("Sending POST request to %s with data: %s", postUrl, string(postData)) log.Printf("Sending POST request to %s with data: %s", postUrl, string(postData))
resp, err := http.Post(postUrl, "application/json", bytes.NewBuffer(postData)) resp, err := http.Post(postUrl, "application/json", bytes.NewBuffer(postData))
if err != nil { if err != nil {
return fmt.Errorf("failed to make POST request: %v", err) return "", fmt.Errorf("failed to make POST request: %v", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("server returned status code: %v, body: %s", resp.StatusCode, body) return "", fmt.Errorf("server returned status code: %v, body: %s", resp.StatusCode, body)
} }
path, err := handleResponse(resp.Body, msg.IP)
// 处理响应中的文件 // 处理响应中的文件
if err := handleResponse(resp.Body, msg.IP); err != nil { if err != nil {
log.Fatalf("Failed to handle response: %v", err) log.Fatalf("Failed to handle response: %v", err)
} }
fmt.Println("Files downloaded successfully") fmt.Println("Files downloaded successfully")
return nil return path, nil
} }
func handleResponse(reader io.Reader, ip string) error { func handleResponse(reader io.Reader, ip string) (string, error) {
// 接收文件的目录 // 接收文件的目录
baseDir, err := libs.GetOsDir() baseDir, err := libs.GetOsDir()
if err != nil { if err != nil {
log.Printf("Failed to get OS directory: %v", err) log.Printf("Failed to get OS directory: %v", err)
return fmt.Errorf("failed to get OS directory") return "", fmt.Errorf("failed to get OS directory")
} }
resPath := filepath.Join("C", "Users", "Reciv", time.Now().Format("2006-01-02")) resPath := filepath.Join("C", "Users", "Reciv", time.Now().Format("2006-01-02"))
@ -78,27 +78,36 @@ func handleResponse(reader io.Reader, ip string) error {
err := os.MkdirAll(receiveDir, 0755) err := os.MkdirAll(receiveDir, 0755)
if err != nil { if err != nil {
log.Printf("Failed to create receive directory: %v", err) log.Printf("Failed to create receive directory: %v", err)
return fmt.Errorf("failed to create receive directory") return "", fmt.Errorf("failed to create receive directory")
}
}
timestamp := time.Now().Format("15-04-05")
revPath := filepath.Join(receiveDir, timestamp)
if !libs.PathExists(revPath) {
err := os.MkdirAll(revPath, 0755)
if err != nil {
log.Printf("Failed to create receive directory: %v", err)
return "", fmt.Errorf("failed to create receive directory")
} }
} }
body, err := io.ReadAll(reader) body, err := io.ReadAll(reader)
if err != nil { if err != nil {
return fmt.Errorf("failed to read response body: %v", err) return "", fmt.Errorf("failed to read response body: %v", err)
} }
log.Printf("Received file list: %v", string(body)) //log.Printf("Received file list: %v", string(body))
// 解析文件列表 // 解析文件列表
var fileList []FileItem var fileList []FileItem
if err := json.Unmarshal(body, &fileList); err != nil { if err := json.Unmarshal(body, &fileList); err != nil {
return fmt.Errorf("failed to unmarshal file list: %v", err) return "", fmt.Errorf("failed to unmarshal file list: %v", err)
} }
log.Printf("Received file list: %v", fileList) //log.Printf("Received file list: %v", fileList)
for _, file := range fileList { for _, file := range fileList {
if runtime.GOOS != "windows" && strings.Contains(file.WritePath, "\\") { if runtime.GOOS != "windows" && strings.Contains(file.WritePath, "\\") {
file.WritePath = strings.ReplaceAll(file.WritePath, "\\", "/") file.WritePath = strings.ReplaceAll(file.WritePath, "\\", "/")
} }
checkpath := filepath.Join(receiveDir, file.WritePath) checkpath := filepath.Join(revPath, file.WritePath)
if !libs.PathExists(checkpath) { if !libs.PathExists(checkpath) {
os.MkdirAll(checkpath, 0755) os.MkdirAll(checkpath, 0755)
@ -109,7 +118,7 @@ func handleResponse(reader io.Reader, ip string) error {
} }
} }
return nil return revPath, nil
} }
// downloadFile 下载单个文件 // downloadFile 下载单个文件

8
godo/localchat/filemsg.go

@ -86,10 +86,14 @@ func HandlerAccessFile(w http.ResponseWriter, r *http.Request) {
msg.Time = time.Now() msg.Time = time.Now()
msg.Type = "fileAccessed" msg.Type = "fileAccessed"
SendToIP(msg) SendToIP(msg)
err = downloadFiles(msg) path, err := downloadFiles(msg)
if err != nil { if err != nil {
libs.ErrorMsg(w, "HandleMessage error") libs.ErrorMsg(w, "HandleMessage error")
return return
} }
libs.SuccessMsg(w, msg.Message, "接收文件中") res := map[string]interface{}{
"path": path,
"msg": msg.Message,
}
libs.SuccessMsg(w, res, "接收文件成功")
} }

Loading…
Cancel
Save