From 35695714ac1de42109306efd2037b2f750db8633 Mon Sep 17 00:00:00 2001 From: sealday Date: Sat, 6 Apr 2024 18:08:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=94=B6=E8=97=8F?= =?UTF-8?q?=E6=AD=8C=E6=9B=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/songs/actions.tsx | 45 +++++++++++++++++++++++++++++++++++++++ app/songs/search-form.tsx | 10 +++++++-- lib/actions/lyrics.ts | 7 +----- lib/types.ts | 7 ++++++ 4 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 lib/types.ts 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