提交 0892dbfe authored 作者: JaxBBLL's avatar JaxBBLL

fix

上级 0ee2a754
......@@ -46,6 +46,8 @@ import {
USER_FILE_NAME,
getUserList,
} from "@/utils/systemCofig";
import { findPhotosUrls } from "@/utils/index.js";
import { copySelectedFiles } from "@/utils/plus.js";
import {
writeInspectionData,
......@@ -74,6 +76,11 @@ export default {
allList: [], // 所有数据
};
},
computed: {
userInfo() {
return this.$store.state.now_user;
},
},
watch: {
list(newVal) {
......@@ -115,7 +122,7 @@ export default {
* 3. 写入数据
* 读取上一次打包的文件, 复制到 [ history ] 文件夹中
*/
clickHandle() {
async clickHandle() {
if (this.loading) return;
const directoryPath = `${SYNCHRONIZE_DATA_PAD}/发送数据`;
const targetDirectoryPath = `${SYNCHRONIZE_DATA_PAD}/history`;
......@@ -137,6 +144,15 @@ export default {
});
});
},
copyPhotos() {
const urls = findPhotosUrls(this.list).map((url) => url.split("/").pop());
console.log("urls", urls);
return copySelectedFiles(
"_documents/data/photos",
`${SYNCHRONIZE_DATA_PAD}/发送数据/${this.userInfo.user}`,
urls
);
},
// 处理数据
coverData() {
const userName = this.$store.state.now_user.user;
......@@ -232,6 +248,10 @@ export default {
// 更新同步时间
this.updateSysTime();
// 复制照片
await this.copyPhotos();
// zip
}, 2 * 1000);
})
.catch((error) => {
......
......@@ -85,6 +85,7 @@ export const readLogData = () => {
return new Promise((resolve, reject) => {
readFilesInDirectory(directoryPath)
.then((res) => {
console.log("readFilesInDirectory", res);
const temp = res.map((element) => {
return JSON.parse(Base64.decode(element));
});
......@@ -174,6 +175,7 @@ export const readInspectionData = () => {
return new Promise((resolve, reject) => {
readFilesInDirectory(directoryPath)
.then((res) => {
console.log("readInspectionData", res);
const temp = res.map((element) => {
return JSON.parse(Base64.decode(element));
});
......@@ -367,16 +369,18 @@ export function copyImagesToFolder(sourcePaths, targetDir) {
console.log("权限已获取,准备目标目录...");
return prepareTargetDirectory(targetDir);
})
.then(targetDirEntry => {
.then((targetDirEntry) => {
console.log("目录准备完成,开始复制图片...");
const copies = sourcePaths.map(path => copySingleImage(path, targetDirEntry));
const copies = sourcePaths.map((path) =>
copySingleImage(path, targetDirEntry)
);
return Promise.all(copies);
})
.then(results => {
.then((results) => {
console.log("所有图片复制成功", results);
resolve(results);
})
.catch(err => {
.catch((err) => {
console.error("复制图片出错:", err);
reject(err);
});
......@@ -394,16 +398,16 @@ function requestFilePermission() {
plus.android.requestPermissions(
[
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE"
"android.permission.WRITE_EXTERNAL_STORAGE",
],
function(result) {
function (result) {
if (Object.values(result.granted).every(Boolean)) {
resolve();
} else {
reject(new Error("用户拒绝了文件权限"));
}
},
function(error) {
function (error) {
reject(error);
}
);
......@@ -413,20 +417,21 @@ function requestFilePermission() {
// 准备目录(返回Promise)
function prepareTargetDirectory(targetDir) {
return new Promise((resolve, reject) => {
plus.io.requestFileSystem(plus.io.PUBLIC_DOCUMENTS,
function(fs) {
plus.io.requestFileSystem(
plus.io.PUBLIC_DOCUMENTS,
function (fs) {
fs.root.getDirectory(
targetDir,
{ create: true, exclusive: false },
function(dirEntry) {
function (dirEntry) {
resolve(dirEntry);
},
function(error) {
function (error) {
reject(error);
}
);
},
function(error) {
function (error) {
reject(error);
}
);
......@@ -438,15 +443,15 @@ function copySingleImage(sourcePath, targetDirEntry) {
return new Promise((resolve, reject) => {
plus.io.resolveLocalFileSystemURL(
sourcePath,
function(fileEntry) {
function (fileEntry) {
if (!fileEntry.isFile) {
reject(new Error("源路径不是文件"));
return;
}
// 获取文件名
const fileName = sourcePath.split('/').pop();
const fileName = sourcePath.split("/").pop();
// 检查文件是否已存在
targetDirEntry.getFile(
fileName,
......@@ -457,10 +462,10 @@ function copySingleImage(sourcePath, targetDirEntry) {
fileEntry.copyTo(
targetDirEntry,
fileName,
function(newEntry) {
function (newEntry) {
resolve(newEntry.toURL());
},
function(error) {
function (error) {
reject(error);
}
);
......@@ -470,17 +475,17 @@ function copySingleImage(sourcePath, targetDirEntry) {
fileEntry.copyTo(
targetDirEntry,
fileName,
function(newEntry) {
function (newEntry) {
resolve(newEntry.toURL());
},
function(error) {
function (error) {
reject(error);
}
);
}
);
},
function(error) {
function (error) {
reject(error);
}
);
......@@ -503,7 +508,7 @@ export function deleteAllFilesInDirectory(directoryPath) {
return resolve({
success: true,
message: "目录为空",
results: []
results: [],
});
}
......@@ -513,9 +518,9 @@ export function deleteAllFilesInDirectory(directoryPath) {
const checkCompletion = () => {
if (completedCount === entries.length) {
resolve({
success: results.every(r => r.success),
success: results.every((r) => r.success),
message: "操作完成",
results: results
results: results,
});
}
};
......@@ -526,7 +531,7 @@ export function deleteAllFilesInDirectory(directoryPath) {
() => {
results.push({
name: entry.name,
success: true
success: true,
});
completedCount++;
checkCompletion();
......@@ -536,7 +541,7 @@ export function deleteAllFilesInDirectory(directoryPath) {
results.push({
name: entry.name,
success: false,
error: error.message || "未知错误"
error: error.message || "未知错误",
});
completedCount++;
checkCompletion();
......@@ -554,7 +559,7 @@ export function deleteAllFilesInDirectory(directoryPath) {
resolve({
success: false,
message: "读取目录失败",
error: error
error: error,
});
}
);
......@@ -564,7 +569,7 @@ export function deleteAllFilesInDirectory(directoryPath) {
resolve({
success: false,
message: "目录不存在",
error: error
error: error,
});
}
);
......@@ -574,7 +579,7 @@ export function deleteAllFilesInDirectory(directoryPath) {
resolve({
success: false,
message: "文件系统访问失败",
error: error
error: error,
});
}
);
......
......@@ -40,58 +40,6 @@ export async function createFolder(relativePath) {
}
}
/**
* 删除指定目录及其内容
* @param {string} folderPath 要删除的目录路径(相对于 PUBLIC_DOCUMENTS 根目录)
* @returns {Promise<void>} 返回一个 Promise,在删除成功或不存在时 resolve,失败时 reject
*/
export function deleteFolder(folderPath) {
return new Promise((resolve, reject) => {
// 获取文件系统对象
plus.io.requestFileSystem(
plus.io.PUBLIC_DOCUMENTS,
(fs) => {
// 构造完整路径
const fullPath = fs.root.fullPath + "/" + folderPath;
// 检查目录是否存在
plus.io.resolveLocalFileSystemURL(
fullPath,
(dirEntry) => {
console.log(`目录存在,开始删除:${fullPath}`);
deleteDirectoryRecursive(dirEntry)
.then(() => {
console.log(`目录已删除:${fullPath}`);
resolve();
})
.catch((err) => {
console.error(
`删除目录失败:${fullPath}, 错误信息:${err.message}`
);
reject(err);
});
},
(err) => {
if (err.code === 1) {
console.log(`目录不存在:${fullPath}`);
resolve(); // 目录不存在时直接 resolve
} else {
console.error(
`检查目录失败:${fullPath}, 错误信息:${err.message}`
);
reject(err);
}
}
);
},
(err) => {
console.error(`请求文件系统失败:${err.message}`);
reject(err);
}
);
});
}
/**
* 复制相册图片
* @param {*} sourcePath
......@@ -159,37 +107,114 @@ export async function copyImage(sourcePath, targetPath) {
}
/**
* 传入照片数组,复制到临时目录
* @param {*} photos ["1.jpg", "2.jpg"]
* @returns
* 从指定文件夹中选取多个文件名并复制到目标目录
* @param {string} sourceDirectoryPath 源文件夹路径(相对于 PUBLIC_DOCUMENTS 根目录)
* @param {string} targetDirectoryPath 目标文件夹路径(相对于 PUBLIC_DOCUMENTS 根目录)
* @param {string[]} fileNames 要复制的文件名列表
* @returns {Promise<void>} 返回一个 Promise,在复制成功时 resolve,失败时 reject
*/
export async function copyPhotosToTemp(photos) {
await deleteFolder("data/temp"); // 删除临时目录
await createFolder("data/temp"); // 创建临时目录
console.log("1111");
export function copySelectedFiles(
sourceDirectoryPath,
targetDirectoryPath,
fileNames
) {
return new Promise((resolve, reject) => {
const copiedPhotos = [];
let completed = 0;
photos.forEach((photo) => {
const targetPath = `_documents/data/temp`;
console.log("before copy", photo, targetPath);
copyImage(photo, targetPath)
.then((copiedUrl) => {
console.log("copiedUrl", copiedUrl);
copiedPhotos.push(photo);
completed += 1;
if (completed === photos.length) {
resolve(copiedPhotos);
// 请求文件系统
plus.io.requestFileSystem(
plus.io.PUBLIC_DOCUMENTS,
(fs) => {
// 获取源文件夹路径的目录条目
fs.root.getDirectory(
sourceDirectoryPath,
{ create: false },
(sourceDirEntry) => {
// 获取目标文件夹路径的目录条目
fs.root.getDirectory(
targetDirectoryPath,
{ create: true, exclusive: false },
(targetDirEntry) => {
// 创建一个空的目录读取器
const directoryReader = sourceDirEntry.createReader();
// 读取源目录中的所有文件和子目录
directoryReader.readEntries(
(entries) => {
// 筛选出需要复制的文件
const filesToCopy = entries.filter(
(entry) => entry.isFile && fileNames.includes(entry.name)
);
// 如果没有匹配的文件,直接返回
if (filesToCopy.length === 0) {
console.log("没有找到需要复制的文件");
return resolve();
}
let copyCount = 0;
// 遍历需要复制的文件
filesToCopy.forEach((fileEntry) => {
// 复制文件到目标目录
fileEntry.copyTo(
targetDirEntry,
fileEntry.name,
() => {
++copyCount;
// 如果所有文件都复制完成,则 resolve
if (copyCount === filesToCopy.length) {
resolve();
}
},
(error) => {
console.error(
`复制文件失败:${fileEntry.name}, 错误信息:${error.message}`
);
reject({
message: `复制文件失败:${fileEntry.name}`,
error,
});
}
);
});
},
(error) => {
console.error(
`读取目录内容失败:${sourceDirectoryPath}, 错误信息:${error.message}`
);
reject({
message: "读取目录内容失败",
error,
});
}
);
},
(error) => {
console.error(
`获取目标目录失败:${targetDirectoryPath}, 错误信息:${error.message}`
);
reject({
message: "获取目标目录失败",
error,
});
}
);
},
(error) => {
console.error(
`获取源目录失败:${sourceDirectoryPath}, 错误信息:${error.message}`
);
reject({
message: "获取源目录失败",
error,
});
}
})
.catch((error) => {
console.error(`复制照片失败: ${error.message}`);
reject(error);
);
},
(error) => {
console.error(`请求文件系统失败:${error.message}`);
reject({
message: "请求文件系统失败",
error,
});
});
}
);
});
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论