提交 9130d61f authored 作者: caodi\cd's avatar caodi\cd

fix:加入文件权限

上级 ec68e525
...@@ -199,7 +199,6 @@ export const writeDeviceData = (content, userName) => { ...@@ -199,7 +199,6 @@ export const writeDeviceData = (content, userName) => {
fileName, fileName,
fileContent fileContent
).then((res) => { ).then((res) => {
console.log("更新",res)
// store 更新 设备上架. 缓存数据置为空,让接口重新读取文件数据 // store 更新 设备上架. 缓存数据置为空,让接口重新读取文件数据
store.commit("SET_DEVICEDATA", []); store.commit("SET_DEVICEDATA", []);
resolve(); resolve();
...@@ -349,24 +348,79 @@ export function copyDirectory(sourceDirectoryPath, targetDirectoryPath) { ...@@ -349,24 +348,79 @@ export function copyDirectory(sourceDirectoryPath, targetDirectoryPath) {
); );
}); });
} }
// 复制多张图片到指定目录的函数 // 复制多张图片到指定目录的函数(带权限请求)
export function copyImagesToFolder(sourcePaths, targetDir) { export function copyImagesToFolder(sourcePaths, targetDir) {
// 第一步:检查运行环境是否支持plus API // 第一步:检查运行环境是否支持plus API
// if (!window.plus || !plus.io) { if (!window.plus || !plus.io) {
// console.error("[文件操作] 当前环境不支持plus API"); console.error("[文件操作] 当前环境不支持plus API");
// return; return Promise.reject(new Error("当前环境不支持plus API"));
// } }
console.log("[文件操作] 开始复制文件:", sourcePaths, "->", targetDir); console.log("[文件操作] 开始复制文件:", sourcePaths, "->", targetDir);
// 第二步:准备目标目录 // 返回Promise以便调用者可以await或then/catch
prepareTargetDirectory(targetDir, (dirEntry) => { return new Promise((resolve, reject) => {
// 目标目录准备好后,开始复制所有文件 // 第二步:检查并请求权限
sourcePaths.forEach((sourcePath) => { requestFilePermission()
copySingleImage(sourcePath, dirEntry); .then(() => {
}); // 权限已授予,准备目标目录
}, (error) => { prepareTargetDirectory(
console.error("[文件操作] 准备目标目录失败:", error.message); targetDir,
(dirEntry) => {
// 目标目录准备好后,开始复制所有文件
const copyPromises = sourcePaths.map((sourcePath) => {
return copySingleImage(sourcePath, dirEntry);
});
// 等待所有复制操作完成
Promise.all(copyPromises)
.then((results) => {
console.log("[文件操作] 所有文件复制完成");
resolve(results);
})
.catch((error) => {
console.error("[文件操作] 部分文件复制失败:", error);
reject(error);
});
},
(error) => {
console.error("[文件操作] 准备目标目录失败:", error.message);
reject(error);
}
);
})
.catch((error) => {
console.error("[文件操作] 权限请求失败:", error.message);
reject(error);
});
});
}
// 请求文件系统权限
function requestFilePermission() {
return new Promise((resolve, reject) => {
// 检查是否已有权限
plus.android.requestPermissions(
[
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE",
],
function (result) {
// 检查权限是否被授予
const granted = Object.values(result.granted).every(Boolean);
if (granted) {
console.log("[文件操作] 文件权限已授予");
resolve();
} else {
console.error("[文件操作] 文件权限被拒绝");
reject(new Error("用户拒绝了文件访问权限"));
}
},
function (error) {
console.error("[文件操作] 权限请求错误:", error);
reject(error);
}
);
}); });
} }
...@@ -374,33 +428,33 @@ export function copyImagesToFolder(sourcePaths, targetDir) { ...@@ -374,33 +428,33 @@ export function copyImagesToFolder(sourcePaths, targetDir) {
function prepareTargetDirectory(targetDir, successCallback, errorCallback) { function prepareTargetDirectory(targetDir, successCallback, errorCallback) {
plus.io.resolveLocalFileSystemURL( plus.io.resolveLocalFileSystemURL(
targetDir, targetDir,
function(dirEntry) { function (dirEntry) {
// 目标目录已存在 // 目标目录已存在
console.log("[文件操作] 目标目录已存在"); console.log("[文件操作] 目标目录已存在");
successCallback(dirEntry); successCallback(dirEntry);
}, },
function(e) { function (e) {
// 目标目录不存在,尝试创建 // 目标目录不存在,尝试创建
console.log("[文件操作] 目标目录不存在,尝试创建..."); console.log("[文件操作] 目标目录不存在,尝试创建...");
plus.io.requestFileSystem( plus.io.requestFileSystem(
plus.io.PUBLIC_DOCUMENTS, plus.io.PUBLIC_DOCUMENTS,
function(fs) { function (fs) {
// 创建目录 // 创建目录
fs.root.getDirectory( fs.root.getDirectory(
targetDir, targetDir,
{ {
create: true, create: true,
}, },
function(dirEntry) { function (dirEntry) {
console.log("[文件操作] 目标目录创建成功"); console.log("[文件操作] 目标目录创建成功");
successCallback(dirEntry); successCallback(dirEntry);
}, },
function(error) { function (error) {
errorCallback(error); errorCallback(error);
} }
); );
}, },
function(error) { function (error) {
errorCallback(error); errorCallback(error);
} }
); );
...@@ -408,35 +462,40 @@ function prepareTargetDirectory(targetDir, successCallback, errorCallback) { ...@@ -408,35 +462,40 @@ function prepareTargetDirectory(targetDir, successCallback, errorCallback) {
); );
} }
// 复制单个图片 // 复制单个图片(返回Promise)
function copySingleImage(sourcePath, targetDirEntry) { function copySingleImage(sourcePath, targetDirEntry) {
plus.io.resolveLocalFileSystemURL( return new Promise((resolve, reject) => {
sourcePath, plus.io.resolveLocalFileSystemURL(
function(entry) { sourcePath,
// 源文件存在,开始复制 function (entry) {
console.log("[文件操作] 源文件存在:", sourcePath); // 源文件存在,开始复制
console.log("[文件操作] 源文件存在:", sourcePath);
// 获取文件名
const fileName = sourcePath.substring(sourcePath.lastIndexOf("/") + 1); // 获取文件名
console.log("[文件操作] 文件名:", fileName); const fileName = sourcePath.substring(sourcePath.lastIndexOf("/") + 1);
console.log("[文件操作] 文件名:", fileName);
// 执行复制操作
entry.copyTo( // 执行复制操作
targetDirEntry, entry.copyTo(
fileName, targetDirEntry,
function(newEntry) { fileName,
console.log("[文件操作] 文件复制成功,新路径:", newEntry.fullPath); function (newEntry) {
}, console.log("[文件操作] 文件复制成功,新路径:", newEntry.fullPath);
function(error) { resolve(newEntry.fullPath);
console.error("[文件操作] 文件复制失败:", error.message); },
} function (error) {
); console.error("[文件操作] 文件复制失败:", error.message);
}, reject(error);
function(e) { }
// 源文件不存在或无法访问 );
console.error("[文件操作] 源文件不存在或无法访问:", e.message); },
} function (e) {
); // 源文件不存在或无法访问
console.error("[文件操作] 源文件不存在或无法访问:", e.message);
reject(e);
}
);
});
} }
// 删除文件夹中所有文件 // 删除文件夹中所有文件
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论