KTVCentral/app/Livewire/Pages/SearchSong.php
allen.yan ef865b50e1 20250902
移除使用的密碼記錄
加入手機點歌介面
加入 ftp驗証
2025-09-02 11:25:28 +08:00

105 lines
2.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Livewire\Pages;
use Livewire\Component;
use App\Models\Artist;
use App\Models\SongLibraryCache;
class SearchSong extends Component
{
public string $search = '';
public $searchCategory = '';
public $selectedLanguage = '國語'; // 預設語言
public $selectedArtists = []; // 儲存已選的歌手 id
public $artistOptions = []; // API 回傳的搜尋結果
public string $artistSearch = ''; // 即時搜尋文字
/**
* 初始化(接收外部傳入的分類參數)
*/
public function mount(string $searchCategory = '')
{
$this->searchCategory = $searchCategory;
}
/**
* 切換語言標籤
*/
public function selectLanguage(string $lang): void
{
$this->selectedLanguage = $lang;
}
/**
* 搜尋歌手(透過 API
*/
public function updatedArtistSearch()
{
$search = $this->artistSearch;
if ($search) {
// 即時搜尋歌手,從資料庫查詢
$this->artistOptions = \App\Models\Artist::query()
->where('name', 'like', "{$search}%")
->limit(100)
->get(['id', 'name'])
->toArray();
} else {
$this->artistOptions = [];
}
}
public function addArtist($id, $name)
{
$this->selectedArtists[$id] = $name;
$this->artistSearch = ''; // 清空搜尋框
$this->artistOptions = [];
}
public function removeArtist($id)
{
unset($this->selectedArtists[$id]);
}
/**
* 點歌
*/
public function orderSong(int $songId): void
{
// TODO: 加入已點歌曲邏輯,例如:
// auth()->user()->room->addSong($songId);
$this->dispatchBrowserEvent('notify', [
'message' => '已加入已點歌曲'
]);
}
/**
* 取得歌曲清單
*/
protected function loadSongs()
{
return SongLibraryCache::query()
->when($this->search, fn($q) => $q->where('song_name', 'like', "{$this->search}%"))
->when($this->selectedLanguage, fn($q) => $q->where('language_name', $this->selectedLanguage))
->when($this->selectedArtists, fn ($q) => $q->whereHas('artists', fn ($q2) => $q2->whereIn('id', array_keys($this->selectedArtists))))
->when($this->searchCategory === 'New', fn($q) => $q->orderByDesc('add_date'))
->when($this->searchCategory === 'Hot', fn($q) => $q->orderByDesc('song_counts'), fn($q) => $q->orderByDesc('song_id'))
//->take($this->search || $this->artistSearch ? 100 : 1000)
->take(1000)
->get();
}
/**
* Render
*/
public function render()
{
$songs = $this->loadSongs();
return view('livewire.pages.search-song', [
'songs' => $songs,
'languages' => \App\Enums\SongLanguageType::options(),
'selectedLanguage' => $this->selectedLanguage,
]);
}
}