/** * Purpose: fileApi supports file.js by holding or executing the file api page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const fileApi = require('../api/file') const { DEFAULT_PRODUCT_IMAGE } = require('./config') const { CACHE_TTL } = require('./config') const { splitCsv } = require('./format') /** * Purpose: urlCache supports file.js by holding or executing the url cache page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const urlCache = {} /** * Purpose: getSignedUrl supports file.js by holding or executing the get signed url page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ async function getSignedUrl(fileId) { const key = String(fileId || '') if (!key) { return '' } const cached = urlCache[key] if (cached && cached.expireAt > Date.now()) { return cached.value || '' } const value = await fileApi.getSignedUrl(key) urlCache[key] = { value: value || '', expireAt: Date.now() + (CACHE_TTL.signedUrl || 50 * 60 * 1000) } return urlCache[key].value } /** * Purpose: extractLegacyFileId supports file.js by holding or executing the extract legacy file id page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function extractLegacyFileId(value) { const match = String(value || '') .replace(/<[^>]*>/g, '') .match(/\/api\/file\/get\/(\d+)/) return match && match[1] ? match[1] : '' } /** * Purpose: resolveImageList supports file.js by holding or executing the resolve image list page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ async function resolveImageList(fileIds, fallbackImage) { const ids = Array.isArray(fileIds) ? fileIds : splitCsv(fileIds) if (!ids.length) { return [fallbackImage || DEFAULT_PRODUCT_IMAGE] } const urls = (await Promise.all(ids.map(async fileId => { try { return await getSignedUrl(fileId) } catch (error) { return '' } }))).filter(Boolean) return urls.length ? urls : [fallbackImage || DEFAULT_PRODUCT_IMAGE] } /** * Purpose: resolveLegacyImage supports file.js by holding or executing the resolve legacy image page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ async function resolveLegacyImage(value, fallbackImage) { const fileId = extractLegacyFileId(value) if (!fileId) { return fallbackImage || DEFAULT_PRODUCT_IMAGE } try { const url = await getSignedUrl(fileId) return url || fallbackImage || DEFAULT_PRODUCT_IMAGE } catch (error) { return fallbackImage || DEFAULT_PRODUCT_IMAGE } } /** * Purpose: previewImagesSafely opens wx.previewImage with a user-facing fallback when signed URLs expire or preview fails. * Usage: pass the current image URL and full URL list from image tap handlers instead of calling wx.previewImage directly. */ function previewImagesSafely(current, urls) { const list = (Array.isArray(urls) ? urls : [current]).filter(Boolean) if (!current || !list.length) { wx.showToast({ title: '图片暂不可预览', icon: 'none' }) return } wx.previewImage({ current, urls: list, fail: () => { wx.showToast({ title: '图片预览失败,请稍后重试', icon: 'none' }) } }) } module.exports = { getSignedUrl, extractLegacyFileId, resolveImageList, resolveLegacyImage, previewImagesSafely, /** * Purpose: clearSignedUrlCache supports file.js by holding or executing the clear signed url cache page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ clearSignedUrlCache(fileId) { if (fileId === undefined || fileId === null || fileId === '') { Object.keys(urlCache).forEach(key => delete urlCache[key]) return } delete urlCache[String(fileId)] } }