update at 2026-02-10 14:10:20

This commit is contained in:
douboer
2026-02-10 14:10:20 +08:00
parent b6742cb13a
commit b43155dd0f
9 changed files with 579 additions and 58 deletions

View File

@@ -1,4 +1,5 @@
const { request } = require('./wx-promisify')
const { ensureRouteReady, checkRouteOnFailure } = require('./route-manager')
function buildApiUrl() {
const app = getApp()
@@ -47,41 +48,50 @@ function decodeArrayBuffer(buffer) {
async function renderSvgByApi(payload) {
const app = getApp()
await ensureRouteReady(app)
const timeout = app && app.globalData && app.globalData.apiTimeoutMs
? Number(app.globalData.apiTimeoutMs)
: 30000
const response = await request({
url: buildApiUrl(),
method: 'POST',
timeout,
header: {
'content-type': 'application/json',
},
data: {
fontId: payload.fontId,
text: payload.text,
fontSize: payload.fontSize,
fillColor: payload.fillColor,
letterSpacing: payload.letterSpacing,
maxCharsPerLine: payload.maxCharsPerLine,
},
})
try {
const response = await request({
url: buildApiUrl(),
method: 'POST',
timeout,
header: {
'content-type': 'application/json',
},
data: {
fontId: payload.fontId,
text: payload.text,
fontSize: payload.fontSize,
fillColor: payload.fillColor,
letterSpacing: payload.letterSpacing,
maxCharsPerLine: payload.maxCharsPerLine,
},
})
if (!response || response.statusCode < 200 || response.statusCode >= 300) {
throw new Error(`渲染服务请求失败,状态码: ${response && response.statusCode}`)
if (!response || response.statusCode < 200 || response.statusCode >= 300) {
throw new Error(`渲染服务请求失败,状态码: ${response && response.statusCode}`)
}
const body = response.data || {}
if (!body.ok) {
throw new Error(body.error || '渲染服务返回错误')
}
return normalizeResult(body.data)
} catch (error) {
await checkRouteOnFailure(app).catch((routeError) => {
console.warn('SVG 渲染失败后的路由检查失败:', routeError)
})
throw error
}
const body = response.data || {}
if (!body.ok) {
throw new Error(body.error || '渲染服务返回错误')
}
return normalizeResult(body.data)
}
async function renderPngByApi(payload) {
const app = getApp()
await ensureRouteReady(app)
const timeout = app && app.globalData && app.globalData.apiTimeoutMs
? Number(app.globalData.apiTimeoutMs)
: 30000
@@ -93,39 +103,46 @@ async function renderPngByApi(payload) {
: `${baseApiUrl.replace(/\/$/, '')}/render-png`
)
const response = await request({
url: apiUrl,
method: 'POST',
timeout,
responseType: 'arraybuffer',
header: {
'content-type': 'application/json',
accept: 'image/png',
},
data: {
fontId: payload.fontId,
text: payload.text,
fontSize: payload.fontSize,
fillColor: payload.fillColor,
letterSpacing: payload.letterSpacing,
maxCharsPerLine: payload.maxCharsPerLine,
},
})
try {
const response = await request({
url: apiUrl,
method: 'POST',
timeout,
responseType: 'arraybuffer',
header: {
'content-type': 'application/json',
accept: 'image/png',
},
data: {
fontId: payload.fontId,
text: payload.text,
fontSize: payload.fontSize,
fillColor: payload.fillColor,
letterSpacing: payload.letterSpacing,
maxCharsPerLine: payload.maxCharsPerLine,
},
})
if (!response || response.statusCode !== 200) {
let message = `PNG 渲染服务请求失败,状态码: ${response && response.statusCode}`
const maybeText = decodeArrayBuffer(response && response.data)
if (maybeText && maybeText.includes('error')) {
message = maybeText
if (!response || response.statusCode !== 200) {
let message = `PNG 渲染服务请求失败,状态码: ${response && response.statusCode}`
const maybeText = decodeArrayBuffer(response && response.data)
if (maybeText && maybeText.includes('error')) {
message = maybeText
}
throw new Error(message)
}
throw new Error(message)
}
if (!(response.data instanceof ArrayBuffer)) {
throw new Error('PNG 渲染服务返回格式无效')
}
if (!(response.data instanceof ArrayBuffer)) {
throw new Error('PNG 渲染服务返回格式无效')
}
return response.data
return response.data
} catch (error) {
await checkRouteOnFailure(app).catch((routeError) => {
console.warn('PNG 渲染失败后的路由检查失败:', routeError)
})
throw error
}
}
module.exports = {