update at 2026-02-09 16:09:44
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
const { request, downloadFile, readFile } = require('./wx-promisify')
|
||||
|
||||
const localFonts = require('../../assets/fonts')
|
||||
const localDefaultConfig = require('../../assets/default')
|
||||
|
||||
const fontBufferCache = new Map()
|
||||
const MAX_FONT_CACHE = 4
|
||||
@@ -44,6 +45,70 @@ function normalizeManifest(fonts, baseUrl) {
|
||||
.filter((item) => item.url)
|
||||
}
|
||||
|
||||
function normalizeDefaultConfig(config) {
|
||||
const payload = config && typeof config === 'object' ? config : {}
|
||||
const selectedRaw = Array.isArray(payload.selectedFontIds)
|
||||
? payload.selectedFontIds
|
||||
: (Array.isArray(payload.selectedFonts) ? payload.selectedFonts : [])
|
||||
const favoriteRaw = Array.isArray(payload.favoriteFontIds)
|
||||
? payload.favoriteFontIds
|
||||
: (Array.isArray(payload.favoriteFonts) ? payload.favoriteFonts : [])
|
||||
|
||||
const selectedFontIds = Array.from(
|
||||
new Set(
|
||||
selectedRaw
|
||||
.map((item) => String(item || '').trim())
|
||||
.filter(Boolean),
|
||||
),
|
||||
)
|
||||
const favoriteFontIds = Array.from(
|
||||
new Set(
|
||||
favoriteRaw
|
||||
.map((item) => String(item || '').trim())
|
||||
.filter(Boolean),
|
||||
),
|
||||
)
|
||||
|
||||
const result = {
|
||||
selectedFontIds,
|
||||
favoriteFontIds,
|
||||
}
|
||||
|
||||
if (typeof payload.inputText === 'string') {
|
||||
result.inputText = payload.inputText
|
||||
}
|
||||
if (typeof payload.textColor === 'string') {
|
||||
result.textColor = payload.textColor
|
||||
}
|
||||
if (payload.fontSize !== undefined && payload.fontSize !== null && payload.fontSize !== '') {
|
||||
const fontSize = Number(payload.fontSize)
|
||||
if (Number.isFinite(fontSize)) {
|
||||
result.fontSize = fontSize
|
||||
}
|
||||
}
|
||||
if (payload.letterSpacing !== undefined && payload.letterSpacing !== null && payload.letterSpacing !== '') {
|
||||
const letterSpacing = Number(payload.letterSpacing)
|
||||
if (Number.isFinite(letterSpacing)) {
|
||||
result.letterSpacing = letterSpacing
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
function buildDefaultConfigUrl(manifestUrl, baseUrl) {
|
||||
const manifest = String(manifestUrl || '').trim()
|
||||
if (manifest) {
|
||||
const withoutHash = manifest.split('#')[0]
|
||||
const [pathPart] = withoutHash.split('?')
|
||||
const slashIndex = pathPart.lastIndexOf('/')
|
||||
if (slashIndex >= 0) {
|
||||
return `${pathPart.slice(0, slashIndex + 1)}default.json`
|
||||
}
|
||||
}
|
||||
return normalizePath('/miniprogram/assets/default.json', baseUrl)
|
||||
}
|
||||
|
||||
async function loadFontsManifest(options = {}) {
|
||||
const app = getApp()
|
||||
const manifestUrl = options.manifestUrl || app.globalData.fontsManifestUrl
|
||||
@@ -79,6 +144,40 @@ async function loadFontsManifest(options = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
async function loadDefaultConfig(options = {}) {
|
||||
const app = getApp()
|
||||
const manifestUrl = options.manifestUrl || app.globalData.fontsManifestUrl
|
||||
const baseUrl = options.baseUrl || app.globalData.fontsBaseUrl
|
||||
const defaultConfigUrl = options.defaultConfigUrl ||
|
||||
app.globalData.defaultConfigUrl ||
|
||||
buildDefaultConfigUrl(manifestUrl, baseUrl)
|
||||
|
||||
if (app.globalData.defaultConfig && typeof app.globalData.defaultConfig === 'object') {
|
||||
return app.globalData.defaultConfig
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await request({
|
||||
url: defaultConfigUrl,
|
||||
method: 'GET',
|
||||
timeout: 10000,
|
||||
})
|
||||
|
||||
if (response.statusCode < 200 || response.statusCode >= 300) {
|
||||
throw new Error(`获取默认配置失败,状态码: ${response.statusCode}`)
|
||||
}
|
||||
|
||||
const remoteConfig = normalizeDefaultConfig(response.data)
|
||||
app.globalData.defaultConfig = remoteConfig
|
||||
return remoteConfig
|
||||
} catch (error) {
|
||||
console.warn('远程 default.json 加载失败,回退到本地默认配置:', error)
|
||||
const fallbackConfig = normalizeDefaultConfig(localDefaultConfig)
|
||||
app.globalData.defaultConfig = fallbackConfig
|
||||
return fallbackConfig
|
||||
}
|
||||
}
|
||||
|
||||
function setLruCache(key, value) {
|
||||
if (fontBufferCache.has(key)) {
|
||||
fontBufferCache.delete(key)
|
||||
@@ -125,6 +224,7 @@ function listCategories(fonts) {
|
||||
|
||||
module.exports = {
|
||||
loadFontsManifest,
|
||||
loadDefaultConfig,
|
||||
loadFontBuffer,
|
||||
listCategories,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user