|
|
@@ -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
|
|
|
+ });
|
|
|
})
|
|
|
}
|
|
|
|