Sfoglia il codice sorgente

优化手机文件处理

“shengjie.huang” 1 anno fa
parent
commit
2fb0b4265b
2 ha cambiato i file con 56 aggiunte e 34 eliminazioni
  1. 0 29
      src/pages/print/index.vue
  2. 56 5
      src/pages/webview/index.vue

+ 0 - 29
src/pages/print/index.vue

@@ -787,25 +787,6 @@ function msgConfirm(title="提示", msg="文件已上传成功!") {
   })
 }
 
-// 处理系统文件webview传入
-function base64ToTempFilePath(fileName, base64Data, success, fail) {
-  const fs = uni.getFileSystemManager()
-  // const fileName = 'temp_' + Date.now() + '.png' // 自定义文件名,可根据需要修改
-  const filePath = uni.env.USER_DATA_PATH + '/' + fileName
-  const buffer = uni.base64ToArrayBuffer(base64Data)
-  fs.writeFile({
-    filePath,
-    data: buffer,
-    encoding: 'binary',
-    success() {
-      success && success(filePath)
-    },
-    fail() {
-      fail && fail()
-    }
-  })
-}
-
 // 打开SSE webview页面
 // function openWebViewSSE() {
 //   let list = JSON.stringify(fileList.value.map(item => item.name).join(",") || "")
@@ -822,16 +803,6 @@ onLoad((option) => {
     if (option.accept == "all" && uni.getStorageSync('fileList')) {
       fileList.value = uni.getStorageSync('fileList');
       uni.removeStorageSync('fileList');
-
-      fileList.value.forEach(item => {
-        item.file = item.file.split('base64,')[1]
-        base64ToTempFilePath(item.name, item.file, (filePath) => {
-          console.log('转换成功,临时地址为:', filePath)
-          item.filePath = filePath
-        }, function() {
-          toast.warning('文件转换失败,请重试')
-        })
-      })
     }
   }
   getPrinterList()

+ 56 - 5
src/pages/webview/index.vue

@@ -16,12 +16,63 @@
 <script lang="ts" setup>
 const url = ref("")
 
-function handleMessage(data) {
+function handleFileList(fileList) {
+  const filePromises = fileList.map(item => {
+    let base64 = item.file.split('base64,')[1] // 获取 base64 数据
+    return base64ToTempFilePath(item.name, base64); // 处理每个文件
+  });
+
+  // 使用 Promise.all 确保所有文件都处理完毕
+  return Promise.all(filePromises);
+}
+
+function base64ToTempFilePath(fileName, base64Data) {
+  return new Promise((resolve, reject) => {
+    const fs = uni.getFileSystemManager()
+    const filePath = uni.env.USER_DATA_PATH + '/' + fileName
+    fs.writeFile({
+      filePath,
+      data: base64Data,
+      encoding: 'base64',
+      success() {
+        // console.log('转换成功,临时地址为:', filePath)
+        resolve({
+          "name": fileName,
+          "filePath": filePath
+        })
+      },
+      fail(err) {
+        // console.log('文件转换失败,请重试');
+        reject(err)
+      }
+    })
+  })
+}
+
+// 处理从webview页面传回来文件
+// setStorageSync 单条只能存储1MB, 总空间10MB
+// getFileSystemManager 使用文件存储API 单个支持10MB, 总存储空间200MB
+async function handleMessage(data) {
   uni.removeStorageSync('fileList');
-  // 判断是否有文件上传
-  uni.setStorageSync('fileList', JSON.parse(data.detail.data))
-  uni.navigateTo({
-    url: `/pages/print/index?accept=all`,
+  let fileList = JSON.parse(data.detail.data)
+  uni.showLoading({
+    title: '文件处理中...', // 提示文字
+    mask: true // 显示透明蒙层,防止触摸穿透
+  });
+  handleFileList(fileList).then((filePaths) => {
+    uni.setStorageSync('fileList', filePaths)
+    uni.hideLoading();
+    uni.navigateTo({
+      url: `/pages/print/index?accept=all`,
+    })
+  }).catch(() => {
+    uni.hideLoading();
+    // 提示用户处理失败
+    uni.showToast({
+      title: '文件处理失败,请重试',
+      icon: 'none',
+      duration: 2000
+    });
   })
 }