Commit f17e470e by yixinjj

添加兼容函数

1 parent 09536d03
...@@ -53,7 +53,7 @@ export default { ...@@ -53,7 +53,7 @@ export default {
filePathOnClient = filePath + fileName, filePathOnClient = filePath + fileName,
} = result.detail.data[0]; } = result.detail.data[0];
if (code == 0) { if (code == 0) {
this.uploadSuccess(recordId, data.filePath, filePathOnClient,fileName); this.uploadSuccess(recordId, data.filePath, filePathOnClient, fileName);
} else { } else {
this.polling(); this.polling();
} }
...@@ -91,7 +91,7 @@ export default { ...@@ -91,7 +91,7 @@ export default {
const _snapshot = await this.getRecordsSnapshotFlatten(); const _snapshot = await this.getRecordsSnapshotFlatten();
this.snapshot = new Queue(_snapshot); this.snapshot = new Queue(_snapshot);
}, },
/** /**
* 获取记录快照并展平数据 * 获取记录快照并展平数据
* 此函数异步获取记录快照,然后使用flattenData函数将这些记录转换为一个文件对象列表 * 此函数异步获取记录快照,然后使用flattenData函数将这些记录转换为一个文件对象列表
* 每个文件对象都包含一个额外的recordId属性,用于指示它所属的记录 * 每个文件对象都包含一个额外的recordId属性,用于指示它所属的记录
...@@ -106,20 +106,6 @@ export default { ...@@ -106,20 +106,6 @@ export default {
return _snapshot; return _snapshot;
}, },
/** /**
* 获取文件系统根目录的完整路径
* 这个函数使用 HTML5 的 plus.io.requestFileSystem API 在私有文档目录中请求文件系统访问权限
* 当文件系统访问权限被授予后,使用 resolve 方法返回根目录的完整路径
* 这个 Promise 风格的函数可以被其他代码使用,以异步方式获取文件系统的根路径
* @returns {Promise<string>} 一个 Promise,当获取到文件系统根目录的完整路径时兑现,并返回根目录的路径作为字符串
*/
getFilePath() {
return new Promise((resolve) => {
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function (fs) {
resolve(fs.root.fullPath);
});
});
},
/**
* 将指定记录上传到服务器 * 将指定记录上传到服务器
* 此函数使用 WebView 的 evalJS 方法,将 OSS 令牌信息和记录详情传递给 UniApp 应用,以便进行文件上传 * 此函数使用 WebView 的 evalJS 方法,将 OSS 令牌信息和记录详情传递给 UniApp 应用,以便进行文件上传
* *
...@@ -129,7 +115,8 @@ export default { ...@@ -129,7 +115,8 @@ export default {
* @throws {Error} - 如果获取 OSS 令牌的过程中出现错误,将抛出错误 * @throws {Error} - 如果获取 OSS 令牌的过程中出现错误,将抛出错误
*/ */
async uploadRecord(record) { async uploadRecord(record) {
const { lsaccessKeySecret, securityToken, lsaccessKeyId } = await getOssToken(); const { lsaccessKeySecret, securityToken, lsaccessKeyId } =
await getOssToken();
wv.evalJS( wv.evalJS(
`msgFromUniapp('${lsaccessKeyId}','${lsaccessKeySecret}','${securityToken}','${record.fileName}','${record.filePath}')` `msgFromUniapp('${lsaccessKeyId}','${lsaccessKeySecret}','${securityToken}','${record.fileName}','${record.filePath}')`
); );
...@@ -146,17 +133,25 @@ export default { ...@@ -146,17 +133,25 @@ export default {
* @returns {void} - 此函数不返回任何值 * @returns {void} - 此函数不返回任何值
* @throws {error} - 如果在尝试获取记录或保存记录时发生错误,将抛出错误 * @throws {error} - 如果在尝试获取记录或保存记录时发生错误,将抛出错误
*/ */
async uploadSuccess(recordId, filePathOnServer, filePathOnClient,fileName) { async uploadSuccess(
recordId,
filePathOnServer,
filePathOnClient,
fileName
) {
const unlock = await recordManager.lock(); const unlock = await recordManager.lock();
try { try {
const record = recordManager.getRecordById(recordId); const record = recordManager.getRecordById(recordId);
if (!record) return; if (!record) return;
const recordFileList = record.recordUrlList; const recordFileList = record.recordUrlList;
recordFileList.forEach((fileInfo) => { recordFileList.forEach((fileInfo) => {
if (fileInfo.file === filePathOnClient || fileInfo.file.endsWith(fileName)) { if (
fileInfo.file = filePathOnServer; fileInfo.file === filePathOnClient ||
delete fileInfo.status; fileInfo.file.endsWith(fileName)
} ) {
fileInfo.file = filePathOnServer;
delete fileInfo.status;
}
}); });
if (recordFileList.every((i) => i.status === undefined)) { if (recordFileList.every((i) => i.status === undefined)) {
const res = await uni.$api.commonApi.saveRecCustomer(record); const res = await uni.$api.commonApi.saveRecCustomer(record);
...@@ -197,15 +192,10 @@ export default { ...@@ -197,15 +192,10 @@ export default {
const result = records.reduce((acc, cur) => { const result = records.reduce((acc, cur) => {
acc = acc.concat( acc = acc.concat(
cur.recordUrlList.map((file) => { cur.recordUrlList.map((file) => {
const fileName = file.file.split("/").pop(); const aFullPath = file.file.split("/");
if (!file.file.startsWith("file://")) { const fileName = aFullPath.pop();
file.file = this.fileSaveFolderOnClient + fileName; const filePath = aFullPath.join("/");
} return { ...file, recordId: cur.id, fileName, filePath };
return { ...file, recordId: cur.id, fileName,filePath:file.file.replace(
fileName,
""
) };
}) })
); );
return acc; return acc;
...@@ -218,6 +208,7 @@ export default { ...@@ -218,6 +208,7 @@ export default {
*/ */
polling() { polling() {
if (this.snapshot.size == 0) { if (this.snapshot.size == 0) {
this.enQueue();
setTimeout(() => { setTimeout(() => {
this.polling(); this.polling();
}, 1000 * 60 * 10); }, 1000 * 60 * 10);
......
import { storageApi } from "./cache"; import { storageApi } from "./cache";
import { getFileSaveFolderOnClient, uuid } from "./utils";
/** /**
* 记录管理器类 * 记录管理器类
* 用于管理记录列表 * 用于管理记录列表
...@@ -9,10 +9,12 @@ class RecordManager { ...@@ -9,10 +9,12 @@ class RecordManager {
* RecordManager 类的构造函数 * RecordManager 类的构造函数
* 用于初始化 RecordManager 实例 * 用于初始化 RecordManager 实例
*/ */
constructor() {} constructor() {
this.fileSaveFolderOnClient = getFileSaveFolderOnClient();
}
// 单例模式 // 单例模式
static instance = null; static instance = null;
// 读写锁 // 读写锁
locker = false; locker = false;
/** /**
* 获取 RecordManager 的单例实例 * 获取 RecordManager 的单例实例
...@@ -25,7 +27,7 @@ class RecordManager { ...@@ -25,7 +27,7 @@ class RecordManager {
} }
return RecordManager.instance; return RecordManager.instance;
} }
/** /**
* 异步获取锁 * 异步获取锁
* 如果锁已被占用,则在一段时间后重新尝试获取锁 * 如果锁已被占用,则在一段时间后重新尝试获取锁
* 如果成功获取锁,将返回一个 Promise,该 Promise 包含一个解锁函数。 * 如果成功获取锁,将返回一个 Promise,该 Promise 包含一个解锁函数。
...@@ -87,7 +89,8 @@ class RecordManager { ...@@ -87,7 +89,8 @@ class RecordManager {
* 根据给定的 ID 获取一条记录 * 根据给定的 ID 获取一条记录
* *
* @param {number} id - 要获取的记录的 ID * @param {number} id - 要获取的记录的 ID
* @returns {Object} - 具有给定 ID 的记录对象,如果没有找到符合条件的记录,则返回 undefined * @returns {Object} - 具有给定 ID 的记录对象,如果没有找到符合条件的记录,则返回 undefined881008
*
*/ */
getRecordById(id) { getRecordById(id) {
const records = this.getRecords(); const records = this.getRecords();
...@@ -113,12 +116,36 @@ class RecordManager { ...@@ -113,12 +116,36 @@ class RecordManager {
updateRecord(record) { updateRecord(record) {
const records = this.getRecords(); const records = this.getRecords();
const index = records.findIndex((i) => i.id === record.id); const index = records.findIndex((i) => i.id === record.id);
if (index!== -1) { if (index !== -1) {
records[index] = record; records[index] = record;
this.setRecords(records); this.setRecords(records);
} }
} }
async compatible() {
const unlock = await this.lock();
try {
let records = this.getRecords();
records.forEach((i) => {
if (i.recordUrlList && i.recordUrlList.length) {
i.recordUrlList.forEach((j) => {
if (!j.file.startsWith("file://")) {
const fileName = j.file.split("/").pop();
j.file = this.fileSaveFolderOnClient + fileName;
}
});
}
if (!i.id) {
i.id = uuid();
}
});
} catch (e) {
throw e;
} finally {
unlock();
}
}
/** /**
* 清除所有记录 * 清除所有记录
* *
...@@ -128,5 +155,4 @@ class RecordManager { ...@@ -128,5 +155,4 @@ class RecordManager {
storageApi.removeRecords(); storageApi.removeRecords();
} }
} }
export default RecordManager export default RecordManager;
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: 前端 qianduan01@didihu.com.cn * @Author: 前端 qianduan01@didihu.com.cn
* @Date: 2024-04-08 17:16:46 * @Date: 2024-04-08 17:16:46
* @LastEditors: limin * @LastEditors: limin
* @LastEditTime: 2024-06-28 17:51:38 * @LastEditTime: 2024-07-02 09:32:33
* @FilePath: \gl_tape\utils\utils.js * @FilePath: \gl_tape\utils\utils.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/ */
...@@ -118,6 +118,25 @@ var uuid = function(len, radix) { ...@@ -118,6 +118,25 @@ var uuid = function(len, radix) {
return uuid.join(''); return uuid.join('');
} }
/**
* 获取文件系统根目录的完整路径
* 这个函数使用 HTML5 的 plus.io.requestFileSystem API 在私有文档目录中请求文件系统访问权限
* 当文件系统访问权限被授予后,使用 resolve 方法返回根目录的完整路径
* 这个 Promise 风格的函数可以被其他代码使用,以异步方式获取文件系统的根路径
* @returns {Promise<string>} 一个 Promise,当获取到文件系统根目录的完整路径时兑现,并返回根目录的路径作为字符串
*/
function getFilePath() {
return new Promise((resolve) => {
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function (fs) {
resolve(fs.root.fullPath);
});
});
}
function getFileSaveFolderOnClient(){
const rootPath = getFilePath();
return `file://${rootPath}uniapp_save/`;
}
export default { export default {
convertDateTimeFormat, convertDateTimeFormat,
deepClone, deepClone,
...@@ -125,5 +144,7 @@ export default { ...@@ -125,5 +144,7 @@ export default {
debounce, debounce,
throttle, throttle,
isNetWork, isNetWork,
uuid uuid,
getFilePath,
getFileSaveFolderOnClient
}; };
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!