Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
glsx
/
gl_tape
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
3
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit e87507e2
authored
Jun 30, 2024
by
yixinjj
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
记录管理添加读写锁
1 parent
9c5abf6b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
121 additions
and
24 deletions
mixin/uploader.js
utils/recordManager/index.js
mixin/uploader.js
View file @
e87507e
...
...
@@ -8,12 +8,14 @@ let wv, getResult;
import
{
uploadFile
,
getOssToken
}
from
"@/utils/ossUploadFile/uploadFile.js"
;
import
config
from
"@/config.js"
;
import
RecordManager
from
"@/utils/recordManager"
;
const
recordManager
=
new
RecordManager
();
const
recordManager
=
RecordManager
.
getInstance
();
export
default
{
onLoad
()
{},
data
()
{
return
{
wv
:
null
,
snapshot
:[]
};
},
mounted
()
{
...
...
@@ -138,26 +140,45 @@ export default {
});
},
async
uploadRecord
()
{
const
record
=
recordManager
.
getRecord
();
if
(
!
record
)
return
;
const
recordFileList
=
record
.
recordUrlList
;
if
(
recordFileList
.
length
==
0
)
{
//TODO 上传记录?
}
for
(
let
fileInfo
of
recordFileList
)
{
const
{
file
,
status
}
=
fileInfo
;
if
(
status
==
UPLOAD_STATUS
.
FAIL
)
{
const
filePath
=
file
.
split
(
"/"
).
pop
();
const
{
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 uploadRecord(record) {
// const record = recordManager.getRecord();
// if (!record) return;
// const recordFileList = record.recordUrlList;
// if (recordFileList.length == 0) {
// //TODO 上传记录?
// }
// for (let fileInfo of recordFileList) {
// const { file, status } = fileInfo;
// if (status == UPLOAD_STATUS.FAIL) {
// const filePath = file.split("/").pop();
// const { 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
();
wv
.
evalJS
(
`msgFromUniapp('
${
lsaccessKeyId
}
','
${
lsaccessKeySecret
}
','
${
securityToken
}
','
${
filePath
}
','file://
${
rootPath
}
uniapp_save/')`
);
},
async
uploadSuccess
(
recordId
,
filePath
)
{
const
record
=
recordManager
.
getRecordById
(
recordId
);
...
...
@@ -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
;
},
},
};
utils/recordManager/index.js
View file @
e87507e
import
{
storageApi
}
from
"@/utils/cache"
;
/**
* 记录管理器类
* 用于管理记录列表
*/
class
RecordManager
{
/**
* RecordManager 类的构造函数
* 用于初始化 RecordManager 实例
*/
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 - 要添加的新记录对象
* @returns {Array} - 包含新记录的更新后的记录列表
*/
addRecord
(
record
)
{
a
sync
a
ddRecord
(
record
)
{
const
records
=
this
.
getRecords
();
records
.
push
(
record
);
this
.
setRecords
(
records
);
...
...
@@ -20,7 +68,7 @@ class RecordManager {
* @param {number} id - 要移除的记录的ID
* @returns {Array} - 移除记录后的更新后的记录列表
*/
removeRecord
(
id
)
{
async
removeRecord
(
id
)
{
let
records
=
this
.
getRecords
();
records
=
records
.
filter
((
i
)
=>
i
.
id
!==
id
);
this
.
setRecords
(
records
);
...
...
@@ -71,4 +119,5 @@ class RecordManager {
storageApi
.
removeRecords
();
}
}
export
default
RecordManager
;
export
default
RecordManager
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment