Browse Source

change onlyoffice

master
godo 6 months ago
parent
commit
2747153b8f
  1. 90
      frontend/src/components/window/OnlyOffice.vue
  2. 3
      godo/cmd/main.go
  3. 54
      godo/files/pwd.go

90
frontend/src/components/window/OnlyOffice.vue

@ -5,11 +5,15 @@
<script lang="ts" setup>
import { DocumentEditor } from "@onlyoffice/document-editor-vue";
import { getSystemConfig } from "@/system/config";
import { getSystemConfig, getSplit } from "@/system/config";
import { BrowserWindow, Dialog, Notify, System } from "@/system";
import { ref, onMounted, inject } from "vue";
import { url } from "inspector";
const config = getSystemConfig();
const sys: any = inject<System>("system");
const win: any = inject<BrowserWindow>("browserWindow");
const props = defineProps({
src: {
type: String,
@ -24,47 +28,99 @@ const props = defineProps({
default: "md",
},
});
const editorConfig: any = ref({})
const editorConfig:any = ref({});
const SP = getSplit();
const getBlobUrl = (content: any) => {
let blobUrl: string;
if (!content || content === "") {
const emptyBlob = new Blob([], { type: 'application/octet-stream' });
blobUrl = URL.createObjectURL(emptyBlob);
} else {
try {
// base64 Blob URL
const byteCharacters = atob(content);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
const blob = new Blob([byteArray], { type: 'application/octet-stream' });
blobUrl = URL.createObjectURL(blob);
} catch (error) {
console.error("Failed to decode base64 content:", error);
const emptyBlob = new Blob([], { type: 'application/octet-stream' });
blobUrl = URL.createObjectURL(emptyBlob);
}
}
return blobUrl;
}
onMounted(() => {
const path = win?.config?.path;
//const uniqueKey = fetchDocumentKey(path)
const readUrl = config.apiUrl + "/file/readfile?stream=1&path=" + path
const title = path.split(SP).pop();
let content = win?.config?.content;
// console.log("Path:", path);
// console.log("Title:", title);
// console.log("Content:", content);
// console.log("ext:",props.ext)
const blobUrl = getBlobUrl(content);
//console.log("Blob URL:", blobUrl);
//console.log("onlyoffice:", config.apiUrl + "/file/onlyoffice");
editorConfig.value = {
document: {
fileType: "docx",
fileType: props.ext, // fileType
key: "docx" + Math.random(),
title: "Example Document Title.docx",
url: readUrl
title: title,
url: blobUrl
//url: config.storenet.url + "/file/readfile?stream=1&path="+path,
},
documentType: "word",
editorConfig: {
//callbackUrl: "https://example.com/url-to-callback.ashx",
customization: {
"anonymous": {
request: true,
label: "Guest",
}
lang: "zh",
//createUrl:config.apiUrl + "file/writefile?path="+path,
callbackUrl: config.storenet.url + "/file/onlyoffice",
},
events: {
onError: (e:any) =>{
console.log("onError:", e);
},
onDocumentStateChange: (e:any) =>{
console.log("onDocumentStateChange:", e);
},
onDocumentReady: (e:any) =>{
console.log("onDocumentReady:", e);
},
onRequestViewRights: (e:any) =>{
console.log("onRequestViewRights:", e);
},
onSave: (e:any)=>{
console.log("onSave:", e);
}
}
}
})
const onDocumentReady = () => {
console.log("Document is loaded");
}
const onLoadComponentError = (errorCode: any, errorDescription: any) => {
switch (errorCode) {
case -1: // Unknown error loading component
console.log(errorDescription);
console.log("Unknown error loading component", errorDescription);
break;
case -2: // Error load DocsAPI from http://documentserver/
console.log(errorDescription);
console.log("Error load DocsAPI from http://documentserver/", errorDescription);
break;
case -3: // DocsAPI is not defined
console.log(errorDescription);
console.log("DocsAPI is not defined", errorDescription);
break;
}
}
</script>

3
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)

54
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)
}

Loading…
Cancel
Save