MusicService.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. // 取表数据
  80. $datas = $this->getTableList(['kw'=> $kw], $page, $pageSize);
  81. $total = isset($datas['total'])? $datas['total'] : 0;
  82. $list = isset($datas['list'])? $datas['list'] : [];
  83. if($total>= $pageSize){
  84. return ['list'=> $list, 'kw'=> $kw,'total'=> $total];
  85. }
  86. // 取网络数据
  87. $url = sprintf(self::$apiUrls['search'], $kw, $page, $pageSize);
  88. $result = httpRequest($url, '', 'get', '', 5);
  89. RedisService::set($cacheKey . '_temp', ['kw' => $kw, 'result' => $result, 'date' => date('Y-m-d H:i:s')], 3600);
  90. $data = isset($result['data']) ? $result['data'] : [];
  91. $list = isset($data['info']) ? $data['info'] : [];
  92. $total = isset($data['total']) ? $data['total'] : 0;
  93. $datas = [];
  94. if ($list) {
  95. foreach ($list as $item) {
  96. $hash = isset($item['hash']) ? $item['hash'] : '';
  97. if ($hash) {
  98. $info = $this->getInfo($hash);
  99. if ($info) {
  100. $datas[] = $info;
  101. }
  102. }
  103. }
  104. RedisService::set($cacheKey, ['list' => $datas, 'kw' => $kw, 'total' => $total], 600);
  105. }
  106. return ['list' => $datas, 'kw' => $kw, 'total' => $total];
  107. }
  108. /**
  109. * 列表
  110. * @param $params
  111. * @param $pageSize
  112. * @return array
  113. */
  114. public function getTableList($params, $page=1, $pageSize=20)
  115. {
  116. $cacheKey = "caches:music:{$page}_{$pageSize}_".md5(json_encode($params));
  117. $datas = RedisService::get($cacheKey);
  118. if($datas){
  119. return $datas;
  120. }
  121. $list = $this->model->where(['status' => 1, 'mark' => 1])
  122. ->where(function ($query) use ($params) {
  123. $kw = isset($params['kw']) ? trim($params['kw']) : '';
  124. if ($kw) {
  125. $query->where('name', 'like', "%{$kw}%")
  126. ->orWhere('songer', 'like', $kw);
  127. }
  128. })->orderBy('create_time', 'desc')
  129. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  130. $list = $list ? $list->toArray() : [];
  131. if ($list && $list['data']) {
  132. foreach ($list['data'] as &$item) {
  133. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : get_image_url('./images/music.jpg');
  134. $item['url'] = isset($item['url']) && $item['url'] ? get_image_url($item['url']) : '';
  135. $item['time_text'] = $item['time'] ? date('i:s', $item['time']) : '00:00';
  136. }
  137. }
  138. $datas = [
  139. 'pageSize' => $pageSize,
  140. 'total' => isset($list['total']) ? $list['total'] : 0,
  141. 'list' => isset($list['data']) ? $list['data'] : []
  142. ];
  143. RedisService::set($cacheKey, $datas, rand(30, 60));
  144. return $datas;
  145. }
  146. /**
  147. * 详情
  148. * @param $hash
  149. * @return array|mixed|null
  150. */
  151. public function getInfo($hash)
  152. {
  153. $cacheKey = "caches:music:info:{$hash}";
  154. $info = RedisService::get($cacheKey);
  155. if ($info) {
  156. return $info;
  157. }
  158. $url = sprintf(self::$apiUrls['info'], $hash);
  159. $result = httpRequest($url, '', 'get', '', 5);
  160. RedisService::set($cacheKey . '_temp', $result, 3600);
  161. $url = isset($result['url']) ? $result['url'] : '';
  162. if ($url) {
  163. $thumb = isset($result['imgUrl']) ? $result['imgUrl'] : '';
  164. $time = isset($result['timeLength']) ? $result['timeLength'] : 0;
  165. $info = [
  166. 'albumid' => isset($result['albumid']) ? $result['albumid'] : 0,
  167. 'time' => $time,
  168. 'time_text' => $time ? date('i:s', $time) : '00:00',
  169. 'name' => isset($result['songName']) && $result['songName'] ? $result['songName'] : 'bgm',
  170. 'singer' => isset($result['singerName']) && $result['singerName'] ? $result['singerName'] : ConfigService::make()->getConfigByCode('app_name'),
  171. 'author_name' => isset($result['author_name']) && $result['author_name'] ? $result['author_name'] : ConfigService::make()->getConfigByCode('app_name'),
  172. 'thumb' => $thumb ? str_replace('{size}', '64', $thumb) : get_image_url('./images/music.jpg'),
  173. 'hash' => $hash,
  174. 'url' => $url,
  175. ];
  176. RedisService::set($cacheKey, $info, 3600);
  177. }
  178. return $info;
  179. }
  180. }