Files
JayuCloud/JC-ArtList-WeChat/utils/file.js
T

165 lines
5.5 KiB
JavaScript
Raw Normal View History

2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
const fileApi = require('../api/file')
const { DEFAULT_PRODUCT_IMAGE } = require('./config')
const { CACHE_TTL } = require('./config')
2026-04-01 15:18:35 +08:00
const { splitCsv } = require('./format')
const { showRecoveryModal } = require('./recovery')
function handlePreviewRecoveryAction(action) {
if (!action || action.key !== 'products') {
return
}
wx.switchTab({
url: '/pages/products/index',
fail() {}
})
}
2026-04-01 15:18:35 +08:00
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
const urlCache = {}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
async function getSignedUrl(fileId) {
const key = String(fileId || '')
if (!key) {
return ''
}
const cached = urlCache[key]
if (cached && cached.expireAt > Date.now()) {
return cached.value || ''
2026-04-01 15:18:35 +08:00
}
const value = await fileApi.getSignedUrl(key)
urlCache[key] = {
value: value || '',
expireAt: Date.now() + (CACHE_TTL.signedUrl || 50 * 60 * 1000)
}
return urlCache[key].value
2026-04-01 15:18:35 +08:00
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
function extractLegacyFileId(value) {
const match = String(value || '')
.replace(/<[^>]*>/g, '')
.match(/\/api\/file\/get\/(\d+)/)
return match && match[1] ? match[1] : ''
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
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 => {
2026-04-01 15:18:35 +08:00
try {
return await getSignedUrl(fileId)
} catch (error) {
return ''
}
}))).filter(Boolean)
2026-04-01 15:18:35 +08:00
return urls.length ? urls : [fallbackImage || DEFAULT_PRODUCT_IMAGE]
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
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
}
}
2026-05-23 18:45:41 +08:00
/**
* 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 || {}
2026-05-23 18:45:41 +08:00
const list = (Array.isArray(urls) ? urls : [current]).filter(Boolean)
if (!current || !list.length) {
return showRecoveryModal({
2026-05-23 18:45:41 +08:00
title: '图片暂不可预览',
message: config.emptyMessage || '没有可预览的图片,请返回作品列表查看其他款式。',
actions: [
{ key: 'dismiss', text: '知道了' },
{ key: 'products', text: '看作品' }
]
}).then(action => {
handlePreviewRecoveryAction(action)
return action
2026-05-23 18:45:41 +08:00
})
}
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)
})
}
})
2026-05-23 18:45:41 +08:00
})
}
2026-04-01 15:18:35 +08:00
module.exports = {
getSignedUrl,
extractLegacyFileId,
resolveImageList,
resolveLegacyImage,
2026-05-23 18:45:41 +08:00
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)]
}
2026-04-01 15:18:35 +08:00
}