import { SEO_REGISTRY, type SeoRegistryEntry } from '../data/seo-registry' import { SEO_AR_RU, buildSeoValuesFromCopy } from '../data/seo-registry-ar-ru' import { nowStr } from './auth' import { execInit, execute, query, queryInit, queryOne, queryOneInit } from './mysql' import { isSiteLocale, type SiteLocale } from '~/types/locale' export type PageSeoRow = { id: number route_key: string locale: string page_label: string title: string description: string keywords: string | null og_title: string | null og_description: string | null og_image: string | null robots: string | null og_type: string | null updated_at: string updated_by: number | null } export type PageSeoRecord = { id: string routeKey: string locale: SiteLocale pageLabel: string title: string description: string keywords?: string ogTitle?: string ogDescription?: string ogImage?: string robots?: string ogType?: 'website' | 'article' updatedAt: string updatedBy?: string } export type PageSeoUpdateInput = { title: string description: string keywords?: string ogTitle?: string ogDescription?: string ogImage?: string robots?: string ogType?: 'website' | 'article' } function rowToRecord(row: PageSeoRow): PageSeoRecord { return { id: String(row.id), routeKey: row.route_key, locale: isSiteLocale(row.locale) ? row.locale : 'en', pageLabel: row.page_label, title: row.title, description: row.description, keywords: row.keywords ?? undefined, ogTitle: row.og_title ?? undefined, ogDescription: row.og_description ?? undefined, ogImage: row.og_image ?? undefined, robots: row.robots ?? undefined, ogType: (row.og_type as 'website' | 'article' | null) ?? undefined, updatedAt: row.updated_at, updatedBy: row.updated_by != null ? String(row.updated_by) : undefined, } } function entryToValues(entry: SeoRegistryEntry, now: string) { return [ entry.routeKey, entry.locale, entry.pageLabel, entry.title, entry.description, entry.keywords ?? null, entry.ogTitle ?? null, entry.ogDescription ?? null, entry.ogImage ?? null, entry.robots ?? 'index,follow', entry.ogType ?? 'website', now, null, ] } export async function initPageSeoSchema() { await execInit(` CREATE TABLE IF NOT EXISTS page_seo ( id BIGINT PRIMARY KEY AUTO_INCREMENT, route_key VARCHAR(512) NOT NULL, locale VARCHAR(8) NOT NULL, page_label VARCHAR(255) NOT NULL, title TEXT NOT NULL, description TEXT NOT NULL, keywords TEXT, og_title TEXT, og_description TEXT, og_image TEXT, robots VARCHAR(64) DEFAULT 'index,follow', og_type VARCHAR(32) DEFAULT 'website', updated_at VARCHAR(32) NOT NULL, updated_by BIGINT, UNIQUE KEY uk_page_seo_route_locale (route_key, locale) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 `) } export async function seedPageSeoIfEmpty() { const count = await queryOneInit<{ c: number }>('SELECT COUNT(*) AS c FROM page_seo') if ((count?.c ?? 0) > 0) return const now = nowStr() for (const entry of SEO_REGISTRY) { await execInit( `INSERT INTO page_seo ( route_key, locale, page_label, title, description, keywords, og_title, og_description, og_image, robots, og_type, updated_at, updated_by ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, entryToValues(entry, now) ) } } export async function syncMissingPageSeo() { const now = nowStr() const existingRows = await queryInit<{ route_key: string; locale: string }>( 'SELECT route_key, locale FROM page_seo' ) const existing = new Set(existingRows.map((r) => `${r.route_key}::${r.locale}`)) for (const entry of SEO_REGISTRY) { const routeLocaleKey = `${entry.routeKey}::${entry.locale}` if (existing.has(routeLocaleKey)) continue await execInit( `INSERT INTO page_seo ( route_key, locale, page_label, title, description, keywords, og_title, og_description, og_image, robots, og_type, updated_at, updated_by ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, entryToValues(entry, now) ) existing.add(routeLocaleKey) } // 以阿语 / 俄语正式译文写入或更新 SEO(替换早期英文占位) for (const [routeKey, locales] of Object.entries(SEO_AR_RU)) { for (const locale of ['ar', 'ru'] as const) { const copy = locales[locale] const values = buildSeoValuesFromCopy(routeKey, locale, copy, now) const routeLocaleKey = `${routeKey}::${locale}` if (existing.has(routeLocaleKey)) { await execInit( `UPDATE page_seo SET page_label = ?, title = ?, description = ?, keywords = ?, og_title = ?, og_description = ?, og_image = ?, robots = ?, og_type = ?, updated_at = ? WHERE route_key = ? AND locale = ?`, [ copy.pageLabel, copy.title, copy.description, copy.keywords ?? null, copy.ogTitle ?? null, copy.ogDescription ?? null, copy.ogImage ?? null, copy.robots ?? 'index,follow', copy.ogType ?? 'website', now, routeKey, locale ] ) } else { await execInit( `INSERT INTO page_seo ( route_key, locale, page_label, title, description, keywords, og_title, og_description, og_image, robots, og_type, updated_at, updated_by ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, values ) existing.add(routeLocaleKey) } } } } const INDEXABLE_POLICY_ROUTES = [ '/privacy-policy', '/terms-of-use', '/accessibility-statement', ] as const /** 将政策页 robots 从 noindex 同步为 index,follow(部署后自动修复线上数据库) */ export async function syncPolicyPageIndexability() { const now = nowStr() for (const routeKey of INDEXABLE_POLICY_ROUTES) { await execInit( `UPDATE page_seo SET robots = 'index,follow', updated_at = ? WHERE route_key = ? AND robots = 'noindex,follow'`, [now, routeKey] ) } } export async function listPageSeo(params?: { locale?: string; keyword?: string }) { let sql = 'SELECT * FROM page_seo WHERE 1=1' const args: unknown[] = [] if (params?.locale && isSiteLocale(params.locale)) { sql += ' AND locale = ?' args.push(params.locale) } if (params?.keyword) { sql += ' AND (page_label LIKE ? OR route_key LIKE ? OR title LIKE ?)' const kw = `%${params.keyword}%` args.push(kw, kw, kw) } sql += ' ORDER BY route_key ASC, locale ASC' const rows = await query(sql, args) return rows.map(rowToRecord) } export async function getPageSeoByRoute(routeKey: string, locale: SiteLocale) { const row = await queryOne( 'SELECT * FROM page_seo WHERE route_key = ? AND locale = ?', [routeKey, locale] ) if (row) return rowToRecord(row) if (locale !== 'en') { const fallback = await queryOne( 'SELECT * FROM page_seo WHERE route_key = ? AND locale = ?', [routeKey, 'en'] ) return fallback ? rowToRecord(fallback) : null } return null } export async function getPageSeoById(id: string | number) { const row = await queryOne('SELECT * FROM page_seo WHERE id = ?', [id]) return row ? rowToRecord(row) : null } export async function updatePageSeo(id: string | number, input: PageSeoUpdateInput, updatedBy?: string) { const existing = await getPageSeoById(id) if (!existing) return null const now = nowStr() await execute( `UPDATE page_seo SET title = ?, description = ?, keywords = ?, og_title = ?, og_description = ?, og_image = ?, robots = ?, og_type = ?, updated_at = ?, updated_by = ? WHERE id = ?`, [ input.title, input.description, input.keywords ?? null, input.ogTitle ?? null, input.ogDescription ?? null, input.ogImage ?? null, input.robots ?? 'index,follow', input.ogType ?? 'website', now, updatedBy ? Number(updatedBy) || null : null, id, ] ) return getPageSeoById(id) }