提交正式官网

This commit is contained in:
parkiss
2026-07-09 14:35:32 +08:00
parent 5efbd6760e
commit e09f0eccda
792 changed files with 91499 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
import { stripLocalePrefix, toLocalePath } from '~/utils/locale-path'
import type { SiteLocale } from '~/types/locale'
export function useLocalePath() {
const { locale } = useSiteLocale()
return (path: string, targetLocale?: SiteLocale) =>
toLocalePath(path, targetLocale ?? locale.value)
}
export function useSwitchLocale() {
const route = useRoute()
const { setLocale } = useSiteLocale()
return (next: SiteLocale) => {
const basePath = stripLocalePrefix(route.path)
setLocale(next)
return navigateTo(toLocalePath(basePath, next))
}
}
+7
View File
@@ -0,0 +1,7 @@
import type { LocaleBundle } from '~/types/locale'
import { pickLocale } from '~/utils/pick-locale'
export function useLocalized<T>(bundle: LocaleBundle<T>) {
const { locale } = useSiteLocale()
return computed(() => pickLocale(bundle, locale.value))
}
+117
View File
@@ -0,0 +1,117 @@
import {
doctors,
getHospitalById,
getDoctorsByHospitalId,
hospitals
} from '~/data/hospitals-content'
import { hospitalsPageContentBundle } from '~/data/hospitals-page-content'
import { doctorZh } from '~/data/locale/zh/doctors.zh'
import { hospitalIntroZh } from '~/data/locale/zh/hospitals-intros.zh'
import { hospitalNameZh } from '~/data/locale/zh/hospitals-names.zh'
import type { DoctorRecord, HospitalRecord } from '~/data/hospitals-content'
function localizeHospital(h: HospitalRecord, locale: 'en' | 'zh'): HospitalRecord {
if (locale !== 'zh') return h
const intro = hospitalIntroZh[h.id]
const name = hospitalNameZh[h.id]
return {
...h,
...(name ? { name } : {}),
...(intro ? { intro } : {})
}
}
function localizeDoctor(d: DoctorRecord, locale: 'en' | 'zh'): DoctorRecord {
if (locale !== 'zh') return d
const zh = doctorZh[d.id]
return zh ? { ...d, ...zh } : d
}
export function useLocalizedHospitals() {
const { locale } = useSiteLocale()
const page = useLocalized(hospitalsPageContentBundle)
const localizedHospitals = computed(() =>
hospitals.map((h) => localizeHospital(h, locale.value))
)
const localizedDoctors = computed(() =>
doctors.map((d) => localizeDoctor(d, locale.value))
)
function getLocalizedHospitalById(id: string) {
const h = getHospitalById(id)
return h ? localizeHospital(h, locale.value) : undefined
}
function getLocalizedDoctorsByHospitalId(hospitalId: string) {
return getDoctorsByHospitalId(hospitalId).map((d) => localizeDoctor(d, locale.value))
}
return {
page,
hospitals: localizedHospitals,
doctors: localizedDoctors,
getHospitalById: getLocalizedHospitalById,
getDoctorsByHospitalId: getLocalizedDoctorsByHospitalId
}
}
+36
View File
@@ -0,0 +1,36 @@
import type { MediaPublicItem } from '~/data/news-content'
type MediaApiResponse = {
code: number
data: {
items: MediaPublicItem[]
articles: MediaPublicItem[]
videos: MediaPublicItem[]
}
}
export function useMediaCenter() {
const { locale } = useSiteLocale()
const { data: mediaResponse } = useAsyncData(
() => `media-center-${locale.value}`,
() => $fetch<MediaApiResponse>('/api/news', { query: { locale: locale.value } }),
{ watch: [locale] }
)
const mediaItems = computed<MediaPublicItem[]>(() =>
mediaResponse.value?.code === 200 ? (mediaResponse.value.data?.items ?? []) : []
)
const articles = computed(() => mediaItems.value.filter((item) => item.mediaType === 'article'))
const videos = computed(() => mediaItems.value.filter((item) => item.mediaType === 'video'))
return { mediaItems, articles, videos }
}
/** @deprecated use useMediaCenter */
export function useNewsArticles() {
const { articles } = useMediaCenter()
return { newsItems: articles }
}
+164
View File
@@ -0,0 +1,164 @@
import type { MaybeRefOrGetter } from 'vue'
import { toValue } from 'vue'
import {
buildGlobalJsonLd,
DEFAULT_OG_IMAGE,
SITE_NAME,
toAbsoluteUrl,
wrapJsonLdGraph,
type JsonLdNode
} from '~/utils/seo'
import { stripLocalePrefix, toLocalePath } from '~/utils/locale-path'
export type PageSeoRecord = {
title: string
description: string
keywords?: string
ogTitle?: string
ogDescription?: string
ogImage?: string
robots?: string
ogType?: 'website' | 'article'
}
export type PageSeoOptions = {
/** 页面路由键,如 /about-us;不传则使用当前路径(不含 /zh 前缀) */
routeKey?: MaybeRefOrGetter<string | undefined>
/** 为 true 时不从数据库读取(用于动态标题等场景) */
skipDb?: MaybeRefOrGetter<boolean>
/** 拼在数据库 title 前的前缀,如「医院名 | 后台 SEO 标题」 */
titlePrefix?: MaybeRefOrGetter<string | undefined>
title: MaybeRefOrGetter<string>
description: MaybeRefOrGetter<string>
path?: MaybeRefOrGetter<string>
ogTitle?: MaybeRefOrGetter<string | undefined>
ogDescription?: MaybeRefOrGetter<string | undefined>
ogImage?: MaybeRefOrGetter<string | undefined>
ogType?: MaybeRefOrGetter<'website' | 'article'>
keywords?: MaybeRefOrGetter<string | undefined>
robots?: MaybeRefOrGetter<string | undefined>
jsonLd?: MaybeRefOrGetter<JsonLdNode | JsonLdNode[] | undefined>
}
function resolveRouteKey(
routeKey: MaybeRefOrGetter<string | undefined> | undefined,
currentPath: string
) {
const custom = routeKey ? toValue(routeKey) : undefined
if (custom) return custom
return stripLocalePrefix(currentPath.split('?')[0])
}
export function usePageSeo(options: PageSeoOptions) {
const route = useRoute()
const { locale } = useSiteLocale()
const siteUrl = useSiteUrl()
const resolvedRouteKey = computed(() =>
resolveRouteKey(options.routeKey, route.path)
)
const skipDb = computed(() => toValue(options.skipDb) ?? false)
const { data: seoResponse } = useAsyncData(
() => `page-seo-${resolvedRouteKey.value}-${locale.value}`,
() => {
if (skipDb.value) return null
return $fetch<{ code: number; data: PageSeoRecord | null }>('/api/seo', {
query: {
routeKey: resolvedRouteKey.value,
locale: locale.value
}
})
},
{ watch: [resolvedRouteKey, locale, skipDb] }
)
const dbSeo = computed(() => {
if (seoResponse.value?.code !== 200) return null
return seoResponse.value.data
})
const siteName = computed(() => SITE_NAME[locale.value])
const canonicalPath = computed(() => {
const customPath = options.path ? toValue(options.path) : route.path
return stripLocalePrefix(customPath.split('?')[0])
})
const canonicalUrl = computed(() =>
toAbsoluteUrl(siteUrl.value, toLocalePath(canonicalPath.value, locale.value))
)
const alternateLinks = computed(() => {
const enHref = toAbsoluteUrl(siteUrl.value, toLocalePath(canonicalPath.value, 'en'))
const zhHref = toAbsoluteUrl(siteUrl.value, toLocalePath(canonicalPath.value, 'zh'))
return [
{ rel: 'canonical', href: canonicalUrl.value },
{ rel: 'alternate', hreflang: 'en', href: enHref },
{ rel: 'alternate', hreflang: 'zh-CN', href: zhHref },
{ rel: 'alternate', hreflang: 'x-default', href: enHref }
]
})
useHead(() => {
const baseTitle = dbSeo.value?.title ?? toValue(options.title)
const titlePrefix = toValue(options.titlePrefix)?.trim()
const title = titlePrefix ? `${titlePrefix} | ${baseTitle}` : baseTitle
const description = dbSeo.value?.description ?? toValue(options.description)
const baseOgTitle = dbSeo.value?.ogTitle ?? toValue(options.ogTitle) ?? baseTitle
const ogTitle = titlePrefix ? `${titlePrefix} | ${baseOgTitle}` : baseOgTitle
const ogDescription =
dbSeo.value?.ogDescription ?? toValue(options.ogDescription) ?? description
const ogImage = toAbsoluteUrl(
siteUrl.value,
dbSeo.value?.ogImage ?? toValue(options.ogImage) ?? DEFAULT_OG_IMAGE
)
const ogType = dbSeo.value?.ogType ?? toValue(options.ogType) ?? 'website'
const keywords = dbSeo.value?.keywords ?? toValue(options.keywords)
const robots = dbSeo.value?.robots ?? toValue(options.robots) ?? 'index,follow'
const meta: Array<Record<string, string>> = [
{ name: 'description', content: description },
{ name: 'robots', content: robots },
{ property: 'og:title', content: ogTitle },
{ property: 'og:description', content: ogDescription },
{ property: 'og:type', content: ogType },
{ property: 'og:url', content: canonicalUrl.value },
{ property: 'og:image', content: ogImage },
{ property: 'og:site_name', content: siteName.value },
{ property: 'og:locale', content: locale.value === 'zh' ? 'zh_CN' : 'en_US' },
{ name: 'twitter:card', content: 'summary_large_image' },
{ name: 'twitter:title', content: ogTitle },
{ name: 'twitter:description', content: ogDescription },
{ name: 'twitter:image', content: ogImage }
]
if (keywords) {
meta.push({ name: 'keywords', content: keywords })
}
const pageSchemas = toValue(options.jsonLd)
const graphNodes = [
...buildGlobalJsonLd(siteUrl.value, siteName.value, locale.value),
...(pageSchemas
? Array.isArray(pageSchemas)
? pageSchemas
: [pageSchemas]
: [])
]
return {
title,
meta,
link: alternateLinks.value,
script: [
{
type: 'application/ld+json',
innerHTML: JSON.stringify(wrapJsonLdGraph(graphNodes))
}
]
}
})
}
+79
View File
@@ -0,0 +1,79 @@
import type { SiteLocale } from '~/types/locale'
import { uiEn } from '~/locales/ui/en'
import { uiZh } from '~/locales/ui/zh'
const LOCALE_COOKIE = 'umedglobal_locale'
const uiMessages = {
en: uiEn,
zh: uiZh
} as const
function getNestedRaw(obj: Record<string, unknown>, path: string): unknown {
const keys = path.split('.')
let current: unknown = obj
for (const key of keys) {
if (!current || typeof current !== 'object' || !(key in current)) {
return undefined
}
current = (current as Record<string, unknown>)[key]
}
return current
}
function getNestedValue(obj: Record<string, unknown>, path: string): string | undefined {
const current = getNestedRaw(obj, path)
return typeof current === 'string' ? current : undefined
}
export function useSiteLocale() {
const locale = useState<SiteLocale>('site-locale', () => 'en')
const cookie = useCookie<SiteLocale>(LOCALE_COOKIE, {
default: () => 'en',
maxAge: 60 * 60 * 24 * 365,
sameSite: 'lax'
})
const htmlLang = computed(() => (locale.value === 'zh' ? 'zh-CN' : 'en'))
useHead(() => ({
htmlAttrs: {
lang: htmlLang.value
}
}))
function t(key: string, params?: Record<string, string>): string {
const messages = uiMessages[locale.value]
const value = getNestedValue(messages as Record<string, unknown>, key)
?? getNestedValue(uiMessages.en as Record<string, unknown>, key)
?? key
if (!params) return value
return Object.entries(params).reduce(
(text, [name, replacement]) => text.replaceAll(`{${name}}`, replacement),
value
)
}
function setLocale(next: SiteLocale) {
locale.value = next
cookie.value = next
}
function tr<T>(key: string): T {
const value = getNestedRaw(uiMessages[locale.value] as Record<string, unknown>, key)
?? getNestedRaw(uiMessages.en as Record<string, unknown>, key)
return value as T
}
return {
locale,
htmlLang,
setLocale,
t,
tr
}
}
+271
View File
@@ -0,0 +1,271 @@
import { siteSearchIndex, type SiteSearchCategory, type SiteSearchEntry } from '~/data/site-search-index'
import { doctorZh } from '~/data/locale/zh/doctors.zh'
import { hospitalIntroZh } from '~/data/locale/zh/hospitals-intros.zh'
import { hospitalNameZh } from '~/data/locale/zh/hospitals-names.zh'
import { joinSearchText } from '~/utils/collect-text'
import type { SiteLocale } from '~/types/locale'
export type SiteSearchResult = SiteSearchEntry & {
score: number
excerpt: string
}
function localizeSearchEntry(entry: SiteSearchEntry, locale: SiteLocale): SiteSearchEntry {
if (locale !== 'zh') return entry
if (entry.id.startsWith('hospital:')) {
const hospitalId = entry.id.slice('hospital:'.length)
const name = hospitalNameZh[hospitalId] ?? entry.title
const intro = hospitalIntroZh[hospitalId] ?? ''
return {
...entry,
title: name,
body: joinSearchText(name, intro)
}
}
if (entry.id.startsWith('doctor:')) {
const doctorId = entry.id.slice('doctor:'.length)
const zh = doctorZh[doctorId]
if (!zh) return entry
return {
...entry,
title: zh.name,
body: joinSearchText(zh.name, zh.department, zh.hospitalName, zh.expertLabel, zh.expertInfo)
}
}
return entry
}
function getSearchIndex(locale: SiteLocale): SiteSearchEntry[] {
if (locale === 'en') return siteSearchIndex
return siteSearchIndex.map((entry) => localizeSearchEntry(entry, locale))
}
function normalizeQuery(query: string): string {
return query.trim().toLowerCase()
}
function tokenize(query: string): string[] {
return normalizeQuery(query)
.split(/\s+/)
.filter((token) => token.length > 0)
}
function buildExcerpt(body: string, tokens: string[], maxLen = 140): string {
const lower = body.toLowerCase()
let idx = -1
for (const token of tokens) {
const found = lower.indexOf(token)
if (found !== -1 && (idx === -1 || found < idx)) idx = found
}
if (idx === -1) {
return body.length <= maxLen ? body : `${body.slice(0, maxLen).trim()}…`
}
const start = Math.max(0, idx - 40)
const slice = body.slice(start, start + maxLen).trim()
const prefix = start > 0 ? '…' : ''
const suffix = start + maxLen < body.length ? '…' : ''
return `${prefix}${slice}${suffix}`
}
function scoreEntry(entry: SiteSearchEntry, tokens: string[], rawQuery: string): number {
if (!tokens.length) return 0
const title = entry.title.toLowerCase()
const body = entry.body.toLowerCase()
const q = normalizeQuery(rawQuery)
let score = 0
if (title === q) score += 120
else if (title.includes(q)) score += 70
if (body.includes(q)) score += 25
for (const token of tokens) {
if (title.includes(token)) score += 24
if (body.includes(token)) score += 8
}
return score
}
export function searchSite(query: string, limit = 24, locale: SiteLocale = 'en'): SiteSearchResult[] {
const tokens = tokenize(query)
if (!tokens.length) return []
const index = getSearchIndex(locale)
const scored = index
.map((entry) => {
const score = scoreEntry(entry, tokens, query)
if (score <= 0) return null
return {
...entry,
score,
excerpt: buildExcerpt(entry.body, tokens)
}
})
.filter((item): item is SiteSearchResult => item !== null)
.sort((a, b) => b.score - a.score || a.title.localeCompare(b.title))
return scored.slice(0, limit)
}
export function useSiteSearch() {
const { locale } = useSiteLocale()
function search(query: string, limit = 24) {
return searchSite(query, limit, locale.value)
}
return {
searchSite: search,
categories: [
'Page',
'Patient Guide',
'Hospital',
'Doctor',
'FAQ',
'Success Story',
'Health Screening',
'Legal'
] as const satisfies readonly SiteSearchCategory[]
}
}
+13
View File
@@ -0,0 +1,13 @@
import { resolveSiteUrl } from '~/utils/seo'
/** 根据当前访问域名解析站点 URL(umedglobal.cn / umedglobal.com.cn / umedglobal.com 等多域名共用) */
export function useSiteUrl() {
const config = useRuntimeConfig()
const fallback = String(config.public.siteUrl)
const requestURL = useRequestURL({
xForwardedHost: true,
xForwardedProto: true
})
return computed(() => resolveSiteUrl(fallback, requestURL.origin))
}