* 获取系统应用列表,并处理图标缓存 * 优先从数据库缓存读取,没有缓存时才扫描
()
| 161 | * 优先从数据库缓存读取,没有缓存时才扫描 |
| 162 | */ |
| 163 | private async getApps(): Promise<any[]> { |
| 164 | console.log('[Commands] 收到获取应用列表请求') |
| 165 | |
| 166 | // 如果本地应用搜索被禁用,直接返回空列表 |
| 167 | if (!this.isLocalAppSearchEnabled) { |
| 168 | console.log('[Commands] 本地应用搜索已关闭,返回空列表') |
| 169 | return [] |
| 170 | } |
| 171 | |
| 172 | // 开发模式下强制重新扫描(方便调试) |
| 173 | if (!app.isPackaged) { |
| 174 | console.log('[Commands] 开发模式:跳过缓存,重新扫描应用...') |
| 175 | return await this.scanAndCacheApps() |
| 176 | } |
| 177 | |
| 178 | // 尝试从数据库缓存读取 |
| 179 | try { |
| 180 | const cachedApps = databaseAPI.dbGet('cached-commands') |
| 181 | const cacheVersion = databaseAPI.dbGet(AppsAPI.APP_CACHE_VERSION_KEY) |
| 182 | if (cachedApps && Array.isArray(cachedApps) && cachedApps.length > 0) { |
| 183 | // 检查缓存的图标格式是否为新协议 |
| 184 | // 只要有一个应用使用了旧的文件路径格式(且不是 .png 结尾的静态资源),就视为旧缓存 |
| 185 | const hasOldFormat = cachedApps.some( |
| 186 | (app) => |
| 187 | app.icon && |
| 188 | !app.icon.startsWith('ztools-icon://') && |
| 189 | !app.icon.startsWith('data:') && |
| 190 | !app.icon.startsWith('http') && |
| 191 | // Windows 上的静态 png 资源除外(通常是手动转换的) |
| 192 | !( |
| 193 | process.platform === 'win32' && |
| 194 | app.icon.startsWith('file:') && |
| 195 | app.icon.endsWith('.png') |
| 196 | ) |
| 197 | ) |
| 198 | |
| 199 | if (cacheVersion !== AppsAPI.APP_CACHE_VERSION) { |
| 200 | console.log('[Commands] 检测到旧版应用缓存,将重新扫描以刷新本地化名称索引...') |
| 201 | } else if (hasOldFormat) { |
| 202 | console.log('[Commands] 检测到旧格式图标缓存,将重新扫描并更新为 ztools-icon 协议...') |
| 203 | } else { |
| 204 | console.log(`从缓存读取到 ${cachedApps.length} 个应用`) |
| 205 | return cachedApps |
| 206 | } |
| 207 | } |
| 208 | } catch (error) { |
| 209 | console.log('[Commands] 读取应用缓存失败,将进行扫描:', error) |
| 210 | } |
| 211 | |
| 212 | // 缓存不存在,执行扫描 |
| 213 | console.log('[Commands] 缓存不存在,开始扫描应用...') |
| 214 | return await this.scanAndCacheApps() |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * 扫描应用并缓存到数据库 |
no test coverage detected