diff --git a/app/songs/actions.tsx b/app/songs/actions.tsx index d2f3f0e..d76d9bf 100644 --- a/app/songs/actions.tsx +++ b/app/songs/actions.tsx @@ -1,7 +1,11 @@ +"use server"; + import { InternetProviderLyricSearchResponse, getSearchResults, } from "@/lib/actions/lyrics"; +import { LyricSource } from "@/lib/types"; +import { cookies } from "next/headers"; export async function searchSongs( _currentState: InternetProviderLyricSearchResponse[], @@ -15,3 +19,44 @@ export async function searchSongs( } return []; } + +export async function addSong( + _: unknown, + item: InternetProviderLyricSearchResponse +): Promise { + const token = cookies().get("token"); + await fetch("https://tachy.daoyoucloud.com/api/songs:create", { + method: "POST", + headers: { + "X-App": "danmu-sim", + "X-Authenticator": "basic", + authorization: "Bearer " + token?.value, + "content-type": "application/json", + }, + body: JSON.stringify({ + artist: item.artist, + name: item.name, + source: LyricSource.NETEASE, + source_id: item.id, + }), + }); + const params = new URLSearchParams(); + params.append('pageSize', '99999'); + params.append('filter', JSON.stringify({"$and":[{"createdBy":{"id":{"$eq":"{{$user.id}}"}}}]})) + const songs = await fetch("https://tachy.daoyoucloud.com/api/songs:list?" + params.toString(), { + method: "GET", + headers: { + "X-App": "danmu-sim", + "X-Authenticator": "basic", + authorization: "Bearer " + token?.value, + "content-type": "application/json", + }, + }); + const result = await songs.json(); + console.log(result.data); + return result?.data; +} + +export async function listSongs() { + +} \ No newline at end of file diff --git a/app/songs/search-form.tsx b/app/songs/search-form.tsx index 8349939..43c664e 100644 --- a/app/songs/search-form.tsx +++ b/app/songs/search-form.tsx @@ -1,7 +1,7 @@ "use client"; import { useFormState } from "react-dom"; -import { searchSongs } from "./actions"; +import { addSong, searchSongs } from "./actions"; export default function SearchForm({ className, @@ -9,6 +9,7 @@ export default function SearchForm({ className?: string | undefined; }) { const [items, dispatch] = useFormState(searchSongs, []); + const [_, dispatchAdd] = useFormState(addSong, undefined); return (
{item.name} - {item.artist} - diff --git a/lib/actions/lyrics.ts b/lib/actions/lyrics.ts index b39d0ee..d16a1ac 100644 --- a/lib/actions/lyrics.ts +++ b/lib/actions/lyrics.ts @@ -2,16 +2,11 @@ import Fuse, { type IFuseOptions } from "fuse.js"; import axios, { type AxiosResponse } from "axios"; +import { LyricSource } from "../types"; const SEARCH_URL = "https://music.163.com/api/search/get"; const LYRICS_URL = "https://music.163.com/api/song/lyric"; -enum LyricSource { - GENIUS = "Genius", - LRCLIB = "lrclib.net", - NETEASE = "NetEase", -} - export interface InternetProviderLyricSearchResponse { artist: string; id: string; diff --git a/lib/types.ts b/lib/types.ts new file mode 100644 index 0000000..1def8d7 --- /dev/null +++ b/lib/types.ts @@ -0,0 +1,7 @@ + +export enum LyricSource { + GENIUS = "Genius", + LRCLIB = "lrclib.net", + NETEASE = "NetEase", + QQMUSIC = "QQMusic", +} \ No newline at end of file