SongController.php
5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?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 (!in_array($request->get('type'), static::$input_types)) {
return Response::successWithCode(ErrorCode::ILLEGAL_PARAMS, ErrorCode::$messages[ErrorCode::ILLEGAL_PARAMS]);
}*/
if (empty($request->get('song_id')) && empty($request->get('song_name')) &&empty($request->get('singer'))) {
return Response::successWithCode(ErrorCode::EMPTY_PARAMS, ErrorCode::$messages[ErrorCode::EMPTY_PARAMS]);
}
$songs = Song::with('subject', 'tags', 'files')
->where([
'lyrics_right' => 1,
'tune_right' => 1,
'performer_right' => 1,
'tape_right' => 1,
])
->when($request->filled('song_id'), function ($query) use ($request) {
$query->where('custom_id', 'like', "%{$request->get('song_id')}%");
})
->when($request->filled('song_name'), function ($query) use ($request) {
$query->where('name', 'like', "%{$request->get('song_name')}%");
})
->when($request->filled('singer'), function ($query) use ($request) {
$query->where('singer', 'like', "%{$request->get('singer')}%");
})
->where('has_all_files', 1);
$total = $songs->count();
$songs = $songs->forPage($request->get('page', 1), $request->get('limit', 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', 'lyrics_right_date',
'tune_right_date', 'performer_right_date', 'tape_right_date'])
->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['lyrics_right_date'] = $this->coverRightDate($song['lyrics_right_date']);
$song['tune_right_date'] = $this->coverRightDate($song['tune_right_date']);
$song['performer_right_date'] = $this->coverRightDate($song['performer_right_date']);
$song['tape_right_date'] = $this->coverRightDate($song['tape_right_date']);
$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', 'lyric_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 3:
$files_arr['lyric_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;
}
public function coverRightDate($right_date)
{
$date = ['start' => '无', 'end' => '无'];
try {
if ($right_date) {
$tmp = explode(' - ', $right_date);
$date = [
'start' => $tmp[0],
'end' => $tmp[1],
];
}
} catch (\Exception $exception) {
return $date;
}
return $date;
}
}