From 3108b92691f639d988e7b506b2d05c4fe316a0a1 Mon Sep 17 00:00:00 2001
From: zjk <852185829@qq.com>
Date: 星期四, 23 七月 2026 15:21:30 +0800
Subject: [PATCH] feat: 优化扫码硬件管理,新增二维码校验与本地缓存功能
---
utils/fs.js | 74 +++++++++++++++++++++++++++++++++---
1 files changed, 67 insertions(+), 7 deletions(-)
diff --git a/utils/fs.js b/utils/fs.js
index 89988b3..ce6a048 100644
--- a/utils/fs.js
+++ b/utils/fs.js
@@ -3,17 +3,18 @@
* 用于将临时抓拍图片持久化到设备存储中,以及同步后的清理
*/
-// 将临时文件保存到本地 _doc/images/ 目录下
-export const saveImageToLocal = (tempFilePath) => {
+const getFileExtensionFromPath = (filePath = '', fallbackExtension = 'jpg') => {
+ const normalizedPath = String(filePath || '').split('?')[0];
+ const match = normalizedPath.match(/\.([a-zA-Z0-9]+)$/);
+ return match ? match[1].toLowerCase() : fallbackExtension;
+};
+
+const moveFileToDocDirectory = (tempFilePath, directoryName = 'images', fileName = '') => {
return new Promise((resolve, reject) => {
- // 只有 App-Plus 环境支持 plus.io
// #ifdef APP-PLUS
- const fileName = `face_${Date.now()}_${Math.floor(Math.random() * 1000)}.jpg`;
-
plus.io.resolveLocalFileSystemURL(tempFilePath, (entry) => {
plus.io.resolveLocalFileSystemURL('_doc/', (docEntry) => {
- // 创建或打开 images 目录
- docEntry.getDirectory('images', { create: true }, (dirEntry) => {
+ docEntry.getDirectory(directoryName, { create: true }, (dirEntry) => {
entry.moveTo(dirEntry, fileName, (newFile) => {
resolve(newFile.fullPath);
}, (err) => {
@@ -32,6 +33,21 @@
console.error('[FS] 访问临时文件失败:', err);
reject(err);
});
+ // #endif
+
+ // #ifndef APP-PLUS
+ resolve(tempFilePath);
+ // #endif
+ });
+};
+
+// 将临时文件保存到本地 _doc/images/ 目录下
+export const saveImageToLocal = (tempFilePath) => {
+ return new Promise((resolve, reject) => {
+ // 只有 App-Plus 环境支持 plus.io
+ // #ifdef APP-PLUS
+ const fileName = `face_${Date.now()}_${Math.floor(Math.random() * 1000)}.jpg`;
+ moveFileToDocDirectory(tempFilePath, 'images', fileName).then(resolve).catch(reject);
// #endif
// #ifndef APP-PLUS
@@ -68,3 +84,47 @@
// #endif
});
};
+
+// 将远程文件下载到本地 _doc 指定目录,常用于离线缓存图片资源
+export const downloadRemoteFileToLocal = (remoteUrl, options = {}) => {
+ const {
+ directoryName = 'downloads',
+ fileNamePrefix = 'file',
+ fallbackExtension = 'jpg',
+ } = options;
+
+ return new Promise((resolve, reject) => {
+ const normalizedRemoteUrl = String(remoteUrl || '').trim();
+ if (!normalizedRemoteUrl) {
+ reject(new Error('远程文件地址为空'));
+ return;
+ }
+
+ // #ifdef APP-PLUS
+ uni.downloadFile({
+ url: normalizedRemoteUrl,
+ success: (downloadResult) => {
+ if (downloadResult.statusCode !== 200 || !downloadResult.tempFilePath) {
+ reject(new Error(`远程文件下载失败,状态码:${downloadResult.statusCode || 'unknown'}`));
+ return;
+ }
+
+ const fileExtension = getFileExtensionFromPath(normalizedRemoteUrl, fallbackExtension);
+ const fileName = `${fileNamePrefix}_${Date.now()}_${Math.floor(Math.random() * 1000)}.${fileExtension}`;
+
+ moveFileToDocDirectory(downloadResult.tempFilePath, directoryName, fileName)
+ .then(resolve)
+ .catch(reject);
+ },
+ fail: (error) => {
+ console.error('[FS] 下载远程文件失败:', error);
+ reject(error);
+ },
+ });
+ // #endif
+
+ // #ifndef APP-PLUS
+ resolve(normalizedRemoteUrl);
+ // #endif
+ });
+};
--
Gitblit v1.7.1