MusicService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
  13. use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
  14. use App\Models\LiveModel;
  15. use App\Models\MemberModel;
  16. use App\Models\MusicModel;
  17. use App\Services\Api\MemberCollectService;
  18. use Darabonba\OpenApi\Models\Config;
  19. use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
  20. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  21. use Illuminate\Support\Facades\DB;
  22. /**
  23. * 在线音乐服务管理-服务类
  24. * @author laravel开发员
  25. * @since 2020/11/11
  26. * @package App\Services
  27. */
  28. class MusicService extends BaseService
  29. {
  30. // 静态对象
  31. protected static $instance = null;
  32. protected static $apiUrls = [
  33. 'info' => 'https://m.kugou.com/app/i/getSongInfo.php?cmd=playInfo&hash=%s',
  34. 'search' => 'http://mobilecdn.kugou.com/api/v3/search/song?format=json&keyword=%s&page=%s&pagesize=%s&showtype=1'
  35. ];
  36. /**
  37. * 构造函数
  38. * @author laravel开发员
  39. * @since 2020/11/11
  40. * ConfigService constructor.
  41. */
  42. public function __construct()
  43. {
  44. $this->model = new MusicModel();
  45. }
  46. /**
  47. * 静态入口
  48. * @return SmsService|static|null
  49. */
  50. public static function make()
  51. {
  52. if (!self::$instance) {
  53. self::$instance = new static();
  54. }
  55. return self::$instance;
  56. }
  57. /**
  58. * 搜索列表
  59. * @param $params
  60. * @param int $pageSize
  61. * @return array|mixed|string
  62. */
  63. public function getDataList($params, $pageSize = 20)
  64. {
  65. $cacheKey = "caches:music:index:{$pageSize}_" . md5(json_encode($params));
  66. $datas = RedisService::get($cacheKey);
  67. if ($datas) {
  68. return $datas;
  69. }
  70. $page = isset($params['page']) ? intval($params['page']) : 1;
  71. $kw = isset($params['kw']) ? trim($params['kw']) : '';
  72. if (empty($kw)) {
  73. $kws = ConfigService::make()->getConfigByCode('music_kws');
  74. $kws = $kws ? explode('|', $kws) : [];
  75. $kws = $kws ? $kws : ['抖音bgm', '抖音背景音乐', '背景音乐', '热门音乐'];
  76. $len = rand(0, count($kws) - 1);
  77. $kw = isset($kws[$len]) ? $kws[$len] : '抖音bgm';
  78. }
  79. $url = sprintf(self::$apiUrls['search'], $kw, $page, $pageSize);
  80. $result = httpRequest($url, '', 'get', '', 5);
  81. RedisService::set($cacheKey . '_temp', ['kw' => $kw, 'result' => $result, 'date' => date('Y-m-d H:i:s')], 3600);
  82. $data = isset($result['data']) ? $result['data'] : [];
  83. $list = isset($data['info']) ? $data['info'] : [];
  84. $total = isset($data['total']) ? $data['total'] : 0;
  85. $datas = [];
  86. if ($list) {
  87. foreach ($list as $item) {
  88. $hash = isset($item['hash']) ? $item['hash'] : '';
  89. if ($hash) {
  90. $info = $this->getInfo($hash);
  91. if ($info) {
  92. $datas[] = $info;
  93. }
  94. }
  95. }
  96. RedisService::set($cacheKey, ['list' => $datas, 'kw' => $kw, 'total' => $total], 600);
  97. }
  98. return ['list' => $datas, 'kw' => $kw, 'total' => $total];
  99. }
  100. /**
  101. * 列表
  102. * @param $params
  103. * @param $pageSize
  104. * @return array
  105. */
  106. public function getTableList($params, $pageSize)
  107. {
  108. $list = $this->model->where(['status' => 1, 'mark' => 1])
  109. ->where(function ($query) use ($params) {
  110. $kw = isset($params['kw']) ? trim($params['kw']) : '';
  111. if ($kw) {
  112. $query->where('name', 'like', "%{$kw}%")
  113. ->orWhere('songer', 'like', $kw);
  114. }
  115. })->orderBy('create_time', 'desc')
  116. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  117. $list = $list ? $list->toArray() : [];
  118. if ($list && $list['data']) {
  119. foreach ($list['data'] as &$item) {
  120. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : get_image_url('./images/music.jpg');
  121. $item['url'] = isset($item['url']) && $item['url'] ? get_image_url($item['url']) : '';
  122. $item['time_text'] = $item['time'] ? date('i:s', $item['time']) : '00:00';
  123. }
  124. }
  125. return [
  126. 'pageSize' => $pageSize,
  127. 'total' => isset($list['total']) ? $list['total'] : 0,
  128. 'list' => isset($list['data']) ? $list['data'] : []
  129. ];
  130. }
  131. /**
  132. * 详情
  133. * @param $hash
  134. * @return array|mixed|null
  135. */
  136. public function getInfo($hash)
  137. {
  138. $cacheKey = "caches:music:info:{$hash}";
  139. $info = RedisService::get($cacheKey);
  140. if ($info) {
  141. return $info;
  142. }
  143. $url = sprintf(self::$apiUrls['info'], $hash);
  144. $result = httpRequest($url, '', 'get', '', 5);
  145. RedisService::set($cacheKey . '_temp', $result, 3600);
  146. $url = isset($result['url']) ? $result['url'] : '';
  147. if ($url) {
  148. $thumb = isset($result['imgUrl']) ? $result['imgUrl'] : '';
  149. $time = isset($result['timeLength']) ? $result['timeLength'] : 0;
  150. $info = [
  151. 'albumid' => isset($result['albumid']) ? $result['albumid'] : 0,
  152. 'time' => $time,
  153. 'time_text' => $time ? date('i:s', $time) : '00:00',
  154. 'name' => isset($result['songName']) && $result['songName'] ? $result['songName'] : 'bgm',
  155. 'singer' => isset($result['singerName']) && $result['singerName'] ? $result['singerName'] : ConfigService::make()->getConfigByCode('app_name'),
  156. 'author_name' => isset($result['author_name']) && $result['author_name'] ? $result['author_name'] : ConfigService::make()->getConfigByCode('app_name'),
  157. 'thumb' => $thumb ? str_replace('{size}', '64', $thumb) : get_image_url('./images/music.jpg'),
  158. 'hash' => $hash,
  159. 'url' => $url,
  160. ];
  161. RedisService::rPush('pools:music', $hash, $info);
  162. RedisService::set($cacheKey, $info, 600);
  163. }
  164. return $info;
  165. }
  166. }