文件
fenleixinxi/uniapp/tools/gen-icons.mjs
linda 376a7ed793 feat(uniapp): 小程序端(用户端 uni-app)源码入库到 uniapp/ 目录
需求:小程序端代码要同时同步到服务器与 git 远程仓库,此前前端仅存在于本地目录(无 remote,从未推送),
服务器与 gitcode 上只有后端。现并入本仓库,复用现有「push 即部署」链路,一次推送双端落地。

- 新增 uniapp/:小程序端源码全量快照(150 文件 / 1.2M)
  pages(13 页面) · components(fc-navbar/fc-tabbar/fc-post-card/fc-empty) ·
  common(api·utils·mock·assets·store·config) · static(74 图标 + 3 轮播插画 + LOGO) ·
  docs(01-04 交付文档) · tests(零依赖全链路用例) · tools(图标/主题/轮播配图生成脚本)
- 明确排除:AGENTS.md(含后台密码、服务器 root 密码、小程序 AppSecret,切勿入库)、
  unpackage/(构建产物,由 uniapp/.gitignore 忽略)、node_modules、.DS_Store、*.log
- .gitignore 新增 /uniapp/AGENTS.md 兜底(与根 /AGENTS.md、/fluniapp/ 同一约定)
- README.md 增补「本项目结构 + 部署链路」章节,说明 uniapp/ 为源码快照、
  小程序真正上线仍需微信开发者工具 cli upload

说明:本次为源码快照,未携带前端本地仓库历史——其历史提交中含 AGENTS.md 凭据,
若做 subtree 合并会把密码永久写入本仓库历史,故采用快照方式。
2026-10-17 14:19:03 +08:00

137 行
6.0 KiB
JavaScript

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

