Skip to content

Commit

Permalink
定时发布相关
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed Jan 20, 2025
1 parent 54e1402 commit 5f7013a
Showing 1 changed file with 83 additions and 84 deletions.
167 changes: 83 additions & 84 deletions lib/db/getSiteData.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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++
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

/**
* 获取导航用的精减文章列表
Expand Down

0 comments on commit 5f7013a

Please sign in to comment.