Commit f598e568 by yixinjj

添加一些注释

1 parent 81f25405
...@@ -15,6 +15,14 @@ export default { ...@@ -15,6 +15,14 @@ export default {
this.init(); this.init();
}, },
methods: { methods: {
/**
* 初始化函数
* 这个函数在对象创建时被调用,它执行一系列的初始化任务
*
* @async
* @returns {Promise<void>} 当初始化完成时,Promise 将解析为 undefined
* @throws {Error} 如果初始化过程中出现错误,将抛出错误
*/
async init() { async init() {
this.wv = await this.initWebview(); this.wv = await this.initWebview();
const rootPath = this.getFilePath(); const rootPath = this.getFilePath();
...@@ -22,6 +30,19 @@ export default { ...@@ -22,6 +30,19 @@ export default {
await this.enQueue(); await this.enQueue();
this.polling(); this.polling();
}, },
/**
* 处理来自 UniApp 的消息
* 此函数接收一个包含消息详情的对象,并从其中解析出文件名、数据、记录标识、文件路径和客户端文件路径。如果消息代码为 0,表示上传成功,函数会调用 uploadSuccess 方法进行进一步处理。如果消息代码不为 0,则表示上传失败,函数会调用 polling 方法重新轮询。
*
* @param {Object} result - 包含消息详情的对象
* @param {number} result.detail.data[0].code - 消息代码,0 表示上传成功
* @param {string} result.detail.data[0].fileName - 文件名
* @param {Object} result.detail.data[0].data - 包含文件路径的对象
* @param {string} result.detail.data[0].recordId - 记录标识,如果需要则进行后续处理
* @param {string} result.detail.data[0].filePath - 文件路径
* @param {string} [result.detail.data[0].filePathOnClient=filePath+fileName] - 客户端文件路径,默认为 filePath 和 fileName 的拼接
* @throws {Error} 如果消息中缺少必要的参数,将抛出错误
*/
handlerMessage(result) { handlerMessage(result) {
const { const {
code, code,
...@@ -37,7 +58,14 @@ export default { ...@@ -37,7 +58,14 @@ export default {
this.polling(); this.polling();
} }
}, },
/**上传记录*/ /**
* 初始化 WebView 的异步函数
* 这个函数返回一个 Promise,当初始化 WebView 完成时,它将解析为 WebView 对象。
* 在页面初始化调用时,这个函数会延迟 1 秒,以确保 WebView 对象已经创建。
*
* @async
* @returns {Promise<any>} 当初始化完成时,Promise 将解析为 WebView 对象。
*/
initWebview() { initWebview() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var currentWebview = this.$parent.$scope.$getAppWebview(); //此对象相当于html5plus里的plus.webview.currentWebview()。在uni-app里vue页面直接使用plus.webview.currentWebview()无效 var currentWebview = this.$parent.$scope.$getAppWebview(); //此对象相当于html5plus里的plus.webview.currentWebview()。在uni-app里vue页面直接使用plus.webview.currentWebview()无效
...@@ -48,15 +76,42 @@ export default { ...@@ -48,15 +76,42 @@ export default {
resolve(this.wv); resolve(this.wv);
}); });
}, },
/**
* 入列操作
* 这个函数异步执行,目的是获取记录的快照并将其扁平化,以形成一个队列
* 通过调用 getRecordsSnapshotFlatten 获取快照数据
* 当所有数据准备好后,创建一个新的 Queue 实例,并将扁平化后的数据作为参数传入
* 这样,队列就被创建并且最初是满的,其中包含了已经获取到的记录快照
*
* @async
* @returns {Promise<void>} 一个 Promise,当初始化队列完成时解析,没有返回值
* @throws {Error} 如果获取快照失败或扁平化数据失败,将抛出错误
*/
async enQueue() { async enQueue() {
const _snapshot = await this.getRecordsSnapshotFlatten(); const _snapshot = await this.getRecordsSnapshotFlatten();
this.snapshot = new Queue(_snapshot); this.snapshot = new Queue(_snapshot);
}, },
/**
* 获取记录快照并展平数据
* 此函数异步获取记录快照,然后使用flattenData函数将这些记录转换为一个文件对象列表
* 每个文件对象都包含一个额外的recordId属性,用于指示它所属的记录
*
* @async
* @returns {Promise<Array>} 一个Promise,当获取并处理完记录时,返回一个文件对象列表
* @throws {Error} 如果在获取记录快照或展平数据的过程中出现错误,将抛出错误
*/
async getRecordsSnapshotFlatten() { async getRecordsSnapshotFlatten() {
const _records = await this.getRecordsSnapshot(); const _records = await this.getRecordsSnapshot();
const _snapshot = this.flattenData(_records); const _snapshot = this.flattenData(_records);
return _snapshot; return _snapshot;
}, },
/**
* 获取文件系统根目录的完整路径
* 这个函数使用 HTML5 的 plus.io.requestFileSystem API 在私有文档目录中请求文件系统访问权限
* 当文件系统访问权限被授予后,使用 resolve 方法返回根目录的完整路径
* 这个 Promise 风格的函数可以被其他代码使用,以异步方式获取文件系统的根路径
* @returns {Promise<string>} 一个 Promise,当获取到文件系统根目录的完整路径时兑现,并返回根目录的路径作为字符串
*/
getFilePath() { getFilePath() {
return new Promise((resolve) => { return new Promise((resolve) => {
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function (fs) { plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function (fs) {
...@@ -64,12 +119,33 @@ export default { ...@@ -64,12 +119,33 @@ export default {
}); });
}); });
}, },
/**
* 将指定记录上传到服务器
* 此函数使用 WebView 的 evalJS 方法,将 OSS 令牌信息和记录详情传递给 UniApp 应用,以便进行文件上传
*
* @async
* @param {Object} record - 要上传的记录对象,包含 fileName 和 filePath 属性
* @returns {void} - 此函数不返回任何值
* @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}')`
); );
}, },
/**
* 处理上传成功后的逻辑
* 此函数在文件上传成功后被调用,它会更新本地记录的文件路径,
* 并根据情况决定是保存记录还是重新上传记录。
* @async
* @param {string} recordId - 记录的唯一标识符
* @param {string} filePathOnServer - 文件在服务器上的新路径
* @param {string} filePathOnClient - 文件在客户端的原始路径
* @param {string} fileName - 文件名
* @returns {void} - 此函数不返回任何值
* @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 {
...@@ -140,14 +216,14 @@ export default { ...@@ -140,14 +216,14 @@ export default {
* 轮询函数 * 轮询函数
* 这个函数的目的是在一定条件下,通过持续轮询来执行上传记录的操作。它检查一个队列(snapshot)的大小。如果队列为空,它会等待十分钟(1000 毫秒 * 60 秒 * 10 分钟)后再次检查。如果队列中有记录,它会取出一条记录并上传它。 * 这个函数的目的是在一定条件下,通过持续轮询来执行上传记录的操作。它检查一个队列(snapshot)的大小。如果队列为空,它会等待十分钟(1000 毫秒 * 60 秒 * 10 分钟)后再次检查。如果队列中有记录,它会取出一条记录并上传它。
*/ */
async polling() { polling() {
if (this.snapshot.size == 0) { if (this.snapshot.size == 0) {
setTimeout(() => { setTimeout(() => {
this.polling(); this.polling();
}, 1000 * 60 * 10); }, 1000 * 60 * 10);
} else { } else {
const record = this.snapshot.dequeue(); const record = this.snapshot.dequeue();
await this.uploadRecord(record); this.uploadRecord(record);
} }
}, },
}, },
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!