wip: debug change

This commit is contained in:
zzs 2025-03-24 11:23:11 +08:00
parent a8e549de60
commit 5fd187eb0d
3 changed files with 99 additions and 14 deletions

View File

@ -1,5 +1,6 @@
import { getConfigKey } from '@/api/infra/config'
import { getAccessToken } from '@/utils/auth'
import { isHttps } from '@/views/infra/bookmark/logic'
/**
*
@ -9,8 +10,14 @@ import { getAccessToken } from '@/utils/auth'
*/
export async function initFilePreviewUrl(fileId: string | number, wopi_client?: string | undefined, wopi_server?: string | undefined): Promise<string> {
if (!wopi_client || !wopi_server) {
wopi_client = await getConfigKey('wopi_client_addr')
wopi_server = await getConfigKey('wopi_server_ip_addr')
if (isHttps()) {
wopi_client = await getConfigKey('wopi_client_addr')
wopi_server = await getConfigKey('wopi_server_ip_addr')
}
else {
wopi_client = await getConfigKey('wopi_client_addr_http')
wopi_server = await getConfigKey('wopi_server_ip_addr_http')
}
}
const wopi = `${wopi_server}/admin-api/infra/file/preview/wopi/files/${fileId}?access_token=${getAccessToken()}`
return `${wopi_client}?lang=zh-cn&WOPISrc=${encodeURIComponent(wopi)}`

View File

@ -14,7 +14,12 @@ import { getConfigKey } from '@/api/infra/config'
import type { FileDO } from '@/types/axios'
import { useGlobSetting } from '@/hooks/setting'
import { getAccessToken } from '@/utils/auth'
import { extractMethodName } from '@/views/infra/bookmark/logic'
import {
base64ToBlob,
encodeBase64,
extractMethodName,
isHttps,
} from '@/views/infra/bookmark/logic'
defineOptions({ name: 'BookmarkReplace' })
const isEmbed = isInIframe()
@ -33,8 +38,23 @@ const currentEditFile = ref<FileDO | undefined>(undefined)
const isPostMessageRead = ref<boolean>(false)
onMounted(async () => {
wopi_client.value = await getConfigKey('wopi_client_addr')
wopi_server.value = await getConfigKey('wopi_server_ip_addr')
if (isHttps()) {
wopi_client.value = await getConfigKey('wopi_client_addr')
wopi_server.value = await getConfigKey('wopi_server_ip_addr')
}
else {
wopi_client.value = await getConfigKey('wopi_client_addr_http')
wopi_server.value = await getConfigKey('wopi_server_ip_addr_http')
}
/**
* 向第三方调用发送准备好消息
*/
sendMessageToCaller({
ActionId: 'WOPIClientReady',
Payload: null,
Timestamp: Date.now(),
}, true)
/**
* 接收消息
@ -53,8 +73,13 @@ onMounted(async () => {
if (!data)
return
const ActionId = data.ActionId
const MessageId = data.MessageId
let ActionId = ''
let MessageId = ''
if (data.content)
ActionId = data.content.ActionId
if (data.MessageId)
MessageId = data.MessageId
// Event
switch (ActionId) {
@ -158,7 +183,7 @@ async function downloadAndSendCurrentEditFile() {
try {
// 1. responseType: 'arraybuffer'
const response = await axios.get(`${globSetting.apiUrl}/infra/file/download/${currentEditFile.value.id}`, {
responseType: 'arraybuffer', // [!code focus]
responseType: 'arraybuffer',
headers: {
Authorization: `Bearer ${getAccessToken()}`,
},
@ -175,8 +200,11 @@ async function downloadAndSendCurrentEditFile() {
ActionId: 'SaveFile',
Payload: {
name: currentEditFile.value.name,
buffer: arrayBuffer,
buffer: encodeBase64(arrayBuffer),
type: currentEditFile.value.type,
size: currentEditFile.value.size,
},
Timestamp: Date.now(),
}, true)
}
catch (error) {
@ -191,8 +219,8 @@ async function downloadAndSendCurrentEditFile() {
* @param data
*/
async function handleFileBinary(data: any) {
const { name, type, buffer } = data.Payload
const blob = new Blob([buffer], { type })
const { name, type, buffer } = data.content.Payload
const blob = base64ToBlob(buffer, type)
const uploadResult = await uploadOneFile({
filename: name,
file: new File([blob], name, { type }),
@ -226,7 +254,7 @@ function proxyReplaceWithJSON(data: any) {
Values: {
params: {
type: 'string',
value: JSON.stringify(data.Payload),
value: JSON.stringify(data.content.Payload),
},
},
})
@ -239,7 +267,6 @@ function proxyReplaceWithJSON(data: any) {
function sendMessageToWopiClient(data: any) {
try {
if (!isPostMessageRead.value) {
console.log('Host准备好')
iframeRef.value?.sendMessageToIframe(JSON.stringify({
MessageId: 'Host_PostmessageReady',
SendTime: Date.now(),
@ -262,6 +289,8 @@ function sendMessageToWopiClient(data: any) {
* 用于转发部分WopiClient回调
*/
function sendMessageToCaller(data: any, ignore: boolean = false) {
console.log('已向第三方调用者发送消息')
console.log(JSON.stringify(data))
try {
const targetWindow = window.parent
@ -283,10 +312,11 @@ function sendMessageToCaller(data: any, ignore: boolean = false) {
}
/**
* Python脚本执行回调
* Python脚本执行回调转发消息个第三方调用者
* @param data
*/
function handlePythonScriptCallBack(data: any) {
console.warn('======handlePythonScriptCallBack')
const Values = data.Values
const success = Values.success as boolean
const commandName = Values.commandName
@ -297,6 +327,7 @@ function handlePythonScriptCallBack(data: any) {
Success: success,
Payload: jsonData,
Message: null,
Timestamp: Date.now(),
})
}
else {
@ -308,10 +339,14 @@ function handlePythonScriptCallBack(data: any) {
Success: success,
Payload: null,
Message: data.Values.result.value,
Timestamp: Date.now(),
})
}
}
/**
* 处理第三方调用者请求保存文件
*/
function handleSaveFile() {
sendMessageToWopiClient({
MessageId: 'Action_Save',
@ -326,6 +361,7 @@ function handleSaveFile() {
function logEvent(e: MessageEvent) {
console.log('=============receive message start=======')
console.log(typeof e.data)
console.log(e.data)
console.log(e.origin)
}

View File

@ -20,3 +20,45 @@ export function extractMethodName(input: string): string | null {
? afterDollar
: afterDollar.substring(0, questionMarkIndex)
}
/**
* Https的
*/
export function isHttps() {
return window.location.protocol === 'https:'
}
/**
* base64转为二进制数据
* @param base64
* @param mimeType MIME类型
*/
export function base64ToBlob(base64: string, mimeType: string) {
// 1. 使用 atob 将 Base64 字符串解码为二进制字符串
const binaryString = atob(base64)
// 2. 创建一个与二进制字符串长度相同的 Uint8Array
const len = binaryString.length
const bytes = new Uint8Array(len)
// 3. 将二进制字符串的每个字符转换为字节并存储在 Uint8Array 中
for (let i = 0; i < len; i++)
bytes[i] = binaryString.charCodeAt(i)
// 4. 使用 Uint8Array 创建 Blob 对象
return new Blob([bytes], { type: mimeType })
}
/**
* ArrayBuffer转为Base64编码
* @param arrayBuffer
*/
export function encodeBase64(arrayBuffer: ArrayBuffer): string {
// 将 ArrayBuffer 转换为二进制字符串
const binaryString = Array.from(new Uint8Array(arrayBuffer))
.map(byte => String.fromCharCode(byte))
.join('')
// 将二进制字符串转换为 Base64
return btoa(binaryString)
}