From 2747153b8f618d80fd4f979c65d87b33c29c8670 Mon Sep 17 00:00:00 2001 From: godo Date: Thu, 12 Dec 2024 01:35:47 +0800 Subject: [PATCH] change onlyoffice --- frontend/src/components/window/OnlyOffice.vue | 90 +++++++++++++++---- godo/cmd/main.go | 3 +- godo/files/pwd.go | 54 +++++++++++ 3 files changed, 129 insertions(+), 18 deletions(-) diff --git a/frontend/src/components/window/OnlyOffice.vue b/frontend/src/components/window/OnlyOffice.vue index b96fe27..ab13431 100644 --- a/frontend/src/components/window/OnlyOffice.vue +++ b/frontend/src/components/window/OnlyOffice.vue @@ -5,11 +5,15 @@ \ No newline at end of file diff --git a/godo/cmd/main.go b/godo/cmd/main.go index b73bd71..adce2dc 100644 --- a/godo/cmd/main.go +++ b/godo/cmd/main.go @@ -50,7 +50,7 @@ func OsStart() { router := mux.NewRouter() router.Use(recoverMiddleware) if libs.GetIsCors() { - router.Use(corsMiddleware()) + //router.Use(corsMiddleware()) } router.Use(loggingMiddleware{}.Middleware) staticDir := libs.GetStaticDir() @@ -104,6 +104,7 @@ func OsStart() { fileRouter.HandleFunc("/unzip", files.HandleUnZip).Methods(http.MethodGet) fileRouter.HandleFunc("/watch", files.WatchHandler).Methods(http.MethodGet) fileRouter.HandleFunc("/setfilepwd", files.HandleSetFilePwd).Methods(http.MethodGet) + fileRouter.HandleFunc("/onlyoffice", files.OnlyOfficeCallbackHandler).Methods(http.MethodPost) localchatRouter := router.PathPrefix("/localchat").Subrouter() localchatRouter.HandleFunc("/message", localchat.HandleMessage).Methods(http.MethodPost) diff --git a/godo/files/pwd.go b/godo/files/pwd.go index 368b1b8..a48f744 100644 --- a/godo/files/pwd.go +++ b/godo/files/pwd.go @@ -2,6 +2,7 @@ package files import ( "encoding/base64" + "encoding/json" "fmt" "godo/libs" "io" @@ -109,6 +110,7 @@ func HandleReadFile(w http.ResponseWriter, r *http.Request) { } func HandleWriteFile(w http.ResponseWriter, r *http.Request) { + path := r.URL.Query().Get("path") if err := validateFilePath(path); err != nil { libs.HTTPError(w, http.StatusBadRequest, err.Error()) @@ -257,3 +259,55 @@ func HandleSetFilePwd(w http.ResponseWriter, r *http.Request) { } libs.SuccessMsg(w, nil, "设置密码成功") } + +// 定义结构体来匹配 JSON 数据结构 +type CallbackData struct { + Key string `json:"key"` + Status int `json:"status"` + Users []string `json:"users"` + Actions []struct { + Type int `json:"type"` + UserID string `json:"userid"` + } `json:"actions"` +} + +// 定义 OnlyOffice 预期的响应结构 +type OnlyOfficeResponse struct { + Error int `json:"error"` +} + +func OnlyOfficeCallbackHandler(w http.ResponseWriter, r *http.Request) { + // 读取请求体 + body, err := io.ReadAll(r.Body) + if err != nil { + http.Error(w, "Failed to read request body", http.StatusBadRequest) + return + } + defer r.Body.Close() + + // 打印原始请求体 + fmt.Printf("Received raw data: %s\n", body) + + // 解析 JSON 数据 + // var callbackData CallbackData + // err = json.Unmarshal(body, &callbackData) + // if err != nil { + // http.Error(w, "Failed to decode request body", http.StatusBadRequest) + // return + // } + + // // 打印解析后的回调数据 + // fmt.Printf("Received callback: %+v\n", callbackData) + + // 构造响应 + response := OnlyOfficeResponse{ + Error: 0, + } + + // 设置响应头为 JSON 格式 + w.Header().Set("Content-Type", "application/json") + + // 返回 JSON 响应 + json.NewEncoder(w).Encode(response) + +}