// +---------------------------------------------------------------------- namespace App\Services; use AlibabaCloud\Tea\Exception\TeaUnableRetryError; use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi; use App\Models\LiveModel; use App\Models\MemberModel; use App\Models\MusicModel; use App\Services\Api\MemberCollectService; use Darabonba\OpenApi\Models\Config; use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest; use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions; use Illuminate\Support\Facades\DB; /** * 在线音乐服务管理-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services */ class MusicService extends BaseService { // 静态对象 protected static $instance = null; protected static $apiUrls = [ 'info' => 'https://m.kugou.com/app/i/getSongInfo.php?cmd=playInfo&hash=%s', 'search' => 'http://mobilecdn.kugou.com/api/v3/search/song?format=json&keyword=%s&page=%s&pagesize=%s&showtype=1' ]; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * ConfigService constructor. */ public function __construct() { $this->model = new MusicModel(); } /** * 静态入口 * @return SmsService|static|null */ public static function make() { if (!self::$instance) { self::$instance = new static(); } return self::$instance; } /** * 搜索列表 * @param $params * @param int $pageSize * @return array|mixed|string */ public function getDataList($params, $pageSize = 20) { $cacheKey = "caches:music:index:{$pageSize}_" . md5(json_encode($params)); $datas = RedisService::get($cacheKey); if ($datas) { return $datas; } $page = isset($params['page']) ? intval($params['page']) : 1; $kw = isset($params['kw']) ? trim($params['kw']) : ''; if (empty($kw)) { $kws = ConfigService::make()->getConfigByCode('music_kws'); $kws = $kws ? explode('|', $kws) : []; $kws = $kws ? $kws : ['抖音bgm', '抖音背景音乐', '背景音乐', '热门音乐']; $len = rand(0, count($kws) - 1); $kw = isset($kws[$len]) ? $kws[$len] : '抖音bgm'; } // 取表数据 $datas = $this->getTableList(['kw'=> $kw], $page, $pageSize); $total = isset($datas['total'])? $datas['total'] : 0; $list = isset($datas['list'])? $datas['list'] : []; if($total>= $pageSize){ return ['list'=> $list, 'kw'=> $kw,'total'=> $total]; } // 取网络数据 $url = sprintf(self::$apiUrls['search'], $kw, $page, $pageSize); $result = httpRequest($url, '', 'get', '', 5); RedisService::set($cacheKey . '_temp', ['kw' => $kw, 'result' => $result, 'date' => date('Y-m-d H:i:s')], 3600); $data = isset($result['data']) ? $result['data'] : []; $list = isset($data['info']) ? $data['info'] : []; $total = isset($data['total']) ? $data['total'] : 0; $datas = []; if ($list) { foreach ($list as $item) { $hash = isset($item['hash']) ? $item['hash'] : ''; if ($hash) { $info = $this->getInfo($hash); if ($info) { $datas[] = $info; } } } RedisService::set($cacheKey, ['list' => $datas, 'kw' => $kw, 'total' => $total], 600); } return ['list' => $datas, 'kw' => $kw, 'total' => $total]; } /** * 列表 * @param $params * @param $pageSize * @return array */ public function getTableList($params, $page=1, $pageSize=20) { $cacheKey = "caches:music:{$page}_{$pageSize}_".md5(json_encode($params)); $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $list = $this->model->where(['status' => 1, 'mark' => 1]) ->where(function ($query) use ($params) { $kw = isset($params['kw']) ? trim($params['kw']) : ''; if ($kw) { $query->where('name', 'like', "%{$kw}%") ->orWhere('songer', 'like', $kw); } })->orderBy('create_time', 'desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list && $list['data']) { foreach ($list['data'] as &$item) { $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : get_image_url('./images/music.jpg'); $item['url'] = isset($item['url']) && $item['url'] ? get_image_url($item['url']) : ''; $item['time_text'] = $item['time'] ? date('i:s', $item['time']) : '00:00'; } } $datas = [ 'pageSize' => $pageSize, 'total' => isset($list['total']) ? $list['total'] : 0, 'list' => isset($list['data']) ? $list['data'] : [] ]; RedisService::set($cacheKey, $datas, rand(30, 60)); return $datas; } /** * 详情 * @param $hash * @return array|mixed|null */ public function getInfo($hash) { $cacheKey = "caches:music:info:{$hash}"; $info = RedisService::get($cacheKey); if ($info) { return $info; } $url = sprintf(self::$apiUrls['info'], $hash); $result = httpRequest($url, '', 'get', '', 5); RedisService::set($cacheKey . '_temp', $result, 3600); $url = isset($result['url']) ? $result['url'] : ''; if ($url) { $thumb = isset($result['imgUrl']) ? $result['imgUrl'] : ''; $time = isset($result['timeLength']) ? $result['timeLength'] : 0; $info = [ 'albumid' => isset($result['albumid']) ? $result['albumid'] : 0, 'time' => $time, 'time_text' => $time ? date('i:s', $time) : '00:00', 'name' => isset($result['songName']) && $result['songName'] ? $result['songName'] : 'bgm', 'singer' => isset($result['singerName']) && $result['singerName'] ? $result['singerName'] : ConfigService::make()->getConfigByCode('app_name'), 'author_name' => isset($result['author_name']) && $result['author_name'] ? $result['author_name'] : ConfigService::make()->getConfigByCode('app_name'), 'thumb' => $thumb ? str_replace('{size}', '64', $thumb) : get_image_url('./images/music.jpg'), 'hash' => $hash, 'url' => $url, ]; RedisService::set($cacheKey, $info, 3600); } return $info; } }