Files
JayuCloud/WeChatProgram/utils/file.js
T

133 lines
4.4 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')
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) {
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'
})
}
})
}
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
}