MusicService.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. 'info' => 'https://kugou.mp.dongerkj.com/kapi/app/i/getSongInfo.php?cmd=playInfo&hash=%s',
  36. 'search' => 'https://kugou.mp.dongerkj.com/api/v3/search/song?format=json&keyword=%s&page=%s&pagesize=%s&showtype=1'
  37. ];
  38. /**
  39. * 构造函数
  40. * @author laravel开发员
  41. * @since 2020/11/11
  42. * ConfigService constructor.
  43. */
  44. public function __construct()
  45. {
  46. $this->model = new MusicModel();
  47. }
  48. /**
  49. * 静态入口
  50. * @return SmsService|static|null
  51. */
  52. public static function make()
  53. {
  54. if (!self::$instance) {
  55. self::$instance = new static();
  56. }
  57. return self::$instance;
  58. }
  59. /**
  60. * 搜索列表
  61. * @param $params
  62. * @param int $pageSize
  63. * @return array|mixed|string
  64. */
  65. public function getDataList($params, $pageSize = 20)
  66. {
  67. $cacheKey = "caches:music:index:{$pageSize}_" . md5(json_encode($params));
  68. $datas = RedisService::get($cacheKey);
  69. if ($datas) {
  70. return $datas;
  71. }
  72. $page = isset($params['page']) ? intval($params['page']) : 1;
  73. $kw = isset($params['kw']) ? trim($params['kw']) : '';
  74. // 取表数据
  75. $datas = $this->getTableList(['kw'=> $kw], $page, $pageSize);
  76. $total = isset($datas['total'])? $datas['total'] : 0;
  77. $list = isset($datas['list'])? $datas['list'] : [];
  78. if($total>= 5){
  79. return ['list'=> $list, 'kw'=> $kw,'total'=> $total];
  80. }
  81. if (empty($kw)) {
  82. $kws = ConfigService::make()->getConfigByCode('music_kws');
  83. $kws = $kws ? explode('|', $kws) : [];
  84. $kws = $kws ? $kws : ['抖音bgm', '抖音背景音乐', '背景音乐', '热门音乐'];
  85. $len = rand(0, count($kws) - 1);
  86. $kw = isset($kws[$len]) ? $kws[$len] : '抖音bgm';
  87. }
  88. // 取网络数据
  89. $url = sprintf(self::$apiUrls['search'], $kw, $page, $pageSize);
  90. $result = httpRequest($url, '', 'get', '', 5);
  91. RedisService::set($cacheKey . '_temp', ['kw' => $kw,'url'=>$url, 'result' => $result, 'date' => date('Y-m-d H:i:s')], 3600);
  92. $data = isset($result['data']) ? $result['data'] : [];
  93. $list = isset($data['info']) ? $data['info'] : [];
  94. $total = isset($data['total']) ? $data['total'] : 0;
  95. $datas = [];
  96. if ($list) {
  97. foreach ($list as $item) {
  98. $hash = isset($item['hash']) ? $item['hash'] : '';
  99. if ($hash) {
  100. $info = $this->getInfo($hash);
  101. if ($info) {
  102. $datas[] = $info;
  103. }
  104. }
  105. }
  106. RedisService::set($cacheKey, ['list' => $datas, 'kw' => $kw, 'total' => $total], 600);
  107. }
  108. return ['list' => $datas, 'kw' => $kw, 'total' => $total];
  109. }
  110. /**
  111. * 列表
  112. * @param $params
  113. * @param $pageSize
  114. * @return array
  115. */
  116. public function getTableList($params, $page=1, $pageSize=20)
  117. {
  118. $cacheKey = "caches:music:{$page}_{$pageSize}_".md5(json_encode($params));
  119. $datas = RedisService::get($cacheKey);
  120. if($datas){
  121. return $datas;
  122. }
  123. $list = $this->model->where(['status' => 1, 'mark' => 1])
  124. ->where(function ($query) use ($params) {
  125. $kw = isset($params['kw']) ? trim($params['kw']) : '';
  126. if ($kw) {
  127. $query->where('name', 'like', "%{$kw}%")
  128. ->orWhere('songer', 'like', $kw);
  129. }
  130. })->orderBy('create_time', 'desc')
  131. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  132. $list = $list ? $list->toArray() : [];
  133. if ($list && $list['data']) {
  134. foreach ($list['data'] as &$item) {
  135. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : get_image_url('./images/music.jpg');
  136. $item['url'] = isset($item['url']) && $item['url'] ? get_image_url($item['url']) : '';
  137. $item['time_text'] = $item['time'] ? date('i:s', $item['time']) : '00:00';
  138. }
  139. }
  140. $total = isset($list['total']) ? $list['total'] : 0;
  141. $datas = [
  142. 'pageSize' => $pageSize,
  143. 'total' => $total,
  144. 'list' => isset($list['data']) ? $list['data'] : []
  145. ];
  146. if($total){
  147. RedisService::set($cacheKey, $datas, rand(30, 60));
  148. }
  149. return $datas;
  150. }
  151. /**
  152. * 详情
  153. * @param $hash
  154. * @return array|mixed|null
  155. */
  156. public function getInfo($hash)
  157. {
  158. $cacheKey = "caches:music:info:{$hash}";
  159. $info = RedisService::get($cacheKey);
  160. if ($info) {
  161. return $info;
  162. }
  163. $url = sprintf(self::$apiUrls['info'], $hash);
  164. $result = httpRequest($url, '', 'get', '', 5);
  165. RedisService::set($cacheKey . '_temp', ['url'=>$url,'result'=>$result], 3600);
  166. $url = isset($result['url']) ? $result['url'] : '';
  167. if ($url) {
  168. $thumb = isset($result['imgUrl']) ? $result['imgUrl'] : '';
  169. $time = isset($result['timeLength']) ? $result['timeLength'] : 0;
  170. $info = [
  171. 'albumid' => isset($result['albumid']) ? $result['albumid'] : 0,
  172. 'time' => $time,
  173. 'time_text' => $time ? date('i:s', $time) : '00:00',
  174. 'name' => isset($result['songName']) && $result['songName'] ? $result['songName'] : 'bgm',
  175. 'singer' => isset($result['singerName']) && $result['singerName'] ? $result['singerName'] : ConfigService::make()->getConfigByCode('app_name'),
  176. 'author_name' => isset($result['author_name']) && $result['author_name'] ? $result['author_name'] : ConfigService::make()->getConfigByCode('app_name'),
  177. 'thumb' => $thumb ? str_replace('{size}', '64', $thumb) : get_image_url('./images/music.jpg'),
  178. 'hash' => $hash,
  179. 'url' => $url,
  180. ];
  181. RedisService::set($cacheKey, $info, 3600);
  182. }
  183. return $info;
  184. }
  185. }