提交正式官网
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
import { requireAuth, nowStr } from '~/server/utils/auth'
|
||||
import { query, queryOne } from '~/server/utils/mysql'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
|
||||
function todayPrefix(): string {
|
||||
return nowStr().slice(0, 10)
|
||||
}
|
||||
|
||||
async function countBy(sql: string, ...params: unknown[]): Promise<number> {
|
||||
const row = await queryOne<{ c: number }>(sql, params)
|
||||
return row?.c ?? 0
|
||||
}
|
||||
|
||||
function mapAssessment(row: Record<string, unknown>) {
|
||||
return {
|
||||
id: row.id,
|
||||
lang: row.lang,
|
||||
firstName: row.first_name,
|
||||
lastName: row.last_name,
|
||||
email: row.email,
|
||||
status: row.status,
|
||||
createdAt: row.created_at,
|
||||
}
|
||||
}
|
||||
|
||||
function mapPartner(row: Record<string, unknown>) {
|
||||
return {
|
||||
id: row.id,
|
||||
lang: row.lang,
|
||||
firstName: row.first_name,
|
||||
lastName: row.last_name,
|
||||
companyName: row.company_name,
|
||||
email: row.email,
|
||||
status: row.status,
|
||||
createdAt: row.created_at,
|
||||
}
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return fail('未授权,请先登录', 401)
|
||||
}
|
||||
|
||||
const today = todayPrefix()
|
||||
|
||||
const assessmentStats = {
|
||||
total: await countBy('SELECT COUNT(*) AS c FROM assessment_submissions'),
|
||||
unread: await countBy("SELECT COUNT(*) AS c FROM assessment_submissions WHERE status = 'unread'"),
|
||||
today: await countBy('SELECT COUNT(*) AS c FROM assessment_submissions WHERE created_at >= ?', `${today} 00:00:00`),
|
||||
zh: await countBy("SELECT COUNT(*) AS c FROM assessment_submissions WHERE lang = 'zh'"),
|
||||
en: await countBy("SELECT COUNT(*) AS c FROM assessment_submissions WHERE lang = 'en'"),
|
||||
}
|
||||
|
||||
const partnerStats = {
|
||||
total: await countBy('SELECT COUNT(*) AS c FROM partner_submissions'),
|
||||
unread: await countBy("SELECT COUNT(*) AS c FROM partner_submissions WHERE status = 'unread'"),
|
||||
today: await countBy('SELECT COUNT(*) AS c FROM partner_submissions WHERE created_at >= ?', `${today} 00:00:00`),
|
||||
zh: await countBy("SELECT COUNT(*) AS c FROM partner_submissions WHERE lang = 'zh'"),
|
||||
en: await countBy("SELECT COUNT(*) AS c FROM partner_submissions WHERE lang = 'en'"),
|
||||
}
|
||||
|
||||
const recentAssessments = (
|
||||
await query<Record<string, unknown>>(
|
||||
'SELECT id, lang, first_name, last_name, email, status, created_at FROM assessment_submissions ORDER BY id DESC LIMIT 5'
|
||||
)
|
||||
).map(mapAssessment)
|
||||
|
||||
const recentPartners = (
|
||||
await query<Record<string, unknown>>(
|
||||
'SELECT id, lang, first_name, last_name, company_name, email, status, created_at FROM partner_submissions ORDER BY id DESC LIMIT 5'
|
||||
)
|
||||
).map(mapPartner)
|
||||
|
||||
return ok({
|
||||
assessments: assessmentStats,
|
||||
partners: partnerStats,
|
||||
recentAssessments,
|
||||
recentPartners,
|
||||
generatedAt: nowStr(),
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,23 @@
|
||||
import { generateToken, md5 } from '~/server/utils/auth'
|
||||
import { getUserByUsername } from '~/server/utils/db'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody<{ username?: string; password?: string }>(event)
|
||||
const { username, password } = body || {}
|
||||
|
||||
if (!username?.trim() || !password) {
|
||||
return fail('用户名和密码不能为空')
|
||||
}
|
||||
|
||||
const user = await getUserByUsername(username.trim())
|
||||
if (!user || user.password !== md5(password)) {
|
||||
return fail('用户名或密码错误')
|
||||
}
|
||||
|
||||
if (user.status === 'inactive') {
|
||||
return fail('用户已被禁用,无法登录')
|
||||
}
|
||||
|
||||
return ok({ token: generateToken(user.id) }, '登录成功')
|
||||
})
|
||||
@@ -0,0 +1,20 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { execute } from '~/server/utils/mysql'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return fail('未授权,请先登录', 401)
|
||||
}
|
||||
|
||||
const id = getRouterParam(event, 'id')
|
||||
if (!id) return fail('缺少 id 参数')
|
||||
|
||||
const body = await readBody<{ status?: string }>(event)
|
||||
if (!body.status) return fail('缺少 status 参数')
|
||||
|
||||
await execute('UPDATE assessment_submissions SET status = ? WHERE id = ?', [body.status, id])
|
||||
return ok(null, '更新成功')
|
||||
})
|
||||
@@ -0,0 +1,67 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { execute, query } from '~/server/utils/mysql'
|
||||
import { fail, ok, pageOk } from '~/server/utils/response'
|
||||
|
||||
function mapAssessment(row: Record<string, unknown>) {
|
||||
return {
|
||||
id: row.id,
|
||||
lang: row.lang,
|
||||
firstName: row.first_name,
|
||||
lastName: row.last_name,
|
||||
email: row.email,
|
||||
condition: row.condition_text,
|
||||
reports: row.reports,
|
||||
message: row.message,
|
||||
status: row.status,
|
||||
createdAt: row.created_at,
|
||||
}
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return fail('未授权,请先登录', 401)
|
||||
}
|
||||
|
||||
const method = event.method
|
||||
|
||||
if (method === 'GET') {
|
||||
const queryParams = getQuery(event)
|
||||
const page = Math.max(1, parseInt(String(queryParams.page || '1'), 10))
|
||||
const pageSize = Math.max(1, parseInt(String(queryParams.pageSize || '10'), 10))
|
||||
const lang = String(queryParams.lang || 'all')
|
||||
const email = String(queryParams.email || '')
|
||||
|
||||
let sql = 'SELECT * FROM assessment_submissions WHERE 1=1'
|
||||
const params: unknown[] = []
|
||||
|
||||
if (lang === 'zh' || lang === 'en') {
|
||||
sql += ' AND lang = ?'
|
||||
params.push(lang)
|
||||
}
|
||||
if (email) {
|
||||
sql += ' AND email LIKE ?'
|
||||
params.push(`%${email}%`)
|
||||
}
|
||||
|
||||
sql += ' ORDER BY id DESC'
|
||||
|
||||
const rows = await query<Record<string, unknown>>(sql, params)
|
||||
const total = rows.length
|
||||
const list = rows.slice((page - 1) * pageSize, page * pageSize).map(mapAssessment)
|
||||
return pageOk(list, total, page, pageSize)
|
||||
}
|
||||
|
||||
if (method === 'DELETE') {
|
||||
const ids = await readBody<number[]>(event)
|
||||
if (!Array.isArray(ids) || !ids.length) return fail('请选择要删除的记录')
|
||||
for (const id of ids) {
|
||||
await execute('DELETE FROM assessment_submissions WHERE id = ?', [id])
|
||||
}
|
||||
return ok(null, '删除成功')
|
||||
}
|
||||
|
||||
setResponseStatus(event, 405)
|
||||
return fail('Method Not Allowed', 405)
|
||||
})
|
||||
@@ -0,0 +1,26 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { execute } from '~/server/utils/mysql'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return fail('未授权,请先登录', 401)
|
||||
}
|
||||
|
||||
const id = getRouterParam(event, 'id')
|
||||
if (!id) return fail('ID不能为空')
|
||||
|
||||
if (event.method === 'PATCH') {
|
||||
const body = await readBody<{ status?: string }>(event)
|
||||
if (body.status !== 'read' && body.status !== 'unread') {
|
||||
return fail('状态无效')
|
||||
}
|
||||
await execute('UPDATE partner_submissions SET status = ? WHERE id = ?', [body.status, id])
|
||||
return ok(null, '更新成功')
|
||||
}
|
||||
|
||||
setResponseStatus(event, 405)
|
||||
return fail('Method Not Allowed', 405)
|
||||
})
|
||||
@@ -0,0 +1,68 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { execute, query } from '~/server/utils/mysql'
|
||||
import { fail, ok, pageOk } from '~/server/utils/response'
|
||||
|
||||
function mapPartner(row: Record<string, unknown>) {
|
||||
return {
|
||||
id: row.id,
|
||||
lang: row.lang,
|
||||
firstName: row.first_name,
|
||||
lastName: row.last_name,
|
||||
companyName: row.company_name,
|
||||
country: row.country,
|
||||
email: row.email,
|
||||
category: row.category,
|
||||
collaborate: row.collaborate,
|
||||
status: row.status,
|
||||
createdAt: row.created_at,
|
||||
}
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return fail('未授权,请先登录', 401)
|
||||
}
|
||||
|
||||
const method = event.method
|
||||
|
||||
if (method === 'GET') {
|
||||
const queryParams = getQuery(event)
|
||||
const page = Math.max(1, parseInt(String(queryParams.page || '1'), 10))
|
||||
const pageSize = Math.max(1, parseInt(String(queryParams.pageSize || '10'), 10))
|
||||
const lang = String(queryParams.lang || 'all')
|
||||
const email = String(queryParams.email || '')
|
||||
|
||||
let sql = 'SELECT * FROM partner_submissions WHERE 1=1'
|
||||
const params: unknown[] = []
|
||||
|
||||
if (lang === 'zh' || lang === 'en') {
|
||||
sql += ' AND lang = ?'
|
||||
params.push(lang)
|
||||
}
|
||||
if (email) {
|
||||
sql += ' AND email LIKE ?'
|
||||
params.push(`%${email}%`)
|
||||
}
|
||||
|
||||
sql += ' ORDER BY id DESC'
|
||||
|
||||
const rows = await query<Record<string, unknown>>(sql, params)
|
||||
const total = rows.length
|
||||
const list = rows.slice((page - 1) * pageSize, page * pageSize).map(mapPartner)
|
||||
return pageOk(list, total, page, pageSize)
|
||||
}
|
||||
|
||||
if (method === 'DELETE') {
|
||||
const ids = await readBody<number[]>(event)
|
||||
if (!Array.isArray(ids) || !ids.length) return fail('请选择要删除的记录')
|
||||
for (const id of ids) {
|
||||
await execute('DELETE FROM partner_submissions WHERE id = ?', [id])
|
||||
}
|
||||
return ok(null, '删除成功')
|
||||
}
|
||||
|
||||
setResponseStatus(event, 405)
|
||||
return fail('Method Not Allowed', 405)
|
||||
})
|
||||
@@ -0,0 +1,147 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import {
|
||||
createNewsArticle,
|
||||
deleteNewsArticles,
|
||||
getNewsArticleById,
|
||||
listNewsArticles,
|
||||
updateNewsArticle,
|
||||
type MediaType,
|
||||
type NewsArticleInput,
|
||||
} from '~/server/utils/news'
|
||||
import { fail, ok, pageOk } from '~/server/utils/response'
|
||||
|
||||
function authOrFail(event: Parameters<typeof requireAuth>[0]) {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return null
|
||||
}
|
||||
return auth
|
||||
}
|
||||
|
||||
function parseMediaType(value: unknown): MediaType | null {
|
||||
return value === 'video' ? 'video' : value === 'article' ? 'article' : null
|
||||
}
|
||||
|
||||
function parseInput(body: Partial<NewsArticleInput>): NewsArticleInput | null {
|
||||
const locale = body.locale === 'zh' ? 'zh' : body.locale === 'en' ? 'en' : null
|
||||
const mediaType = parseMediaType(body.mediaType)
|
||||
const title = body.title?.trim()
|
||||
const summary = body.summary?.trim()
|
||||
const thumbnail = body.thumbnail?.trim()
|
||||
|
||||
if (!locale || !mediaType || !title || !summary || !thumbnail) return null
|
||||
|
||||
if (mediaType === 'article') {
|
||||
const url = body.url?.trim()
|
||||
if (!url) return null
|
||||
return {
|
||||
locale,
|
||||
mediaType,
|
||||
title,
|
||||
summary,
|
||||
thumbnail,
|
||||
thumbnailAlt: body.thumbnailAlt?.trim(),
|
||||
url,
|
||||
sortOrder: typeof body.sortOrder === 'number' ? body.sortOrder : undefined,
|
||||
status: body.status === 'draft' ? 'draft' : 'published',
|
||||
}
|
||||
}
|
||||
|
||||
const youtubeUrl = body.youtubeUrl?.trim()
|
||||
const tiktokUrl = body.tiktokUrl?.trim()
|
||||
if (!youtubeUrl && !tiktokUrl) return null
|
||||
|
||||
return {
|
||||
locale,
|
||||
mediaType,
|
||||
title,
|
||||
summary,
|
||||
thumbnail,
|
||||
thumbnailAlt: body.thumbnailAlt?.trim(),
|
||||
youtubeUrl: youtubeUrl || undefined,
|
||||
tiktokUrl: tiktokUrl || undefined,
|
||||
sortOrder: typeof body.sortOrder === 'number' ? body.sortOrder : undefined,
|
||||
status: body.status === 'draft' ? 'draft' : 'published',
|
||||
}
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = authOrFail(event)
|
||||
if (!auth) return fail('未授权,请先登录', 401)
|
||||
|
||||
const method = event.method
|
||||
|
||||
if (method === 'GET') {
|
||||
const query = getQuery(event)
|
||||
const page = Math.max(1, parseInt(String(query.page || '1'), 10))
|
||||
const pageSize = Math.max(1, parseInt(String(query.pageSize || '10'), 10))
|
||||
const localeRaw = String(query.locale || '')
|
||||
const mediaTypeRaw = String(query.mediaType || query.media_type || '')
|
||||
const keyword = String(query.keyword || '').trim()
|
||||
const statusRaw = String(query.status || '')
|
||||
|
||||
const { list, total } = await listNewsArticles({
|
||||
page,
|
||||
pageSize,
|
||||
locale: localeRaw === 'en' || localeRaw === 'zh' ? localeRaw : undefined,
|
||||
mediaType: parseMediaType(mediaTypeRaw) || undefined,
|
||||
keyword: keyword || undefined,
|
||||
status: statusRaw === 'published' || statusRaw === 'draft' ? statusRaw : undefined,
|
||||
})
|
||||
|
||||
return pageOk(list, total, page, pageSize)
|
||||
}
|
||||
|
||||
if (method === 'POST') {
|
||||
const body = await readBody<Partial<NewsArticleInput>>(event)
|
||||
const input = parseInput(body)
|
||||
if (!input) {
|
||||
return fail(
|
||||
body.mediaType === 'video'
|
||||
? '请填写完整信息(语言、标题、简介、缩略图,以及 YouTube 或 TikTok 链接至少一项)'
|
||||
: '请填写完整信息(语言、标题、简介、缩略图、原文链接)'
|
||||
)
|
||||
}
|
||||
|
||||
const record = await createNewsArticle(input)
|
||||
return ok(record, '创建成功')
|
||||
}
|
||||
|
||||
if (method === 'PUT') {
|
||||
const body = await readBody<Partial<NewsArticleInput> & { id?: string }>(event)
|
||||
if (!body.id) return fail('内容 ID 不能为空')
|
||||
|
||||
const existing = await getNewsArticleById(body.id)
|
||||
if (!existing) return fail('内容不存在')
|
||||
|
||||
const input = parseInput({
|
||||
locale: body.locale ?? existing.locale,
|
||||
mediaType: body.mediaType ?? existing.mediaType,
|
||||
title: body.title ?? existing.title,
|
||||
summary: body.summary ?? existing.summary,
|
||||
thumbnail: body.thumbnail ?? existing.thumbnail,
|
||||
thumbnailAlt: body.thumbnailAlt ?? existing.thumbnailAlt,
|
||||
url: body.url ?? existing.url,
|
||||
youtubeUrl: body.youtubeUrl ?? existing.youtubeUrl,
|
||||
tiktokUrl: body.tiktokUrl ?? existing.tiktokUrl,
|
||||
sortOrder: body.sortOrder ?? existing.sortOrder,
|
||||
status: body.status ?? existing.status,
|
||||
})
|
||||
if (!input) return fail('请填写完整的内容信息')
|
||||
|
||||
const record = await updateNewsArticle(body.id, input)
|
||||
return ok(record, '更新成功')
|
||||
}
|
||||
|
||||
if (method === 'DELETE') {
|
||||
const ids = await readBody<string[]>(event)
|
||||
if (!Array.isArray(ids) || !ids.length) return fail('请选择要删除的内容')
|
||||
|
||||
await deleteNewsArticles(ids)
|
||||
return ok(null, '删除成功')
|
||||
}
|
||||
|
||||
setResponseStatus(event, 405)
|
||||
return fail('Method Not Allowed', 405)
|
||||
})
|
||||
@@ -0,0 +1,29 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { rowToRole, type AdminRoleRow } from '~/server/utils/db'
|
||||
import { query } from '~/server/utils/mysql'
|
||||
import { fail, pageOk } from '~/server/utils/response'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return fail('未授权,请先登录', 401)
|
||||
}
|
||||
|
||||
const queryParams = getQuery(event)
|
||||
const page = Math.max(1, parseInt(String(queryParams.page || '1'), 10))
|
||||
const pageSize = Math.max(1, parseInt(String(queryParams.pageSize || '10'), 10))
|
||||
const name = String(queryParams.name || '')
|
||||
const code = String(queryParams.code || '')
|
||||
const status = queryParams.status ? String(queryParams.status) : ''
|
||||
|
||||
let rows = await query<AdminRoleRow>('SELECT * FROM admin_roles ORDER BY create_time DESC')
|
||||
|
||||
if (name) rows = rows.filter((r) => r.name.includes(name))
|
||||
if (code) rows = rows.filter((r) => r.code.includes(code))
|
||||
if (status) rows = rows.filter((r) => r.status === status)
|
||||
|
||||
const total = rows.length
|
||||
const list = rows.slice((page - 1) * pageSize, page * pageSize).map(rowToRole)
|
||||
return pageOk(list, total, page, pageSize)
|
||||
})
|
||||
@@ -0,0 +1,58 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { getPageSeoById, updatePageSeo, type PageSeoUpdateInput } from '~/server/utils/page-seo'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return fail('未授权,请先登录', 401)
|
||||
}
|
||||
|
||||
const id = getRouterParam(event, 'id')
|
||||
if (!id) {
|
||||
setResponseStatus(event, 400)
|
||||
return fail('缺少 id 参数', 400)
|
||||
}
|
||||
|
||||
if (event.method === 'GET') {
|
||||
const record = await getPageSeoById(id)
|
||||
if (!record) {
|
||||
setResponseStatus(event, 404)
|
||||
return fail('未找到 SEO 配置', 404)
|
||||
}
|
||||
return ok(record)
|
||||
}
|
||||
|
||||
if (event.method === 'PUT') {
|
||||
const body = await readBody<PageSeoUpdateInput>(event)
|
||||
if (!body?.title?.trim() || !body?.description?.trim()) {
|
||||
return fail('标题和描述不能为空')
|
||||
}
|
||||
|
||||
const updated = await updatePageSeo(
|
||||
id,
|
||||
{
|
||||
title: body.title.trim(),
|
||||
description: body.description.trim(),
|
||||
keywords: body.keywords?.trim() || undefined,
|
||||
ogTitle: body.ogTitle?.trim() || undefined,
|
||||
ogDescription: body.ogDescription?.trim() || undefined,
|
||||
ogImage: body.ogImage?.trim() || undefined,
|
||||
robots: body.robots?.trim() || 'index,follow',
|
||||
ogType: body.ogType === 'article' ? 'article' : 'website',
|
||||
},
|
||||
auth.userId
|
||||
)
|
||||
|
||||
if (!updated) {
|
||||
setResponseStatus(event, 404)
|
||||
return fail('未找到 SEO 配置', 404)
|
||||
}
|
||||
|
||||
return ok(updated, '保存成功')
|
||||
}
|
||||
|
||||
setResponseStatus(event, 405)
|
||||
return fail('Method Not Allowed', 405)
|
||||
})
|
||||
@@ -0,0 +1,22 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { listPageSeo } from '~/server/utils/page-seo'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return fail('未授权,请先登录', 401)
|
||||
}
|
||||
|
||||
const queryParams = getQuery(event)
|
||||
const locale = String(queryParams.locale || '')
|
||||
const keyword = String(queryParams.keyword || '')
|
||||
|
||||
const list = await listPageSeo({
|
||||
locale: locale === 'en' || locale === 'zh' ? locale : undefined,
|
||||
keyword: keyword || undefined,
|
||||
})
|
||||
|
||||
return ok(list)
|
||||
})
|
||||
@@ -0,0 +1,44 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { updatePageSeo, type PageSeoUpdateInput } from '~/server/utils/page-seo'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
|
||||
type SeoUpdateBody = PageSeoUpdateInput & { id: string }
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return fail('未授权,请先登录', 401)
|
||||
}
|
||||
|
||||
const body = await readBody<SeoUpdateBody>(event)
|
||||
if (!body?.id?.trim()) {
|
||||
setResponseStatus(event, 400)
|
||||
return fail('缺少 id 参数', 400)
|
||||
}
|
||||
if (!body.title?.trim() || !body.description?.trim()) {
|
||||
return fail('标题和描述不能为空')
|
||||
}
|
||||
|
||||
const updated = await updatePageSeo(
|
||||
body.id.trim(),
|
||||
{
|
||||
title: body.title.trim(),
|
||||
description: body.description.trim(),
|
||||
keywords: body.keywords?.trim() || undefined,
|
||||
ogTitle: body.ogTitle?.trim() || undefined,
|
||||
ogDescription: body.ogDescription?.trim() || undefined,
|
||||
ogImage: body.ogImage?.trim() || undefined,
|
||||
robots: body.robots?.trim() || 'index,follow',
|
||||
ogType: body.ogType === 'article' ? 'article' : 'website',
|
||||
},
|
||||
auth.userId
|
||||
)
|
||||
|
||||
if (!updated) {
|
||||
setResponseStatus(event, 404)
|
||||
return fail('未找到 SEO 配置', 404)
|
||||
}
|
||||
|
||||
return ok(updated, '保存成功')
|
||||
})
|
||||
@@ -0,0 +1,19 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { getUserById } from '~/server/utils/db'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return fail('未授权,请先登录', 401)
|
||||
}
|
||||
|
||||
const id = getRouterParam(event, 'id')
|
||||
if (!id) return fail('用户ID不能为空')
|
||||
|
||||
const user = await getUserById(id)
|
||||
if (!user) return fail('用户不存在')
|
||||
|
||||
return ok(user, '获取成功')
|
||||
})
|
||||
@@ -0,0 +1,132 @@
|
||||
import { md5, nowStr, requireAuth } from '~/server/utils/auth'
|
||||
import { getUserById, rowToUser, type AdminUserRow } from '~/server/utils/db'
|
||||
import { execute, query, queryOne } from '~/server/utils/mysql'
|
||||
import { fail, ok, pageOk } from '~/server/utils/response'
|
||||
|
||||
function authOrFail(event: Parameters<typeof requireAuth>[0]) {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return null
|
||||
}
|
||||
return auth
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = authOrFail(event)
|
||||
if (!auth) return fail('未授权,请先登录', 401)
|
||||
|
||||
const method = event.method
|
||||
|
||||
if (method === 'GET') {
|
||||
const queryParams = getQuery(event)
|
||||
const page = Math.max(1, parseInt(String(queryParams.page || '1'), 10))
|
||||
const pageSize = Math.max(1, parseInt(String(queryParams.pageSize || '10'), 10))
|
||||
const username = String(queryParams.username || '')
|
||||
const name = String(queryParams.name || '')
|
||||
const status = queryParams.status ? String(queryParams.status) : ''
|
||||
|
||||
let rows = await query<AdminUserRow>('SELECT * FROM admin_users ORDER BY create_time DESC')
|
||||
|
||||
if (username) rows = rows.filter((r) => r.username.includes(username))
|
||||
if (name) rows = rows.filter((r) => (r.name || '').includes(name))
|
||||
if (status) rows = rows.filter((r) => r.status === status)
|
||||
|
||||
const total = rows.length
|
||||
const list = rows.slice((page - 1) * pageSize, page * pageSize).map(rowToUser)
|
||||
return pageOk(list, total, page, pageSize)
|
||||
}
|
||||
|
||||
if (method === 'POST') {
|
||||
const body = await readBody<{
|
||||
username?: string
|
||||
password?: string
|
||||
name?: string
|
||||
phone?: string
|
||||
email?: string
|
||||
roleId?: string
|
||||
status?: 'active' | 'inactive'
|
||||
}>(event)
|
||||
|
||||
if (!body.username?.trim() || !body.password) {
|
||||
return fail('用户名和密码不能为空')
|
||||
}
|
||||
|
||||
const exists = await queryOne<{ id: number }>(
|
||||
'SELECT id FROM admin_users WHERE username = ?',
|
||||
[body.username.trim()]
|
||||
)
|
||||
if (exists) return fail('用户名已存在')
|
||||
|
||||
const now = nowStr()
|
||||
await execute(
|
||||
`INSERT INTO admin_users (username, password, name, phone, email, role_id, status, is_built_in, create_time, update_time)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?, ?)`,
|
||||
[
|
||||
body.username.trim(),
|
||||
md5(body.password),
|
||||
body.name || null,
|
||||
body.phone || null,
|
||||
body.email || null,
|
||||
body.roleId ? Number(body.roleId) || null : null,
|
||||
body.status || 'active',
|
||||
now,
|
||||
now,
|
||||
]
|
||||
)
|
||||
|
||||
return ok(null, '创建成功')
|
||||
}
|
||||
|
||||
if (method === 'PUT') {
|
||||
const body = await readBody<{
|
||||
id?: string
|
||||
username?: string
|
||||
password?: string
|
||||
name?: string
|
||||
phone?: string
|
||||
email?: string
|
||||
roleId?: string
|
||||
status?: 'active' | 'inactive'
|
||||
}>(event)
|
||||
|
||||
if (!body.id) return fail('用户ID不能为空')
|
||||
const existing = await getUserById(body.id)
|
||||
if (!existing) return fail('用户不存在')
|
||||
|
||||
const now = nowStr()
|
||||
const password = body.password ? md5(body.password) : existing.password
|
||||
|
||||
await execute(
|
||||
`UPDATE admin_users
|
||||
SET username = ?, password = ?, name = ?, phone = ?, email = ?, role_id = ?, status = ?, update_time = ?
|
||||
WHERE id = ?`,
|
||||
[
|
||||
body.username?.trim() || existing.username,
|
||||
password,
|
||||
body.name ?? existing.name ?? null,
|
||||
body.phone ?? existing.phone ?? null,
|
||||
body.email ?? existing.email ?? null,
|
||||
body.roleId ? Number(body.roleId) || null : existing.roleId ? Number(existing.roleId) || null : null,
|
||||
body.status || existing.status,
|
||||
now,
|
||||
body.id,
|
||||
]
|
||||
)
|
||||
|
||||
return ok(null, '更新成功')
|
||||
}
|
||||
|
||||
if (method === 'DELETE') {
|
||||
const ids = await readBody<string[]>(event)
|
||||
if (!Array.isArray(ids) || !ids.length) return fail('请选择要删除的用户')
|
||||
|
||||
for (const id of ids) {
|
||||
await execute('DELETE FROM admin_users WHERE id = ? AND is_built_in = 0', [id])
|
||||
}
|
||||
return ok(null, '删除成功')
|
||||
}
|
||||
|
||||
setResponseStatus(event, 405)
|
||||
return fail('Method Not Allowed', 405)
|
||||
})
|
||||
@@ -0,0 +1,16 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { getUserById } from '~/server/utils/db'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return fail('未授权,请先登录', 401)
|
||||
}
|
||||
|
||||
const user = await getUserById(auth.userId)
|
||||
if (!user) return fail('用户不存在')
|
||||
|
||||
return ok(user, '获取成功')
|
||||
})
|
||||
@@ -0,0 +1,14 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
|
||||
/** 记录登录日志(当前仅接收并返回成功,避免前端报错) */
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return fail('未授权,请先登录', 401)
|
||||
}
|
||||
|
||||
await readBody(event).catch(() => null)
|
||||
return ok(null, '添加登录日志成功')
|
||||
})
|
||||
@@ -0,0 +1,49 @@
|
||||
import { md5, nowStr, requireAuth } from '~/server/utils/auth'
|
||||
import { getUserById } from '~/server/utils/db'
|
||||
import { execute } from '~/server/utils/mysql'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
|
||||
/** 当前登录用户修改密码 */
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return fail('未授权,请先登录', 401)
|
||||
}
|
||||
|
||||
const user = await getUserById(auth.userId)
|
||||
if (!user) return fail('用户不存在')
|
||||
|
||||
const body = await readBody<{
|
||||
oldPassword?: string
|
||||
newPassword?: string
|
||||
confirmPassword?: string
|
||||
}>(event)
|
||||
|
||||
const { oldPassword, newPassword, confirmPassword } = body
|
||||
|
||||
if (!oldPassword || !newPassword || !confirmPassword) {
|
||||
return fail('旧密码、新密码和确认密码不能为空')
|
||||
}
|
||||
|
||||
if (newPassword.length < 6) {
|
||||
return fail('新密码长度至少6位')
|
||||
}
|
||||
|
||||
if (newPassword !== confirmPassword) {
|
||||
return fail('新密码和确认密码不一致')
|
||||
}
|
||||
|
||||
if (user.password !== md5(oldPassword)) {
|
||||
return fail('旧密码错误')
|
||||
}
|
||||
|
||||
const now = nowStr()
|
||||
await execute('UPDATE admin_users SET password = ?, update_time = ? WHERE id = ?', [
|
||||
md5(newPassword),
|
||||
now,
|
||||
auth.userId,
|
||||
])
|
||||
|
||||
return ok(null, '密码修改成功')
|
||||
})
|
||||
@@ -0,0 +1,26 @@
|
||||
import { requireAuth } from '~/server/utils/auth'
|
||||
import { getRoleById, getUserById } from '~/server/utils/db'
|
||||
import { getPermissionsForRole } from '~/server/utils/menus'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return fail('未授权,请先登录', 401)
|
||||
}
|
||||
|
||||
const user = await getUserById(auth.userId)
|
||||
if (!user) return fail('用户不存在')
|
||||
|
||||
if (!user.roleId) {
|
||||
return ok({ menus: [], buttonPermissions: [] }, '获取成功')
|
||||
}
|
||||
|
||||
const role = await getRoleById(user.roleId)
|
||||
if (!role?.menuIds?.length) {
|
||||
return ok({ menus: [], buttonPermissions: [] }, '获取成功')
|
||||
}
|
||||
|
||||
return ok(getPermissionsForRole(role.menuIds), '获取成功')
|
||||
})
|
||||
@@ -0,0 +1,48 @@
|
||||
import { nowStr, requireAuth } from '~/server/utils/auth'
|
||||
import { getUserById } from '~/server/utils/db'
|
||||
import { execute } from '~/server/utils/mysql'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
|
||||
/** 当前登录用户更新个人资料 */
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuth(event)
|
||||
if (!auth) {
|
||||
setResponseStatus(event, 401)
|
||||
return fail('未授权,请先登录', 401)
|
||||
}
|
||||
|
||||
const user = await getUserById(auth.userId)
|
||||
if (!user) return fail('用户不存在')
|
||||
|
||||
const body = await readBody<{
|
||||
name?: string
|
||||
phone?: string
|
||||
email?: string
|
||||
avatar?: string
|
||||
bio?: string
|
||||
tags?: string
|
||||
}>(event)
|
||||
|
||||
const now = nowStr()
|
||||
await execute(
|
||||
`UPDATE admin_users
|
||||
SET name = ?, phone = ?, email = ?, avatar = ?, bio = ?, tags = ?, update_time = ?
|
||||
WHERE id = ?`,
|
||||
[
|
||||
body.name ?? user.name ?? null,
|
||||
body.phone ?? user.phone ?? null,
|
||||
body.email ?? user.email ?? null,
|
||||
body.avatar ?? user.avatar ?? null,
|
||||
body.bio ?? user.bio ?? null,
|
||||
body.tags ?? user.tags ?? null,
|
||||
now,
|
||||
auth.userId,
|
||||
]
|
||||
)
|
||||
|
||||
const updated = await getUserById(auth.userId)
|
||||
if (!updated) return fail('用户不存在')
|
||||
|
||||
const { password: _password, ...safeUser } = updated
|
||||
return ok(safeUser, '更新成功')
|
||||
})
|
||||
@@ -0,0 +1,61 @@
|
||||
import { nowStr } from '~/server/utils/auth'
|
||||
import { execute } from '~/server/utils/mysql'
|
||||
import { notifyAssessmentSubmission } from '~/server/utils/form-notification'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody<{
|
||||
lang?: string
|
||||
firstName?: string
|
||||
lastName?: string
|
||||
email?: string
|
||||
condition?: string
|
||||
reports?: string
|
||||
message?: string
|
||||
}>(event)
|
||||
|
||||
const lang = body.lang === 'zh' ? 'zh' : 'en'
|
||||
const firstName = body.firstName?.trim()
|
||||
const email = body.email?.trim()
|
||||
const condition = body.condition?.trim()
|
||||
|
||||
if (!firstName) return fail('请填写患者名')
|
||||
if (!email) return fail('请填写联系邮箱')
|
||||
if (!condition) return fail('请填写主要病情/诊断')
|
||||
|
||||
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
||||
if (!emailPattern.test(email)) return fail('邮箱格式不正确')
|
||||
|
||||
const createdAt = nowStr()
|
||||
|
||||
await execute(
|
||||
`INSERT INTO assessment_submissions
|
||||
(lang, first_name, last_name, email, condition_text, reports, message, status, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, 'unread', ?)`,
|
||||
[
|
||||
lang,
|
||||
firstName,
|
||||
body.lastName?.trim() || null,
|
||||
email,
|
||||
condition,
|
||||
body.reports || null,
|
||||
body.message?.trim() || null,
|
||||
createdAt,
|
||||
]
|
||||
)
|
||||
|
||||
void notifyAssessmentSubmission({
|
||||
lang,
|
||||
firstName,
|
||||
lastName: body.lastName?.trim() || null,
|
||||
email,
|
||||
condition,
|
||||
reports: body.reports || null,
|
||||
message: body.message?.trim() || null,
|
||||
createdAt,
|
||||
}).catch((error) => {
|
||||
console.error('[mail] 免费评估表通知发送失败:', error)
|
||||
})
|
||||
|
||||
return ok(null, '提交成功')
|
||||
})
|
||||
@@ -0,0 +1,62 @@
|
||||
import { nowStr } from '~/server/utils/auth'
|
||||
import { execute } from '~/server/utils/mysql'
|
||||
import { notifyPartnerSubmission } from '~/server/utils/form-notification'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody<{
|
||||
lang?: string
|
||||
firstName?: string
|
||||
lastName?: string
|
||||
companyName?: string
|
||||
country?: string
|
||||
email?: string
|
||||
category?: string
|
||||
collaborate?: string
|
||||
}>(event)
|
||||
|
||||
const lang = body.lang === 'zh' ? 'zh' : 'en'
|
||||
const firstName = body.firstName?.trim()
|
||||
const lastName = body.lastName?.trim()
|
||||
const companyName = body.companyName?.trim()
|
||||
const country = body.country?.trim()
|
||||
const email = body.email?.trim()
|
||||
const category = body.category?.trim()
|
||||
const collaborate = body.collaborate?.trim()
|
||||
|
||||
if (!firstName) return fail('请填写名')
|
||||
if (!lastName) return fail('请填写姓')
|
||||
if (!companyName) return fail('请填写公司名称')
|
||||
if (!country) return fail('请填写国家/地区')
|
||||
if (!email) return fail('请填写电子邮箱')
|
||||
if (!category) return fail('请选择合作类别')
|
||||
if (!collaborate) return fail('请填写合作说明')
|
||||
|
||||
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
||||
if (!emailPattern.test(email)) return fail('邮箱格式不正确')
|
||||
|
||||
const createdAt = nowStr()
|
||||
|
||||
await execute(
|
||||
`INSERT INTO partner_submissions
|
||||
(lang, first_name, last_name, company_name, country, email, category, collaborate, status, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'unread', ?)`,
|
||||
[lang, firstName, lastName, companyName, country, email, category, collaborate, createdAt]
|
||||
)
|
||||
|
||||
void notifyPartnerSubmission({
|
||||
lang,
|
||||
firstName,
|
||||
lastName,
|
||||
companyName,
|
||||
country,
|
||||
email,
|
||||
category,
|
||||
collaborate,
|
||||
createdAt,
|
||||
}).catch((error) => {
|
||||
console.error('[mail] 合作申请通知发送失败:', error)
|
||||
})
|
||||
|
||||
return ok(null, '提交成功')
|
||||
})
|
||||
@@ -0,0 +1,11 @@
|
||||
import { listPublishedMedia } from '~/server/utils/news'
|
||||
import { ok } from '~/server/utils/response'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const query = getQuery(event)
|
||||
const localeRaw = String(query.locale || 'en').trim()
|
||||
const locale = localeRaw === 'zh' ? 'zh' : 'en'
|
||||
|
||||
const media = await listPublishedMedia(locale)
|
||||
return ok(media)
|
||||
})
|
||||
@@ -0,0 +1,23 @@
|
||||
import { getPageSeoByRoute } from '~/server/utils/page-seo'
|
||||
import { fail, ok } from '~/server/utils/response'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const query = getQuery(event)
|
||||
const routeKey = String(query.routeKey || query.route_key || '').trim()
|
||||
const localeRaw = String(query.locale || 'en').trim()
|
||||
|
||||
if (!routeKey) {
|
||||
setResponseStatus(event, 400)
|
||||
return fail('缺少 routeKey 参数', 400)
|
||||
}
|
||||
|
||||
const locale = localeRaw === 'zh' ? 'zh' : 'en'
|
||||
const record = await getPageSeoByRoute(routeKey, locale)
|
||||
|
||||
if (!record) {
|
||||
setResponseStatus(event, 404)
|
||||
return fail('未找到 SEO 配置', 404)
|
||||
}
|
||||
|
||||
return ok(record)
|
||||
})
|
||||
Reference in New Issue
Block a user