SongController.php 4.4 KB
<?php


namespace App\Http\Controllers\OpenApi;


use App\Helper\ErrorCode;
use App\Helper\Response;
use App\Http\Controllers\Controller;
use App\Models\Legal\Song;
use App\Models\Legal\SongTagsClass;
use Illuminate\Http\Request;

class SongController extends Controller
{

    protected static $songs_limit = 10;

    protected static $input_types = [1,2,3];

    public function list(Request $request)
    {
        if (!$request->has('type') || !$request->has('value')) {
            return Response::successWithCode(ErrorCode::MISSING_PARAMS, ErrorCode::$messages[ErrorCode::MISSING_PARAMS]);
        }

        if (!$request->filled('type') || !$request->filled('value')) {
            return Response::successWithCode(ErrorCode::EMPTY_PARAMS, ErrorCode::$messages[ErrorCode::EMPTY_PARAMS]);
        }

        if (!in_array($request->get('type'), static::$input_types)) {
            return Response::successWithCode(ErrorCode::ILLEGAL_PARAMS, ErrorCode::$messages[ErrorCode::ILLEGAL_PARAMS]);
        }

        $songs = Song::with('subject', 'tags', 'files')
            ->where([
                'lyrics_right'    => 1,
                'tune_right'      => 1,
                'performer_right' => 1,
                'tape_right'      => 1,
            ])
            ->when($request->filled('value'), function ($query) use ($request) {
                switch ($request->get('type')) {
                    case 1:
                        $query->where('custom_id', $request->get('value'));
                        break;
                    case 2:
                        $query->where('name', 'like', "%{$request->get('value')}%");
                        break;
                    case 3:
                        $query->where('singer', 'like', "%{$request->get('value')}%");
                        break;
                }
            });

        $total = $songs->count();

        $songs = $songs->orderByDesc('id')
            ->forPage($request->get('page', 1), static::$songs_limit)
            ->get(['custom_id as song_id', 'name as song_name', 'lyricist', 'composer', 'singer', 'lyrics_right',
                'tune_right', 'performer_right', 'tape_right', 'status', 'subject_no', 'tag_id'])
            ->toArray();

        if (count($songs)) {
            $tags = SongTagsClass::pluck('name', 'id')->toArray();
            foreach ($songs as &$song) {
                $song['lyrics_right'] = $song['tune_right'] = $song['performer_right'] = $song['tape_right'] = '自有';
                $song['subject'] = $song['subject'] ? $song['subject']['name'] : '';

                if ($song['tags']) {
                    $song['tags'] = $this->coverTags($song['tags'], $tags);
                }

                if ($song['files']) {
                    $song['files'] = $this->coverFiles($song['files']);
                }

                $song['type'] = 1;

                unset($song['subject_no'], $song['tag_id']);
            }
        }

        return Response::successWithCode(ErrorCode::SERVER_OK,
            ErrorCode::$messages[ErrorCode::SERVER_OK],
            compact('total', 'songs')
        );
    }

    public function coverTags($song_tags, $tags)
    {
        unset($song_tags['id'], $song_tags['token']);
        foreach ($song_tags as $tag_name => $tag) {
            if ('tag_speed' != $tag_name) {
                $song_tags[$tag_name] = coverData($tag, $tags);
            }
        }

        return $song_tags;
    }

    public function coverFiles($song_files): array
    {
        $files_need = ['mp3_url', 'accompany_mp3_url', 'cover_url', 'wav_url', 'accompany_wav_url'];
        $files_arr = [];
        foreach ($song_files as $file) {
            switch ($file['type']) {
                case 1:
                    $files_arr['mp3_url'] = $file['url'];
                    break;
                case 2:
                    $files_arr['accompany_mp3_url'] = $file['url'];
                    break;
                case 4:
                    $files_arr['cover_url'] = $file['url'];
                    break;
                case 11:
                    $files_arr['wav_url'] = $file['url'];
                    break;
                case 12:
                    $files_arr['accompany_wav_url'] = $file['url'];
                    break;
            }
        }

        foreach ($files_need as $v) {
            if (!isset($files_arr[$v])) {
                $files_arr[$v] = null;
            }
        }

        return $files_arr;
    }
}