功能:收口小程序前后台安全与防重复提交

This commit is contained in:
xiaowang
2026-04-20 16:02:57 +08:00
parent eb1c209d79
commit a3df262fae
237 changed files with 14068 additions and 728 deletions
+24 -13
View File
@@ -1,5 +1,6 @@
const fileApi = require('../api/file')
const { DEFAULT_PRODUCT_IMAGE } = require('./config')
const { CACHE_TTL } = require('./config')
const { splitCsv } = require('./format')
const urlCache = {}
@@ -10,11 +11,17 @@ async function getSignedUrl(fileId) {
return ''
}
if (!urlCache[key]) {
urlCache[key] = await fileApi.getSignedUrl(key)
const cached = urlCache[key]
if (cached && cached.expireAt > Date.now()) {
return cached.value || ''
}
return urlCache[key] || ''
const value = await fileApi.getSignedUrl(key)
urlCache[key] = {
value: value || '',
expireAt: Date.now() + (CACHE_TTL.signedUrl || 50 * 60 * 1000)
}
return urlCache[key].value
}
function extractLegacyFileId(value) {
@@ -31,16 +38,13 @@ async function resolveImageList(fileIds, fallbackImage) {
return [fallbackImage || DEFAULT_PRODUCT_IMAGE]
}
const urls = []
for (let index = 0; index < ids.length; index += 1) {
const fileId = ids[index]
const urls = (await Promise.all(ids.map(async fileId => {
try {
const url = await getSignedUrl(fileId)
if (url) {
urls.push(url)
}
} catch (error) {}
}
return await getSignedUrl(fileId)
} catch (error) {
return ''
}
}))).filter(Boolean)
return urls.length ? urls : [fallbackImage || DEFAULT_PRODUCT_IMAGE]
}
@@ -63,5 +67,12 @@ module.exports = {
getSignedUrl,
extractLegacyFileId,
resolveImageList,
resolveLegacyImage
resolveLegacyImage,
clearSignedUrlCache(fileId) {
if (fileId === undefined || fileId === null || fileId === '') {
Object.keys(urlCache).forEach(key => delete urlCache[key])
return
}
delete urlCache[String(fileId)]
}
}