|
@@ -5,8 +5,10 @@ namespace App\Services\Book;
|
|
|
use App\Consts\ErrorConst;
|
|
|
use App\Facade\Site;
|
|
|
use App\Libs\Utils;
|
|
|
+use getID3;
|
|
|
use GuzzleHttp\Client;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
use Illuminate\Support\Facades\Redis;
|
|
|
use OSS\Core\OssException;
|
|
|
use OSS\OssClient;
|
|
@@ -273,6 +275,15 @@ class BookService
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
+ public function audioEffectList($data) {
|
|
|
+ $audio_effect_name = getProp($data, 'audio_effect_name');
|
|
|
+
|
|
|
+ $query = DB::table('mp_audio_effects')->where('is_enabled', 1);
|
|
|
+ if ($audio_effect_name) $query->where('audio_effect_name', 'like', '%'.$audio_effect_name.'%');
|
|
|
+
|
|
|
+ return $query->paginate();
|
|
|
+ }
|
|
|
+
|
|
|
public function audioEffects($data) {
|
|
|
return DB::table('mp_audio_effects')->where('is_enabled', 1)->select('audio_effect_name', 'audio_effect_url')->get()->map(function ($value) {
|
|
|
return (array)$value;
|
|
@@ -283,6 +294,19 @@ class BookService
|
|
|
$audio_effect_name = getProp($data, 'audio_effect_name');
|
|
|
|
|
|
if (!$file) Utils::throwError('20003:请上传音频文件');
|
|
|
+
|
|
|
+ // 获取音频时长
|
|
|
+ $filename = $file->getPathname();
|
|
|
+ try {
|
|
|
+ $getID3 = new getID3();
|
|
|
+ $ThisFileInfo = $getID3->analyze($filename);
|
|
|
+ $seconds = floor($ThisFileInfo['playtime_seconds']) ?? 0;
|
|
|
+ if ($seconds <= 0) $seconds = 1;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::info('音频文件解析失败: '.$e->getMessage());
|
|
|
+ Utils::throwError('20003:音频文件解析失败');
|
|
|
+ }
|
|
|
+
|
|
|
// 验证文件格式(常见音频格式)
|
|
|
if (!in_array($file->getClientOriginalExtension(), ['mp3', 'wav', 'flac', 'm4a', 'ogg', 'wma', 'cda', 'aiff', 'au', 'aac', 'amr'])) Utils::throwError('20003:音频格式错误');
|
|
|
if (!$audio_effect_name) Utils::throwError('20003:请输入音频效果名称');
|
|
@@ -294,15 +318,64 @@ class BookService
|
|
|
|
|
|
$boolen = DB::table('mp_audio_effects')->insert([
|
|
|
'audio_effect_name' => $audio_effect_name,
|
|
|
- 'audio_effect_url' => $audio_effect_url,
|
|
|
- 'is_enabled' => 1,
|
|
|
- 'created_at' => date('Y-m-d H:i:s'),
|
|
|
- 'updated_at' => date('Y-m-d H:i:s')
|
|
|
+ 'audio_effect_url' => $audio_effect_url,
|
|
|
+ 'is_enabled' => 1,
|
|
|
+ 'playtime_seconds' => $seconds,
|
|
|
+ 'created_at' => date('Y-m-d H:i:s'),
|
|
|
+ 'updated_at' => date('Y-m-d H:i:s')
|
|
|
]);
|
|
|
if (!$boolen) Utils::throwError('20003:添加音效失败');
|
|
|
return ['audio_effect_name' => $audio_effect_name, 'audio_effect_url' => $audio_effect_url];
|
|
|
}
|
|
|
|
|
|
+ public function editAudioEffect($data, $file) {
|
|
|
+ $audio_effect_id = getProp($data, 'audio_effect_id');
|
|
|
+ if (!$audio_effect_id) Utils::throwError('20003:请选择音效');
|
|
|
+ $audio_effect_name = getProp($data, 'audio_effect_name');
|
|
|
+ $exists_id = DB::table('mp_audio_effects')->where('audio_effect_name', $audio_effect_name)->value('id');
|
|
|
+ if ($exists_id && $exists_id != $audio_effect_id) Utils::throwError('20003:音效名称已存在');
|
|
|
+
|
|
|
+ $update_data = [
|
|
|
+ 'audio_effect_name' => $audio_effect_name,
|
|
|
+ 'updated_at' => date('Y-m-d H:i:s')
|
|
|
+ ];
|
|
|
+ if ($file) {
|
|
|
+ // 获取音频时长
|
|
|
+ $filename = $file->getPathname();
|
|
|
+ try {
|
|
|
+ $getID3 = new getID3();
|
|
|
+ $ThisFileInfo = $getID3->analyze($filename);
|
|
|
+ $seconds = floor($ThisFileInfo['playtime_seconds']) ?? 0;
|
|
|
+ if ($seconds <= 0) $seconds = 1;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::info('音频文件解析失败: '.$e->getMessage());
|
|
|
+ Utils::throwError('20003:音频文件解析失败');
|
|
|
+ }
|
|
|
+ $update_data['playtime_seconds'] = $seconds;
|
|
|
+
|
|
|
+ // 验证文件格式(常见音频格式)
|
|
|
+ if (!in_array($file->getClientOriginalExtension(), ['mp3', 'wav', 'flac', 'm4a', 'ogg', 'wma', 'cda', 'aiff', 'au', 'aac', 'amr'])) Utils::throwError('20003:音频格式错误');
|
|
|
+ if (!$audio_effect_name) Utils::throwError('20003:请输入音频效果名称');
|
|
|
+ if (DB::table('mp_audio_effects')->where('audio_effect_name', $audio_effect_name)->exists()) Utils::throwError('20003:音效名称已存在');
|
|
|
+
|
|
|
+ // 上传文件到oss
|
|
|
+ $audio_effect_url = uploadFile('audio_effects', $file, $audio_effect_name);
|
|
|
+ if (!$audio_effect_url) Utils::throwError('20003:上传音频文件失败');
|
|
|
+ $update_data['audio_effect_url'] = $audio_effect_url;
|
|
|
+ }
|
|
|
+
|
|
|
+ return DB::table('mp_audio_effects')->where('id', $audio_effect_id)->update($update_data);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function bgmList($data) {
|
|
|
+ $bgm_name = getProp($data, 'bgm_name');
|
|
|
+
|
|
|
+ $query = DB::table('mp_bgms')->where('is_enabled', 1);
|
|
|
+ if ($bgm_name) $query->where('bgm_name', 'like', '%'.$bgm_name.'%');
|
|
|
+
|
|
|
+ return $query->paginate();
|
|
|
+ }
|
|
|
+
|
|
|
public function bgms($data) {
|
|
|
return DB::table('mp_bgms')->where('is_enabled', 1)->select('bgm_name', 'bgm_url')->get()->map(function ($value) {
|
|
|
return (array)$value;
|
|
@@ -313,6 +386,19 @@ class BookService
|
|
|
$bgm_name = getProp($data, 'bgm_name');
|
|
|
|
|
|
if (!$file) Utils::throwError('20003:请上传音频文件');
|
|
|
+
|
|
|
+ // 获取音频时长
|
|
|
+ $filename = $file->getPathname();
|
|
|
+ try {
|
|
|
+ $getID3 = new getID3();
|
|
|
+ $ThisFileInfo = $getID3->analyze($filename);
|
|
|
+ $seconds = floor($ThisFileInfo['playtime_seconds']) ?? 0;
|
|
|
+ if ($seconds <= 0) $seconds = 1;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::info('音频文件解析失败: '.$e->getMessage());
|
|
|
+ Utils::throwError('20003:音频文件解析失败');
|
|
|
+ }
|
|
|
+
|
|
|
// 验证文件格式(常见音频格式)
|
|
|
if (!in_array($file->getClientOriginalExtension(), ['mp3', 'wav', 'flac', 'm4a', 'ogg', 'wma', 'cda', 'aiff', 'au', 'aac', 'amr'])) Utils::throwError('20003:音频格式错误');
|
|
|
if (!$bgm_name) Utils::throwError('20003:请输入音频效果名称');
|
|
@@ -332,4 +418,43 @@ class BookService
|
|
|
if (!$boolen) Utils::throwError('20003:添加bgm失败');
|
|
|
return ['bgm_name' => $bgm_name, 'bgm_url' => $bgm_url];
|
|
|
}
|
|
|
+
|
|
|
+ public function editBgm($data, $file) {
|
|
|
+ $bgm_id = getProp($data, 'bgm_id');
|
|
|
+ if (!$bgm_id) Utils::throwError('20003:请选择bgm');
|
|
|
+ $bgm_name = getProp($data, 'bgm_name');
|
|
|
+ $exists_id = DB::table('mp_bgms')->where('bgm_name', $bgm_name)->value('id');
|
|
|
+ if ($exists_id && $exists_id != $bgm_id) Utils::throwError('20003:bgm名称已存在');
|
|
|
+
|
|
|
+ $update_data = [
|
|
|
+ 'bgm_name' => $bgm_name,
|
|
|
+ 'updated_at' => date('Y-m-d H:i:s')
|
|
|
+ ];
|
|
|
+ if ($file) {
|
|
|
+ // 获取音频时长
|
|
|
+ $filename = $file->getPathname();
|
|
|
+ try {
|
|
|
+ $getID3 = new getID3();
|
|
|
+ $ThisFileInfo = $getID3->analyze($filename);
|
|
|
+ $seconds = floor($ThisFileInfo['playtime_seconds']) ?? 0;
|
|
|
+ if ($seconds <= 0) $seconds = 1;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::info('音频文件解析失败: '.$e->getMessage());
|
|
|
+ Utils::throwError('20003:音频文件解析失败');
|
|
|
+ }
|
|
|
+ $update_data['playtime_seconds'] = $seconds;
|
|
|
+
|
|
|
+ // 验证文件格式(常见音频格式)
|
|
|
+ if (!in_array($file->getClientOriginalExtension(), ['mp3', 'wav', 'flac', 'm4a', 'ogg', 'wma', 'cda', 'aiff', 'au', 'aac', 'amr'])) Utils::throwError('20003:音频格式错误');
|
|
|
+ if (!$bgm_name) Utils::throwError('20003:请输入音频效果名称');
|
|
|
+ if (DB::table('mp_bgms')->where('bgm_name', $bgm_name)->exists()) Utils::throwError('20003:bgm名称已存在');
|
|
|
+
|
|
|
+ // 上传文件到oss
|
|
|
+ $bgm_url = uploadFile('bgms', $file, $bgm_name);
|
|
|
+ if (!$bgm_url) Utils::throwError('20003:上传音频文件失败');
|
|
|
+ $update_data['bgm_url'] = $bgm_url;
|
|
|
+ }
|
|
|
+
|
|
|
+ return DB::table('mp_bgms')->where('id', $bgm_id)->update($update_data);
|
|
|
+ }
|
|
|
}
|