BookService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. namespace App\Services\Book;
  3. use App\Consts\ErrorConst;
  4. use App\Facade\Site;
  5. use App\Libs\Utils;
  6. use getID3;
  7. use GuzzleHttp\Client;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Support\Facades\Redis;
  11. use OSS\Core\OssException;
  12. use OSS\OssClient;
  13. class BookService
  14. {
  15. public function __construct() {
  16. }
  17. public function getBookList($data) {
  18. $bid = getProp($data, 'bid');
  19. $book_name = getProp($data, 'book_name');
  20. $status = getProp($data, 'status');
  21. $page = getProp($data, 'page', 1);
  22. $per_page = getProp($data, 'pageSize', 15);
  23. $query = DB::table('books as b')->leftJoin('book_configs as bc', 'b.id', '=', 'bc.bid')
  24. ->where('b.size', '<', 30000)->where('b.status', 1)
  25. ->whereIn('bc.cp_source', ['ycsd', 'yqsd'])->select('b.status', 'bc.*',
  26. DB::raw('(select count(id) from mp_book_version where bid = b.id) as version_count'));
  27. if ($bid) {
  28. $query->where('b.id', $bid);
  29. }
  30. if ($book_name) {
  31. $query->where('bc.book_name', 'like', "%{$book_name}%");
  32. }
  33. if ($status !== '') {
  34. $query->where('b.status', $status);
  35. }
  36. $result = $query->orderBy('b.id')->paginate($per_page);
  37. return $result;
  38. }
  39. public function getAllBooks($data) {
  40. $bid = getProp($data, 'bid');
  41. $book_name = getProp($data, 'book_name');
  42. $query = DB::table('books as b')->leftJoin('book_configs as bc', 'b.id', '=', 'bc.bid')
  43. ->where('b.size', '<', 30000)->where('b.status', 1)
  44. ->whereIn('bc.cp_source', ['ycsd', 'yqsd'])->select('bc.bid', 'bc.book_name');
  45. if ($bid) {
  46. $query->where('bc.bid', $bid);
  47. }
  48. if ($book_name) {
  49. $query->where('bc.book_name', 'like', "%{$book_name}%");
  50. }
  51. return $query->orderBy('bc.bid', 'desc')->get()->map(function($value) {
  52. return (array)$value;
  53. })->toArray();
  54. }
  55. public function getBookVersion($data) {
  56. $bid = getProp($data, 'bid');
  57. if (!$bid) Utils::throwError('20003:请选择书籍');
  58. return DB::table('mp_book_version')->where('bid', $bid)->orderBy('id', 'desc')->select('bid', 'book_name', 'id as version_id', 'version_name')
  59. ->get()->map(function($value) {
  60. $value = (array)$value;
  61. $value['version_name'] = $value['book_name']."【{$value['version_name']}】";
  62. return $value;
  63. })->toArray();
  64. }
  65. public function addBookVersion($data) {
  66. $bid = getProp($data, 'bid');
  67. if (!$bid) Utils::throwError('20003:请选择书籍');
  68. $version_name = trim(getProp($data, 'version_name'));
  69. if (!$version_name) Utils::throwError('20003:请填写版本名');
  70. $book_name = DB::table('book_configs')->where('bid', $bid)->value('book_name');
  71. if (DB::table('mp_book_version')->where('bid', $bid)->where('version_name', $version_name)->exists()) Utils::throwError('20003:版本名已存在');
  72. $all_chapters = DB::table('chapters')->where('bid', $bid)->where('is_check', 1)->where('is_deleted', 0)->select('id', 'name', 'sequence', 'size', 'chapter_content_id')->get();
  73. try {
  74. DB::beginTransaction();
  75. $version_id = DB::table('mp_book_version')->insertGetId([
  76. 'bid' => $bid,
  77. 'book_name' => $book_name,
  78. 'version_name' => $version_name,
  79. 'created_at' => date('Y-m-d H:i:s'),
  80. 'updated_at' => date('Y-m-d H:i:s'),
  81. ]);
  82. if (!$version_id) {
  83. DB::rollBack();
  84. Utils::throwError('20003:创建版本失败');
  85. }
  86. $insert_data = [];
  87. foreach ($all_chapters as $chapter) {
  88. $insert_data[] = [
  89. 'bid' => $bid,
  90. 'book_name' => $book_name,
  91. 'version_id' => $version_id,
  92. 'version_name' => $version_name,
  93. 'cid' => getProp($chapter, 'id'),
  94. 'chapter_name' => getProp($chapter, 'name'),
  95. 'sequence' => getProp($chapter, 'sequence'),
  96. 'size' => getProp($chapter, 'size'),
  97. 'generate_status' => '待制作',
  98. 'created_at' => date('Y-m-d H:i:s'),
  99. 'updated_at' => date('Y-m-d H:i:s'),
  100. ];
  101. }
  102. $boolen = DB::table('mp_chapter_audios')->insert($insert_data);
  103. if (!$boolen) {
  104. DB::rollBack();
  105. Utils::throwError('20003:带入章节失败');
  106. }
  107. }catch (\Exception $e) {
  108. DB::rollBack();
  109. Utils::throwError('20003:'.$e->getMessage());
  110. }
  111. DB::commit();
  112. return [
  113. 'bid' => $bid,
  114. 'version_id' => $version_id,
  115. 'version_name' => $book_name."【{$version_name}】",
  116. ];
  117. }
  118. public function editBookVersion($data) {
  119. $version_id = getProp($data, 'version_id');
  120. $version_name = trim(getProp($data, 'version_name'));
  121. if (!$version_id) Utils::throwError('20003:请选择版本');
  122. if (!$version_name) Utils::throwError('20003:请填写版本名');
  123. DB::beginTransaction();
  124. $boolen = DB::table('mp_book_version')->where('id', $version_id)->update([
  125. 'version_name' => $version_name,
  126. 'updated_at' => date('Y-m-d H:i:s'),
  127. ]);
  128. if (!$boolen) {
  129. DB::rollBack();
  130. Utils::throwError('20003:修改书籍版本名称失败');
  131. }
  132. $boolen1 = DB::table('mp_chapter_audios')->where('version_id', $version_id)->update([
  133. 'version_name' => $version_name,
  134. 'updated_at' => date('Y-m-d H:i:s')
  135. ]);
  136. if (!$boolen1) {
  137. DB::rollBack();
  138. Utils::throwError('20003:修改章节版本名称失败');
  139. }
  140. DB::commit();
  141. return true;
  142. }
  143. public function getChapterList($data) {
  144. $bid = getProp($data, 'bid');
  145. $version_id = getProp($data, 'version_id');
  146. $page = getProp($data, 'page', 1);
  147. $per_page = getProp($data, 'pageSize', 15);
  148. if (!$bid) Utils::throwError('20003:请选择书籍');
  149. if (!$version_id) Utils::throwError('20003:请选择版本');
  150. $generate_status = getProp($data, 'generate_status');
  151. $book = DB::table('books as b')->leftJoin('book_configs as bc', 'b.id', '=', 'bc.bid')->where('b.id', $bid)
  152. ->select('bc.book_name', 'bc.cover', 'b.size', 'b.chapter_count', 'b.intro')->first();
  153. if (!$book) Utils::throwError('20003:书籍不存在');
  154. $header = (array)$book;
  155. $header['cover'] = addPrefix($header['cover']);
  156. $header['intro'] = filterIntro($header['intro']);
  157. $role_info = DB::table('mp_book_version')->where('id', $version_id)->where('bid', $bid)->value('role_info');
  158. if ($role_info) {
  159. foreach(json_decode($role_info, true) as $role => $timbre) {
  160. // 获取音色信息
  161. $voice_type = getProp($timbre, 'timbre_type');
  162. $timbre_info = DB::table('mp_timbres')->where('timbre_type', $voice_type)->first();
  163. $header['role_info'][] = [
  164. 'role' => $role,
  165. 'voice_type' => $voice_type,
  166. 'voice_name' => getProp($timbre, 'timbre_name'),
  167. 'emotion' => getProp($timbre, 'emotion'),
  168. 'emotion_type' => getProp($timbre, 'emotion_type'),
  169. 'speed_ratio' => getProp($timbre, 'speed_ratio'),
  170. 'loudness_ratio' => getProp($timbre, 'loudness_ratio'),
  171. 'emotion_scale' => getProp($timbre, 'emotion_scale'),
  172. 'pitch' => getProp($timbre, 'pitch'),
  173. 'language' => getProp($timbre_info,'language'),
  174. 'dialect' => getProp($timbre_info,'dialect'),
  175. 'emotion' => getProp($timbre_info,'emotion'),
  176. 'first_category_id' => getProp($timbre_info,'first_category_id'),
  177. 'first_category_name' => getProp($timbre_info, 'first_category_name'),
  178. 'second_category_id' => getProp($timbre_info,'second_category_id'),
  179. 'second_category_name' => getProp($timbre_info, 'second_category_name'),
  180. 'third_category_id' => getProp($timbre_info,'third_category_id'),
  181. 'third_category_name' => getProp($timbre_info,'third_category_name'),
  182. 'audio_url' => getProp($timbre_info,'audio_url'),
  183. 'label' => getProp($timbre_info, 'label'),
  184. ];
  185. }
  186. }else {
  187. $header['role_info'] = [];
  188. }
  189. $query = DB::table('mp_chapter_audios')->where('bid', $bid)->where('version_id', $version_id)
  190. ->select('cid', 'chapter_name', 'sequence', 'size', 'generate_status', 'audio_url', 'remark');
  191. if ($generate_status) {
  192. $query->where('generate_status', $generate_status);
  193. }
  194. $list = $query->orderBy('sequence', 'asc')->paginate($per_page);
  195. return ['header' => $header, 'list' => $list];
  196. }
  197. public function getChapterContent($data) {
  198. $cid = getProp($data, 'cid');
  199. if (!$cid) Utils::throwError('20003:请选择章节');
  200. $content = DB::table('chapters as c')->leftJoin('chapter_contents as cc', 'c.chapter_content_id', '=', 'cc.id')->where('c.id', $cid)->value('cc.content');
  201. if (!$content) Utils::throwError('20003:章节内容不存在');
  202. $content = filterContent($content);
  203. return $content;
  204. }
  205. public function getVersionList($data) {
  206. $version_id = getProp($data, 'version_id');
  207. $version_name = getProp($data, 'version_name');
  208. $book_name = getProp($data, 'book_name');
  209. $page = getProp($data, 'page', 1);
  210. $per_page = getProp($data, 'pageSize', 15);
  211. $query = DB::table('mp_book_version as v')->leftJoin('book_configs as bc', 'v.bid', '=', 'bc.bid');
  212. if ($version_id) $query->where('v.id', $version_id);
  213. if ($version_name) $query->where('v.version_name', 'like', '%'.$version_name.'%');
  214. if ($book_name) $query->where('bc.book_name', 'like', '%'.$book_name.'%');
  215. $list = $query->select('v.*', 'bc.book_name')->paginate($per_page);
  216. return $list;
  217. }
  218. public function getTaskList($data) {
  219. $task_id = getProp($data, 'task_id');
  220. $task_name = getProp($data, 'task_name');
  221. $generate_status = getProp($data, 'generate_status');
  222. $page = getProp($data, 'page', 1);
  223. $per_page = getProp($data, 'pageSize', 15);
  224. $query = DB::table('mp_audio_tasks')->select('id', 'task_name', 'generate_status', 'bid', 'version_id', 'cid', 'created_at');
  225. if ($task_id) $query->where('id', $task_id);
  226. if ($task_name) $query->where('task_name', 'like', '%'.$task_name.'%');
  227. if ($generate_status) $query->where('generate_status', $generate_status);
  228. $list = $query->orderBy('id', 'desc')->paginate($per_page);
  229. return $list;
  230. }
  231. public function getTaskDetail($data) {
  232. $task_id = getProp($data, 'task_id');
  233. $result = DB::table('mp_audio_tasks')->where('id', $task_id)->select('id as task_id', 'task_name', 'generate_status', 'generate_json', 'bid', 'version_id', 'cid')->first();
  234. if (!$result) Utils::throwError('20003:任务不存在');
  235. $result = (array)$result;
  236. $result['generate_json'] = json_decode($result['generate_json'], true);
  237. return $result;
  238. }
  239. public function audioEffectList($data) {
  240. $audio_effect_name = getProp($data, 'audio_effect_name');
  241. $page = getProp($data, 'page', 1);
  242. $per_page = getProp($data, 'pageSize', 15);
  243. $query = DB::table('mp_audio_effects')->where('is_enabled', 1);
  244. if ($audio_effect_name) $query->where('audio_effect_name', 'like', '%'.$audio_effect_name.'%');
  245. return $query->paginate($per_page);
  246. }
  247. public function audioEffects($data) {
  248. return DB::table('mp_audio_effects')->where('is_enabled', 1)->select('audio_effect_name', 'audio_effect_url')->get()->map(function ($value) {
  249. return (array)$value;
  250. })->toArray();
  251. }
  252. public function uploadAudioEffect($data, $file) {
  253. $audio_effect_name = getProp($data, 'audio_effect_name');
  254. if (!$file) Utils::throwError('20003:请上传音频文件');
  255. // 获取音频时长
  256. $filename = $file->getPathname();
  257. try {
  258. $getID3 = new getID3();
  259. $ThisFileInfo = $getID3->analyze($filename);
  260. $seconds = floor($ThisFileInfo['playtime_seconds']) ?? 0;
  261. if ($seconds <= 0) $seconds = 1;
  262. } catch (\Exception $e) {
  263. Log::info('音频文件解析失败: '.$e->getMessage());
  264. Utils::throwError('20003:音频文件解析失败');
  265. }
  266. // 验证文件格式(常见音频格式)
  267. if (!in_array($file->getClientOriginalExtension(), ['mp3', 'wav', 'flac', 'm4a', 'ogg', 'wma', 'cda', 'aiff', 'au', 'aac', 'amr'])) Utils::throwError('20003:音频格式错误');
  268. if (!$audio_effect_name) Utils::throwError('20003:请输入音频效果名称');
  269. if (DB::table('mp_audio_effects')->where('audio_effect_name', $audio_effect_name)->exists()) Utils::throwError('20003:音效名称已存在');
  270. // // 上传文件到oss
  271. // $audio_effect_url = uploadFile('audio_effects', $file, $audio_effect_name);
  272. // 上传文件到tos
  273. $audio_effect_url = uploadFileByTos('effects', $file, $audio_effect_name);
  274. if (!$audio_effect_url) Utils::throwError('20003:上传音频文件失败');
  275. $boolen = DB::table('mp_audio_effects')->insert([
  276. 'audio_effect_name' => $audio_effect_name,
  277. 'audio_effect_url' => $audio_effect_url,
  278. 'is_enabled' => 1,
  279. 'playtime_seconds' => $seconds,
  280. 'created_at' => date('Y-m-d H:i:s'),
  281. 'updated_at' => date('Y-m-d H:i:s')
  282. ]);
  283. if (!$boolen) Utils::throwError('20003:添加音效失败');
  284. return ['audio_effect_name' => $audio_effect_name, 'audio_effect_url' => $audio_effect_url];
  285. }
  286. public function editAudioEffect($data, $file) {
  287. $audio_effect_id = getProp($data, 'audio_effect_id');
  288. if (!$audio_effect_id) Utils::throwError('20003:请选择音效');
  289. $audio_effect_name = getProp($data, 'audio_effect_name');
  290. $exists_id = DB::table('mp_audio_effects')->where('audio_effect_name', $audio_effect_name)->value('id');
  291. if ($exists_id && $exists_id != $audio_effect_id) Utils::throwError('20003:音效名称已存在');
  292. $update_data = [
  293. 'audio_effect_name' => $audio_effect_name,
  294. 'updated_at' => date('Y-m-d H:i:s')
  295. ];
  296. if ($file) {
  297. // 获取音频时长
  298. $filename = $file->getPathname();
  299. try {
  300. $getID3 = new getID3();
  301. $ThisFileInfo = $getID3->analyze($filename);
  302. $seconds = floor($ThisFileInfo['playtime_seconds']) ?? 0;
  303. if ($seconds <= 0) $seconds = 1;
  304. } catch (\Exception $e) {
  305. Log::info('音频文件解析失败: '.$e->getMessage());
  306. Utils::throwError('20003:音频文件解析失败');
  307. }
  308. $update_data['playtime_seconds'] = $seconds;
  309. // 验证文件格式(常见音频格式)
  310. if (!in_array($file->getClientOriginalExtension(), ['mp3', 'wav', 'flac', 'm4a', 'ogg', 'wma', 'cda', 'aiff', 'au', 'aac', 'amr'])) Utils::throwError('20003:音频格式错误');
  311. if (!$audio_effect_name) Utils::throwError('20003:请输入音频效果名称');
  312. if (DB::table('mp_audio_effects')->where('audio_effect_name', $audio_effect_name)->exists()) Utils::throwError('20003:音效名称已存在');
  313. // // 上传文件到oss
  314. // $audio_effect_url = uploadFile('audio_effects', $file, $audio_effect_name);
  315. // 上传文件到tos
  316. $audio_effect_url = uploadFileByTos('effects', $file, $audio_effect_name);
  317. if (!$audio_effect_url) Utils::throwError('20003:上传音频文件失败');
  318. $update_data['audio_effect_url'] = $audio_effect_url;
  319. }
  320. return DB::table('mp_audio_effects')->where('id', $audio_effect_id)->update($update_data);
  321. }
  322. public function bgmList($data) {
  323. $bgm_name = getProp($data, 'bgm_name');
  324. $page = getProp($data, 'page', 1);
  325. $per_page = getProp($data, 'pageSize', 15);
  326. $query = DB::table('mp_bgms')->where('is_enabled', 1);
  327. if ($bgm_name) $query->where('bgm_name', 'like', '%'.$bgm_name.'%');
  328. return $query->paginate($per_page);
  329. }
  330. public function bgms($data) {
  331. return DB::table('mp_bgms')->where('is_enabled', 1)->select('bgm_name', 'bgm_url')->get()->map(function ($value) {
  332. return (array)$value;
  333. })->toArray();
  334. }
  335. public function uploadBgm($data, $file) {
  336. $bgm_name = getProp($data, 'bgm_name');
  337. if (!$file) Utils::throwError('20003:请上传音频文件');
  338. // 获取音频时长
  339. $filename = $file->getPathname();
  340. try {
  341. $getID3 = new getID3();
  342. $ThisFileInfo = $getID3->analyze($filename);
  343. $seconds = floor($ThisFileInfo['playtime_seconds']) ?? 0;
  344. if ($seconds <= 0) $seconds = 1;
  345. } catch (\Exception $e) {
  346. Log::info('音频文件解析失败: '.$e->getMessage());
  347. Utils::throwError('20003:音频文件解析失败');
  348. }
  349. // 验证文件格式(常见音频格式)
  350. if (!in_array($file->getClientOriginalExtension(), ['mp3', 'wav', 'flac', 'm4a', 'ogg', 'wma', 'cda', 'aiff', 'au', 'aac', 'amr'])) Utils::throwError('20003:音频格式错误');
  351. if (!$bgm_name) Utils::throwError('20003:请输入音频效果名称');
  352. if (DB::table('mp_bgms')->where('bgm_name', $bgm_name)->exists()) Utils::throwError('20003:bgm名称已存在');
  353. // // 上传文件到oss
  354. // $bgm_url = uploadFile('bgms', $file, $bgm_name);
  355. // 上传文件到tos
  356. $bgm_url = uploadFileByTos('bgm', $file, $bgm_name);
  357. if (!$bgm_url) Utils::throwError('20003:上传音频文件失败');
  358. $boolen = DB::table('mp_bgms')->insert([
  359. 'bgm_name' => $bgm_name,
  360. 'bgm_url' => $bgm_url,
  361. 'is_enabled' => 1,
  362. 'created_at' => date('Y-m-d H:i:s'),
  363. 'updated_at' => date('Y-m-d H:i:s')
  364. ]);
  365. if (!$boolen) Utils::throwError('20003:添加bgm失败');
  366. return ['bgm_name' => $bgm_name, 'bgm_url' => $bgm_url];
  367. }
  368. public function editBgm($data, $file) {
  369. $bgm_id = getProp($data, 'bgm_id');
  370. if (!$bgm_id) Utils::throwError('20003:请选择bgm');
  371. $bgm_name = getProp($data, 'bgm_name');
  372. $exists_id = DB::table('mp_bgms')->where('bgm_name', $bgm_name)->value('id');
  373. if ($exists_id && $exists_id != $bgm_id) Utils::throwError('20003:bgm名称已存在');
  374. $update_data = [
  375. 'bgm_name' => $bgm_name,
  376. 'updated_at' => date('Y-m-d H:i:s')
  377. ];
  378. if ($file) {
  379. // 获取音频时长
  380. $filename = $file->getPathname();
  381. try {
  382. $getID3 = new getID3();
  383. $ThisFileInfo = $getID3->analyze($filename);
  384. $seconds = floor($ThisFileInfo['playtime_seconds']) ?? 0;
  385. if ($seconds <= 0) $seconds = 1;
  386. } catch (\Exception $e) {
  387. Log::info('音频文件解析失败: '.$e->getMessage());
  388. Utils::throwError('20003:音频文件解析失败');
  389. }
  390. $update_data['playtime_seconds'] = $seconds;
  391. // 验证文件格式(常见音频格式)
  392. if (!in_array($file->getClientOriginalExtension(), ['mp3', 'wav', 'flac', 'm4a', 'ogg', 'wma', 'cda', 'aiff', 'au', 'aac', 'amr'])) Utils::throwError('20003:音频格式错误');
  393. if (!$bgm_name) Utils::throwError('20003:请输入音频效果名称');
  394. if (DB::table('mp_bgms')->where('bgm_name', $bgm_name)->exists()) Utils::throwError('20003:bgm名称已存在');
  395. // // 上传文件到oss
  396. // $bgm_url = uploadFile('bgms', $file, $bgm_name);
  397. // 上传文件到tos
  398. $bgm_url = uploadFileByTos('bgm', $file, $bgm_name);
  399. if (!$bgm_url) Utils::throwError('20003:上传音频文件失败');
  400. $update_data['bgm_url'] = $bgm_url;
  401. }
  402. return DB::table('mp_bgms')->where('id', $bgm_id)->update($update_data);
  403. }
  404. }