Commit e87507e2 by yixinjj

记录管理添加读写锁

1 parent 9c5abf6b
...@@ -8,12 +8,14 @@ let wv, getResult; ...@@ -8,12 +8,14 @@ let wv, getResult;
import { uploadFile, getOssToken } from "@/utils/ossUploadFile/uploadFile.js"; import { uploadFile, getOssToken } from "@/utils/ossUploadFile/uploadFile.js";
import config from "@/config.js"; import config from "@/config.js";
import RecordManager from "@/utils/recordManager"; import RecordManager from "@/utils/recordManager";
const recordManager = new RecordManager(); const recordManager = RecordManager.getInstance();
export default { export default {
onLoad() {}, onLoad() {},
data() { data() {
return { return {
wv: null, wv: null,
snapshot:[]
}; };
}, },
mounted() { mounted() {
...@@ -138,26 +140,45 @@ export default { ...@@ -138,26 +140,45 @@ export default {
}); });
}, },
async uploadRecord() {
const record = recordManager.getRecord(); // async uploadRecord(record) {
if (!record) return; // const record = recordManager.getRecord();
const recordFileList = record.recordUrlList; // if (!record) return;
if (recordFileList.length == 0) { // const recordFileList = record.recordUrlList;
//TODO 上传记录? // if (recordFileList.length == 0) {
} // //TODO 上传记录?
for (let fileInfo of recordFileList) { // }
const { file, status } = fileInfo; // for (let fileInfo of recordFileList) {
if (status == UPLOAD_STATUS.FAIL) { // const { file, status } = fileInfo;
const filePath = file.split("/").pop(); // if (status == UPLOAD_STATUS.FAIL) {
const { lsaccessKeySecret, securityToken, lsaccessKeyId } = // const filePath = file.split("/").pop();
await getOssToken(); // const { lsaccessKeySecret, securityToken, lsaccessKeyId } =
// console.log(lsaccessKeySecret, securityToken, lsaccessKeyId); // await getOssToken();
// // console.log(lsaccessKeySecret, securityToken, lsaccessKeyId);
// const rootPath = await this.getFilePath();
// wv.evalJS(
// `msgFromUniapp('${lsaccessKeyId}','${lsaccessKeySecret}','${securityToken}','${filePath}','file://${rootPath}uniapp_save/')`
// );
// }
// }
// },
async recordCtrl(){
const snapshot = await this.getRecordsSnapshot()
const records = this.flattenData(snapshot)
},
async getRecordsSnapshotFlatten(){
const snapshot = await this.getRecordsSnapshot()
const records = this.flattenData(snapshot)
return records
},
async uploadRecord(record) {
const filePath = record.file.split("/").pop();
const { lsaccessKeySecret, securityToken, lsaccessKeyId } = await getOssToken();
const rootPath = await this.getFilePath(); const rootPath = await this.getFilePath();
wv.evalJS( wv.evalJS(
`msgFromUniapp('${lsaccessKeyId}','${lsaccessKeySecret}','${securityToken}','${filePath}','file://${rootPath}uniapp_save/')` `msgFromUniapp('${lsaccessKeyId}','${lsaccessKeySecret}','${securityToken}','${filePath}','file://${rootPath}uniapp_save/')`
); );
}
}
}, },
async uploadSuccess(recordId, filePath) { async uploadSuccess(recordId, filePath) {
const record = recordManager.getRecordById(recordId); const record = recordManager.getRecordById(recordId);
...@@ -184,5 +205,32 @@ export default { ...@@ -184,5 +205,32 @@ export default {
} }
} }
}, },
/**
* 获取记录快照的异步函数
* 此函数获取当前记录列表的快照,并返回这些记录
*
* @async
* @returns {Promise<Array>} 解析为包含记录列表的 Promise
*/
async getRecordsSnapshot(){
const unlock = await recordManager.lock()
const records = recordManager.getRecords()
unlock()
return records
},
/**
* 展开数据
* 将嵌套的记录列表转换为平面的文件对象列表,其中每个文件对象都包含一个 recordId 属性
*
* @param {Array} records - 要展开的记录列表
* @returns {Array} - 展开后包含文件对象的列表,每个文件对象都包含一个 recordId 属性
*/
flattenData(records) {
const result= records.reduce((acc, cur) => {
acc= acc.concat(cur.recordUrlList.map(file => ({...file, recordId: cur.id})))
return acc
}, [])
return result;
},
}, },
}; };
import { storageApi } from "@/utils/cache"; import { storageApi } from "@/utils/cache";
/**
* 记录管理器类
* 用于管理记录列表
*/
class RecordManager { class RecordManager {
/**
* RecordManager 类的构造函数
* 用于初始化 RecordManager 实例
*/
constructor() {} constructor() {}
// 单例模式
static instance = null;
// 读写锁
locker = false;
/**
* 获取 RecordManager 的单例实例
* 如果 instance 不存在,则创建一个新实例并赋值给 instance
* @return {RecordManager} RecordManager 的单例实例
*/
static getInstance() {
if (!RecordManager.instance) {
RecordManager.instance = new RecordManager();
}
return RecordManager.instance;
}
/**
* 异步获取锁
* 如果锁已被占用,则在一段时间后重新尝试获取锁
* 如果成功获取锁,将返回一个 Promise,该 Promise 包含一个解锁函数。
* @return {Promise} 一个 Promise,在锁可用时兑现,并返回一个解锁函数
*/
async lock() {
return new Promise((resolve) => {
if (this.locker) {
setTimeout(() => {
this.lock();
}, 100);
} else {
this.locker = true;
resolve(this.unlock);
}
});
}
/**
* 释放本地存储的读写锁
*/
unlock() {
this.locker = false;
}
/** /**
* 将一条新记录添加到当前对象的记录列表中 * 将一条新记录添加到当前对象的记录列表中
* *
* @param {Object} record - 要添加的新记录对象 * @param {Object} record - 要添加的新记录对象
* @returns {Array} - 包含新记录的更新后的记录列表 * @returns {Array} - 包含新记录的更新后的记录列表
*/ */
addRecord(record) { async addRecord(record) {
const records = this.getRecords(); const records = this.getRecords();
records.push(record); records.push(record);
this.setRecords(records); this.setRecords(records);
...@@ -20,7 +68,7 @@ class RecordManager { ...@@ -20,7 +68,7 @@ class RecordManager {
* @param {number} id - 要移除的记录的ID * @param {number} id - 要移除的记录的ID
* @returns {Array} - 移除记录后的更新后的记录列表 * @returns {Array} - 移除记录后的更新后的记录列表
*/ */
removeRecord(id) { async removeRecord(id) {
let records = this.getRecords(); let records = this.getRecords();
records = records.filter((i) => i.id !== id); records = records.filter((i) => i.id !== id);
this.setRecords(records); this.setRecords(records);
...@@ -71,4 +119,5 @@ class RecordManager { ...@@ -71,4 +119,5 @@ class RecordManager {
storageApi.removeRecords(); storageApi.removeRecords();
} }
} }
export default RecordManager; export default RecordManager
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!