提交正式官网
This commit is contained in:
@@ -0,0 +1,403 @@
|
||||
import type { MediaPublicItem } from '~/data/news-content'
|
||||
import { newsItems as seedNewsEn, seedVideosEn } from '~/data/news-content'
|
||||
import { newsItemsZh, seedVideosZh } from '~/data/locale/zh/news.zh'
|
||||
import { nowStr } from './auth'
|
||||
import { execInit, execute, query, queryInit, queryOne, queryOneInit } from './mysql'
|
||||
|
||||
export type MediaType = 'article' | 'video'
|
||||
|
||||
export type NewsArticleRow = {
|
||||
id: number
|
||||
locale: string
|
||||
media_type: string
|
||||
title: string
|
||||
summary: string
|
||||
thumbnail: string
|
||||
thumbnail_alt: string | null
|
||||
url: string | null
|
||||
youtube_url: string | null
|
||||
tiktok_url: string | null
|
||||
sort_order: number
|
||||
status: string
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export type NewsArticleRecord = {
|
||||
id: string
|
||||
locale: 'en' | 'zh'
|
||||
mediaType: MediaType
|
||||
title: string
|
||||
summary: string
|
||||
thumbnail: string
|
||||
thumbnailAlt?: string
|
||||
url?: string
|
||||
youtubeUrl?: string
|
||||
tiktokUrl?: string
|
||||
sortOrder: number
|
||||
status: 'published' | 'draft'
|
||||
createdAt: string
|
||||
updatedAt: string
|
||||
}
|
||||
|
||||
export type NewsArticleInput = {
|
||||
locale: 'en' | 'zh'
|
||||
mediaType: MediaType
|
||||
title: string
|
||||
summary: string
|
||||
thumbnail: string
|
||||
thumbnailAlt?: string
|
||||
url?: string
|
||||
youtubeUrl?: string
|
||||
tiktokUrl?: string
|
||||
sortOrder?: number
|
||||
status?: 'published' | 'draft'
|
||||
}
|
||||
|
||||
export type ArticlePublic = {
|
||||
title: string
|
||||
summary: string
|
||||
thumbnail: string
|
||||
thumbnailAlt: string
|
||||
url: string
|
||||
}
|
||||
|
||||
export type VideoPublic = {
|
||||
title: string
|
||||
summary: string
|
||||
thumbnail: string
|
||||
thumbnailAlt: string
|
||||
youtubeUrl?: string
|
||||
tiktokUrl?: string
|
||||
}
|
||||
|
||||
export type PublishedMedia = {
|
||||
items: MediaPublicItem[]
|
||||
articles: ArticlePublic[]
|
||||
videos: VideoPublic[]
|
||||
}
|
||||
|
||||
function rowToRecord(row: NewsArticleRow): NewsArticleRecord {
|
||||
return {
|
||||
id: String(row.id),
|
||||
locale: row.locale as 'en' | 'zh',
|
||||
mediaType: (row.media_type === 'video' ? 'video' : 'article') as MediaType,
|
||||
title: row.title,
|
||||
summary: row.summary,
|
||||
thumbnail: row.thumbnail,
|
||||
thumbnailAlt: row.thumbnail_alt ?? undefined,
|
||||
url: row.url ?? undefined,
|
||||
youtubeUrl: row.youtube_url ?? undefined,
|
||||
tiktokUrl: row.tiktok_url ?? undefined,
|
||||
sortOrder: row.sort_order,
|
||||
status: row.status as 'published' | 'draft',
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at,
|
||||
}
|
||||
}
|
||||
|
||||
function rowToMediaPublic(row: NewsArticleRow): MediaPublicItem {
|
||||
const base = {
|
||||
title: row.title,
|
||||
summary: row.summary,
|
||||
thumbnail: row.thumbnail,
|
||||
thumbnailAlt: row.thumbnail_alt || row.title,
|
||||
}
|
||||
|
||||
if (row.media_type === 'video') {
|
||||
return {
|
||||
...base,
|
||||
mediaType: 'video',
|
||||
youtubeUrl: row.youtube_url ?? undefined,
|
||||
tiktokUrl: row.tiktok_url ?? undefined,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...base,
|
||||
mediaType: 'article',
|
||||
url: row.url || '',
|
||||
}
|
||||
}
|
||||
|
||||
export async function initNewsSchema() {
|
||||
await execInit(`
|
||||
CREATE TABLE IF NOT EXISTS news_articles (
|
||||
id BIGINT PRIMARY KEY AUTO_INCREMENT,
|
||||
locale VARCHAR(8) NOT NULL,
|
||||
media_type VARCHAR(32) NOT NULL DEFAULT 'article',
|
||||
title TEXT NOT NULL,
|
||||
summary TEXT NOT NULL,
|
||||
thumbnail TEXT NOT NULL,
|
||||
thumbnail_alt TEXT,
|
||||
url TEXT,
|
||||
youtube_url TEXT,
|
||||
tiktok_url TEXT,
|
||||
sort_order INT NOT NULL DEFAULT 0,
|
||||
status VARCHAR(32) NOT NULL DEFAULT 'published',
|
||||
created_at VARCHAR(32) NOT NULL,
|
||||
updated_at VARCHAR(32) NOT NULL,
|
||||
INDEX idx_news_locale_sort (locale, sort_order, id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
|
||||
`)
|
||||
}
|
||||
|
||||
export async function seedNewsIfEmpty() {
|
||||
const count = await queryOneInit<{ c: number }>('SELECT COUNT(*) AS c FROM news_articles')
|
||||
if ((count?.c ?? 0) > 0) return
|
||||
|
||||
await seedArticles()
|
||||
await seedVideos()
|
||||
}
|
||||
|
||||
async function seedArticles() {
|
||||
const now = nowStr()
|
||||
|
||||
for (const [index, item] of seedNewsEn.entries()) {
|
||||
await execInit(
|
||||
`INSERT INTO news_articles (
|
||||
locale, media_type, title, summary, thumbnail, thumbnail_alt, url, youtube_url, tiktok_url,
|
||||
sort_order, status, created_at, updated_at
|
||||
) VALUES (?, 'article', ?, ?, ?, ?, ?, NULL, NULL, ?, 'published', ?, ?)`,
|
||||
['en', item.title, item.summary, item.thumbnail, item.thumbnailAlt, item.url, seedNewsEn.length - index + 3, now, now]
|
||||
)
|
||||
}
|
||||
|
||||
for (const [index, item] of newsItemsZh.entries()) {
|
||||
await execInit(
|
||||
`INSERT INTO news_articles (
|
||||
locale, media_type, title, summary, thumbnail, thumbnail_alt, url, youtube_url, tiktok_url,
|
||||
sort_order, status, created_at, updated_at
|
||||
) VALUES (?, 'article', ?, ?, ?, ?, ?, NULL, NULL, ?, 'published', ?, ?)`,
|
||||
['zh', item.title, item.summary, item.thumbnail, item.thumbnailAlt, item.url, newsItemsZh.length - index + 3, now, now]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
async function seedVideos() {
|
||||
const now = nowStr()
|
||||
|
||||
for (const [index, item] of seedVideosEn.entries()) {
|
||||
await execInit(
|
||||
`INSERT INTO news_articles (
|
||||
locale, media_type, title, summary, thumbnail, thumbnail_alt, url, youtube_url, tiktok_url,
|
||||
sort_order, status, created_at, updated_at
|
||||
) VALUES (?, 'video', ?, ?, ?, ?, '', ?, ?, ?, 'published', ?, ?)`,
|
||||
[
|
||||
'en',
|
||||
item.title,
|
||||
item.summary,
|
||||
item.thumbnail,
|
||||
item.thumbnailAlt,
|
||||
item.youtubeUrl ?? null,
|
||||
item.tiktokUrl ?? null,
|
||||
seedVideosEn.length - index + 2,
|
||||
now,
|
||||
now,
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
for (const [index, item] of seedVideosZh.entries()) {
|
||||
await execInit(
|
||||
`INSERT INTO news_articles (
|
||||
locale, media_type, title, summary, thumbnail, thumbnail_alt, url, youtube_url, tiktok_url,
|
||||
sort_order, status, created_at, updated_at
|
||||
) VALUES (?, 'video', ?, ?, ?, ?, '', ?, ?, ?, 'published', ?, ?)`,
|
||||
[
|
||||
'zh',
|
||||
item.title,
|
||||
item.summary,
|
||||
item.thumbnail,
|
||||
item.thumbnailAlt,
|
||||
item.youtubeUrl ?? null,
|
||||
item.tiktokUrl ?? null,
|
||||
seedVideosZh.length - index + 2,
|
||||
now,
|
||||
now,
|
||||
]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function seedVideosIfEmpty() {
|
||||
const count = await queryOneInit<{ c: number }>(
|
||||
`SELECT COUNT(*) AS c FROM news_articles WHERE media_type = 'video'`
|
||||
)
|
||||
if ((count?.c ?? 0) > 0) return
|
||||
|
||||
await seedVideos()
|
||||
}
|
||||
|
||||
export const NEWS_MENU_IDS = [
|
||||
'menu_content_dir',
|
||||
'menu_news_list',
|
||||
'menu_news_add',
|
||||
'menu_news_edit',
|
||||
'menu_news_delete',
|
||||
'menu_news_view',
|
||||
]
|
||||
|
||||
export async function migrateNewsMenu() {
|
||||
const roles = await queryInit<{ id: number; menu_ids: string }>('SELECT id, menu_ids FROM admin_roles')
|
||||
|
||||
for (const role of roles) {
|
||||
const ids = JSON.parse(role.menu_ids || '[]') as string[]
|
||||
if (!ids.includes('menu_sys_dir')) continue
|
||||
const missing = NEWS_MENU_IDS.filter((id) => !ids.includes(id))
|
||||
if (!missing.length) continue
|
||||
|
||||
await execInit('UPDATE admin_roles SET menu_ids = ?, update_time = ? WHERE id = ?', [
|
||||
JSON.stringify([...ids, ...missing]),
|
||||
nowStr(),
|
||||
role.id,
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
export async function listNewsArticles(options?: {
|
||||
locale?: 'en' | 'zh'
|
||||
mediaType?: MediaType
|
||||
keyword?: string
|
||||
status?: 'published' | 'draft'
|
||||
page?: number
|
||||
pageSize?: number
|
||||
}) {
|
||||
let rows = await query<NewsArticleRow>(
|
||||
'SELECT * FROM news_articles ORDER BY sort_order DESC, created_at DESC'
|
||||
)
|
||||
|
||||
if (options?.locale) rows = rows.filter((r) => r.locale === options.locale)
|
||||
if (options?.mediaType) rows = rows.filter((r) => r.media_type === options.mediaType)
|
||||
if (options?.status) rows = rows.filter((r) => r.status === options.status)
|
||||
if (options?.keyword) {
|
||||
const kw = options.keyword.toLowerCase()
|
||||
rows = rows.filter(
|
||||
(r) => r.title.toLowerCase().includes(kw) || r.summary.toLowerCase().includes(kw)
|
||||
)
|
||||
}
|
||||
|
||||
const total = rows.length
|
||||
if (options?.page && options?.pageSize) {
|
||||
const page = Math.max(1, options.page)
|
||||
const pageSize = Math.max(1, options.pageSize)
|
||||
rows = rows.slice((page - 1) * pageSize, page * pageSize)
|
||||
}
|
||||
|
||||
return { list: rows.map(rowToRecord), total }
|
||||
}
|
||||
|
||||
export async function listPublishedMedia(locale: 'en' | 'zh'): Promise<PublishedMedia> {
|
||||
const rows = await query<NewsArticleRow>(
|
||||
`SELECT * FROM news_articles
|
||||
WHERE locale = ? AND status = 'published'
|
||||
ORDER BY sort_order DESC, created_at DESC`,
|
||||
[locale]
|
||||
)
|
||||
|
||||
const items = rows.map(rowToMediaPublic)
|
||||
|
||||
return {
|
||||
items,
|
||||
articles: items.filter((item) => item.mediaType === 'article').map((item) => ({
|
||||
title: item.title,
|
||||
summary: item.summary,
|
||||
thumbnail: item.thumbnail,
|
||||
thumbnailAlt: item.thumbnailAlt,
|
||||
url: item.url || '',
|
||||
})),
|
||||
videos: items.filter((item) => item.mediaType === 'video').map((item) => ({
|
||||
title: item.title,
|
||||
summary: item.summary,
|
||||
thumbnail: item.thumbnail,
|
||||
thumbnailAlt: item.thumbnailAlt,
|
||||
youtubeUrl: item.youtubeUrl,
|
||||
tiktokUrl: item.tiktokUrl,
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
export async function getNewsArticleById(id: string | number) {
|
||||
const row = await queryOne<NewsArticleRow>('SELECT * FROM news_articles WHERE id = ?', [id])
|
||||
return row ? rowToRecord(row) : null
|
||||
}
|
||||
|
||||
export async function createNewsArticle(input: NewsArticleInput) {
|
||||
const now = nowStr()
|
||||
const maxSort = await queryOne<{ m: number }>(
|
||||
'SELECT COALESCE(MAX(sort_order), 0) AS m FROM news_articles WHERE locale = ?',
|
||||
[input.locale]
|
||||
)
|
||||
|
||||
const result = await execute(
|
||||
`INSERT INTO news_articles (
|
||||
locale, media_type, title, summary, thumbnail, thumbnail_alt, url, youtube_url, tiktok_url,
|
||||
sort_order, status, created_at, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[
|
||||
input.locale,
|
||||
input.mediaType,
|
||||
input.title.trim(),
|
||||
input.summary.trim(),
|
||||
input.thumbnail.trim(),
|
||||
input.thumbnailAlt?.trim() || null,
|
||||
input.mediaType === 'article' ? input.url?.trim() || '' : '',
|
||||
input.mediaType === 'video' ? input.youtubeUrl?.trim() || null : null,
|
||||
input.mediaType === 'video' ? input.tiktokUrl?.trim() || null : null,
|
||||
input.sortOrder ?? (maxSort?.m ?? 0) + 1,
|
||||
input.status ?? 'published',
|
||||
now,
|
||||
now,
|
||||
]
|
||||
)
|
||||
|
||||
return (await getNewsArticleById(result.insertId))!
|
||||
}
|
||||
|
||||
export async function updateNewsArticle(id: string | number, input: Partial<NewsArticleInput>) {
|
||||
const existing = await getNewsArticleById(id)
|
||||
if (!existing) return null
|
||||
|
||||
const mediaType = input.mediaType ?? existing.mediaType
|
||||
const now = nowStr()
|
||||
|
||||
await execute(
|
||||
`UPDATE news_articles SET
|
||||
locale = ?,
|
||||
media_type = ?,
|
||||
title = ?,
|
||||
summary = ?,
|
||||
thumbnail = ?,
|
||||
thumbnail_alt = ?,
|
||||
url = ?,
|
||||
youtube_url = ?,
|
||||
tiktok_url = ?,
|
||||
sort_order = ?,
|
||||
status = ?,
|
||||
updated_at = ?
|
||||
WHERE id = ?`,
|
||||
[
|
||||
input.locale ?? existing.locale,
|
||||
mediaType,
|
||||
input.title?.trim() ?? existing.title,
|
||||
input.summary?.trim() ?? existing.summary,
|
||||
input.thumbnail?.trim() ?? existing.thumbnail,
|
||||
input.thumbnailAlt !== undefined ? input.thumbnailAlt.trim() || null : existing.thumbnailAlt ?? null,
|
||||
mediaType === 'article' ? (input.url?.trim() ?? existing.url ?? '') : '',
|
||||
mediaType === 'video' ? (input.youtubeUrl?.trim() ?? existing.youtubeUrl ?? null) : null,
|
||||
mediaType === 'video' ? (input.tiktokUrl?.trim() ?? existing.tiktokUrl ?? null) : null,
|
||||
input.sortOrder ?? existing.sortOrder,
|
||||
input.status ?? existing.status,
|
||||
now,
|
||||
id,
|
||||
]
|
||||
)
|
||||
|
||||
return getNewsArticleById(id)
|
||||
}
|
||||
|
||||
export async function deleteNewsArticles(ids: Array<string | number>) {
|
||||
for (const id of ids) {
|
||||
await execute('DELETE FROM news_articles WHERE id = ?', [id])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user