MusicService.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\MusicCollectModel;
  13. use App\Models\MusicModel;
  14. /**
  15. * 佛音管理-服务类
  16. * @author wesmiler
  17. * @since 2020/11/11
  18. * Class MusicService
  19. * @package App\Services
  20. */
  21. class MusicService extends BaseService
  22. {
  23. /**
  24. * 构造函数
  25. * @author wesmiler
  26. * @since 2020/11/11
  27. * MusicService constructor.
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new MusicModel();
  32. }
  33. /**
  34. * 获取列表
  35. * @return array
  36. * @since 2020/11/11
  37. * @author wesmiler
  38. */
  39. public function getList()
  40. {
  41. $params = request()->all();
  42. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  43. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  44. $dataList = $this->model::from('musics as m')
  45. ->leftJoin('music_sheets as ms', 'm.sheet_id', '=', 'ms.id')
  46. ->where(function ($query) use ($params) {
  47. $query->where('m.mark', 1);
  48. $title = isset($params['title']) ? trim($params['title']) : '';
  49. if (!empty($title)) {
  50. $query->where('m.title', 'like', "%{$title}%");
  51. }
  52. $sheetId = isset($params['sheet_id']) ? intval($params['sheet_id']) : 0;
  53. if ($sheetId > 0) {
  54. $query->where('m.sheet_id', $sheetId);
  55. }
  56. $isRecommand = isset($params['is_recommand']) ? intval($params['is_recommand']) : 0;
  57. if ($isRecommand > 0) {
  58. $query->where('m.is_recommand', $isRecommand);
  59. }
  60. $status = isset($params['status']) ? $params['status'] : 0;
  61. if ($status > 0) {
  62. $query->where('m.status', $status);
  63. } else {
  64. $query->whereIn('m.status', [1, 2]);
  65. }
  66. })
  67. ->select(['m.id', 'm.sheet_id', 'ms.name as sheet_name', 'm.title', 'm.author', 'm.is_recommand', 'm.play_num', 'm.thumb', 'm.file_url', 'm.status', 'm.create_time', 'm.update_time', 'm.description', 'm.sort'])
  68. ->orderBy('m.update_time', 'desc')
  69. ->paginate($pageSize);
  70. $dataList = $dataList ? $dataList->toArray() : [];
  71. if ($dataList) {
  72. foreach ($dataList['data'] as &$item) {
  73. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  74. $item['file_url'] = $item['file_url'] ? get_file_url($item['file_url']) : '';
  75. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  76. }
  77. unset($item);
  78. }
  79. return [
  80. 'code' => 0,
  81. 'success'=> true,
  82. 'msg' => '操作成功',
  83. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  84. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  85. ];
  86. }
  87. /**
  88. * 获取列表
  89. * @return array
  90. * @since 2020/11/11
  91. * @author wesmiler
  92. */
  93. public function getDataList($params)
  94. {
  95. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  96. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  97. $dataList = $this->model::from('musics as m')
  98. ->leftJoin('music_sheets as ms', 'm.sheet_id', '=', 'ms.id')
  99. ->where(function ($query) use ($params) {
  100. $query->where(['m.mark'=>1,'m.status'=> 1]);
  101. $title = isset($params['title']) ? trim($params['title']) : '';
  102. if (!empty($title)) {
  103. $query->where('m.title', 'like', "%{$title}%");
  104. }
  105. $sheetId = isset($params['sheet_id']) ? intval($params['sheet_id']) : 0;
  106. if ($sheetId > 0) {
  107. $query->where('m.sheet_id', $sheetId);
  108. }
  109. $isRecommand = isset($params['is_recommand']) ? intval($params['is_recommand']) : 0;
  110. if ($isRecommand > 0) {
  111. $query->where('m.is_recommand', $isRecommand);
  112. }
  113. })
  114. ->select(['m.id', 'm.sheet_id', 'ms.name as sheet_name', 'm.title', 'm.author', 'm.is_recommand', 'm.play_num', 'm.thumb', 'm.file_url', 'm.status', 'm.create_time', 'm.update_time', 'm.description', 'm.sort'])
  115. ->orderBy('m.update_time', 'desc')
  116. ->paginate($pageSize);
  117. $dataList = $dataList ? $dataList->toArray() : [];
  118. if ($dataList) {
  119. $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
  120. foreach ($dataList['data'] as &$item) {
  121. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  122. $item['file_url'] = $item['file_url'] ? get_file_url($item['file_url']) : '';
  123. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  124. $isCollect = MusicCollectModel::where(['user_id'=> $userId,'source_id'=> $item['id'],'status'=> 1,'mark'=>1])->value('id');
  125. $item['is_collect'] =$isCollect? 1 : 0;
  126. }
  127. unset($item);
  128. }
  129. return [
  130. 'code' => 0,
  131. 'success'=> true,
  132. 'msg' => '操作成功',
  133. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  134. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  135. ];
  136. }
  137. /**
  138. * 添加或编辑
  139. * @return array
  140. * @since 2020/11/11
  141. * @author wesmiler
  142. */
  143. public function edit()
  144. {
  145. $data = request()->all();
  146. // 图片处理
  147. $image = trim($data['thumb']);
  148. $id = isset($data['id']) ? $data['id'] : 0;
  149. if (!$id && !$image) {
  150. return message('请上传图片', false);
  151. }
  152. if (strpos($image, "temp")) {
  153. $data['thumb'] = save_image($image, 'item');
  154. } else {
  155. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  156. }
  157. if(isset($data['file_url'])){
  158. $data['file_url'] = str_replace(IMG_URL, "", $data['file_url']);
  159. }
  160. $data['update_time'] = time();
  161. $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
  162. return parent::edit($data); // TODO: Change the autogenerated stub
  163. }
  164. /**
  165. * 获取详情
  166. * @param $id
  167. */
  168. public function getDetail($id, $userId=0){
  169. $info = $this->model::from('musics as a')
  170. ->leftJoin('music_sheets as c', 'a.sheet_id', '=', 'c.id')
  171. ->where(['a.mark'=> 1,'a.status'=> 1,'a.id'=> $id])
  172. ->select(['a.id','a.title','a.thumb','a.author','a.sheet_id','a.play_num','a.file_url','c.name as sheet_name','a.description','a.create_time'])
  173. ->first();
  174. $info = $info? $info->toArray() : [];
  175. if($info){
  176. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  177. $info['file_url'] = $info['file_url']? get_file_url($info['file_url']) : '';
  178. $info['create_time'] = $info['create_time']? datetime($info['create_time'],'Y-m-d H:i:s') : '';
  179. // 是否已收藏
  180. $info['is_collect'] = 0;
  181. if($userId){
  182. if(MusicCollectModel::where(['user_id'=> $userId,'source_id'=> $id,'mark'=> 1,'status'=> 1])->value('id')){
  183. $info['is_collect'] = 1;
  184. }
  185. }
  186. }
  187. return $info;
  188. }
  189. /**
  190. * 访问量
  191. * @return mixed
  192. */
  193. public function updateVisit($userId=0){
  194. $id = request()->get('id');
  195. $cacheKey = "caches:music:visit:{$userId}_{$id}";
  196. $check = RedisService::get($cacheKey);
  197. if($id && !$check){
  198. RedisService::set($cacheKey, $id, 3600);
  199. return $this->model::where(['id'=> $id])->increment('play_num', 1);
  200. }
  201. }
  202. }