사이트 코드와 홈페이지 위젯 추가 v1.5.34
This commit is contained in:
72
server/api/homepage-widget.get.js
Normal file
72
server/api/homepage-widget.get.js
Normal file
@@ -0,0 +1,72 @@
|
||||
import { getSiteSettings } from '../repositories/content-repository'
|
||||
import { getAnalyticsSummary } from '../repositories/analytics-repository'
|
||||
|
||||
/**
|
||||
* 초 단위 체류 시간을 짧은 한국어 문자열로 변환한다.
|
||||
* @param {number} seconds - 초 단위 시간
|
||||
* @returns {string} 표시 문자열
|
||||
*/
|
||||
const formatDuration = (seconds) => {
|
||||
const safeSeconds = Math.max(0, Math.round(Number(seconds) || 0))
|
||||
|
||||
if (safeSeconds < 60) {
|
||||
return `${safeSeconds}초`
|
||||
}
|
||||
|
||||
const minutes = Math.floor(safeSeconds / 60)
|
||||
const restSeconds = safeSeconds % 60
|
||||
|
||||
return restSeconds > 0 ? `${minutes}분 ${restSeconds}초` : `${minutes}분`
|
||||
}
|
||||
|
||||
/**
|
||||
* 숫자를 한국어 단위 문자열로 변환한다.
|
||||
* @param {number} value - 숫자 값
|
||||
* @param {string} unit - 단위
|
||||
* @returns {string} 표시 문자열
|
||||
*/
|
||||
const formatNumberLabel = (value, unit) => `${Number(value || 0).toLocaleString('ko-KR')}${unit}`
|
||||
|
||||
/**
|
||||
* gethomepage 커스텀 API용 사이트 요약 위젯 데이터를 반환한다.
|
||||
* @returns {Promise<Object>} 위젯 데이터
|
||||
*/
|
||||
export default defineEventHandler(async () => {
|
||||
const [settings, summary] = await Promise.all([
|
||||
getSiteSettings(),
|
||||
getAnalyticsSummary({ days: 1 })
|
||||
])
|
||||
const avgEngagedSeconds = summary.todayAvgEngagedSeconds || summary.avgEngagedSeconds || 0
|
||||
|
||||
return {
|
||||
title: settings.title || 'sori.studio',
|
||||
updatedAt: new Date().toISOString(),
|
||||
todayVisitors: summary.todayVisitors,
|
||||
todayPageViews: summary.todayPageViews,
|
||||
onlineNow: summary.onlineNow,
|
||||
loggedInNow: summary.loggedInNow,
|
||||
avgEngagedSeconds,
|
||||
items: [
|
||||
{
|
||||
name: '오늘 방문자',
|
||||
label: formatNumberLabel(summary.todayVisitors, '명'),
|
||||
value: summary.todayVisitors
|
||||
},
|
||||
{
|
||||
name: '오늘 페이지뷰',
|
||||
label: formatNumberLabel(summary.todayPageViews, '회'),
|
||||
value: summary.todayPageViews
|
||||
},
|
||||
{
|
||||
name: '현재 접속자',
|
||||
label: formatNumberLabel(summary.onlineNow, '명'),
|
||||
value: summary.onlineNow
|
||||
},
|
||||
{
|
||||
name: '평균 체류',
|
||||
label: formatDuration(avgEngagedSeconds),
|
||||
value: avgEngagedSeconds
|
||||
}
|
||||
]
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user