From bdd109381a4c73f02ce9c30f02d0d7d31d2bda52 Mon Sep 17 00:00:00 2001 From: anime Date: Mon, 7 Jul 2025 17:32:26 +0800 Subject: [PATCH] feat(mailEncrypt): add email encryption/decryption functions - refactor email handling logic in handleEmailClick - add new encryptEmail and decryptEmail utility functions - improve error handling for decryption process - simplify the email click handler implementation --- lib/notion/getNotionConfig.js | 3 ++- lib/plugins/mailEncrypt.js | 23 ++++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/notion/getNotionConfig.js b/lib/notion/getNotionConfig.js index 3e111fcd..d32b46fa 100644 --- a/lib/notion/getNotionConfig.js +++ b/lib/notion/getNotionConfig.js @@ -10,6 +10,7 @@ import { getDateValue, getTextContent } from 'notion-utils' import { deepClone } from '../utils' import getAllPageIds from './getAllPageIds' import { getPage } from './getPostBlocks' +import { encryptEmail } from '@/lib/plugins/mailEncrypt' /** * 从Notion中读取Config配置表 @@ -159,7 +160,7 @@ export async function getConfigMapFromConfigPage(allPages) { // console.log('[Notion配置]', config.key, config.value) if (config.key === 'CONTACT_EMAIL') { notionConfig[config.key] = - (config.value && btoa(unescape(encodeURIComponent(config.value)))) || null + (config.value && encryptEmail(config.value)) || null } else { notionConfig[config.key] = parseTextToJson(config.value) || config.value || null diff --git a/lib/plugins/mailEncrypt.js b/lib/plugins/mailEncrypt.js index 091c39b3..a7c6c2bc 100644 --- a/lib/plugins/mailEncrypt.js +++ b/lib/plugins/mailEncrypt.js @@ -1,12 +1,21 @@ export const handleEmailClick = (e, emailIcon, CONTACT_EMAIL) => { if (CONTACT_EMAIL && emailIcon && !emailIcon.current.href) { e.preventDefault() - try { - const email = decodeURIComponent(escape(atob(CONTACT_EMAIL))) - emailIcon.current.href = `mailto:${email}` - emailIcon.current.click() - } catch (error) { - console.error('解密邮箱失败:', error) - } + const email = decryptEmail(CONTACT_EMAIL) + emailIcon.current.href = `mailto:${email}` + emailIcon.current.click() + } +} + +export const encryptEmail = email => { + return btoa(unescape(encodeURIComponent(email))) +} + +export const decryptEmail = encryptedEmail => { + try { + return decodeURIComponent(escape(atob(encryptedEmail))) + } catch (error) { + console.error('解密邮箱失败:', error) + return encryptedEmail } }