修复传参bug;解压显示bug
This commit is contained in:
@@ -40,6 +40,7 @@ export const clearCachedWyxd = (workspaceId, filePath) => {
|
||||
// ===== 下载 =====
|
||||
|
||||
const getDownloadUrl = (token, workspaceId, filePath) => {
|
||||
console.log("执行getDownloadUrl");
|
||||
return new Promise((resolve, reject) => {
|
||||
const cleanPath = filePath.startsWith('./') ? filePath.slice(2) : filePath
|
||||
uni.request({
|
||||
@@ -47,18 +48,21 @@ const getDownloadUrl = (token, workspaceId, filePath) => {
|
||||
method: 'POST',
|
||||
header: { 'Content-Type': 'application/json' },
|
||||
data: {
|
||||
access_token: token,
|
||||
workspaces_id: workspaceId,
|
||||
file_path: cleanPath
|
||||
"access_token": token,
|
||||
"workspaces_id": workspaceId,
|
||||
"file_path": cleanPath
|
||||
},
|
||||
success: (res) => {
|
||||
if (res.statusCode === 200) {
|
||||
const respond = res.data
|
||||
const arr = respond.success ? (respond.message || respond.data) : respond
|
||||
const fileInfo = Array.isArray(arr) ? arr[0] : arr
|
||||
console.log("fileInfo:",fileInfo);
|
||||
if (fileInfo && fileInfo.url) {
|
||||
console.log("fileInfo.url",fileInfo.url);
|
||||
resolve(fileInfo.url)
|
||||
} else {
|
||||
console.log("获取下载地址失败");
|
||||
reject(respond?.error || '获取下载地址失败')
|
||||
}
|
||||
} else {
|
||||
@@ -73,11 +77,14 @@ const getDownloadUrl = (token, workspaceId, filePath) => {
|
||||
export const downloadWyxdAsZip = (token, workspaceId, filePath, onProgress) => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
console.log("执行downloadWyxdAsZip");
|
||||
const downloadUrl = await getDownloadUrl(token, workspaceId, filePath)
|
||||
console.log("downloadWyxdAsZip-downloadUrl",downloadUrl);
|
||||
const downloadTask = uni.downloadFile({
|
||||
url: downloadUrl,
|
||||
success: (res) => {
|
||||
if (res.statusCode === 200) {
|
||||
console.log("downloadWyxdAsZip-tempFilePath",res.tempFilePath);
|
||||
resolve(res.tempFilePath)
|
||||
} else {
|
||||
reject(`下载失败:${res.statusCode}`)
|
||||
@@ -123,23 +130,85 @@ const utf8Decode = (uint8arr) => {
|
||||
|
||||
const readFileAsArrayBuffer = (filePath) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
// App 环境:uni.getFileSystemManager
|
||||
const fs = uni.getFileSystemManager
|
||||
if (typeof fs === 'function') {
|
||||
fs().readFile({
|
||||
filePath: filePath,
|
||||
encoding: 'base64',
|
||||
success: (res) => {
|
||||
const base64 = res.data || ''
|
||||
const binary = atob(base64)
|
||||
const bytes = new Uint8Array(binary.length)
|
||||
for (let i = 0; i < binary.length; i++) {
|
||||
bytes[i] = binary.charCodeAt(i)
|
||||
// App 环境检测:plus 对象仅在 App 运行时存在
|
||||
if (typeof plus !== 'undefined') {
|
||||
console.log('[readFileAsArrayBuffer] App环境,filePath:', filePath)
|
||||
console.log('[readFileAsArrayBuffer] typeof plus.io:', typeof plus.io)
|
||||
console.log('[readFileAsArrayBuffer] typeof plus.io.resolveLocalFileSystemURL:', typeof plus.io?.resolveLocalFileSystemURL)
|
||||
console.log('[readFileAsArrayBuffer] typeof plus.io.FileReader:', typeof plus.io?.FileReader)
|
||||
|
||||
// 方式1:尝试 plus.io.resolveLocalFileSystemURL
|
||||
if (plus.io && typeof plus.io.resolveLocalFileSystemURL === 'function') {
|
||||
plus.io.resolveLocalFileSystemURL(filePath, (entry) => {
|
||||
console.log('[readFileAsArrayBuffer] entry.isFile:', entry.isFile)
|
||||
console.log('[readFileAsArrayBuffer] entry.isDirectory:', entry.isDirectory)
|
||||
console.log('[readFileAsArrayBuffer] entry.fullPath:', entry.fullPath)
|
||||
console.log('[readFileAsArrayBuffer] typeof entry.file:', typeof entry.file)
|
||||
|
||||
if (typeof entry.file !== 'function') {
|
||||
reject(new Error('entry 没有 file 方法,isFile=' + entry.isFile + ' isDirectory=' + entry.isDirectory))
|
||||
return
|
||||
}
|
||||
resolve(bytes)
|
||||
},
|
||||
fail: (err) => reject(new Error('无法读取文件:' + (err.errMsg || err.message || '')))
|
||||
})
|
||||
|
||||
try {
|
||||
entry.file((file) => {
|
||||
console.log('[readFileAsArrayBuffer] entry.file 成功,file.size:', file.size)
|
||||
const reader = new plus.io.FileReader()
|
||||
reader.onloadend = (e) => {
|
||||
const dataUrl = e.target.result
|
||||
console.log('[readFileAsArrayBuffer] readAsDataURL 完成,dataUrl 长度:', dataUrl?.length)
|
||||
try {
|
||||
// dataUrl 格式: "data:;base64,<base64data>"
|
||||
const base64 = dataUrl.substring(dataUrl.indexOf(',') + 1)
|
||||
const binary = atob(base64)
|
||||
const bytes = new Uint8Array(binary.length)
|
||||
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i)
|
||||
console.log('[readFileAsArrayBuffer] 成功转为 Uint8Array,长度:', bytes.length)
|
||||
resolve(bytes)
|
||||
} catch (err) {
|
||||
console.error('[readFileAsArrayBuffer] base64 解码失败', err)
|
||||
reject(new Error('文件解码失败:' + err.message))
|
||||
}
|
||||
}
|
||||
reader.onerror = (e) => {
|
||||
console.error('[readFileAsArrayBuffer] plus.io 读取失败', e)
|
||||
reject(new Error('文件读取失败'))
|
||||
}
|
||||
reader.readAsDataURL(file)
|
||||
}, (err) => {
|
||||
console.error('[readFileAsArrayBuffer] 获取File对象失败', err)
|
||||
reject(new Error('获取文件失败:' + (err.message || '')))
|
||||
})
|
||||
} catch (e) {
|
||||
console.error('[readFileAsArrayBuffer] entry.file 调用同步异常:', e)
|
||||
reject(new Error('entry.file 调用失败:' + (e.message || e)))
|
||||
}
|
||||
}, (err) => {
|
||||
console.error('[readFileAsArrayBuffer] 解析文件路径失败', err)
|
||||
reject(new Error('解析文件路径失败:' + (err.message || '')))
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 方式2:降级 - 尝试 uni.getFileSystemManager
|
||||
if (typeof uni !== 'undefined' && typeof uni.getFileSystemManager === 'function') {
|
||||
console.log('[readFileAsArrayBuffer] 降级使用 uni.getFileSystemManager')
|
||||
const fs = uni.getFileSystemManager()
|
||||
fs.readFile({
|
||||
filePath: filePath,
|
||||
success: (res) => {
|
||||
const base64 = res.data || ''
|
||||
const binary = atob(base64)
|
||||
const bytes = new Uint8Array(binary.length)
|
||||
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i)
|
||||
resolve(bytes)
|
||||
},
|
||||
fail: (err) => reject(new Error('无法读取文件:' + (err.errMsg || '')))
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
reject(new Error('App 环境下无可用的文件读取 API'))
|
||||
return
|
||||
}
|
||||
// H5 环境:fetch blob URL
|
||||
@@ -155,6 +224,7 @@ const readFileAsArrayBuffer = (filePath) => {
|
||||
|
||||
export const extractWyxd = async (zipFilePath) => {
|
||||
const data = await readFileAsArrayBuffer(zipFilePath)
|
||||
console.log("data",data);
|
||||
const zip = await JSZip.loadAsync(data)
|
||||
const result = { header: '', html: '', images: {} }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user