feat: 添加收藏歌曲

This commit is contained in:
sealday 2024-04-06 18:08:51 +08:00
parent 01f7a19de7
commit 35695714ac
4 changed files with 61 additions and 8 deletions

View File

@ -1,7 +1,11 @@
"use server";
import { import {
InternetProviderLyricSearchResponse, InternetProviderLyricSearchResponse,
getSearchResults, getSearchResults,
} from "@/lib/actions/lyrics"; } from "@/lib/actions/lyrics";
import { LyricSource } from "@/lib/types";
import { cookies } from "next/headers";
export async function searchSongs( export async function searchSongs(
_currentState: InternetProviderLyricSearchResponse[], _currentState: InternetProviderLyricSearchResponse[],
@ -15,3 +19,44 @@ export async function searchSongs(
} }
return []; return [];
} }
export async function addSong(
_: unknown,
item: InternetProviderLyricSearchResponse
): Promise<unknown> {
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() {
}

View File

@ -1,7 +1,7 @@
"use client"; "use client";
import { useFormState } from "react-dom"; import { useFormState } from "react-dom";
import { searchSongs } from "./actions"; import { addSong, searchSongs } from "./actions";
export default function SearchForm({ export default function SearchForm({
className, className,
@ -9,6 +9,7 @@ export default function SearchForm({
className?: string | undefined; className?: string | undefined;
}) { }) {
const [items, dispatch] = useFormState(searchSongs, []); const [items, dispatch] = useFormState(searchSongs, []);
const [_, dispatchAdd] = useFormState(addSong, undefined);
return ( return (
<form className={className} action={dispatch}> <form className={className} action={dispatch}>
<input <input
@ -27,7 +28,12 @@ export default function SearchForm({
<span> <span>
{item.name} - {item.artist} {item.name} - {item.artist}
</span> </span>
<button className="text-green-500 hover:text-green-700"> <button
className="text-green-500 hover:text-green-700"
onClick={() => {
dispatchAdd(item);
}}
>
</button> </button>
</div> </div>

View File

@ -2,16 +2,11 @@
import Fuse, { type IFuseOptions } from "fuse.js"; import Fuse, { type IFuseOptions } from "fuse.js";
import axios, { type AxiosResponse } from "axios"; import axios, { type AxiosResponse } from "axios";
import { LyricSource } from "../types";
const SEARCH_URL = "https://music.163.com/api/search/get"; const SEARCH_URL = "https://music.163.com/api/search/get";
const LYRICS_URL = "https://music.163.com/api/song/lyric"; const LYRICS_URL = "https://music.163.com/api/song/lyric";
enum LyricSource {
GENIUS = "Genius",
LRCLIB = "lrclib.net",
NETEASE = "NetEase",
}
export interface InternetProviderLyricSearchResponse { export interface InternetProviderLyricSearchResponse {
artist: string; artist: string;
id: string; id: string;

7
lib/types.ts Normal file
View File

@ -0,0 +1,7 @@
export enum LyricSource {
GENIUS = "Genius",
LRCLIB = "lrclib.net",
NETEASE = "NetEase",
QQMUSIC = "QQMusic",
}