165 lines
5.5 KiB
JavaScript
165 lines
5.5 KiB
JavaScript
/**
|
|
* 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')
|
|
const { showRecoveryModal } = require('./recovery')
|
|
|
|
function handlePreviewRecoveryAction(action) {
|
|
if (!action || action.key !== 'products') {
|
|
return
|
|
}
|
|
wx.switchTab({
|
|
url: '/pages/products/index',
|
|
fail() {}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 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, options) {
|
|
const config = options || {}
|
|
const list = (Array.isArray(urls) ? urls : [current]).filter(Boolean)
|
|
if (!current || !list.length) {
|
|
return showRecoveryModal({
|
|
title: '图片暂不可预览',
|
|
message: config.emptyMessage || '没有可预览的图片,请返回作品列表查看其他款式。',
|
|
actions: [
|
|
{ key: 'dismiss', text: '知道了' },
|
|
{ key: 'products', text: '看作品' }
|
|
]
|
|
}).then(action => {
|
|
handlePreviewRecoveryAction(action)
|
|
return action
|
|
})
|
|
}
|
|
return new Promise(resolve => {
|
|
wx.previewImage({
|
|
current,
|
|
urls: list,
|
|
success: () => resolve(null),
|
|
fail: () => {
|
|
showRecoveryModal({
|
|
title: '图片预览失败',
|
|
message: config.failMessage || '图片链接可能已过期,请重试;仍失败时可以返回作品列表重新进入。',
|
|
actions: [
|
|
{ key: 'retry', text: '重试' },
|
|
{ key: 'products', text: '看作品' }
|
|
]
|
|
}).then(action => {
|
|
if (action && action.key === 'retry') {
|
|
previewImagesSafely(current, list, config).then(resolve)
|
|
return
|
|
}
|
|
handlePreviewRecoveryAction(action)
|
|
resolve(action)
|
|
})
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
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)]
|
|
}
|
|
}
|