Skip to content

Commit

Permalink
修复定时文章功能
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed Jan 19, 2025
1 parent ce44bd2 commit 9ba0fe1
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 74 deletions.
2 changes: 1 addition & 1 deletion lib/cache/cache_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function getOrSetDataWithCustomCache(
* @returns
*/
export async function getDataFromCache(key, force) {
if (BLOG.ENABLE_CACHE || force) {
if (JSON.parse(BLOG.ENABLE_CACHE) || force) {
const dataFromCache = await getApi().getCache(key)
if (!dataFromCache || JSON.stringify(dataFromCache) === '[]') {
return null
Expand Down
157 changes: 84 additions & 73 deletions lib/db/getSiteData.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,20 +230,12 @@ 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++
}

// 新特性,判断文章的发布和下架时间,如果不在有效期内则进行下架处理
const publish = isInRange(post.date)
if (!publish && siteConfig('POST_SCHEDULE_PUBLISH', '', NOTION_CONFIG)) {
console.log(post.title, '未处于发布时段 [', post.date, '] 内,将强制隐藏')
// 隐藏
post.status = 'Invisible'
}

return (
post &&
post?.slug &&
Expand Down Expand Up @@ -352,6 +344,22 @@ 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){
// console.log('[定时发布] 开启检测')
db.allPages?.forEach(p=>{
// 新特性,判断文章的发布和下架时间,如果不在有效期内则进行下架处理
const publish = isInRange(p.title, p.date)
if (!publish) {
console.log('[定时发布] 隐藏-->', p.title)
// 隐藏
p.status = 'Invisible'
}
})
}


return db
}

Expand Down Expand Up @@ -615,72 +623,75 @@ function getSiteInfo({ collection, block, NOTION_CONFIG }) {
* @param {*} param0
* @returns
*/
function isInRange(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${getTimezoneOffset(timeZone)}`
return new Date(dateTimeString) // 返回一个 Date 对象
}

// 辅助函数:获取时区的偏移量
function getTimezoneOffset(timeZone) {
const date = new Date()
const options = { timeZone, hour12: false, timeZoneName: 'short' }
const timeString = new Intl.DateTimeFormat('en-US', options).format(date)
const match = timeString.match(/([A-Za-z]+)([+\-]\d{1,2})(\d{2})/) // 捕获时区信息,确保偏移小时和分钟
if (match) {
const offset = match[2] + match[3] // 组合偏移小时和分钟
return offset // 返回格式:+08:00 或 -03:00
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 })
);
}
return '' // 默认没有时区偏移
}

// 当前时间(转换为目标时区)
const currentDateTime = new Date()
const currentDateTimeInZone = parseDateTime(
currentDateTime.toISOString().slice(0, 10),
currentDateTime.toISOString().slice(11, 16),
effectiveTimeZone
)

// 判断开始时间范围
let startDateTime = null
if (start_date) {
startDateTime = parseDateTime(
start_date,
start_time || '00:00',
effectiveTimeZone
) // 如果没有 start_time 默认设置为 '00:00'
}

// 判断结束时间范围
let endDateTime = null
if (end_date) {
endDateTime = parseDateTime(
end_date,
end_time || '23:59',
effectiveTimeZone
) // 如果没有 end_time 默认设置为 '23:59'
}

// 如果有 start_date,当前时间必须大于等于 start_date 和 start_time
if (startDateTime && currentDateTimeInZone < startDateTime) {
return false
}

// 如果有 end_date,当前时间必须小于等于 end_date 和 end_time
if (endDateTime && currentDateTimeInZone > endDateTime) {
return false

// 获取当前时间(基于目标时区)
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
}

return true // 如果都通过了判断,返回 true
}


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

0 comments on commit 9ba0fe1

Please sign in to comment.