/**
* 配图资源生成器 v3
* ------------------------------------------------------------
* 1. 每个图标用同一份 SVG 渲染「白底」「黑底」两次
* 2. 通过两式联立解出真实 alpha(见 tools/png-util.mjs)→ 得到精确抗锯齿的透明 PNG
* 3. 徽章类图形使用「垂直渐变圆角底」,提升质感与统一性
* 运行: node tools/gen-icons.mjs
*/
import fs from 'node:fs'
import path from 'node:path'
import os from 'node:os'
import { execFileSync } from 'node:child_process'
import url from 'node:url'
import { ART, OUTPUTS } from './icon-svg.mjs'
import { decodePng, encodePng, extractAlpha, transparentRatio, contentBox, crop, downscale } from './png-util.mjs'
const ROOT = path.resolve(path.dirname(url.fileURLToPath(import.meta.url)), '..')
const OUT_DIR = path.join(ROOT, 'static', 'icons')
const SIZE = 384 // 渲染分辨率
const TARGET = 192 // 最终输出边长(2 倍降采样,锐度与体积兼顾)
const BADGE_SCALE = 0.66
const BADGE_OFFSET = (96 - 96 * BADGE_SCALE) / 2
const TMP = fs.mkdtempSync(path.join(os.tmpdir(), 'fc-illus-'))
const SRC = path.join(TMP, 'src')
const RST = path.join(TMP, 'out')
fs.mkdirSync(SRC, { recursive: true })
fs.mkdirSync(RST, { recursive: true })
fs.mkdirSync(OUT_DIR, { recursive: true })
/** 调色板替换 */
function paint(art, item) {
const p = { c1: item.c1, c2: item.c2 || item.c1, c3: item.c3 || '#FFFFFF' }
return art.replace(/\{(c1|c2|c3)\}/g, (_, k) => p[k])
}
/** 生成一版 SVG:backdrop 用于两次渲染(白 / 黑) */
function buildSvg(item, backdrop) {
const body = paint(ART[item.art], item)
const head = `<svg xmlns="http://www.w3.org/2000/svg" width="${SIZE}" height="${SIZE}" viewBox="0 0 96 96">`
// 底色层:徽章用渐变圆角块,其余用纯色 backdrop 便于抠出 alpha
let layer
if (item.bg) {
layer =
`<defs><linearGradient id="g" x1="0" y1="0" x2="0.35" y2="1">` +
`<stop offset="0" stop-color="${item.bg[0]}"/><stop offset="1" stop-color="${item.bg[1]}"/>` +
`</linearGradient></defs>` +
`<rect x="0" y="0" width="96" height="96" rx="30" fill="url(#g)"/>`
} else {
layer = ''
}
const base = `<rect x="0" y="0" width="96" height="96" fill="${backdrop}"/>`
// 徽章类:插画缩到 66% 居中,四周留出圆角底
// 透明类:插画铺满画布,保证在 CSS 给定尺寸下的视觉重量
const inner = item.bg
? `<g transform="translate(${BADGE_OFFSET.toFixed(2)},${BADGE_OFFSET.toFixed(2)}) scale(${BADGE_SCALE})">${body}</g>`
: body
return head + base + layer + inner + '</svg>'
}
/** 品牌 LOGO */
const LOGO_SVG = `<svg xmlns="http://www.w3.org/2000/svg" width="512" height="512" viewBox="0 0 512 512">
<defs><linearGradient id="g" x1="0" y1="0" x2="0.4" y2="1">
<stop offset="0" stop-color="#00E070"/><stop offset="0.5" stop-color="#00A855"/><stop offset="1" stop-color="#046B3B"/>
</linearGradient></defs>
<rect width="512" height="512" rx="112" fill="url(#g)"/>
<path d="M256 404C340 330 370 272 370 222A114 114 0 1 0 142 222C142 272 172 330 256 404Z" fill="#ffffff"/>
<g fill="#F5A623">
<rect x="188" y="178" width="136" height="22" rx="11"/>
<rect x="188" y="226" width="96" height="22" rx="11"/>
<rect x="188" y="274" width="120" height="22" rx="11"/>
</g></svg>`
// 1. 生成两版 SVG
const names = []
for (const item of OUTPUTS) {
const base = item.file.replace(/\.png$/, '')
fs.writeFileSync(path.join(SRC, `${base}__w.svg`), buildSvg(item, '#FFFFFF'), 'utf8')
fs.writeFileSync(path.join(SRC, `${base}__b.svg`), buildSvg(item, '#000000'), 'utf8')
names.push(base)
}
fs.writeFileSync(path.join(SRC, 'logo__w.svg'), LOGO_SVG.replace('<path d="M256', '<path d="M256'), 'utf8')
// 2. 批量渲染
function render(list, size) {
for (let i = 0; i < list.length; i += 12) {
const chunk = list.slice(i, i + 12).map((n) => path.join(SRC, n))
try {
execFileSync('qlmanage', ['-t', '-s', String(size), '-o', RST, ...chunk], { stdio: 'ignore' })
} catch (e) {
/* 忽略,后续按产物校验 */
}
}
}
render(names.map((n) => `${n}__w.svg`).concat(names.map((n) => `${n}__b.svg`)), SIZE)
render(['logo__w.svg'], 512)
// 3. 两式联立 -> 透明 PNG
let ok = 0
const missing = []
const lowAlpha = []
const sizes = new Set()
for (const n of names) {
const fw = path.join(RST, `${n}__w.svg.png`)
const fb = path.join(RST, `${n}__b.svg.png`)
if (!fs.existsSync(fw) || !fs.existsSync(fb)) { missing.push(n); continue }
const black = decodePng(fs.readFileSync(fb))
// qlmanage 会把内容渲染到子矩形并补白衬,先用黑底图定位真实内容区
const box = contentBox(black)
let img = extractAlpha(crop(decodePng(fs.readFileSync(fw)), box), crop(black, box))
// 整数倍降采样到目标尺寸
const factor = Math.max(1, Math.floor(img.width / TARGET))
img = downscale(img, factor)
const ratio = transparentRatio(img)
sizes.add(`${img.width}x${img.height}`)
// 非徽章类图标应当有明显透明区域(徽章类自带圆角底,透明仅出现在四角)
const item = OUTPUTS.find((o) => o.file === `${n}.png`)
if (!item.bg && ratio < 0.1) lowAlpha.push(`${n}(${ratio.toFixed(2)})`)
fs.writeFileSync(path.join(OUT_DIR, `${n}.png`), encodePng(img))
ok++
}
// 4. LOGO(白底渲染 -> 裁剪 -> 直接使用,自身不透明)
const logoW = path.join(RST, 'logo__w.svg.png')
if (fs.existsSync(logoW)) {
const limg = decodePng(fs.readFileSync(logoW))
fs.writeFileSync(path.join(ROOT, 'static', 'logo.png'), encodePng(crop(limg, contentBox(limg))))
}
const legacy = path.join(ROOT, 'static', 'logo-512.png')
if (fs.existsSync(legacy)) fs.rmSync(legacy, { force: true })
fs.rmSync(TMP, { recursive: true, force: true })
console.log(`✅ 配图生成完成:${ok}/${names.length} 个 → static/icons/(${[...sizes].join(', ')},真实透明)`)
if (missing.length) console.log(`⚠️ 未生成:${missing.join(', ')}`)
if (lowAlpha.length) console.log(`⚠️ 透明区域异常:${lowAlpha.join(', ')}`)
console.log('✅ LOGO 生成:static/logo.png')