VideoService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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\Api;
  12. use AlibabaCloud\Client\Request\Request;
  13. use App\Models\AgentModel;
  14. use App\Models\GoodsModel;
  15. use App\Models\MemberModel;
  16. use App\Models\MerchantModel;
  17. use App\Models\OrderModel;
  18. use App\Models\ShopModel;
  19. use App\Models\TradeModel;
  20. use App\Models\VideoCollectModel;
  21. use App\Models\VideoModel;
  22. use App\Services\BaseService;
  23. use App\Services\ConfigService;
  24. use App\Services\RedisService;
  25. use Illuminate\Support\Facades\DB;
  26. /**
  27. * 短视频管理-服务类
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * @package App\Services\Api
  31. */
  32. class VideoService extends BaseService
  33. {
  34. // 静态对象
  35. protected static $instance = null;
  36. /**
  37. * 构造函数
  38. * @author laravel开发员
  39. * @since 2020/11/11
  40. * GoodsService constructor.
  41. */
  42. public function __construct()
  43. {
  44. $this->model = new VideoModel();
  45. }
  46. /**
  47. * 静态入口
  48. * @return 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
  62. */
  63. public function getDataList($params, $pageSize = 18, $field = '', $userId=0)
  64. {
  65. $where = ['a.mark' => 1,'b.mark'=>1];
  66. $field = $field? $field : 'lev_a.*';
  67. $order = 'lev_a.id desc';
  68. $sortType = isset($params['sort_type']) ? $params['sort_type'] : 0;
  69. $list = $this->model->with(['member'])->from('video as a')
  70. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  71. ->where($where)
  72. ->where(function ($query) use ($params) {
  73. $type = isset($params['type']) ? $params['type'] : 0;
  74. if ($type > 0) {
  75. $query->where('a.type', $type);
  76. }
  77. $uid = isset($params['user_id']) ? $params['user_id'] : 0;
  78. if ($uid > 0) {
  79. $query->where('a.user_id', $uid);
  80. }else{
  81. $query->where('a.status', 2);
  82. }
  83. })
  84. ->where(function ($query) use ($params) {
  85. $keyword = isset($params['kw']) ? $params['kw'] : '';
  86. if ($keyword) {
  87. $query->where('a.title', 'like', "%{$keyword}%")
  88. ->orWhere('a.tags', 'like', "%{$keyword}%")
  89. ->orWhere('a.description', 'like', "%{$keyword}%")
  90. ->orWhere('b.nickname', 'like', "%{$keyword}%");
  91. }
  92. })
  93. ->selectRaw($field)
  94. ->orderByRaw($order)
  95. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  96. $list = $list ? $list->toArray() : [];
  97. if ($list && $list['data']) {
  98. foreach ($list['data'] as &$item) {
  99. $item['create_time'] = isset($item['create_time']) ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  100. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  101. if(isset($item['albums'])){
  102. $albums = $item['albums']? json_decode($item['albums'], true):[];
  103. $item['albums'] = $albums? get_images_preview($albums) : [];
  104. }
  105. if(isset($item['file_url'])){
  106. $item['file_url'] = $item['file_url']? get_image_url($item['file_url']) : '';
  107. }
  108. if(isset($item['music_url'])){
  109. $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
  110. }
  111. $member = isset($item['member'])? $item['member'] : [];
  112. if($member){
  113. $member['avatar'] = isset($member['avatar'])? get_image_url($member['avatar']) : '';
  114. }
  115. $item['tags'] = isset($item['tags']) && $item['tags']? explode(',', $item['tags']) : [];
  116. $item['like_num'] = isset($item['like_num']) && $item['like_num']? format_num($item['like_num']) : 0;
  117. $item['collect_num'] = isset($item['collect_num']) && $item['collect_num']? format_num($item['collect_num']) : 0;
  118. $item['views'] = isset($item['views']) && $item['views']? format_num($item['views']) : 0;
  119. $item['member'] = $member;
  120. }
  121. }
  122. return [
  123. 'pageSize' => $pageSize,
  124. 'total' => isset($list['total']) ? $list['total'] : 0,
  125. 'list' => isset($list['data']) ? $list['data'] : []
  126. ];
  127. }
  128. /**
  129. * 列表数据
  130. * @param $params
  131. * @param int $pageSize
  132. * @return array
  133. */
  134. public function getIndexList($params, $pageSize = 6, $field='', $userId=0)
  135. {
  136. $where = ['a.mark' => 1,'a.status'=>2,'b.mark'=>1];
  137. $field = $field? $field : 'lev_a.*';
  138. $order = 'rand()';
  139. $model = $this->model->with(['member'])->from('video as a')
  140. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  141. ->where($where)
  142. ->where(function ($query) use ($params) {
  143. $type = isset($params['type']) ? $params['type'] : 0;
  144. if ($type > 0) {
  145. $query->where('a.type', $type);
  146. }
  147. $uid = isset($params['user_id']) ? $params['user_id'] : 0;
  148. if ($uid > 0) {
  149. $query->where('a.user_id', $uid);
  150. }else{
  151. $query->where('a.status', 2);
  152. }
  153. })
  154. ->where(function ($query) use ($params) {
  155. $keyword = isset($params['kw']) ? $params['kw'] : '';
  156. if ($keyword) {
  157. $query->where('a.title', 'like', "%{$keyword}%")
  158. ->orWhere('a.tags', 'like', "%{$keyword}%")
  159. ->orWhere('a.description', 'like', "%{$keyword}%")
  160. ->orWhere('b.nickname', 'like', "%{$keyword}%");
  161. }
  162. });
  163. // 推荐的数据
  164. $countModel = clone $model;
  165. $total = $countModel->where(function($query) use($params, $userId){
  166. // 推荐视频数据
  167. $isRecommend = isset($params['is_recommend']) ? $params['is_recommend'] : 0;
  168. if ($isRecommend > 0) {
  169. $recommendData = VideoCollectService::make()->getRecommendData($userId);
  170. $uids = isset($recommendData['uids'])? $recommendData['uids'] : []; // 按用户推荐
  171. $tags = isset($recommendData['tags'])? $recommendData['tags'] : []; // 按标签推荐
  172. if($uids){
  173. $query->orWhere(function($query) use($uids){
  174. $query->whereIn('a.user_id', $uids);
  175. });
  176. }
  177. if($tags){
  178. $query->orWhere(function($query) use($tags){
  179. foreach($tags as $tag){
  180. $query->where('a.tags', 'like',"%{$tag}%")
  181. ->orWhere('a.description','like',"%{$tag}%");
  182. }
  183. });
  184. }
  185. }
  186. })->count('lev_a.id');
  187. if($total > 0){
  188. // 关联推荐数据
  189. $list = $countModel->selectRaw($field)
  190. ->orderByRaw($order)
  191. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  192. }else{
  193. // 默认推荐数据
  194. $list = $model->selectRaw($field)
  195. ->orderByRaw($order)
  196. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  197. }
  198. $list = $list ? $list->toArray() : [];
  199. if ($list && $list['data']) {
  200. foreach ($list['data'] as &$item) {
  201. $item['create_time'] = isset($item['create_time']) ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  202. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  203. if(isset($item['albums'])){
  204. $albums = $item['albums']? json_decode($item['albums'], true):[];
  205. $item['albums'] = $albums? get_images_preview($albums) : [];
  206. }
  207. if(isset($item['file_url'])){
  208. $item['file_url'] = $item['file_url']? get_image_url($item['file_url']) : '';
  209. }
  210. if(isset($item['music_url'])){
  211. $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
  212. }
  213. $member = isset($item['member'])? $item['member'] : [];
  214. if($member){
  215. $member['avatar'] = isset($member['avatar'])? get_image_url($member['avatar']) : '';
  216. }
  217. $item['tags'] = isset($item['tags']) && $item['tags']? explode(',', $item['tags']) : [];
  218. $item['like_num'] = isset($item['like_num']) && $item['like_num']? format_num($item['like_num']) : 0;
  219. $item['collect_num'] = isset($item['collect_num']) && $item['collect_num']? format_num($item['collect_num']) : 0;
  220. $item['views'] = isset($item['views']) && $item['views']? format_num($item['views']) : 0;
  221. $item['member'] = $member;
  222. }
  223. }
  224. return [
  225. 'pageSize' => $pageSize,
  226. 'total' => isset($list['total']) ? $list['total'] : 0,
  227. 'list' => isset($list['data']) ? $list['data'] : []
  228. ];
  229. }
  230. /**
  231. * 详情
  232. * @param $id
  233. * @return array
  234. */
  235. public function getInfo($id, $userId=0, $field=[])
  236. {
  237. $field = $field? $field : ['a.*'];
  238. $info = $this->model->from('video as a')->with(['member'])
  239. ->leftJoin('member as b','b.id','=','a.user_id')
  240. ->where(['a.id'=> $id,'a.mark'=>1,'b.mark'=>1])
  241. ->select($field)
  242. ->first();
  243. $info = $info? $info->toArray() : [];
  244. if($info){
  245. if(isset($info['thumb'])){
  246. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  247. }
  248. if(isset($info['file_url'])){
  249. $info['file_url'] = $info['file_url']? get_image_url($info['file_url']) : '';
  250. }
  251. if(isset($item['music_url'])){
  252. $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
  253. }
  254. if(isset($info['albums'])){
  255. $info['albums'] = $info['albums']? json_decode($info['albums'], true) : [];
  256. $info['albums'] = $info['albums']? get_images_preview($info['albums']) : [];
  257. }
  258. // 浏览历史
  259. if(!VideoCollectService::make()->getCollectCacheInfo($userId, $id, 1)){
  260. $data = [
  261. 'user_id'=> $userId,
  262. 'type'=> 1,
  263. 'collect_id'=> $id,
  264. 'collect_uid'=> isset($info['user_id'])? $info['user_id'] : 0,
  265. 'tags'=> isset($info['tags'])? $info['tags'] : '',
  266. 'create_time'=> time(),
  267. 'status'=> 1,
  268. ];
  269. VideoCollectModel::insert($data);
  270. RedisService::set("caches:videos:collect:temp_{$userId}_{$id}_1", $data, rand(10,30));
  271. RedisService::clear("caches:videos:recommend:{$userId}");
  272. }
  273. // 浏览量
  274. $this->updateView($userId, $id);
  275. }
  276. return $info;
  277. }
  278. /**
  279. * 更新浏览量
  280. * @param $userId
  281. * @param $dynamicId
  282. * @return array|mixed
  283. */
  284. public function updateView($userId, $id)
  285. {
  286. $cacheKey = "caches:videos:views:u{$userId}_d{$id}";
  287. $data = RedisService::get($cacheKey);
  288. if($data){
  289. return false;
  290. }
  291. $data = $this->model->where(['id'=> $id])->update(['views'=>DB::raw('views + 1'),'update_time'=>time()]);
  292. RedisService::set($cacheKey, $id, rand(1,3)*3600);
  293. return $data;
  294. }
  295. /**
  296. * 发布
  297. * @param $goodsId
  298. * @return bool
  299. */
  300. public function publish($userId, $params, $request)
  301. {
  302. $goodsId = isset($params['goods_id'])? intval($params['goods_id']) : 0;
  303. $type = isset($params['type'])? intval($params['type']) : 2;
  304. $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])
  305. ->select(['id','nickname','status'])
  306. ->first();
  307. $status = isset($userInfo['status'])? $userInfo['status'] : 0;
  308. $nickname = isset($userInfo['nickname'])? $userInfo['nickname'] : '';
  309. if(empty($userInfo) || $status != 1){
  310. $this->error = 2017;
  311. return false;
  312. }
  313. $thumb = isset($params['thumb'])? trim($params['thumb']) : '';
  314. if($thumb){
  315. $result = upload_base64($thumb);
  316. $thumb = isset($result['file_path'])? $result['file_path'] : '';
  317. if(empty($thumb)){
  318. $this->error = 2208;
  319. return false;
  320. }
  321. }
  322. // 音乐
  323. $musicUrl = isset($params['music_url'])? trim($params['music_url']) : '';
  324. if($musicUrl){
  325. $result = upload_remote_image($musicUrl,'mp3','music');
  326. $musicUrl = isset($result['file_path'])? $result['file_path'] : '';
  327. if(empty($musicUrl)){
  328. $this->error = 2209;
  329. return false;
  330. }
  331. }
  332. // 视频
  333. $fileUrl = isset($params['file_url'])? trim($params['file_url']) : '';
  334. $albums = isset($params['albums'])? $params['albums'] : [];
  335. if($type == 1) {
  336. if($fileUrl){
  337. $result = upload_video($request,'video');
  338. $data = isset($result['data'])? $result['data'] : [];
  339. $fileUrl = isset($data['file_path'])? $data['file_path'] : '';
  340. if(empty($fileUrl)){
  341. $this->error = 2212;
  342. return false;
  343. }
  344. }else{
  345. $this->error = 2213;
  346. return false;
  347. }
  348. }
  349. // 相册
  350. else if($type == 2){
  351. if(empty($albums) || !is_array($albums)){
  352. $this->error = 2211;
  353. return false;
  354. }
  355. $files = [];
  356. foreach ($albums as $album){
  357. $result = upload_base64($album);
  358. $url = isset($result['file_path'])? $result['file_path'] : '';
  359. if(empty($url)){
  360. $this->error = 2212;
  361. return false;
  362. }
  363. $files[] = ['url'=> $url];
  364. }
  365. $albums = $files;
  366. }
  367. $title = isset($params['title']) && $params['title']? trim($params['title']) : "{$nickname} 发布的短视频";
  368. $description = isset($params['description']) && $params['description']? trim($params['description']) : "{$nickname} 发布的短视频";
  369. $publishCheck = ConfigService::make()->getConfigByCode('video_publish_check',0);
  370. $publishCheck = $publishCheck>0? $publishCheck : 0;
  371. $data = [
  372. 'user_id' => $userId,
  373. 'title' => $title,
  374. 'type' => $type,
  375. 'description' => $description,
  376. 'tags' => isset($params['tags']) && $params['tags']? implode(',', $params['tags']) : '',
  377. 'file_url' => $fileUrl,
  378. 'thumb' => $thumb,
  379. 'albums' => $albums? json_encode($albums) : '',
  380. 'music_hash' => isset($params['music_hash'])? trim($params['music_hash']) : '',
  381. 'music_url' => $musicUrl,
  382. 'goods_id' => $goodsId,
  383. 'visible_type' => isset($params['visible_type'])? intval($params['visible_type']) : 1,
  384. 'is_comment' => isset($params['is_comment'])? intval($params['is_comment']) : 1,
  385. 'status' => $publishCheck? 1 : 2,
  386. 'mark' => 1,
  387. 'publish_at' => date('Y-m-d H:i:s'),
  388. 'create_time' => time(),
  389. ];
  390. if($id = $this->model->insertGetId($data)){
  391. $this->error = 1023;
  392. return ['id'=> $id];
  393. }
  394. $this->error = 1024;
  395. return false;
  396. }
  397. /**
  398. * 状态设置
  399. * @return bool
  400. */
  401. public function status()
  402. {
  403. $id = request()->post('id', 0);
  404. $status = request()->post('status', 1);
  405. if ($id && !$this->model->where(['id' => $id, 'mark' => 1])->value('id')) {
  406. $this->error = 2981;
  407. return false;
  408. }
  409. if($this->model->where(['id'=> $id,'mark'=>1])->update(['status'=>$status, 'update_time'=> time()])){
  410. $this->error = 1002;
  411. return true;
  412. }
  413. $this->error = 1003;
  414. return true;
  415. }
  416. /**
  417. * 删除
  418. * @return bool
  419. */
  420. public function delete()
  421. {
  422. // 参数
  423. $param = request()->all();
  424. $id = getter($param, "id");
  425. if (empty($id)) {
  426. $this->error = 2014;
  427. return false;
  428. }
  429. if(!$this->model->where(['id'=> $id])->value('id')){
  430. $this->error = 1039;
  431. return false;
  432. }
  433. if($this->model->where(['id'=> $id])->update(['mark'=>0,'update_time'=>time()])){
  434. $this->model->where(['mark'=> 0])->where('update_time','<=', time() - 3 * 86400)->delete();
  435. $this->error = 1025;
  436. return true;
  437. }else{
  438. $this->error = 1026;
  439. return false;
  440. }
  441. }
  442. }