新增聊天功能

This commit is contained in:
2024-10-14 17:40:11 +08:00
parent 99ed84f534
commit 245875881c
13 changed files with 360 additions and 12 deletions
+31
View File
@@ -2,17 +2,21 @@ package service
import (
"archive/zip"
"bytes"
"context"
"encoding/json"
"fmt"
"fyne.io/fyne/v2/data/binding"
"github.com/gin-gonic/gin"
"io"
"net/http"
"os"
"path/filepath"
"time"
"work_cation/cfg"
"work_cation/global"
"work_cation/models"
"work_cation/repo"
)
type ClientService struct {
@@ -110,6 +114,33 @@ func (c *ClientService) Download(online *models.Online, uuid string, progress bi
return nil
}
func (c *ClientService) Chat(online *models.Online, text string) (string, error) {
var user = repo.User.GetUserInfo(global.DB)
ctx, carnal := context.WithTimeout(context.TODO(), time.Millisecond*500)
defer carnal()
b, err := json.Marshal(gin.H{"text": text, "uuid": user.ID})
if err != nil {
return "", err
}
res, err := http.NewRequestWithContext(ctx, "POST", online.Url("/chat"), bytes.NewBuffer(b))
if err != nil {
return "", err
}
res.Header.Set("Content-Type", "application/json")
res.Header.Set("User-ID", user.ID)
resp, err := http.DefaultClient.Do(res)
if err != nil {
return "", err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
var msg = make(map[string]any)
if err = json.Unmarshal(data, &msg); err != nil {
return "", err
}
return msg["message"].(string), err
}
type Rr struct {
file *os.File
tot int64