diff --git a/.npmrc b/.npmrc index 7041058..0974ada 100644 --- a/.npmrc +++ b/.npmrc @@ -1,2 +1,5 @@ # 使用项目内 store,避免本机全局 store 目录(D:\.pnpm-store)的写权限问题 store-dir=.pnpm-store +# 本项目在沙箱环境执行命令时,符号链接会被重定向到虚拟路径并失效, +# 因此改用 hoisted 扁平结构,避免 junction 依赖。 +node-linker=hoisted diff --git a/astro.config.mjs b/astro.config.mjs index 8b2ee10..bc6a0da 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -6,8 +6,8 @@ import tailwindcss from '@tailwindcss/vite'; // https://astro.build/config export default defineConfig({ - // TODO: 上线时改成正式域名,影响 sitemap / canonical / OG - site: 'http://localhost:4321', + // 正式站点地址,影响 canonical / sitemap / OG + site: 'https://specs.atie.fun', output: 'server', adapter: node({ mode: 'standalone' }), integrations: [react()], diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2fd245c..3389aae 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -37,7 +37,7 @@ importers: version: 6.2.14 lucide-react: specifier: ^1.64.0 - version: 1.64.0(@types/react@19.3.0)(react@19.3.1) + version: 1.65.0(@types/react@19.3.0)(react@19.3.1) motion: specifier: ^14.8.0 version: 14.8.0(react-dom@19.3.1(react@19.3.1))(react@19.3.1) @@ -1356,8 +1356,8 @@ packages: resolution: {integrity: sha512-U4N8FgzmWxc8k1VH8Kr6lQg18U7Fjvby6wXHVRX/ZZ7IwWbRMgrRbP0Wrb5q5NVinryp4SQampHKdvtecItxUg==} engines: {node: 20 || >=22} - lucide-react@1.64.0: - resolution: {integrity: sha512-v626F/bcLNvFom6vzztHILtG9tUgXXbgA5AGkd82YkV401+Ko/NHyrWafbPwGofa+6p/EgDJcvEkIwsMH9098w==} + lucide-react@1.65.0: + resolution: {integrity: sha512-roSdtzudEY39i2hn5LLjA8toYfhF2z4S10WSqZF4ubXpHlOmNTvaxMxZLdJfQDi6pEifQWX+cRmYihKSsvAxfA==} peerDependencies: '@types/react': '*' react: ^16.5.1 || ^17.0.0 || ^18.0.0 || ^19.0.0 @@ -3031,7 +3031,7 @@ snapshots: lru-cache@11.5.3: {} - lucide-react@1.64.0(@types/react@19.3.0)(react@19.3.1): + lucide-react@1.65.0(@types/react@19.3.0)(react@19.3.1): dependencies: react: 19.3.1 optionalDependencies: diff --git a/public/robots.txt b/public/robots.txt new file mode 100644 index 0000000..ac03e15 --- /dev/null +++ b/public/robots.txt @@ -0,0 +1,6 @@ +User-agent: * +Allow: / +Disallow: /admin +Disallow: /api + +Sitemap: https://specs.atie.fun/sitemap.xml diff --git a/src/layouts/BaseLayout.astro b/src/layouts/BaseLayout.astro index 7c40905..4cf0af4 100644 --- a/src/layouts/BaseLayout.astro +++ b/src/layouts/BaseLayout.astro @@ -29,6 +29,7 @@ const { diff --git a/src/lib/repo.ts b/src/lib/repo.ts index 933d2e7..a785009 100644 --- a/src/lib/repo.ts +++ b/src/lib/repo.ts @@ -53,16 +53,61 @@ function rowToChip(row: Row): Chip { /* ------------------------------- devices ------------------------------- */ -export function listDevices(options: { category?: DeviceCategory } = {}): Device[] { +export type DeviceSort = 'latest' | 'name' | 'brand'; + +export interface DeviceQuery { + category?: DeviceCategory; + brand?: string; + q?: string; + sort?: DeviceSort; +} + +/** 列表查询:分类 / 品牌 / 关键词 / 排序,全部走参数化 SQL */ +export function listDevices(query: DeviceQuery = {}): Device[] { const db = getDb(); - const rows = options.category + const where: string[] = []; + const params: string[] = []; + + if (query.category) { + where.push('category = ?'); + params.push(query.category); + } + if (query.brand) { + where.push('brand = ?'); + params.push(query.brand); + } + if (query.q) { + where.push('(brand LIKE ? OR model LIKE ? OR aliases_json LIKE ? OR summary LIKE ?)'); + const like = `%${query.q}%`; + params.push(like, like, like, like); + } + + // 排序字段白名单,避免拼接外部输入 + const orderBy = + query.sort === 'name' + ? 'model ASC' + : query.sort === 'brand' + ? 'brand ASC, model ASC' + : "COALESCE(release_date, announce_date, '') DESC, brand, model"; + + const sql = `SELECT * FROM devices ${ + where.length > 0 ? `WHERE ${where.join(' AND ')}` : '' + } ORDER BY ${orderBy}`; + + const rows = db.prepare(sql).all(...params); + return (rows as Row[]).map(rowToDevice); +} + +export function listBrands(category?: DeviceCategory): { brand: string; count: number }[] { + const db = getDb(); + const rows = category ? db .prepare( - 'SELECT * FROM devices WHERE category = ? ORDER BY release_date DESC, brand, model', + 'SELECT brand, COUNT(*) AS count FROM devices WHERE category = ? GROUP BY brand ORDER BY brand', ) - .all(options.category) - : db.prepare('SELECT * FROM devices ORDER BY release_date DESC, brand, model').all(); - return (rows as Row[]).map(rowToDevice); + .all(category) + : db.prepare('SELECT brand, COUNT(*) AS count FROM devices GROUP BY brand ORDER BY brand').all(); + return rows as { brand: string; count: number }[]; } export function getDevice(slug: string): Device | undefined { diff --git a/src/pages/brands/[brand].astro b/src/pages/brands/[brand].astro new file mode 100644 index 0000000..dfbf344 --- /dev/null +++ b/src/pages/brands/[brand].astro @@ -0,0 +1,49 @@ +--- +import BaseLayout from '@/layouts/BaseLayout.astro'; +import { listBrands, listDevices } from '@/lib/repo'; + +const brand = decodeURIComponent(Astro.params.brand ?? ''); +const devices = listDevices({ brand }); +const brands = listBrands(); +const totalOfBrand = brands.find((item) => item.brand === brand)?.count ?? devices.length; + +if (devices.length === 0) { + return new Response('未找到该品牌', { status: 404 }); +} +--- + + + + +
+

