update at 2026-02-08 22:31:25

This commit is contained in:
douboer
2026-02-08 22:31:25 +08:00
parent 0f5a7f0d85
commit f078dd3261
39 changed files with 587 additions and 213 deletions

View File

@@ -25,6 +25,17 @@ async function saveSvgToUserPath(svgText, fontName, text) {
return writeTextToUserPath(svgText, 'svg', filename)
}
function saveSvgToUserPathSync(svgText, fontName, text) {
const filename = buildFilename(fontName, text, 'svg')
const filePath = `${wx.env.USER_DATA_PATH}/${filename}`
const fs = wx.getFileSystemManager()
fs.writeFileSync(filePath, svgText, 'utf8')
return {
filePath,
fileName: filename,
}
}
async function shareLocalFile(filePath, fileName) {
if (typeof wx.shareFileMessage !== 'function') {
throw new Error('当前微信版本不支持文件分享')
@@ -40,10 +51,29 @@ async function shareLocalFile(filePath, fileName) {
})
}
function shareSvgFromUserTap(svgText, fontName, text) {
if (typeof wx.shareFileMessage !== 'function') {
throw new Error('当前微信版本不支持文件分享')
}
const { filePath, fileName } = saveSvgToUserPathSync(svgText, fontName, text)
return new Promise((resolve, reject) => {
wx.shareFileMessage({
filePath,
fileName,
success: resolve,
fail: reject,
})
})
}
module.exports = {
sanitizeFilename,
buildFilename,
writeTextToUserPath,
saveSvgToUserPath,
saveSvgToUserPathSync,
shareLocalFile,
shareSvgFromUserTap,
}

View File

@@ -27,6 +27,24 @@ function normalizeResult(data) {
}
}
function decodeArrayBuffer(buffer) {
try {
if (!buffer) return ''
if (typeof buffer === 'string') return buffer
if (typeof TextDecoder === 'function') {
return new TextDecoder('utf-8').decode(buffer)
}
const bytes = new Uint8Array(buffer)
let text = ''
for (let i = 0; i < bytes.length; i += 1) {
text += String.fromCharCode(bytes[i])
}
return decodeURIComponent(escape(text))
} catch (error) {
return ''
}
}
async function renderSvgByApi(payload) {
const app = getApp()
const timeout = app && app.globalData && app.globalData.apiTimeoutMs
@@ -62,6 +80,52 @@ async function renderSvgByApi(payload) {
return normalizeResult(body.data)
}
async function renderPngByApi(payload) {
const app = getApp()
const timeout = app && app.globalData && app.globalData.apiTimeoutMs
? Number(app.globalData.apiTimeoutMs)
: 30000
const baseApiUrl = buildApiUrl()
const apiUrl = /\/api\/render-svg$/.test(baseApiUrl)
? baseApiUrl.replace(/\/api\/render-svg$/, '/api/render-png')
: `${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,
},
})
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)
}
if (!(response.data instanceof ArrayBuffer)) {
throw new Error('PNG 渲染服务返回格式无效')
}
return response.data
}
module.exports = {
renderSvgByApi,
renderPngByApi,
}