Commit e047003c by chenwq

fix: 增加车系选择后素材缓存失败日志记录,优化下载失败信息

1 parent f2c96300
......@@ -135,7 +135,7 @@ const getImageVersion = async () => {
}
/**获取素材配置 */
const sourceRequests = new Map()
const getSoureData = async (shopMerchantId, sceneType, params = {}) => {
const getSoureData = async (shopMerchantId, sceneType, params = {}, options = {}) => {
const storageName = materialCache.getSourceKey(shopMerchantId, sceneType, params)
const requestId = (sourceRequests.get(storageName) || 0) + 1
sourceRequests.set(storageName, requestId)
......@@ -199,7 +199,7 @@ const getSoureData = async (shopMerchantId, sceneType, params = {}) => {
if (!isCurrent()) return
// 首屏只等待基础数据;图片下载、转换和写入在后台进行。
setTimeout(() => {
if (cacheSaved && isCurrent()) materialCache.cacheMaterialImages(storageName, data).catch(() => {})
if (cacheSaved && isCurrent()) materialCache.cacheMaterialImages(storageName, data, options).catch(() => {})
}, 0)
return data
} else throw false
......
......@@ -625,6 +625,7 @@ export default {
subBrandId: carChildsBrandId
}
const carKey = this.getCustomerCarKey()
materialCache.setActiveMaterialCarKey(carKey)
console.log('setPageData params', { ...params, carKey })
const online = typeof networkStatus === 'boolean' ? networkStatus : await materialCache.getNetworkStatus()
if (!isCurrent()) return
......@@ -665,7 +666,7 @@ export default {
}
if (!online) return
try {
const requestData = await uni.$api.systemApi.getSoureData(shopMerchantId, sceneType, params)
const requestData = await uni.$api.systemApi.getSoureData(shopMerchantId, sceneType, params, { carKey })
if (!isCurrent() || !requestData) return
// 接口返回不走原始 URL,改为和缓存一致:以接口数据为源补齐 base64 后再渲染。
this._offlineSource = { key: storageName, requestedKey: storageName, base: requestData, carKey, requestId, online: true }
......
......@@ -26,14 +26,17 @@ function createUni(options = {}) {
storage.delete(key)
success({})
},
downloadFile({ url, success }) {
downloadFile({ url, success, fail }) {
calls.downloadFile++
calls.downloadUrls.push(url)
const complete = () => success({
statusCode: 200,
tempFilePath: `_doc/temp/${encodeURIComponent(url)}.jpg`,
header: options.downloadHeader || {}
})
const complete = () => {
if (options.downloadError) return fail(options.downloadError)
success({
statusCode: 200,
tempFilePath: `_doc/temp/${encodeURIComponent(url)}.jpg`,
header: options.downloadHeader || {}
})
}
if (options.deferDownloads) pendingDownloads.push(complete)
else complete()
},
......@@ -66,6 +69,9 @@ function createUni(options = {}) {
arrayBufferToBase64() {
calls.arrayBufferToBase64++
throw new Error('素材图片不应再转换为 Base64')
},
getNetworkType({ success }) {
success({ networkType: options.networkType || 'wifi' })
}
}
api.releaseDownloads = () => pendingDownloads.splice(0).forEach(complete => complete())
......@@ -102,7 +108,7 @@ async function run() {
assert.ok([...uni.storage.values()].every(value => !String(value).startsWith('data:image/')), 'storage 不应保存 Base64 图片')
assert.equal(uni.calls.logs.length, 1, '同一次读取只应输出一条 OSS 来源汇总')
const ossLog = uni.calls.logs[0]
assert.ok(ossLog.startsWith('[素材来源汇总] OSS=1 | 文件=0 | OSS失败=0\nOSS明细:\n素材1 | '), '首次展示应计入分行的 OSS 汇总')
assert.ok(ossLog.startsWith('[本次素材读取] OSS=1 | 文件=0 | OSS失败=0\nOSS明细:\n素材1 | '), '首次展示应计入分行的 OSS 汇总')
assert.ok(/开始下载时间=\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}/.test(ossLog), 'OSS 明细应包含开始下载时间')
assert.ok(/下载成功时间=\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}/.test(ossLog), 'OSS 明细应包含下载成功时间')
assert.ok(ossLog.includes(`OSS路径=${url}`) && ossLog.includes('文件读取路径=_doc/uniapp_save/material-1.jpg'),
......@@ -117,7 +123,7 @@ async function run() {
moduleIds: [101], toolIds: [], isCurrent: () => true
})))
await waitForSourceLog()
assert.deepEqual(uni.calls.logs, ['[素材来源汇总] OSS=0 | 文件=1 | OSS失败=0'],
assert.deepEqual(uni.calls.logs, ['[本次素材读取] OSS=0 | 文件=1 | OSS失败=0'],
'短时间重复读取同一图片应去重并合并为一条文件来源汇总')
const missingFilePath = display.moduleList[0].contentPicUrl
......@@ -163,7 +169,7 @@ async function run() {
assert.equal(uni.calls.downloadFile, 5, '图片地址变化后应下载新文件')
assert.equal(uni.calls.removeSavedFile, 3, '新文件成功后应删除旧文件')
assert.equal(changedDisplay.moduleList[0].contentPicUrl, '_doc/uniapp_save/material-5.jpg')
assert.ok(uni.calls.logs.some(message => message.startsWith('[素材来源汇总] OSS=1 | 文件=0 | OSS失败=0\nOSS明细:')),
assert.ok(uni.calls.logs.some(message => message.startsWith('[本次素材读取] OSS=1 | 文件=0 | OSS失败=0\nOSS明细:')),
'更新后的图片首次展示时应计入带成功明细的 OSS 汇总')
const withoutImages = { id: 'source-1', moduleList: [], toolList: [] }
......@@ -175,20 +181,85 @@ async function run() {
delete require.cache[modulePath]
const retryCache = require(modulePath)
const retryKey = retryCache.getSourceKey('shop-2', 'scene-1', { brandId: 10, subBrandId: 20 })
await retryCache.cacheMaterialImages(retryKey, base)
retryCache.setActiveMaterialCarKey('car-2')
await retryCache.cacheMaterialImages(retryKey, base, { carKey: 'car-2' })
assert.equal(uni.calls.downloadFile, 1)
assert.equal(uni.calls.saveFile, 1)
await waitForSourceLog()
assert.equal(uni.calls.logs.length, 1, 'OSS 下载或保存失败应输出一条失败汇总')
assert.ok(uni.calls.logs[0].startsWith('[素材来源汇总] OSS=0 | 文件=0 | OSS失败=1\nOSS失败明细:\n素材1 | '),
assert.ok(uni.calls.logs[0].startsWith('[本次素材读取] OSS=0 | 文件=0 | OSS失败=1\nOSS失败明细:\n素材1 | '),
'OSS 失败明细应单独分行')
assert.ok(uni.calls.logs[0].includes('下载状态=disk full') && uni.calls.logs[0].includes(`OSS路径=${url}`),
'OSS 失败明细应包含失败状态和地址')
assert.ok(/开始下载时间=.+下载失败时间=/.test(uni.calls.logs[0]), 'OSS 失败明细应包含开始和失败时间')
await retryCache.cacheMaterialImages(retryKey, base)
assert.ok(uni.calls.logs[0].includes('下载耗时=') && uni.calls.logs[0].includes('网络类型=wifi') &&
uni.calls.logs[0].includes('错误详情={"errMsg":"disk full"}'), 'OSS 失败明细应包含耗时、网络和系统错误')
await retryCache.cacheMaterialImages(retryKey, base, { carKey: 'car-2' })
assert.equal(uni.calls.downloadFile, 2, '保存失败后再次缓存必须重试下载')
assert.equal(uni.calls.saveFile, 2, '保存失败后再次缓存必须重试保存')
global.uni = createUni({
networkType: '4g',
downloadError: { errMsg: 'downloadFile:fail undefined', errCode: 600003, errno: -118 }
})
delete require.cache[modulePath]
const requestFailureCache = require(modulePath)
const requestFailureKey = requestFailureCache.getSourceKey('shop-download-fail', 'scene-1')
requestFailureCache.setActiveMaterialCarKey('car-download-fail')
await requestFailureCache.cacheMaterialImages(requestFailureKey, base, { carKey: 'car-download-fail' })
await waitForSourceLog()
assert.equal(uni.calls.logs.length, 1, '下载接口失败应写入一条失败汇总')
assert.ok(uni.calls.logs[0].includes('下载状态=downloadFile:fail undefined') &&
uni.calls.logs[0].includes('网络类型=4g') && uni.calls.logs[0].includes('错误码=600003') &&
uni.calls.logs[0].includes('错误详情={"errMsg":"downloadFile:fail undefined","errCode":600003,"errno":-118}'),
'下载接口失败应保留系统错误码和完整错误信息')
// 选择车系后,后台仍可缓存同品牌全部车系,但失败明细只记录当前车系。
global.uni = createUni({ downloadError: { errMsg: 'downloadFile:fail undefined' } })
delete require.cache[modulePath]
const carFailureCache = require(modulePath)
const carFailureKey = carFailureCache.getSourceKey('shop-car-filter', 'scene-1', { brandId: 10, subBrandId: 20 })
const currentCarUrl = 'https://example.test/current-car.webp'
const otherCarUrl = 'https://example.test/other-car.webp'
const carFailureSource = {
id: 'source-car-filter',
moduleList: [{
id: 210,
materialDetailList: [
{ relVehicleSeriesId: 1001, contentPicUrl: currentCarUrl },
{ relVehicleSeriesId: 1002, contentPicUrl: otherCarUrl }
]
}],
toolList: []
}
carFailureCache.setActiveMaterialCarKey(1001)
await carFailureCache.cacheMaterialImages(carFailureKey, carFailureSource, { carKey: 1001 })
await waitForSourceLog()
assert.equal(uni.calls.downloadFile, 2, '后台仍应尝试缓存同品牌的其他车系素材')
assert.equal(uni.calls.logs.length, 1, '当前车系下载失败应输出失败汇总')
assert.ok(uni.calls.logs[0].includes('OSS失败=1') && uni.calls.logs[0].includes(currentCarUrl),
'失败明细应保留当前车系素材')
assert.ok(!uni.calls.logs[0].includes(otherCarUrl), '失败明细不应包含其他车系素材')
// 切换车系后,上一车系尚未结束的下载即使随后失败,也不能写入新车系日志。
global.uni = createUni({ deferDownloads: true, downloadError: { errMsg: 'downloadFile:fail undefined' } })
delete require.cache[modulePath]
const switchedFailureCache = require(modulePath)
const switchedFailureKey = switchedFailureCache.getSourceKey('shop-car-switch-filter', 'scene-1', { brandId: 10, subBrandId: 20 })
switchedFailureCache.setActiveMaterialCarKey(1001)
const firstCarTask = switchedFailureCache.cacheMaterialImages(switchedFailureKey, carFailureSource, { carKey: 1001 })
await new Promise(resolve => setImmediate(resolve))
switchedFailureCache.setActiveMaterialCarKey(1002)
const secondCarTask = switchedFailureCache.cacheMaterialImages(switchedFailureKey, carFailureSource, { carKey: 1002 })
await new Promise(resolve => setImmediate(resolve))
uni.releaseDownloads()
await Promise.all([firstCarTask, secondCarTask])
await waitForSourceLog()
assert.equal(uni.calls.logs.length, 1, '切换后只应输出当前车系的失败汇总')
assert.ok(uni.calls.logs[0].includes('OSS失败=1') && uni.calls.logs[0].includes(otherCarUrl),
'切换后应记录新车系素材失败')
assert.ok(!uni.calls.logs[0].includes(currentCarUrl), '切换后不应记录上一车系迟到的失败')
global.uni = createUni({ deferDownloads: true })
delete require.cache[modulePath]
const concurrentCache = require(modulePath)
......@@ -266,7 +337,7 @@ async function run() {
.every(value => value.startsWith('_doc/uniapp_save/')), '后台补齐时应恢复全部图片')
await waitForSourceLog()
assert.equal(uni.calls.logs.length, 1, '同一车型的分批读取应合并成一条日志')
assert.ok(uni.calls.logs[0].startsWith('[素材来源汇总] OSS=3 | 文件=0 | OSS失败=0\nOSS明细:'),
assert.ok(uni.calls.logs[0].startsWith('[本次素材读取] OSS=3 | 文件=0 | OSS失败=0\nOSS明细:'),
'合并日志应按图片去重并保留 OSS 明细')
assert.equal((uni.calls.logs[0].match(/^素材\d+ \|/gm) || []).length, 3, '每张 OSS 图片应单独占一行')
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!