{brand}

+ {totalOfBrand} 款 +
+ +
+ { + devices.map((device) => ( + +
{device.model}
+
+
SoC
+
{device.specs.platform?.chipset ?? '—'}
+
发布日期
+
{device.releaseDate ?? device.announceDate ?? '—'}
+
屏幕
+
{device.specs.display?.size ?? '—'}
+
电池
+
{device.specs.battery?.capacity ?? '—'}
+
+
+ )) + } +
+
diff --git a/src/pages/brands/index.astro b/src/pages/brands/index.astro new file mode 100644 index 0000000..dd4c0bc --- /dev/null +++ b/src/pages/brands/index.astro @@ -0,0 +1,36 @@ +--- +import BaseLayout from '@/layouts/BaseLayout.astro'; +import { listBrands } from '@/lib/repo'; + +const brands = listBrands(); +const total = brands.reduce((sum, item) => sum + item.count, 0); +--- + + +
+

品牌

+ {brands.length} 个品牌 · {total} 款设备 +
+ +
+ { + brands.map((item) => ( + + {item.brand} + {item.count} 款 + + )) + } +
+ + { + brands.length === 0 && ( +

+ 暂无品牌数据。 +

+ ) + } +
diff --git a/src/pages/devices/[slug].astro b/src/pages/devices/[slug].astro index e06c5ee..9508a2e 100644 --- a/src/pages/devices/[slug].astro +++ b/src/pages/devices/[slug].astro @@ -137,6 +137,19 @@ const cameras = [ { title: '后置影像', list: s.rearCamera }, { title: '前置影像', list: s.frontCamera }, ].filter((c) => c.list.length > 0); + +const lifecycleText = + device.lifecycle === 'discontinued' ? '已停产' : device.lifecycle === 'announced' ? '已发布' : '在售'; + +const productLd = { + '@context': 'https://schema.org', + '@type': 'Product', + name: `${device.brand} ${device.model}`, + brand: { '@type': 'Brand', name: device.brand }, + model: device.model, + ...(device.summary ? { description: device.summary } : {}), + ...(device.releaseDate ? { releaseDate: device.releaseDate } : {}), +}; --- @@ -147,11 +160,27 @@ const cameras = [
-

{device.brand}

+

+ + {device.brand} + +

{device.model}

- {device.aliases.length > 0 && ( -

别名:{device.aliases.join('、')}

- )} +
+ {device.aliases.length > 0 && 别名:{device.aliases.join('、')}} + {device.releaseDate && 上市:{device.releaseDate}} + {!device.releaseDate && device.announceDate && 发布:{device.announceDate}} + {lifecycleText} +
+ { + device.tags.length > 0 && ( +
+ {device.tags.map((tag) => ( + {tag} + ))} +
+ ) + } {device.summary &&

{device.summary}

}
@@ -248,7 +277,37 @@ const cameras = [ )) } -

- 共 {device.meta.length} 条字段级来源记录,其中人工核实 {device.meta.filter((m) => m.verified).length} 条。 -

+
+

数据来源

+ +
+ +