Commit f17e470e by yixinjj

添加兼容函数

1 parent 09536d03
......@@ -53,7 +53,7 @@ export default {
filePathOnClient = filePath + fileName,
} = result.detail.data[0];
if (code == 0) {
this.uploadSuccess(recordId, data.filePath, filePathOnClient,fileName);
this.uploadSuccess(recordId, data.filePath, filePathOnClient, fileName);
} else {
this.polling();
}
......@@ -91,7 +91,7 @@ export default {
const _snapshot = await this.getRecordsSnapshotFlatten();
this.snapshot = new Queue(_snapshot);
},
/**
/**
* 获取记录快照并展平数据
* 此函数异步获取记录快照,然后使用flattenData函数将这些记录转换为一个文件对象列表
* 每个文件对象都包含一个额外的recordId属性,用于指示它所属的记录
......@@ -106,20 +106,6 @@ export default {
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 应用,以便进行文件上传
*
......@@ -129,7 +115,8 @@ export default {
* @throws {Error} - 如果获取 OSS 令牌的过程中出现错误,将抛出错误
*/
async uploadRecord(record) {
const { lsaccessKeySecret, securityToken, lsaccessKeyId } = await getOssToken();
const { lsaccessKeySecret, securityToken, lsaccessKeyId } =
await getOssToken();
wv.evalJS(
`msgFromUniapp('${lsaccessKeyId}','${lsaccessKeySecret}','${securityToken}','${record.fileName}','${record.filePath}')`
);
......@@ -146,17 +133,25 @@ export default {
* @returns {void} - 此函数不返回任何值
* @throws {error} - 如果在尝试获取记录或保存记录时发生错误,将抛出错误
*/
async uploadSuccess(recordId, filePathOnServer, filePathOnClient,fileName) {
async uploadSuccess(
recordId,
filePathOnServer,
filePathOnClient,
fileName
) {
const unlock = await recordManager.lock();
try {
const record = recordManager.getRecordById(recordId);
if (!record) return;
const recordFileList = record.recordUrlList;
recordFileList.forEach((fileInfo) => {
if (fileInfo.file === filePathOnClient || fileInfo.file.endsWith(fileName)) {
fileInfo.file = filePathOnServer;
delete fileInfo.status;
}
if (
fileInfo.file === filePathOnClient ||
fileInfo.file.endsWith(fileName)
) {
fileInfo.file = filePathOnServer;
delete fileInfo.status;
}
});
if (recordFileList.every((i) => i.status === undefined)) {
const res = await uni.$api.commonApi.saveRecCustomer(record);
......@@ -197,15 +192,10 @@ export default {
const result = records.reduce((acc, cur) => {
acc = acc.concat(
cur.recordUrlList.map((file) => {
const fileName = file.file.split("/").pop();
if (!file.file.startsWith("file://")) {
file.file = this.fileSaveFolderOnClient + fileName;
}
return { ...file, recordId: cur.id, fileName,filePath:file.file.replace(
fileName,
""
) };
const aFullPath = file.file.split("/");
const fileName = aFullPath.pop();
const filePath = aFullPath.join("/");
return { ...file, recordId: cur.id, fileName, filePath };
})
);
return acc;
......@@ -218,6 +208,7 @@ export default {
*/
polling() {
if (this.snapshot.size == 0) {
this.enQueue();
setTimeout(() => {
this.polling();
}, 1000 * 60 * 10);
......
import { storageApi } from "./cache";
import { getFileSaveFolderOnClient, uuid } from "./utils";
/**
* 记录管理器类
* 用于管理记录列表
......@@ -9,10 +9,12 @@ class RecordManager {
* RecordManager 类的构造函数
* 用于初始化 RecordManager 实例
*/
constructor() {}
constructor() {
this.fileSaveFolderOnClient = getFileSaveFolderOnClient();
}
// 单例模式
static instance = null;
// 读写锁
// 读写锁
locker = false;
/**
* 获取 RecordManager 的单例实例
......@@ -25,7 +27,7 @@ class RecordManager {
}
return RecordManager.instance;
}
/**
/**
* 异步获取锁
* 如果锁已被占用,则在一段时间后重新尝试获取锁
* 如果成功获取锁,将返回一个 Promise,该 Promise 包含一个解锁函数。
......@@ -87,7 +89,8 @@ class RecordManager {
* 根据给定的 ID 获取一条记录
*
* @param {number} id - 要获取的记录的 ID
* @returns {Object} - 具有给定 ID 的记录对象,如果没有找到符合条件的记录,则返回 undefined
* @returns {Object} - 具有给定 ID 的记录对象,如果没有找到符合条件的记录,则返回 undefined881008
*
*/
getRecordById(id) {
const records = this.getRecords();
......@@ -113,12 +116,36 @@ class RecordManager {
updateRecord(record) {
const records = this.getRecords();
const index = records.findIndex((i) => i.id === record.id);
if (index!== -1) {
if (index !== -1) {
records[index] = record;
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 {
storageApi.removeRecords();
}
}
export default RecordManager
export default RecordManager;
......@@ -2,7 +2,7 @@
* @Author: 前端 qianduan01@didihu.com.cn
* @Date: 2024-04-08 17:16:46
* @LastEditors: limin
* @LastEditTime: 2024-06-28 17:51:38
* @LastEditTime: 2024-07-02 09:32:33
* @FilePath: \gl_tape\utils\utils.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
......@@ -118,6 +118,25 @@ var uuid = function(len, radix) {
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 {
convertDateTimeFormat,
deepClone,
......@@ -125,5 +144,7 @@ export default {
debounce,
throttle,
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!