merge.js
6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { storageApi } from '@/utils/cache'
export default {
mounted() {
this.mergeRecord()
this.clearPrevFile()
},
methods: {
deleteDirectory(dirPath) {
const _this = this
plus.io.resolveLocalFileSystemURL(
dirPath,
function (entry) {
if (entry.isDirectory) {
const directoryReader = entry.createReader()
directoryReader.readEntries(
function (entries) {
if (entries.length === 0) {
// 如果目录为空,直接删除
entry.remove(
function () {
console.log('目录删除成功:', dirPath)
uni.$logger.local.info(`目录删除成功: ->${dirPath}`)
},
function (e) {
console.log('目录删除失败:', e.message)
uni.$logger.local.info(`目录删除失败: ->${dirPath} ->${e.message}`)
}
)
} else {
// 递归删除子文件和子目录
let deleteCount = 0
const totalEntries = entries.length
function processNextEntry() {
if (deleteCount >= totalEntries) {
// 所有条目处理完毕,尝试删除当前目录
entry.remove(
function () {
console.log('目录删除成功:', dirPath)
uni.$logger.local.info(`目录删除成功: ->${dirPath}`)
},
function (e) {
console.log('目录删除失败:', e.message)
uni.$logger.local.info(`目录删除失败: ->${dirPath} ->${e.message}`)
}
)
return
}
const entry = entries[deleteCount]
entry.getMetadata(
function (metadata) {
if (entry.isFile) {
// 删除文件
entry.remove(
function () {
console.log('文件删除成功:', entry.fullPath)
uni.$logger.local.info(`文件删除成功: ->${entry.fullPath}`)
deleteCount++
processNextEntry() // 处理下一个条目
},
function (e) {
console.log('文件删除失败:', e.message)
uni.$logger.local.info(`文件删除失败: ->${entry.fullPath} ->${e.message}`)
deleteCount++
processNextEntry() // 处理下一个条目
}
)
} else if (entry.isDirectory) {
// 递归删除子目录
_this.deleteDirectory(entry.fullPath)
deleteCount++
processNextEntry() // 处理下一个条目
}
},
function (e) {
console.log('获取元数据失败:', e.message)
deleteCount++
processNextEntry() // 处理下一个条目
}
)
}
// 开始处理第一个条目
processNextEntry()
}
},
function (e) {
console.log('读取目录内容失败:', e.message)
uni.$logger.local.info(`读取目录内容失败: ->${e.message}`)
}
)
} else {
console.log('路径不是目录:', dirPath)
uni.$logger.local.info(`路径不是目录: ->${dirPath}`)
}
},
function (e) {
console.log('无法访问目录:', e.message)
uni.$logger.local.info(`无法访问目录: ->${dirPath}`)
}
)
},
clearPrevFile() {
let version = plus.runtime.version
let clearVersion = uni.$storage.get('clearVersion')
if (clearVersion != version) {
const wwwPath = plus.io.convertLocalFileSystemURL('doc')
// const filesPath = wwwPath.replace(/apps.+/, 'apps/')
// console.log(filesPath)
const filesPath = wwwPath.replace(/gltap.+/, 'gltap/')
this.getDirectoryContents(filesPath)
}
setTimeout(() => {
uni.$storage.set('clearVersion', version)
}, 5000)
},
getDirectoryContents(dirPath) {
// 解析目录路径
const _this = this
const excludeFile = ['databases']
plus.io.resolveLocalFileSystemURL(
dirPath,
function (entry) {
// 创建 DirectoryReader
const directoryReader = entry.createReader()
// 读取目录内容
directoryReader.readEntries(
function (entries) {
console.log('目录内容读取成功:')
_this.directoryList = entries
entries.forEach(function (entry) {
console.log('文件路径:', entry.fullPath)
if (!excludeFile.includes(entry.name)) {
_this.deleteDirectory(entry.fullPath)
}
})
},
function (e) {
console.log('读取目录内容失败:', e.message)
}
)
},
function (e) {
console.log('无法访问目录:', e.message)
}
)
},
async mergeRecord() {
try {
const tempRecords = storageApi.getRecords('preRecordList') || []
uni.$logger.local.info(`获取待合并记录${JSON.stringify(tempRecords)}`)
for (let i = 0; i < tempRecords.length; i++) {
tempRecords[i].recordUrlList.forEach(record => {
record.file = record.file.split('/').pop()
})
uni.$logger.local.info(`合并记录${JSON.stringify(tempRecords[i])}`)
this.record.addRecord(tempRecords[i])
}
storageApi.removeRecords('preRecordList')
} catch (error) {
uni.$logger.error(`【合并异常】 ${JSON.stringify(error?.message)}`)
}
}
}
}