tvbox支持根据用户细分
This commit is contained in:
11
migrations/004_add_tvbox_subscribe_token.sql
Normal file
11
migrations/004_add_tvbox_subscribe_token.sql
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
-- ============================================
|
||||||
|
-- 添加 TVBox 订阅 token 字段
|
||||||
|
-- 版本: 004
|
||||||
|
-- 创建时间: 2026-02-25
|
||||||
|
-- ============================================
|
||||||
|
|
||||||
|
-- 为 users 表添加 tvbox_subscribe_token 字段
|
||||||
|
ALTER TABLE users ADD COLUMN tvbox_subscribe_token TEXT;
|
||||||
|
|
||||||
|
-- 创建索引以加速 token 查询
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_users_tvbox_token ON users(tvbox_subscribe_token) WHERE tvbox_subscribe_token IS NOT NULL;
|
||||||
14
migrations/postgres/004_add_tvbox_subscribe_token.sql
Normal file
14
migrations/postgres/004_add_tvbox_subscribe_token.sql
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
-- ============================================
|
||||||
|
-- 添加 TVBox 订阅 token 字段
|
||||||
|
-- 版本: 004
|
||||||
|
-- 创建时间: 2026-02-25
|
||||||
|
-- ============================================
|
||||||
|
|
||||||
|
-- 为 users 表添加 tvbox_subscribe_token 字段
|
||||||
|
ALTER TABLE users ADD COLUMN IF NOT EXISTS tvbox_subscribe_token TEXT;
|
||||||
|
|
||||||
|
-- 创建索引以加速 token 查询
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_users_tvbox_token ON users(tvbox_subscribe_token) WHERE tvbox_subscribe_token IS NOT NULL;
|
||||||
|
|
||||||
|
-- 添加注释
|
||||||
|
COMMENT ON COLUMN users.tvbox_subscribe_token IS 'TVBox订阅token,用于用户独立订阅';
|
||||||
@@ -29,11 +29,29 @@ export async function GET(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证 TVBox Token
|
// 验证 TVBox Token(从路径中获取)
|
||||||
const requestToken = params.token;
|
const requestToken = params.token;
|
||||||
const subscribeToken = process.env.TVBOX_SUBSCRIBE_TOKEN;
|
const globalToken = process.env.TVBOX_SUBSCRIBE_TOKEN;
|
||||||
|
|
||||||
if (!subscribeToken || requestToken !== subscribeToken) {
|
// 检查是否是全局token或用户token
|
||||||
|
let isValidToken = false;
|
||||||
|
if (globalToken && requestToken === globalToken) {
|
||||||
|
// 全局token
|
||||||
|
isValidToken = true;
|
||||||
|
} else {
|
||||||
|
// 检查是否是用户token
|
||||||
|
const { db } = await import('@/lib/db');
|
||||||
|
const username = await db.getUsernameByTvboxToken(requestToken);
|
||||||
|
if (username) {
|
||||||
|
// 检查用户是否被封禁
|
||||||
|
const userInfo = await db.getUserInfoV2(username);
|
||||||
|
if (userInfo && !userInfo.banned) {
|
||||||
|
isValidToken = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isValidToken) {
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
code: 401,
|
code: 401,
|
||||||
msg: '无效的访问token',
|
msg: '无效的访问token',
|
||||||
|
|||||||
@@ -35,13 +35,29 @@ export async function GET(
|
|||||||
try {
|
try {
|
||||||
const { searchParams } = new URL(request.url);
|
const { searchParams } = new URL(request.url);
|
||||||
|
|
||||||
// 双重验证:TVBox Token 或 用户登录
|
// 双重验证:TVBox Token(全局或用户) 或 用户登录
|
||||||
const requestToken = params.token;
|
const requestToken = params.token;
|
||||||
const subscribeToken = process.env.TVBOX_SUBSCRIBE_TOKEN;
|
const globalToken = process.env.TVBOX_SUBSCRIBE_TOKEN;
|
||||||
const authInfo = getAuthInfoFromCookie(request);
|
const authInfo = getAuthInfoFromCookie(request);
|
||||||
|
|
||||||
// 验证 TVBox Token
|
// 验证 TVBox Token(全局token或用户token)
|
||||||
const hasValidToken = subscribeToken && requestToken === subscribeToken;
|
let hasValidToken = false;
|
||||||
|
if (globalToken && requestToken === globalToken) {
|
||||||
|
// 全局token
|
||||||
|
hasValidToken = true;
|
||||||
|
} else {
|
||||||
|
// 检查是否是用户token
|
||||||
|
const { db } = await import('@/lib/db');
|
||||||
|
const username = await db.getUsernameByTvboxToken(requestToken);
|
||||||
|
if (username) {
|
||||||
|
// 检查用户是否被封禁
|
||||||
|
const userInfo = await db.getUserInfoV2(username);
|
||||||
|
if (userInfo && !userInfo.banned) {
|
||||||
|
hasValidToken = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 验证用户登录
|
// 验证用户登录
|
||||||
const hasValidAuth = authInfo && authInfo.username;
|
const hasValidAuth = authInfo && authInfo.username;
|
||||||
|
|
||||||
|
|||||||
@@ -30,9 +30,27 @@ export async function GET(
|
|||||||
|
|
||||||
// 验证 TVBox Token(从路径中获取)
|
// 验证 TVBox Token(从路径中获取)
|
||||||
const requestToken = params.token;
|
const requestToken = params.token;
|
||||||
const subscribeToken = process.env.TVBOX_SUBSCRIBE_TOKEN;
|
const globalToken = process.env.TVBOX_SUBSCRIBE_TOKEN;
|
||||||
|
|
||||||
if (!subscribeToken || requestToken !== subscribeToken) {
|
// 检查是否是全局token或用户token
|
||||||
|
let isValidToken = false;
|
||||||
|
if (globalToken && requestToken === globalToken) {
|
||||||
|
// 全局token
|
||||||
|
isValidToken = true;
|
||||||
|
} else {
|
||||||
|
// 检查是否是用户token
|
||||||
|
const { db } = await import('@/lib/db');
|
||||||
|
const username = await db.getUsernameByTvboxToken(requestToken);
|
||||||
|
if (username) {
|
||||||
|
// 检查用户是否被封禁
|
||||||
|
const userInfo = await db.getUserInfoV2(username);
|
||||||
|
if (userInfo && !userInfo.banned) {
|
||||||
|
isValidToken = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isValidToken) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
code: 401,
|
code: 401,
|
||||||
|
|||||||
@@ -22,13 +22,29 @@ export async function GET(
|
|||||||
try {
|
try {
|
||||||
const { searchParams } = new URL(request.url);
|
const { searchParams } = new URL(request.url);
|
||||||
|
|
||||||
// 双重验证:TVBox Token 或 用户登录
|
// 双重验证:TVBox Token(全局或用户) 或 用户登录
|
||||||
const requestToken = params.token;
|
const requestToken = params.token;
|
||||||
const subscribeToken = process.env.TVBOX_SUBSCRIBE_TOKEN;
|
const globalToken = process.env.TVBOX_SUBSCRIBE_TOKEN;
|
||||||
const authInfo = getAuthInfoFromCookie(request);
|
const authInfo = getAuthInfoFromCookie(request);
|
||||||
|
|
||||||
// 验证 TVBox Token
|
// 验证 TVBox Token(全局token或用户token)
|
||||||
const hasValidToken = subscribeToken && requestToken === subscribeToken;
|
let hasValidToken = false;
|
||||||
|
if (globalToken && requestToken === globalToken) {
|
||||||
|
// 全局token
|
||||||
|
hasValidToken = true;
|
||||||
|
} else {
|
||||||
|
// 检查是否是用户token
|
||||||
|
const { db } = await import('@/lib/db');
|
||||||
|
const username = await db.getUsernameByTvboxToken(requestToken);
|
||||||
|
if (username) {
|
||||||
|
// 检查用户是否被封禁
|
||||||
|
const userInfo = await db.getUserInfoV2(username);
|
||||||
|
if (userInfo && !userInfo.banned) {
|
||||||
|
hasValidToken = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 验证用户登录
|
// 验证用户登录
|
||||||
const hasValidAuth = authInfo && authInfo.username;
|
const hasValidAuth = authInfo && authInfo.username;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
import { NextRequest, NextResponse } from 'next/server';
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
|
|
||||||
import { getAvailableApiSites, getConfig } from '@/lib/config';
|
import { getAvailableApiSites, getConfig } from '@/lib/config';
|
||||||
|
import { db } from '@/lib/db';
|
||||||
import { getCachedLiveChannels } from '@/lib/live';
|
import { getCachedLiveChannels } from '@/lib/live';
|
||||||
|
|
||||||
export const runtime = 'nodejs';
|
export const runtime = 'nodejs';
|
||||||
@@ -10,6 +11,7 @@ export const runtime = 'nodejs';
|
|||||||
/**
|
/**
|
||||||
* TVBOX订阅API
|
* TVBOX订阅API
|
||||||
* 根据视频源和直播源生成TVBOX订阅
|
* 根据视频源和直播源生成TVBOX订阅
|
||||||
|
* 支持全局token(管理员)和用户token(普通用户)
|
||||||
*/
|
*/
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
// 检查是否开启订阅功能
|
// 检查是否开启订阅功能
|
||||||
@@ -24,22 +26,53 @@ export async function GET(request: NextRequest) {
|
|||||||
// 验证token
|
// 验证token
|
||||||
const searchParams = request.nextUrl.searchParams;
|
const searchParams = request.nextUrl.searchParams;
|
||||||
const token = searchParams.get('token');
|
const token = searchParams.get('token');
|
||||||
const subscribeToken = process.env.TVBOX_SUBSCRIBE_TOKEN;
|
const globalToken = process.env.TVBOX_SUBSCRIBE_TOKEN;
|
||||||
const adFilter = searchParams.get('adFilter') === 'true'; // 获取去广告参数
|
const adFilter = searchParams.get('adFilter') === 'true'; // 获取去广告参数
|
||||||
|
|
||||||
if (!subscribeToken || token !== subscribeToken) {
|
if (!token) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: '无效的订阅token' },
|
{ error: '缺少订阅token' },
|
||||||
{ status: 401 }
|
{ status: 401 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 判断是全局token还是用户token
|
||||||
|
let username: string | undefined;
|
||||||
|
let isGlobalToken = false;
|
||||||
|
|
||||||
|
if (globalToken && token === globalToken) {
|
||||||
|
// 全局token(管理员订阅)
|
||||||
|
isGlobalToken = true;
|
||||||
|
console.log('使用全局token访问TVBox订阅');
|
||||||
|
} else {
|
||||||
|
// 用户token,查询用户名
|
||||||
|
username = await db.getUsernameByTvboxToken(token) || undefined;
|
||||||
|
if (!username) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: '无效的订阅token' },
|
||||||
|
{ status: 401 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查用户是否被封禁
|
||||||
|
const userInfo = await db.getUserInfoV2(username);
|
||||||
|
if (userInfo?.banned) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: '用户已被封禁' },
|
||||||
|
{ status: 403 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`用户 ${username} 访问TVBox订阅`);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 获取配置
|
// 获取配置
|
||||||
const config = await getConfig();
|
const config = await getConfig();
|
||||||
|
|
||||||
// 获取视频源(TVBox客户端不会发送Cookie,直接获取所有启用的源)
|
// 获取视频源
|
||||||
const apiSites = await getAvailableApiSites();
|
// 全局token返回所有源,用户token返回该用户有权限的源
|
||||||
|
const apiSites = await getAvailableApiSites(username);
|
||||||
|
|
||||||
// 获取直播源
|
// 获取直播源
|
||||||
const liveConfig = config.LiveConfig?.filter(live => !live.disabled) || [];
|
const liveConfig = config.LiveConfig?.filter(live => !live.disabled) || [];
|
||||||
@@ -75,7 +108,7 @@ export async function GET(request: NextRequest) {
|
|||||||
key: 'openlist',
|
key: 'openlist',
|
||||||
name: '私人影库',
|
name: '私人影库',
|
||||||
type: 1,
|
type: 1,
|
||||||
api: `${baseUrl}/api/openlist/cms-proxy/${encodeURIComponent(subscribeToken)}`,
|
api: `${baseUrl}/api/openlist/cms-proxy/${encodeURIComponent(token)}`,
|
||||||
searchable: 1,
|
searchable: 1,
|
||||||
quickSearch: 1,
|
quickSearch: 1,
|
||||||
filterable: 1,
|
filterable: 1,
|
||||||
@@ -87,7 +120,7 @@ export async function GET(request: NextRequest) {
|
|||||||
key: `emby_${source.key}`,
|
key: `emby_${source.key}`,
|
||||||
name: source.name || 'Emby媒体库',
|
name: source.name || 'Emby媒体库',
|
||||||
type: 1,
|
type: 1,
|
||||||
api: `${baseUrl}/api/emby/cms-proxy/${encodeURIComponent(subscribeToken)}?embyKey=${source.key}`,
|
api: `${baseUrl}/api/emby/cms-proxy/${encodeURIComponent(token)}?embyKey=${source.key}`,
|
||||||
searchable: 1,
|
searchable: 1,
|
||||||
quickSearch: 1,
|
quickSearch: 1,
|
||||||
filterable: 1,
|
filterable: 1,
|
||||||
|
|||||||
46
src/app/api/user/tvbox-token/reset/route.ts
Normal file
46
src/app/api/user/tvbox-token/reset/route.ts
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
|
|
||||||
|
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||||
|
import { db } from '@/lib/db';
|
||||||
|
import { generateTvboxToken } from '@/lib/tvbox-token';
|
||||||
|
|
||||||
|
export const runtime = 'nodejs';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重置用户的TVBox订阅token
|
||||||
|
* 旧token将失效
|
||||||
|
*/
|
||||||
|
export async function POST(request: NextRequest) {
|
||||||
|
try {
|
||||||
|
// 验证用户登录
|
||||||
|
const authInfo = getAuthInfoFromCookie(request);
|
||||||
|
if (!authInfo?.username) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: '未登录' },
|
||||||
|
{ status: 401 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const username = authInfo.username;
|
||||||
|
|
||||||
|
// 生成新token
|
||||||
|
const newToken = generateTvboxToken();
|
||||||
|
await db.setTvboxSubscribeToken(username, newToken);
|
||||||
|
|
||||||
|
console.log(`用户 ${username} 重置了TVBox订阅token`);
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
token: newToken,
|
||||||
|
message: '订阅token已重置,旧链接已失效',
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('重置TVBox订阅token失败:', error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: '重置订阅token失败',
|
||||||
|
details: (error as Error).message,
|
||||||
|
},
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
47
src/app/api/user/tvbox-token/route.ts
Normal file
47
src/app/api/user/tvbox-token/route.ts
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
|
|
||||||
|
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||||
|
import { db } from '@/lib/db';
|
||||||
|
import { generateTvboxToken } from '@/lib/tvbox-token';
|
||||||
|
|
||||||
|
export const runtime = 'nodejs';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户的TVBox订阅token
|
||||||
|
* 如果用户没有token,自动生成一个
|
||||||
|
*/
|
||||||
|
export async function GET(request: NextRequest) {
|
||||||
|
try {
|
||||||
|
// 验证用户登录
|
||||||
|
const authInfo = getAuthInfoFromCookie(request);
|
||||||
|
if (!authInfo?.username) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: '未登录' },
|
||||||
|
{ status: 401 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const username = authInfo.username;
|
||||||
|
|
||||||
|
// 获取token,如果没有则生成
|
||||||
|
let token = await db.getTvboxSubscribeToken(username);
|
||||||
|
|
||||||
|
if (!token) {
|
||||||
|
// 懒加载:首次访问时生成token
|
||||||
|
token = generateTvboxToken();
|
||||||
|
await db.setTvboxSubscribeToken(username, token);
|
||||||
|
console.log(`为用户 ${username} 生成TVBox订阅token`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({ token });
|
||||||
|
} catch (error) {
|
||||||
|
console.error('获取TVBox订阅token失败:', error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: '获取订阅token失败',
|
||||||
|
details: (error as Error).message,
|
||||||
|
},
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -76,8 +76,12 @@ export const UserMenu: React.FC = () => {
|
|||||||
// 订阅相关状态
|
// 订阅相关状态
|
||||||
const [subscribeEnabled, setSubscribeEnabled] = useState(false);
|
const [subscribeEnabled, setSubscribeEnabled] = useState(false);
|
||||||
const [subscribeUrl, setSubscribeUrl] = useState('');
|
const [subscribeUrl, setSubscribeUrl] = useState('');
|
||||||
|
const [subscribeUrlWithAdFilter, setSubscribeUrlWithAdFilter] = useState('');
|
||||||
const [copySuccess, setCopySuccess] = useState(false);
|
const [copySuccess, setCopySuccess] = useState(false);
|
||||||
const [adFilterEnabled, setAdFilterEnabled] = useState(true); // 去广告开关,默认开启
|
const [copySuccessAdFilter, setCopySuccessAdFilter] = useState(false);
|
||||||
|
const [tvboxToken, setTvboxToken] = useState('');
|
||||||
|
const [isResettingToken, setIsResettingToken] = useState(false);
|
||||||
|
const [isLoadingSubscribeUrl, setIsLoadingSubscribeUrl] = useState(false);
|
||||||
|
|
||||||
// Body 滚动锁定 - 使用 overflow 方式避免布局问题
|
// Body 滚动锁定 - 使用 overflow 方式避免布局问题
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -304,20 +308,91 @@ export const UserMenu: React.FC = () => {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// 懒加载订阅 URL - 只在打开订阅面板时请求
|
// 懒加载订阅 URL - 只在打开订阅面板时请求
|
||||||
const fetchSubscribeUrl = async (adFilter?: boolean) => {
|
const fetchSubscribeUrl = async () => {
|
||||||
|
setIsLoadingSubscribeUrl(true);
|
||||||
try {
|
try {
|
||||||
const currentOrigin = window.location.origin;
|
// 获取用户的 TVBox token
|
||||||
const filterValue = adFilter !== undefined ? adFilter : adFilterEnabled;
|
const response = await fetch('/api/user/tvbox-token');
|
||||||
const response = await fetch(`/api/tvbox/config?origin=${encodeURIComponent(currentOrigin)}&adFilter=${filterValue}`);
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
setSubscribeUrl(data.url);
|
const token = data.token;
|
||||||
|
setTvboxToken(token);
|
||||||
|
|
||||||
|
// 前端拼接订阅链接
|
||||||
|
const currentOrigin = window.location.origin;
|
||||||
|
const standardUrl = `${currentOrigin}/api/tvbox/subscribe?token=${token}`;
|
||||||
|
const adFilterUrl = `${currentOrigin}/api/tvbox/subscribe?token=${token}&adFilter=true`;
|
||||||
|
|
||||||
|
setSubscribeUrl(standardUrl);
|
||||||
|
setSubscribeUrlWithAdFilter(adFilterUrl);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('获取订阅URL失败:', error);
|
console.error('获取订阅URL失败:', error);
|
||||||
|
} finally {
|
||||||
|
setIsLoadingSubscribeUrl(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 重置 TVBox token
|
||||||
|
const handleResetToken = async () => {
|
||||||
|
setConfirmDialog({
|
||||||
|
isOpen: true,
|
||||||
|
title: '重置订阅Token',
|
||||||
|
message: '确定要重置订阅token吗?重置后旧的订阅链接将失效。',
|
||||||
|
onConfirm: async () => {
|
||||||
|
setConfirmDialog({ ...confirmDialog, isOpen: false });
|
||||||
|
setIsResettingToken(true);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/user/tvbox-token/reset', {
|
||||||
|
method: 'POST',
|
||||||
|
});
|
||||||
|
|
||||||
|
const messageEl = document.getElementById('tvbox-token-message');
|
||||||
|
if (response.ok) {
|
||||||
|
const data = await response.json();
|
||||||
|
const token = data.token;
|
||||||
|
setTvboxToken(token);
|
||||||
|
|
||||||
|
// 更新订阅链接
|
||||||
|
const currentOrigin = window.location.origin;
|
||||||
|
const standardUrl = `${currentOrigin}/api/tvbox/subscribe?token=${token}`;
|
||||||
|
const adFilterUrl = `${currentOrigin}/api/tvbox/subscribe?token=${token}&adFilter=true`;
|
||||||
|
|
||||||
|
setSubscribeUrl(standardUrl);
|
||||||
|
setSubscribeUrlWithAdFilter(adFilterUrl);
|
||||||
|
|
||||||
|
if (messageEl) {
|
||||||
|
messageEl.textContent = '订阅token已重置!';
|
||||||
|
messageEl.className = 'text-xs text-center text-green-600 dark:text-green-400 mt-2';
|
||||||
|
messageEl.classList.remove('hidden');
|
||||||
|
setTimeout(() => {
|
||||||
|
messageEl.classList.add('hidden');
|
||||||
|
}, 3000);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const data = await response.json();
|
||||||
|
if (messageEl) {
|
||||||
|
messageEl.textContent = data.error || '重置失败,请重试';
|
||||||
|
messageEl.className = 'text-xs text-center text-red-600 dark:text-red-400 mt-2';
|
||||||
|
messageEl.classList.remove('hidden');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('重置token失败:', error);
|
||||||
|
const messageEl = document.getElementById('tvbox-token-message');
|
||||||
|
if (messageEl) {
|
||||||
|
messageEl.textContent = '重置失败,请重试';
|
||||||
|
messageEl.className = 'text-xs text-center text-red-600 dark:text-red-400 mt-2';
|
||||||
|
messageEl.classList.remove('hidden');
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
setIsResettingToken(false);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// 获取认证信息和存储类型
|
// 获取认证信息和存储类型
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
@@ -726,12 +801,7 @@ export const UserMenu: React.FC = () => {
|
|||||||
const handleCloseSubscribe = () => {
|
const handleCloseSubscribe = () => {
|
||||||
setIsSubscribeOpen(false);
|
setIsSubscribeOpen(false);
|
||||||
setCopySuccess(false);
|
setCopySuccess(false);
|
||||||
};
|
setCopySuccessAdFilter(false);
|
||||||
|
|
||||||
const handleAdFilterToggle = async (checked: boolean) => {
|
|
||||||
setAdFilterEnabled(checked);
|
|
||||||
// 当去广告开关改变时,重新获取订阅URL,传入新的值
|
|
||||||
await fetchSubscribeUrl(checked);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleCopySubscribeUrl = async () => {
|
const handleCopySubscribeUrl = async () => {
|
||||||
@@ -746,6 +816,18 @@ export const UserMenu: React.FC = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleCopySubscribeUrlWithAdFilter = async () => {
|
||||||
|
try {
|
||||||
|
await navigator.clipboard.writeText(subscribeUrlWithAdFilter);
|
||||||
|
setCopySuccessAdFilter(true);
|
||||||
|
setTimeout(() => {
|
||||||
|
setCopySuccessAdFilter(false);
|
||||||
|
}, 2000);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('复制失败:', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const handleSubmitChangePassword = async () => {
|
const handleSubmitChangePassword = async () => {
|
||||||
setPasswordError('');
|
setPasswordError('');
|
||||||
|
|
||||||
@@ -2527,7 +2609,7 @@ export const UserMenu: React.FC = () => {
|
|||||||
{/* 标题栏 */}
|
{/* 标题栏 */}
|
||||||
<div className='flex items-center justify-between mb-6'>
|
<div className='flex items-center justify-between mb-6'>
|
||||||
<h3 className='text-xl font-bold text-gray-800 dark:text-gray-200'>
|
<h3 className='text-xl font-bold text-gray-800 dark:text-gray-200'>
|
||||||
订阅
|
TVBox订阅
|
||||||
</h3>
|
</h3>
|
||||||
<button
|
<button
|
||||||
onClick={handleCloseSubscribe}
|
onClick={handleCloseSubscribe}
|
||||||
@@ -2540,58 +2622,105 @@ export const UserMenu: React.FC = () => {
|
|||||||
|
|
||||||
{/* 内容 */}
|
{/* 内容 */}
|
||||||
<div className='space-y-4'>
|
<div className='space-y-4'>
|
||||||
{/* 去广告开关 */}
|
{isLoadingSubscribeUrl ? (
|
||||||
<div className='flex items-center justify-between p-3 bg-gray-50 dark:bg-gray-800 rounded-lg'>
|
<>
|
||||||
<div>
|
{/* 加载骨架 - 订阅链接(标准) */}
|
||||||
<h4 className='text-sm font-medium text-gray-700 dark:text-gray-300'>
|
<div>
|
||||||
去广告
|
<div className='h-5 w-32 bg-gray-200 dark:bg-gray-700 rounded mb-2 animate-pulse'></div>
|
||||||
</h4>
|
<div className='flex gap-2'>
|
||||||
<p className='text-xs text-gray-500 dark:text-gray-400 mt-1'>
|
<div className='flex-1 h-10 bg-gray-200 dark:bg-gray-700 rounded animate-pulse'></div>
|
||||||
开启后自动过滤视频广告
|
<div className='w-20 h-10 bg-gray-200 dark:bg-gray-700 rounded animate-pulse'></div>
|
||||||
</p>
|
</div>
|
||||||
</div>
|
|
||||||
<label className='flex items-center cursor-pointer'>
|
|
||||||
<div className='relative'>
|
|
||||||
<input
|
|
||||||
type='checkbox'
|
|
||||||
className='sr-only peer'
|
|
||||||
checked={adFilterEnabled}
|
|
||||||
onChange={(e) => handleAdFilterToggle(e.target.checked)}
|
|
||||||
/>
|
|
||||||
<div className='w-11 h-6 bg-gray-300 rounded-full peer-checked:bg-green-500 transition-colors dark:bg-gray-600'></div>
|
|
||||||
<div className='absolute top-0.5 left-0.5 w-5 h-5 bg-white rounded-full transition-transform peer-checked:translate-x-5'></div>
|
|
||||||
</div>
|
</div>
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* TVBOX订阅 */}
|
{/* 加载骨架 - 订阅链接(去广告) */}
|
||||||
<div>
|
<div>
|
||||||
<h4 className='text-sm font-medium text-gray-700 dark:text-gray-300 mb-2'>
|
<div className='h-5 w-36 bg-gray-200 dark:bg-gray-700 rounded mb-2 animate-pulse'></div>
|
||||||
TVBOX订阅
|
<div className='flex gap-2'>
|
||||||
</h4>
|
<div className='flex-1 h-10 bg-gray-200 dark:bg-gray-700 rounded animate-pulse'></div>
|
||||||
<div className='flex gap-2'>
|
<div className='w-20 h-10 bg-gray-200 dark:bg-gray-700 rounded animate-pulse'></div>
|
||||||
<input
|
</div>
|
||||||
type='text'
|
<div className='h-4 w-full bg-gray-200 dark:bg-gray-700 rounded mt-1 animate-pulse'></div>
|
||||||
className='flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md text-sm bg-gray-50 dark:bg-gray-800 text-gray-900 dark:text-gray-100 cursor-not-allowed'
|
</div>
|
||||||
value={subscribeUrl}
|
|
||||||
disabled
|
{/* 加载骨架 - 重置按钮 */}
|
||||||
readOnly
|
<div className='pt-2'>
|
||||||
/>
|
<div className='w-full h-10 bg-gray-200 dark:bg-gray-700 rounded animate-pulse'></div>
|
||||||
<button
|
<div className='h-4 w-40 bg-gray-200 dark:bg-gray-700 rounded mt-2 mx-auto animate-pulse'></div>
|
||||||
onClick={handleCopySubscribeUrl}
|
</div>
|
||||||
className='px-4 py-2 bg-green-600 hover:bg-green-700 dark:bg-green-700 dark:hover:bg-green-600 text-white text-sm font-medium rounded-md transition-colors flex items-center gap-2'
|
</>
|
||||||
>
|
) : (
|
||||||
<Copy className='w-4 h-4' />
|
<>
|
||||||
{copySuccess ? '已复制' : '复制'}
|
{/* 订阅链接(标准) */}
|
||||||
</button>
|
<div>
|
||||||
</div>
|
<h4 className='text-sm font-medium text-gray-700 dark:text-gray-300 mb-2'>
|
||||||
</div>
|
订阅链接(标准)
|
||||||
|
</h4>
|
||||||
|
<div className='flex gap-2'>
|
||||||
|
<input
|
||||||
|
type='text'
|
||||||
|
className='flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md text-sm bg-gray-50 dark:bg-gray-800 text-gray-900 dark:text-gray-100'
|
||||||
|
value={subscribeUrl}
|
||||||
|
readOnly
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
onClick={handleCopySubscribeUrl}
|
||||||
|
className='px-4 py-2 bg-green-600 hover:bg-green-700 dark:bg-green-700 dark:hover:bg-green-600 text-white text-sm font-medium rounded-md transition-colors flex items-center gap-2 whitespace-nowrap'
|
||||||
|
>
|
||||||
|
<Copy className='w-4 h-4' />
|
||||||
|
{copySuccess ? '已复制' : '复制'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* 订阅链接(去广告) */}
|
||||||
|
<div>
|
||||||
|
<h4 className='text-sm font-medium text-gray-700 dark:text-gray-300 mb-2'>
|
||||||
|
订阅链接(去广告)
|
||||||
|
</h4>
|
||||||
|
<div className='flex gap-2'>
|
||||||
|
<input
|
||||||
|
type='text'
|
||||||
|
className='flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md text-sm bg-gray-50 dark:bg-gray-800 text-gray-900 dark:text-gray-100'
|
||||||
|
value={subscribeUrlWithAdFilter}
|
||||||
|
readOnly
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
onClick={handleCopySubscribeUrlWithAdFilter}
|
||||||
|
className='px-4 py-2 bg-green-600 hover:bg-green-700 dark:bg-green-700 dark:hover:bg-green-600 text-white text-sm font-medium rounded-md transition-colors flex items-center gap-2 whitespace-nowrap'
|
||||||
|
>
|
||||||
|
<Copy className='w-4 h-4' />
|
||||||
|
{copySuccessAdFilter ? '已复制' : '复制'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<p className='text-xs text-gray-500 dark:text-gray-400 mt-1'>
|
||||||
|
💡 去广告需要经过服务器代理,某些源可能因为区域或兼容问题无法播放
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* 重置Token按钮 */}
|
||||||
|
<div className='pt-2'>
|
||||||
|
<button
|
||||||
|
onClick={handleResetToken}
|
||||||
|
disabled={isResettingToken}
|
||||||
|
className='w-full px-4 py-2 bg-red-600 hover:bg-red-700 dark:bg-red-700 dark:hover:bg-red-600 text-white text-sm font-medium rounded-md transition-colors disabled:opacity-50 disabled:cursor-not-allowed'
|
||||||
|
>
|
||||||
|
{isResettingToken ? '重置中...' : '重置订阅Token'}
|
||||||
|
</button>
|
||||||
|
<p className='text-xs text-gray-500 dark:text-gray-400 mt-2 text-center'>
|
||||||
|
⚠️ 重置后旧链接将失效
|
||||||
|
</p>
|
||||||
|
{/* 消息提示 */}
|
||||||
|
<p id='tvbox-token-message' className='text-xs text-center hidden'></p>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* 底部说明 */}
|
{/* 底部说明 */}
|
||||||
<div className='mt-6 pt-4 border-t border-gray-200 dark:border-gray-700'>
|
<div className='mt-6 pt-4 border-t border-gray-200 dark:border-gray-700'>
|
||||||
<p className='text-xs text-gray-500 dark:text-gray-400 text-center'>
|
<p className='text-xs text-gray-500 dark:text-gray-400 text-center'>
|
||||||
将订阅链接复制到TVBOX应用中使用
|
将订阅链接复制到TVBox应用中使用
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1346,6 +1346,51 @@ export class D1Storage implements IStorage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ==================== TVBox订阅token ====================
|
||||||
|
|
||||||
|
async getTvboxSubscribeToken?(userName: string): Promise<string | null> {
|
||||||
|
try {
|
||||||
|
const result = await this.db
|
||||||
|
.prepare('SELECT tvbox_subscribe_token FROM users WHERE username = ?')
|
||||||
|
.bind(userName)
|
||||||
|
.first();
|
||||||
|
|
||||||
|
return result?.tvbox_subscribe_token || null;
|
||||||
|
} catch (err) {
|
||||||
|
console.error('D1Storage.getTvboxSubscribeToken error:', err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async setTvboxSubscribeToken?(userName: string, token: string): Promise<void> {
|
||||||
|
try {
|
||||||
|
await this.db
|
||||||
|
.prepare('UPDATE users SET tvbox_subscribe_token = ? WHERE username = ?')
|
||||||
|
.bind(token, userName)
|
||||||
|
.run();
|
||||||
|
|
||||||
|
// 清除缓存
|
||||||
|
userInfoCache?.delete(userName);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('D1Storage.setTvboxSubscribeToken error:', err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getUsernameByTvboxToken?(token: string): Promise<string | null> {
|
||||||
|
try {
|
||||||
|
const result = await this.db
|
||||||
|
.prepare('SELECT username FROM users WHERE tvbox_subscribe_token = ?')
|
||||||
|
.bind(token)
|
||||||
|
.first();
|
||||||
|
|
||||||
|
return result?.username || null;
|
||||||
|
} catch (err) {
|
||||||
|
console.error('D1Storage.getUsernameByTvboxToken error:', err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ==================== 搜索历史 ====================
|
// ==================== 搜索历史 ====================
|
||||||
|
|
||||||
async getSearchHistory(userName: string): Promise<string[]> {
|
async getSearchHistory(userName: string): Promise<string[]> {
|
||||||
|
|||||||
@@ -482,6 +482,27 @@ export class DbManager {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------- TVBox订阅token ----------
|
||||||
|
async getTvboxSubscribeToken(userName: string): Promise<string | null> {
|
||||||
|
if (typeof (this.storage as any).getTvboxSubscribeToken === 'function') {
|
||||||
|
return (this.storage as any).getTvboxSubscribeToken(userName);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async setTvboxSubscribeToken(userName: string, token: string): Promise<void> {
|
||||||
|
if (typeof (this.storage as any).setTvboxSubscribeToken === 'function') {
|
||||||
|
await (this.storage as any).setTvboxSubscribeToken(userName, token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getUsernameByTvboxToken(token: string): Promise<string | null> {
|
||||||
|
if (typeof (this.storage as any).getUsernameByTvboxToken === 'function') {
|
||||||
|
return (this.storage as any).getUsernameByTvboxToken(token);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// ---------- 播放记录迁移 ----------
|
// ---------- 播放记录迁移 ----------
|
||||||
async migratePlayRecords(userName: string): Promise<void> {
|
async migratePlayRecords(userName: string): Promise<void> {
|
||||||
if (typeof (this.storage as any).migratePlayRecords === 'function') {
|
if (typeof (this.storage as any).migratePlayRecords === 'function') {
|
||||||
|
|||||||
@@ -911,6 +911,52 @@ export class PostgresStorage implements IStorage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ==================== TVBox订阅token ====================
|
||||||
|
|
||||||
|
async getTvboxSubscribeToken(userName: string): Promise<string | null> {
|
||||||
|
try {
|
||||||
|
const result = await this.db
|
||||||
|
.prepare('SELECT tvbox_subscribe_token FROM users_v2 WHERE username = $1')
|
||||||
|
.bind(userName)
|
||||||
|
.first();
|
||||||
|
|
||||||
|
return result?.tvbox_subscribe_token || null;
|
||||||
|
} catch (err) {
|
||||||
|
console.error('PostgresStorage.getTvboxSubscribeToken error:', err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async setTvboxSubscribeToken(userName: string, token: string): Promise<void> {
|
||||||
|
try {
|
||||||
|
await this.db
|
||||||
|
.prepare('UPDATE users_v2 SET tvbox_subscribe_token = $1 WHERE username = $2')
|
||||||
|
.bind(token, userName)
|
||||||
|
.run();
|
||||||
|
|
||||||
|
// 清除用户信息缓存
|
||||||
|
const { userInfoCache } = await import('./user-cache');
|
||||||
|
userInfoCache.delete(userName);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('PostgresStorage.setTvboxSubscribeToken error:', err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getUsernameByTvboxToken(token: string): Promise<string | null> {
|
||||||
|
try {
|
||||||
|
const result = await this.db
|
||||||
|
.prepare('SELECT username FROM users_v2 WHERE tvbox_subscribe_token = $1')
|
||||||
|
.bind(token)
|
||||||
|
.first();
|
||||||
|
|
||||||
|
return result?.username || null;
|
||||||
|
} catch (err) {
|
||||||
|
console.error('PostgresStorage.getUsernameByTvboxToken error:', err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ==================== 音乐播放记录 ====================
|
// ==================== 音乐播放记录 ====================
|
||||||
|
|
||||||
async getMusicPlayRecord(userName: string, key: string): Promise<any | null> {
|
async getMusicPlayRecord(userName: string, key: string): Promise<any | null> {
|
||||||
|
|||||||
@@ -1723,4 +1723,35 @@ export abstract class BaseRedisStorage implements IStorage {
|
|||||||
// 清除缓存
|
// 清除缓存
|
||||||
userInfoCache?.delete(userName);
|
userInfoCache?.delete(userName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------- TVBox订阅token相关 ----------
|
||||||
|
async getTvboxSubscribeToken(userName: string): Promise<string | null> {
|
||||||
|
// 直接从数据库读取,不使用缓存
|
||||||
|
const token = await this.withRetry(() =>
|
||||||
|
this.adapter.hGet(this.userInfoKey(userName), 'tvboxSubscribeToken')
|
||||||
|
);
|
||||||
|
return token || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async setTvboxSubscribeToken(userName: string, token: string): Promise<void> {
|
||||||
|
// 保存token到用户信息
|
||||||
|
await this.withRetry(() =>
|
||||||
|
this.adapter.hSet(this.userInfoKey(userName), 'tvboxSubscribeToken', token)
|
||||||
|
);
|
||||||
|
|
||||||
|
// 创建token到用户名的反向索引
|
||||||
|
await this.withRetry(() =>
|
||||||
|
this.adapter.set(`tvbox:token:${token}`, userName)
|
||||||
|
);
|
||||||
|
|
||||||
|
// 清除缓存
|
||||||
|
userInfoCache?.delete(userName);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getUsernameByTvboxToken(token: string): Promise<string | null> {
|
||||||
|
const userName = await this.withRetry(() =>
|
||||||
|
this.adapter.get(`tvbox:token:${token}`)
|
||||||
|
);
|
||||||
|
return userName || null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
9
src/lib/tvbox-token.ts
Normal file
9
src/lib/tvbox-token.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { randomBytes } from 'crypto';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成TVBox订阅token
|
||||||
|
* 使用crypto生成32位随机hex字符串
|
||||||
|
*/
|
||||||
|
export function generateTvboxToken(): string {
|
||||||
|
return randomBytes(16).toString('hex');
|
||||||
|
}
|
||||||
@@ -161,6 +161,11 @@ export interface IStorage {
|
|||||||
setUserEmail?(userName: string, email: string): Promise<void>;
|
setUserEmail?(userName: string, email: string): Promise<void>;
|
||||||
getEmailNotificationPreference?(userName: string): Promise<boolean>;
|
getEmailNotificationPreference?(userName: string): Promise<boolean>;
|
||||||
setEmailNotificationPreference?(userName: string, enabled: boolean): Promise<void>;
|
setEmailNotificationPreference?(userName: string, enabled: boolean): Promise<void>;
|
||||||
|
|
||||||
|
// TVBox订阅token相关
|
||||||
|
getTvboxSubscribeToken?(userName: string): Promise<string | null>;
|
||||||
|
setTvboxSubscribeToken?(userName: string, token: string): Promise<void>;
|
||||||
|
getUsernameByTvboxToken?(token: string): Promise<string | null>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 搜索结果数据结构
|
// 搜索结果数据结构
|
||||||
|
|||||||
Reference in New Issue
Block a user