From 5f7013a197d62d6e9fdd671021d9070d275cbe76 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Mon, 20 Jan 2025 12:35:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E6=97=B6=E5=8F=91=E5=B8=83=E7=9B=B8?= =?UTF-8?q?=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/db/getSiteData.js | 167 +++++++++++++++++++++--------------------- 1 file changed, 83 insertions(+), 84 deletions(-) diff --git a/lib/db/getSiteData.js b/lib/db/getSiteData.js index b7229586..53fbd724 100755 --- a/lib/db/getSiteData.js +++ b/lib/db/getSiteData.js @@ -1,4 +1,5 @@ import BLOG from '@/blog.config' +import { getOrSetDataWithCache } from '@/lib/cache/cache_manager' import { getAllCategories } from '@/lib/notion/getAllCategories' import getAllPageIds from '@/lib/notion/getAllPageIds' import { getAllTags } from '@/lib/notion/getAllTags' @@ -12,7 +13,6 @@ import { deepClone } from '@/lib/utils' import { idToUuid } from 'notion-utils' import { siteConfig } from '../config' import { extractLangId, extractLangPrefix, getShortId } from '../utils/pageId' -import { getOrSetDataWithCache } from '@/lib/cache/cache_manager' export { getAllTags } from '../notion/getAllTags' export { getPost } from '../notion/getNotionPost' @@ -230,7 +230,7 @@ async function convertNotionToSiteDate(pageId, from, pageRecordMap) { // 文章计数 let postCount = 0 - // 查找所有的Post和Page + // 查找所有的Post和Page const allPages = collectionData.filter(post => { if (post?.type === 'Post' && post.status === 'Published') { postCount++ @@ -344,22 +344,32 @@ function handleDataBeforeReturn(db) { db.allNavPages = cleanPages(db?.allNavPages, db.tagOptions) db.allPages = cleanPages(db.allPages, db.tagOptions) db.latestPosts = cleanPages(db.latestPosts, db.tagOptions) - - const POST_SCHEDULE_PUBLISH = siteConfig('POST_SCHEDULE_PUBLISH', null, db.NOTION_CONFIG) - if(POST_SCHEDULE_PUBLISH){ + + const POST_SCHEDULE_PUBLISH = siteConfig( + 'POST_SCHEDULE_PUBLISH', + null, + db.NOTION_CONFIG + ) + if (POST_SCHEDULE_PUBLISH) { // console.log('[定时发布] 开启检测') - db.allPages?.forEach(p=>{ - // 新特性,判断文章的发布和下架时间,如果不在有效期内则进行下架处理 - const publish = isInRange(p.title, p.date) - if (!publish) { - console.log('[定时发布] 隐藏-->', p.title) - // 隐藏 - p.status = 'Invisible' - } - }) + db.allPages?.forEach(p => { + // 新特性,判断文章的发布和下架时间,如果不在有效期内则进行下架处理 + const publish = isInRange(p.title, p.date) + if (!publish) { + console.log( + '[定时发布] 隐藏--> 文章:', + p.title, + '当前时间:', + Date.now(), + '目标', + p.date + ) + // 隐藏 + p.status = 'Invisible' + } + }) } - return db } @@ -620,78 +630,67 @@ function getSiteInfo({ collection, block, NOTION_CONFIG }) { /** * 判断文章是否在发布时间内 - * @param {*} param0 - * @returns + * @param {string} title - 文章标题 + * @param {Object} date - 时间范围参数 + * @param {string} date.start_date - 开始日期(格式:YYYY-MM-DD) + * @param {string} date.start_time - 开始时间(可选,格式:HH:mm) + * @param {string} date.end_date - 结束日期(格式:YYYY-MM-DD) + * @param {string} date.end_time - 结束时间(可选,格式:HH:mm) + * @param {string} date.time_zone - 时区(IANA格式,如 "Asia/Shanghai") + * @returns {boolean} 是否在范围内 */ function isInRange(title, date = {}) { - const { start_date, start_time, end_date, end_time, time_zone } = date; - - // 默认使用北京时间 (Asia/Shanghai) - const effectiveTimeZone = time_zone || "Asia/Shanghai"; - - // 辅助函数:根据时区和日期时间字符串创建 Date 对象 - function parseDateTime(date, time, timeZone) { - if (!date) return null; // 如果没有日期,返回 null - const dateTimeString = `${date}T${time || "00:00"}:00`; // 默认时间为 00:00 - // 创建时区正确的 Date 对象 - return new Date( - new Date(dateTimeString).toLocaleString("en-US", { timeZone }) - ); - } - - // 获取当前时间(基于目标时区) - function getCurrentDateTimeInZone(timeZone) { - const now = new Date(); - const localString = now.toLocaleString("en-US", { timeZone }); - return new Date(localString); - } - - // 获取当前时间(转换为目标时区) - const currentDateTimeInZone = getCurrentDateTimeInZone(effectiveTimeZone); - - // 判断开始时间范围 - let startDateTime = null; - if (start_date) { - startDateTime = parseDateTime(start_date, start_time, effectiveTimeZone); - } - - // 判断结束时间范围 - let endDateTime = null; - if (end_date) { - endDateTime = parseDateTime(end_date, end_time || "23:59", effectiveTimeZone); - } - - let isInRange = true; - - // 检查是否在开始时间之后 - if (startDateTime && currentDateTimeInZone < startDateTime) { - isInRange = false; - console.log( - "[定时发布] 未到发布时间:", - title, - "指定时间:", - startDateTime, - "当前时间:", - currentDateTimeInZone - ); - } - - // 检查是否在结束时间之前 - if (endDateTime && currentDateTimeInZone > endDateTime) { - isInRange = false; - console.log( - "[定时发布] 超过下架时间:", - title, - "指定时间:", - endDateTime, - "当前时间:", - currentDateTimeInZone - ); - } - - return isInRange; // 如果都符合条件,返回 true + const { + start_date, + start_time = '00:00', + end_date, + end_time = '23:59', + time_zone = 'Asia/Shanghai' + } = date + + // 获取当前时间的时间戳(基于目标时区) + const currentTimestamp = Date.now() + + // 辅助函数:生成指定日期时间的时间戳(基于目标时区) + function getTimestamp(date, time) { + if (!date) return null + const dateTimeString = `${date}T${time}:00` // 拼接日期时间 + return new Date( + new Date(dateTimeString).toLocaleString('en-US', { timeZone: time_zone }) + ).getTime() } - + + // 获取开始和结束时间的时间戳 + const startTimestamp = getTimestamp(start_date, start_time) + const endTimestamp = getTimestamp(end_date, end_time) + + // 判断是否在范围内 + if (startTimestamp && currentTimestamp < startTimestamp) { + console.log( + '[定时发布] 未到发布时间:', + title, + '指定时间:', + new Date(startTimestamp), + '当前时间:', + new Date(currentTimestamp) + ) + return false + } + + if (endTimestamp && currentTimestamp > endTimestamp) { + console.log( + '[定时发布] 超过下架时间:', + title, + '指定时间:', + new Date(endTimestamp), + '当前时间:', + new Date(currentTimestamp) + ) + return false + } + + return true +} /** * 获取导航用的精减文章列表