mirror of
https://github.com/zimplexing/OrionTV.git
synced 2026-02-04 03:36:29 +08:00
feat: Implement user authentication and data management features
- Added LoginModal component for user login functionality. - Introduced API routes for user login, favorites, play records, and search history management. - Created JSON files for storing favorites, play records, and search history. - Updated API service to handle new endpoints and refactored data management to use API calls instead of local storage. - Adjusted data structures in types and services to align with new API responses.
This commit is contained in:
220
services/api.ts
220
services/api.ts
@@ -1,5 +1,7 @@
|
||||
import { SettingsManager } from "./storage";
|
||||
import useAuthStore from "@/stores/authStore";
|
||||
|
||||
// region: --- Interface Definitions ---
|
||||
export interface DoubanItem {
|
||||
title: string;
|
||||
poster: string;
|
||||
@@ -13,23 +15,18 @@ export interface DoubanResponse {
|
||||
}
|
||||
|
||||
export interface VideoDetail {
|
||||
code: number;
|
||||
episodes: string[];
|
||||
detailUrl: string;
|
||||
videoInfo: {
|
||||
title: string;
|
||||
cover?: string;
|
||||
desc?: string;
|
||||
type?: string;
|
||||
year?: string;
|
||||
area?: string;
|
||||
director?: string;
|
||||
actor?: string;
|
||||
remarks?: string;
|
||||
source_name: string;
|
||||
source: string;
|
||||
id: string;
|
||||
};
|
||||
id: string;
|
||||
title: string;
|
||||
poster: string;
|
||||
source: string;
|
||||
source_name: string;
|
||||
desc?: string;
|
||||
type?: string;
|
||||
year?: string;
|
||||
area?: string;
|
||||
director?: string;
|
||||
actor?: string;
|
||||
remarks?: string;
|
||||
}
|
||||
|
||||
export interface SearchResult {
|
||||
@@ -45,17 +42,22 @@ export interface SearchResult {
|
||||
type_name?: string;
|
||||
}
|
||||
|
||||
// Data structure for play records
|
||||
export interface Favorite {
|
||||
title: string;
|
||||
poster: string;
|
||||
source_name: string;
|
||||
save_time: number;
|
||||
}
|
||||
|
||||
export interface PlayRecord {
|
||||
title: string;
|
||||
source_name: string;
|
||||
cover: string;
|
||||
index: number; // Episode number
|
||||
total_episodes: number; // Total number of episodes
|
||||
play_time: number; // Play progress in seconds
|
||||
total_time: number; // Total duration in seconds
|
||||
save_time: number; // Timestamp of when the record was saved
|
||||
user_id: number; // User ID, always 0 in this version
|
||||
poster: string;
|
||||
index: number;
|
||||
total_episodes: number;
|
||||
play_time: number;
|
||||
total_time: number;
|
||||
save_time: number;
|
||||
}
|
||||
|
||||
export interface ApiSite {
|
||||
@@ -65,6 +67,12 @@ export interface ApiSite {
|
||||
detail?: string;
|
||||
}
|
||||
|
||||
export interface ServerConfig {
|
||||
SiteName: string;
|
||||
StorageType: "localstorage" | "redis" | string;
|
||||
}
|
||||
// endregion
|
||||
|
||||
export class API {
|
||||
public baseURL: string = "";
|
||||
|
||||
@@ -78,84 +86,142 @@ export class API {
|
||||
this.baseURL = url;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成图片代理 URL
|
||||
*/
|
||||
getImageProxyUrl(imageUrl: string): string {
|
||||
return `${this.baseURL}/api/image-proxy?url=${encodeURIComponent(
|
||||
imageUrl
|
||||
)}`;
|
||||
private async _fetch(url: string, options: RequestInit = {}): Promise<Response> {
|
||||
if (!this.baseURL) {
|
||||
throw new Error("API_URL_NOT_SET");
|
||||
}
|
||||
|
||||
const response = await fetch(`${this.baseURL}${url}`, options);
|
||||
|
||||
if (response.status === 401) {
|
||||
useAuthStore.getState().showLoginModal();
|
||||
throw new Error("UNAUTHORIZED");
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
// region: --- New API Methods ---
|
||||
async getServerConfig(): Promise<ServerConfig> {
|
||||
const response = await this._fetch("/api/server-config");
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async login(password: string): Promise<{ ok: boolean }> {
|
||||
const response = await this._fetch("/api/login", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ password }),
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async getFavorites(key?: string): Promise<Record<string, Favorite> | Favorite | null> {
|
||||
const url = key ? `/api/favorites?key=${key}` : "/api/favorites";
|
||||
const response = await this._fetch(url);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async addFavorite(key: string, favorite: Omit<Favorite, "save_time">): Promise<{ success: boolean }> {
|
||||
const response = await this._fetch("/api/favorites", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ key, favorite }),
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async deleteFavorite(key?: string): Promise<{ success: boolean }> {
|
||||
const url = key ? `/api/favorites?key=${key}` : "/api/favorites";
|
||||
const response = await this._fetch(url, { method: "DELETE" });
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async getPlayRecords(): Promise<Record<string, PlayRecord>> {
|
||||
const response = await this._fetch("/api/playrecords");
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async savePlayRecord(key: string, record: Omit<PlayRecord, "save_time">): Promise<{ success: boolean }> {
|
||||
const response = await this._fetch("/api/playrecords", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ key, record }),
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async deletePlayRecord(key?: string): Promise<{ success: boolean }> {
|
||||
const url = key ? `/api/playrecords?key=${key}` : "/api/playrecords";
|
||||
const response = await this._fetch(url, { method: "DELETE" });
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async getSearchHistory(): Promise<string[]> {
|
||||
const response = await this._fetch("/api/searchhistory");
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async addSearchHistory(keyword: string): Promise<string[]> {
|
||||
const response = await this._fetch("/api/searchhistory", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ keyword }),
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async deleteSearchHistory(keyword?: string): Promise<{ success: boolean }> {
|
||||
const url = keyword ? `/api/searchhistory?keyword=${keyword}` : "/api/searchhistory";
|
||||
const response = await this._fetch(url, { method: "DELETE" });
|
||||
return response.json();
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region: --- Existing API Methods (Refactored) ---
|
||||
getImageProxyUrl(imageUrl: string): string {
|
||||
return `${this.baseURL}/api/image-proxy?url=${encodeURIComponent(imageUrl)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取豆瓣数据
|
||||
*/
|
||||
async getDoubanData(
|
||||
type: "movie" | "tv",
|
||||
tag: string,
|
||||
pageSize: number = 16,
|
||||
pageStart: number = 0
|
||||
): Promise<DoubanResponse> {
|
||||
if (!this.baseURL) {
|
||||
throw new Error("API_URL_NOT_SET");
|
||||
}
|
||||
const url = `${
|
||||
this.baseURL
|
||||
}/api/douban?type=${type}&tag=${encodeURIComponent(
|
||||
tag
|
||||
)}&pageSize=${pageSize}&pageStart=${pageStart}`;
|
||||
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
const url = `/api/douban?type=${type}&tag=${encodeURIComponent(tag)}&pageSize=${pageSize}&pageStart=${pageStart}`;
|
||||
const response = await this._fetch(url);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索视频
|
||||
*/
|
||||
async searchVideos(query: string): Promise<{ results: SearchResult[] }> {
|
||||
if (!this.baseURL) {
|
||||
throw new Error("API_URL_NOT_SET");
|
||||
}
|
||||
const url = `${this.baseURL}/api/search?q=${encodeURIComponent(query)}`;
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
const url = `/api/search?q=${encodeURIComponent(query)}`;
|
||||
const response = await this._fetch(url);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async searchVideo(query: string, resourceId: string, signal?: AbortSignal): Promise<{ results: SearchResult[] }> {
|
||||
if (!this.baseURL) {
|
||||
throw new Error("API_URL_NOT_SET");
|
||||
}
|
||||
const url = `${this.baseURL}/api/search/one?q=${encodeURIComponent(query)}&resourceId=${encodeURIComponent(resourceId)}`;
|
||||
const response = await fetch(url, { signal });
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
const url = `/api/search/one?q=${encodeURIComponent(query)}&resourceId=${encodeURIComponent(resourceId)}`;
|
||||
const response = await this._fetch(url, { signal });
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async getResources(signal?: AbortSignal): Promise<ApiSite[]> {
|
||||
if (!this.baseURL) {
|
||||
throw new Error("API_URL_NOT_SET");
|
||||
}
|
||||
const url = `${this.baseURL}/api/search/resources`;
|
||||
const response = await fetch(url, { signal });
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
const url = `/api/search/resources`;
|
||||
const response = await this._fetch(url, { signal });
|
||||
return response.json();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取视频详情
|
||||
*/
|
||||
async getVideoDetail(source: string, id: string): Promise<VideoDetail> {
|
||||
if (!this.baseURL) {
|
||||
throw new Error("API_URL_NOT_SET");
|
||||
}
|
||||
const url = `${this.baseURL}/api/detail?source=${source}&id=${id}`;
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
const url = `/api/detail?source=${source}&id=${id}`;
|
||||
const response = await this._fetch(url);
|
||||
return response.json();
|
||||
}
|
||||
// endregion
|
||||
}
|
||||
|
||||
// 默认实例
|
||||
|
||||
@@ -1,28 +1,18 @@
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import { PlayRecord as ApiPlayRecord } from "./api"; // Use a consistent type
|
||||
import { api, PlayRecord as ApiPlayRecord, Favorite as ApiFavorite } from "./api";
|
||||
|
||||
// --- Storage Keys ---
|
||||
const STORAGE_KEYS = {
|
||||
FAVORITES: "mytv_favorites",
|
||||
PLAY_RECORDS: "mytv_play_records",
|
||||
SEARCH_HISTORY: "mytv_search_history",
|
||||
SETTINGS: "mytv_settings",
|
||||
} as const;
|
||||
|
||||
// --- Type Definitions (aligned with api.ts) ---
|
||||
export interface PlayRecord extends ApiPlayRecord {
|
||||
// Re-exporting for consistency, though they are now primarily API types
|
||||
export type PlayRecord = ApiPlayRecord & {
|
||||
introEndTime?: number;
|
||||
outroStartTime?: number;
|
||||
}
|
||||
|
||||
export interface FavoriteItem {
|
||||
id: string;
|
||||
source: string;
|
||||
title: string;
|
||||
poster: string;
|
||||
source_name: string;
|
||||
save_time: number;
|
||||
}
|
||||
};
|
||||
export type Favorite = ApiFavorite;
|
||||
|
||||
export interface AppSettings {
|
||||
apiBaseUrl: string;
|
||||
@@ -32,59 +22,36 @@ export interface AppSettings {
|
||||
sources: {
|
||||
[key: string]: boolean;
|
||||
};
|
||||
},
|
||||
};
|
||||
m3uUrl: string;
|
||||
}
|
||||
|
||||
// --- Helper ---
|
||||
const generateKey = (source: string, id: string) => `${source}+${id}`;
|
||||
|
||||
// --- FavoriteManager ---
|
||||
// --- FavoriteManager (Refactored to use API) ---
|
||||
export class FavoriteManager {
|
||||
static async getAll(): Promise<Record<string, FavoriteItem>> {
|
||||
try {
|
||||
const data = await AsyncStorage.getItem(STORAGE_KEYS.FAVORITES);
|
||||
return data ? JSON.parse(data) : {};
|
||||
} catch (error) {
|
||||
console.error("Failed to get favorites:", error);
|
||||
return {};
|
||||
}
|
||||
static async getAll(): Promise<Record<string, Favorite>> {
|
||||
return (await api.getFavorites()) as Record<string, Favorite>;
|
||||
}
|
||||
|
||||
static async save(
|
||||
source: string,
|
||||
id: string,
|
||||
item: Omit<FavoriteItem, "id" | "source" | "save_time">
|
||||
): Promise<void> {
|
||||
const favorites = await this.getAll();
|
||||
static async save(source: string, id: string, item: Omit<Favorite, "save_time">): Promise<void> {
|
||||
const key = generateKey(source, id);
|
||||
favorites[key] = { ...item, id, source, save_time: Date.now() };
|
||||
await AsyncStorage.setItem(
|
||||
STORAGE_KEYS.FAVORITES,
|
||||
JSON.stringify(favorites)
|
||||
);
|
||||
await api.addFavorite(key, item);
|
||||
}
|
||||
|
||||
static async remove(source: string, id: string): Promise<void> {
|
||||
const favorites = await this.getAll();
|
||||
const key = generateKey(source, id);
|
||||
delete favorites[key];
|
||||
await AsyncStorage.setItem(
|
||||
STORAGE_KEYS.FAVORITES,
|
||||
JSON.stringify(favorites)
|
||||
);
|
||||
await api.deleteFavorite(key);
|
||||
}
|
||||
|
||||
static async isFavorited(source: string, id: string): Promise<boolean> {
|
||||
const favorites = await this.getAll();
|
||||
return generateKey(source, id) in favorites;
|
||||
const key = generateKey(source, id);
|
||||
const favorite = await api.getFavorites(key);
|
||||
return favorite !== null;
|
||||
}
|
||||
|
||||
static async toggle(
|
||||
source: string,
|
||||
id: string,
|
||||
item: Omit<FavoriteItem, "id" | "source" | "save_time">
|
||||
): Promise<boolean> {
|
||||
static async toggle(source: string, id: string, item: Omit<Favorite, "save_time">): Promise<boolean> {
|
||||
const isFav = await this.isFavorited(source, id);
|
||||
if (isFav) {
|
||||
await this.remove(source, id);
|
||||
@@ -96,34 +63,20 @@ export class FavoriteManager {
|
||||
}
|
||||
|
||||
static async clearAll(): Promise<void> {
|
||||
await AsyncStorage.removeItem(STORAGE_KEYS.FAVORITES);
|
||||
await api.deleteFavorite();
|
||||
}
|
||||
}
|
||||
|
||||
// --- PlayRecordManager ---
|
||||
// --- PlayRecordManager (Refactored to use API) ---
|
||||
export class PlayRecordManager {
|
||||
static async getAll(): Promise<Record<string, PlayRecord>> {
|
||||
try {
|
||||
const data = await AsyncStorage.getItem(STORAGE_KEYS.PLAY_RECORDS);
|
||||
return data ? JSON.parse(data) : {};
|
||||
} catch (error) {
|
||||
console.error("Failed to get play records:", error);
|
||||
return {};
|
||||
}
|
||||
return (await api.getPlayRecords()) as Record<string, PlayRecord>;
|
||||
}
|
||||
|
||||
static async save(
|
||||
source: string,
|
||||
id: string,
|
||||
record: Omit<PlayRecord, "user_id" | "save_time">
|
||||
): Promise<void> {
|
||||
const records = await this.getAll();
|
||||
static async save(source: string, id: string, record: Omit<PlayRecord, "save_time">): Promise<void> {
|
||||
const key = generateKey(source, id);
|
||||
records[key] = { ...record, user_id: 0, save_time: Date.now() };
|
||||
await AsyncStorage.setItem(
|
||||
STORAGE_KEYS.PLAY_RECORDS,
|
||||
JSON.stringify(records)
|
||||
);
|
||||
// The API will handle setting the save_time
|
||||
await api.savePlayRecord(key, record);
|
||||
}
|
||||
|
||||
static async get(source: string, id: string): Promise<PlayRecord | null> {
|
||||
@@ -132,54 +85,33 @@ export class PlayRecordManager {
|
||||
}
|
||||
|
||||
static async remove(source: string, id: string): Promise<void> {
|
||||
const records = await this.getAll();
|
||||
delete records[generateKey(source, id)];
|
||||
await AsyncStorage.setItem(
|
||||
STORAGE_KEYS.PLAY_RECORDS,
|
||||
JSON.stringify(records)
|
||||
);
|
||||
const key = generateKey(source, id);
|
||||
await api.deletePlayRecord(key);
|
||||
}
|
||||
|
||||
static async clearAll(): Promise<void> {
|
||||
await AsyncStorage.removeItem(STORAGE_KEYS.PLAY_RECORDS);
|
||||
await api.deletePlayRecord();
|
||||
}
|
||||
}
|
||||
|
||||
// --- SearchHistoryManager ---
|
||||
const SEARCH_HISTORY_LIMIT = 20;
|
||||
|
||||
// --- SearchHistoryManager (Refactored to use API) ---
|
||||
export class SearchHistoryManager {
|
||||
static async get(): Promise<string[]> {
|
||||
try {
|
||||
const data = await AsyncStorage.getItem(STORAGE_KEYS.SEARCH_HISTORY);
|
||||
return data ? JSON.parse(data) : [];
|
||||
} catch (error) {
|
||||
console.error("Failed to get search history:", error);
|
||||
return [];
|
||||
}
|
||||
return api.getSearchHistory();
|
||||
}
|
||||
|
||||
static async add(keyword: string): Promise<void> {
|
||||
const trimmed = keyword.trim();
|
||||
if (!trimmed) return;
|
||||
|
||||
const history = await this.get();
|
||||
const newHistory = [trimmed, ...history.filter((k) => k !== trimmed)];
|
||||
if (newHistory.length > SEARCH_HISTORY_LIMIT) {
|
||||
newHistory.length = SEARCH_HISTORY_LIMIT;
|
||||
}
|
||||
await AsyncStorage.setItem(
|
||||
STORAGE_KEYS.SEARCH_HISTORY,
|
||||
JSON.stringify(newHistory)
|
||||
);
|
||||
await api.addSearchHistory(trimmed);
|
||||
}
|
||||
|
||||
static async clear(): Promise<void> {
|
||||
await AsyncStorage.removeItem(STORAGE_KEYS.SEARCH_HISTORY);
|
||||
await api.deleteSearchHistory();
|
||||
}
|
||||
}
|
||||
|
||||
// --- SettingsManager ---
|
||||
// --- SettingsManager (Remains unchanged, uses AsyncStorage) ---
|
||||
export class SettingsManager {
|
||||
static async get(): Promise<AppSettings> {
|
||||
const defaultSettings: AppSettings = {
|
||||
@@ -189,13 +121,12 @@ export class SettingsManager {
|
||||
enabledAll: true,
|
||||
sources: {},
|
||||
},
|
||||
m3uUrl: "https://ghfast.top/https://raw.githubusercontent.com/sjnhnp/adblock/refs/heads/main/filtered_http_only_valid.m3u",
|
||||
m3uUrl:
|
||||
"https://ghfast.top/https://raw.githubusercontent.com/sjnhnp/adblock/refs/heads/main/filtered_http_only_valid.m3u",
|
||||
};
|
||||
try {
|
||||
const data = await AsyncStorage.getItem(STORAGE_KEYS.SETTINGS);
|
||||
return data
|
||||
? { ...defaultSettings, ...JSON.parse(data) }
|
||||
: defaultSettings;
|
||||
return data ? { ...defaultSettings, ...JSON.parse(data) } : defaultSettings;
|
||||
} catch (error) {
|
||||
console.error("Failed to get settings:", error);
|
||||
return defaultSettings;
|
||||
@@ -205,10 +136,7 @@ export class SettingsManager {
|
||||
static async save(settings: Partial<AppSettings>): Promise<void> {
|
||||
const currentSettings = await this.get();
|
||||
const updatedSettings = { ...currentSettings, ...settings };
|
||||
await AsyncStorage.setItem(
|
||||
STORAGE_KEYS.SETTINGS,
|
||||
JSON.stringify(updatedSettings)
|
||||
);
|
||||
await AsyncStorage.setItem(STORAGE_KEYS.SETTINGS, JSON.stringify(updatedSettings));
|
||||
}
|
||||
|
||||
static async reset(): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user