Merge branch 'dev' of https://github.com/mtvpls/moontvplus-dev into dev
This commit is contained in:
@@ -206,6 +206,24 @@ export interface AdminConfig {
|
||||
Password?: string; // 密码认证(备选)
|
||||
DisableVideoPreview?: boolean; // 禁用预览视频,直接返回直连链接
|
||||
};
|
||||
EmailConfig?: {
|
||||
enabled: boolean; // 是否启用邮件通知
|
||||
provider: 'smtp' | 'resend'; // 邮件发送方式
|
||||
// SMTP配置
|
||||
smtp?: {
|
||||
host: string; // SMTP服务器地址
|
||||
port: number; // SMTP端口(25/465/587)
|
||||
secure: boolean; // 是否使用SSL/TLS
|
||||
user: string; // SMTP用户名
|
||||
password: string; // SMTP密码
|
||||
from: string; // 发件人邮箱
|
||||
};
|
||||
// Resend配置
|
||||
resend?: {
|
||||
apiKey: string; // Resend API Key
|
||||
from: string; // 发件人邮箱
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export interface AdminConfigResult {
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
import nodemailer from 'nodemailer';
|
||||
import type { AdminConfig } from './admin.types';
|
||||
|
||||
export interface EmailOptions {
|
||||
to: string;
|
||||
subject: string;
|
||||
html: string;
|
||||
}
|
||||
|
||||
export class EmailService {
|
||||
/**
|
||||
* 通过SMTP发送邮件
|
||||
*/
|
||||
static async sendViaSMTP(
|
||||
config: NonNullable<AdminConfig['EmailConfig']>['smtp'],
|
||||
options: EmailOptions
|
||||
): Promise<void> {
|
||||
if (!config) {
|
||||
throw new Error('SMTP配置不存在');
|
||||
}
|
||||
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: config.host,
|
||||
port: config.port,
|
||||
secure: config.secure,
|
||||
auth: {
|
||||
user: config.user,
|
||||
pass: config.password,
|
||||
},
|
||||
});
|
||||
|
||||
await transporter.sendMail({
|
||||
from: config.from,
|
||||
to: options.to,
|
||||
subject: options.subject,
|
||||
html: options.html,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过Resend API发送邮件
|
||||
*/
|
||||
static async sendViaResend(
|
||||
config: NonNullable<AdminConfig['EmailConfig']>['resend'],
|
||||
options: EmailOptions
|
||||
): Promise<void> {
|
||||
if (!config) {
|
||||
throw new Error('Resend配置不存在');
|
||||
}
|
||||
|
||||
const response = await fetch('https://api.resend.com/emails', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${config.apiKey}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
from: config.from,
|
||||
to: options.to,
|
||||
subject: options.subject,
|
||||
html: options.html,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(`Resend API错误: ${response.statusText} - ${errorText}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一发送接口
|
||||
*/
|
||||
static async send(
|
||||
emailConfig: AdminConfig['EmailConfig'],
|
||||
options: EmailOptions
|
||||
): Promise<void> {
|
||||
if (!emailConfig || !emailConfig.enabled) {
|
||||
console.log('邮件通知未启用,跳过发送');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (emailConfig.provider === 'smtp' && emailConfig.smtp) {
|
||||
await this.sendViaSMTP(emailConfig.smtp, options);
|
||||
console.log(`邮件已通过SMTP发送至: ${options.to}`);
|
||||
} else if (emailConfig.provider === 'resend' && emailConfig.resend) {
|
||||
await this.sendViaResend(emailConfig.resend, options);
|
||||
console.log(`邮件已通过Resend发送至: ${options.to}`);
|
||||
} else {
|
||||
throw new Error('邮件配置不完整');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('邮件发送失败:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送测试邮件
|
||||
*/
|
||||
static async sendTestEmail(
|
||||
emailConfig: AdminConfig['EmailConfig'],
|
||||
toEmail: string,
|
||||
siteName?: string
|
||||
): Promise<void> {
|
||||
const displayName = siteName || 'MoonTVPlus';
|
||||
await this.send(emailConfig, {
|
||||
to: toEmail,
|
||||
subject: `测试邮件 - ${displayName}`,
|
||||
html: `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<style>
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
|
||||
line-height: 1.6;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
.container {
|
||||
max-width: 600px;
|
||||
margin: 20px auto;
|
||||
background: white;
|
||||
border-radius: 10px;
|
||||
overflow: hidden;
|
||||
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
||||
}
|
||||
.header {
|
||||
background: white;
|
||||
color: #333;
|
||||
padding: 30px 20px;
|
||||
text-align: center;
|
||||
border-bottom: 2px solid #f0f0f0;
|
||||
}
|
||||
.header h1 {
|
||||
margin: 0;
|
||||
font-size: 24px;
|
||||
font-weight: 600;
|
||||
}
|
||||
.content {
|
||||
padding: 30px 20px;
|
||||
background: white;
|
||||
}
|
||||
.content p {
|
||||
color: #333;
|
||||
margin: 10px 0;
|
||||
}
|
||||
.footer {
|
||||
padding: 20px;
|
||||
text-align: center;
|
||||
color: #999;
|
||||
font-size: 12px;
|
||||
background: white;
|
||||
border-top: 1px solid #eee;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="header">
|
||||
<h1>📧 测试邮件</h1>
|
||||
</div>
|
||||
<div class="content">
|
||||
<p>这是一封来自 ${displayName} 的测试邮件。</p>
|
||||
<p>如果您收到这封邮件,说明邮件配置正确!</p>
|
||||
<p style="color: #666;">发送时间: ${new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' })}</p>
|
||||
</div>
|
||||
<div class="footer">
|
||||
<p>此邮件由 ${displayName} 自动发送</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,265 @@
|
||||
/**
|
||||
* 邮件模板
|
||||
*/
|
||||
|
||||
export interface FavoriteUpdate {
|
||||
title: string;
|
||||
oldEpisodes: number;
|
||||
newEpisodes: number;
|
||||
url: string;
|
||||
cover?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 收藏更新邮件模板
|
||||
*/
|
||||
export function getFavoriteUpdateEmailTemplate(
|
||||
userName: string,
|
||||
updates: FavoriteUpdate[],
|
||||
siteUrl: string,
|
||||
siteName?: string
|
||||
): string {
|
||||
const updatesList = updates
|
||||
.map(
|
||||
(u) => `
|
||||
<div style="margin: 15px 0; padding: 15px; background: white; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.1);">
|
||||
${
|
||||
u.cover
|
||||
? `<img src="${u.cover}" alt="${u.title}" style="width: 100%; max-width: 200px; border-radius: 5px; margin-bottom: 10px;" />`
|
||||
: ''
|
||||
}
|
||||
<div style="font-size: 16px; font-weight: bold; margin-bottom: 8px;">${u.title}</div>
|
||||
<div style="color: #666; margin-bottom: 10px;">
|
||||
更新:第 ${u.oldEpisodes} 集 → <span style="color: #4F46E5; font-weight: bold;">第 ${u.newEpisodes} 集</span>
|
||||
</div>
|
||||
<a href="${u.url}" style="display: inline-block; padding: 8px 16px; background: #4F46E5; color: white; text-decoration: none; border-radius: 5px; font-size: 14px;">立即观看</a>
|
||||
</div>
|
||||
`
|
||||
)
|
||||
.join('');
|
||||
|
||||
return `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<style>
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
|
||||
line-height: 1.6;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
.container {
|
||||
max-width: 600px;
|
||||
margin: 20px auto;
|
||||
background: white;
|
||||
border-radius: 10px;
|
||||
overflow: hidden;
|
||||
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
||||
}
|
||||
.header {
|
||||
background: white;
|
||||
color: #333;
|
||||
padding: 30px 20px;
|
||||
text-align: center;
|
||||
border-bottom: 2px solid #f0f0f0;
|
||||
}
|
||||
.header h1 {
|
||||
margin: 0;
|
||||
font-size: 24px;
|
||||
font-weight: 600;
|
||||
}
|
||||
.content {
|
||||
padding: 30px 20px;
|
||||
background: white;
|
||||
}
|
||||
.greeting {
|
||||
font-size: 16px;
|
||||
color: #333;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.footer {
|
||||
padding: 20px;
|
||||
text-align: center;
|
||||
color: #999;
|
||||
font-size: 12px;
|
||||
background: white;
|
||||
border-top: 1px solid #eee;
|
||||
}
|
||||
.footer a {
|
||||
color: #4F46E5;
|
||||
text-decoration: none;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="header">
|
||||
<h1>📺 收藏更新通知</h1>
|
||||
</div>
|
||||
<div class="content">
|
||||
<div class="greeting">
|
||||
Hi <strong>${userName}</strong>,
|
||||
</div>
|
||||
<p style="color: #666; margin-bottom: 20px;">您收藏的以下影片有更新:</p>
|
||||
${updatesList}
|
||||
<p style="color: #666; margin-top: 20px;">快去观看吧!</p>
|
||||
</div>
|
||||
<div class="footer">
|
||||
<p>此邮件由 <a href="${siteUrl}">${siteName || 'MoonTVPlus'}</a> 自动发送</p>
|
||||
<p>如不想接收此类邮件,请在用户设置中关闭邮件通知</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 单个收藏更新邮件模板(简化版)
|
||||
*/
|
||||
export function getSingleFavoriteUpdateEmailTemplate(
|
||||
userName: string,
|
||||
title: string,
|
||||
oldEpisodes: number,
|
||||
newEpisodes: number,
|
||||
url: string,
|
||||
cover?: string,
|
||||
siteName?: string
|
||||
): string {
|
||||
return getFavoriteUpdateEmailTemplate(
|
||||
userName,
|
||||
[{ title, oldEpisodes, newEpisodes, url, cover }],
|
||||
url.split('/play')[0] || 'http://localhost:3000',
|
||||
siteName
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量收藏更新邮件模板(每日汇总)
|
||||
*/
|
||||
export function getBatchFavoriteUpdateEmailTemplate(
|
||||
userName: string,
|
||||
updates: FavoriteUpdate[],
|
||||
siteUrl: string,
|
||||
siteName?: string
|
||||
): string {
|
||||
const totalUpdates = updates.length;
|
||||
const totalNewEpisodes = updates.reduce(
|
||||
(sum, u) => sum + (u.newEpisodes - u.oldEpisodes),
|
||||
0
|
||||
);
|
||||
|
||||
const updatesList = updates
|
||||
.map(
|
||||
(u) => `
|
||||
<div style="margin: 15px 0; padding: 15px; background: white; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.1);">
|
||||
<div style="display: flex; align-items: center; gap: 15px;">
|
||||
${
|
||||
u.cover
|
||||
? `<img src="${u.cover}" alt="${u.title}" style="width: 80px; height: 120px; object-fit: cover; border-radius: 5px;" />`
|
||||
: ''
|
||||
}
|
||||
<div style="flex: 1;">
|
||||
<div style="font-size: 16px; font-weight: bold; margin-bottom: 8px;">${u.title}</div>
|
||||
<div style="color: #666; margin-bottom: 10px;">
|
||||
第 ${u.oldEpisodes} 集 → <span style="color: #4F46E5; font-weight: bold;">第 ${u.newEpisodes} 集</span>
|
||||
<span style="color: #10b981; font-weight: bold;">(+${u.newEpisodes - u.oldEpisodes})</span>
|
||||
</div>
|
||||
<a href="${u.url}" style="display: inline-block; padding: 6px 12px; background: #4F46E5; color: white; text-decoration: none; border-radius: 5px; font-size: 13px;">立即观看</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`
|
||||
)
|
||||
.join('');
|
||||
|
||||
return `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<style>
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
|
||||
line-height: 1.6;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
.container {
|
||||
max-width: 600px;
|
||||
margin: 20px auto;
|
||||
background: white;
|
||||
border-radius: 10px;
|
||||
overflow: hidden;
|
||||
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
||||
}
|
||||
.header {
|
||||
background: white;
|
||||
color: #333;
|
||||
padding: 30px 20px;
|
||||
text-align: center;
|
||||
border-bottom: 2px solid #f0f0f0;
|
||||
}
|
||||
.header h1 {
|
||||
margin: 0 0 10px 0;
|
||||
font-size: 24px;
|
||||
font-weight: 600;
|
||||
}
|
||||
.header .stats {
|
||||
font-size: 14px;
|
||||
color: #666;
|
||||
}
|
||||
.content {
|
||||
padding: 30px 20px;
|
||||
background: white;
|
||||
}
|
||||
.greeting {
|
||||
font-size: 16px;
|
||||
color: #333;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.footer {
|
||||
padding: 20px;
|
||||
text-align: center;
|
||||
color: #999;
|
||||
font-size: 12px;
|
||||
background: white;
|
||||
border-top: 1px solid #eee;
|
||||
}
|
||||
.footer a {
|
||||
color: #4F46E5;
|
||||
text-decoration: none;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="header">
|
||||
<h1>📺 收藏更新汇总</h1>
|
||||
<div class="stats">
|
||||
${totalUpdates} 部影片更新 · 共 ${totalNewEpisodes} 集新内容
|
||||
</div>
|
||||
</div>
|
||||
<div class="content">
|
||||
<div class="greeting">
|
||||
Hi <strong>${userName}</strong>,
|
||||
</div>
|
||||
<p style="color: #666; margin-bottom: 20px;">您收藏的影片有以下更新:</p>
|
||||
${updatesList}
|
||||
<p style="color: #666; margin-top: 20px;">快去观看吧!</p>
|
||||
</div>
|
||||
<div class="footer">
|
||||
<p>此邮件由 <a href="${siteUrl}">${siteName || 'MoonTVPlus'}</a> 自动发送</p>
|
||||
<p>如不想接收此类邮件,请在用户设置中关闭邮件通知</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`;
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { createClient, RedisClientType } from 'redis';
|
||||
|
||||
import { AdminConfig } from './admin.types';
|
||||
import { Favorite, IStorage, PlayRecord, SkipConfig } from './types';
|
||||
import { userInfoCache } from './user-cache';
|
||||
|
||||
// 搜索历史最大条数
|
||||
const SEARCH_HISTORY_LIMIT = 20;
|
||||
@@ -660,6 +661,8 @@ export abstract class BaseRedisStorage implements IStorage {
|
||||
favorite_migrated?: boolean;
|
||||
skip_migrated?: boolean;
|
||||
last_movie_request_time?: number;
|
||||
email?: string;
|
||||
emailNotifications?: boolean;
|
||||
} | null> {
|
||||
const userInfo = await this.withRetry(() =>
|
||||
this.client.hGetAll(this.userInfoKey(userName))
|
||||
@@ -680,6 +683,8 @@ export abstract class BaseRedisStorage implements IStorage {
|
||||
favorite_migrated: userInfo.favorite_migrated === 'true',
|
||||
skip_migrated: userInfo.skip_migrated === 'true',
|
||||
last_movie_request_time: userInfo.last_movie_request_time ? parseInt(userInfo.last_movie_request_time, 10) : undefined,
|
||||
email: userInfo.email,
|
||||
emailNotifications: userInfo.emailNotifications === 'true',
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1290,4 +1295,31 @@ export abstract class BaseRedisStorage implements IStorage {
|
||||
async removeUserMovieRequest(userName: string, requestId: string): Promise<void> {
|
||||
await this.withRetry(() => this.client.sRem(this.userMovieRequestsKey(userName), requestId));
|
||||
}
|
||||
|
||||
// ---------- 用户邮箱相关 ----------
|
||||
async getUserEmail(userName: string): Promise<string | null> {
|
||||
const userInfo = await this.getUserInfoV2(userName);
|
||||
return userInfo?.email || null;
|
||||
}
|
||||
|
||||
async setUserEmail(userName: string, email: string): Promise<void> {
|
||||
await this.withRetry(() =>
|
||||
this.client.hSet(this.userInfoKey(userName), 'email', email)
|
||||
);
|
||||
// 清除缓存
|
||||
userInfoCache?.delete(userName);
|
||||
}
|
||||
|
||||
async getEmailNotificationPreference(userName: string): Promise<boolean> {
|
||||
const userInfo = await this.getUserInfoV2(userName);
|
||||
return userInfo?.emailNotifications || false;
|
||||
}
|
||||
|
||||
async setEmailNotificationPreference(userName: string, enabled: boolean): Promise<void> {
|
||||
await this.withRetry(() =>
|
||||
this.client.hSet(this.userInfoKey(userName), 'emailNotifications', enabled.toString())
|
||||
);
|
||||
// 清除缓存
|
||||
userInfoCache?.delete(userName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,7 +140,15 @@ export interface IStorage {
|
||||
favorite_migrated?: boolean;
|
||||
skip_migrated?: boolean;
|
||||
last_movie_request_time?: number;
|
||||
email?: string; // 用户邮箱
|
||||
emailNotifications?: boolean; // 是否接收邮件通知
|
||||
} | null>;
|
||||
|
||||
// 用户邮箱相关
|
||||
getUserEmail?(userName: string): Promise<string | null>;
|
||||
setUserEmail?(userName: string, email: string): Promise<void>;
|
||||
getEmailNotificationPreference?(userName: string): Promise<boolean>;
|
||||
setEmailNotificationPreference?(userName: string, enabled: boolean): Promise<void>;
|
||||
}
|
||||
|
||||
// 搜索结果数据结构
|
||||
|
||||
@@ -590,6 +590,8 @@ export class UpstashRedisStorage implements IStorage {
|
||||
favorite_migrated?: boolean;
|
||||
skip_migrated?: boolean;
|
||||
last_movie_request_time?: number;
|
||||
email?: string;
|
||||
emailNotifications?: boolean;
|
||||
} | null> {
|
||||
// 先从缓存获取
|
||||
const cached = userInfoCache?.get(userName);
|
||||
@@ -688,6 +690,8 @@ export class UpstashRedisStorage implements IStorage {
|
||||
? userInfo.last_movie_request_time
|
||||
: parseInt(userInfo.last_movie_request_time as string, 10))
|
||||
: undefined,
|
||||
email: userInfo.email as string | undefined,
|
||||
emailNotifications: userInfo.emailNotifications === 'true' || userInfo.emailNotifications === true,
|
||||
};
|
||||
|
||||
// 存入缓存
|
||||
@@ -1335,6 +1339,33 @@ export class UpstashRedisStorage implements IStorage {
|
||||
async removeUserMovieRequest(userName: string, requestId: string): Promise<void> {
|
||||
await withRetry(() => this._client.srem(this.userMovieRequestsKey(userName), requestId));
|
||||
}
|
||||
|
||||
// ---------- 用户邮箱相关 ----------
|
||||
async getUserEmail(userName: string): Promise<string | null> {
|
||||
const userInfo = await this.getUserInfoV2(userName);
|
||||
return userInfo?.email || null;
|
||||
}
|
||||
|
||||
async setUserEmail(userName: string, email: string): Promise<void> {
|
||||
await withRetry(() =>
|
||||
this._client.hset(this.userInfoKey(userName), { email })
|
||||
);
|
||||
// 清除缓存
|
||||
userInfoCache?.delete(userName);
|
||||
}
|
||||
|
||||
async getEmailNotificationPreference(userName: string): Promise<boolean> {
|
||||
const userInfo = await this.getUserInfoV2(userName);
|
||||
return userInfo?.emailNotifications || false;
|
||||
}
|
||||
|
||||
async setEmailNotificationPreference(userName: string, enabled: boolean): Promise<void> {
|
||||
await withRetry(() =>
|
||||
this._client.hset(this.userInfoKey(userName), { emailNotifications: enabled.toString() })
|
||||
);
|
||||
// 清除缓存
|
||||
userInfoCache?.delete(userName);
|
||||
}
|
||||
}
|
||||
|
||||
// 单例 Upstash Redis 客户端
|
||||
|
||||
Reference in New Issue
Block a user