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

View File

@ -20,3 +20,45 @@ export function extractMethodName(input: string): string | null {
? afterDollar ? afterDollar
: afterDollar.substring(0, questionMarkIndex) : 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)
}