VideoService.php 18 KB

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