VideoServices.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\activity\video;
  13. use app\dao\activity\video\VideoDao;
  14. use app\jobs\activity\VideoJob;
  15. use app\services\activity\live\LiveRoomServices;
  16. use app\services\BaseServices;
  17. use app\services\out\StoreProductServices;
  18. use app\services\product\category\StoreProductCategoryServices;
  19. use app\services\store\SystemStoreServices;
  20. use app\services\user\UserRelationServices;
  21. use think\exception\ValidateException;
  22. /**
  23. * Class VideoServices
  24. * @package app\services\activity\video
  25. * @mixin VideoDao
  26. */
  27. class VideoServices extends BaseServices
  28. {
  29. /**
  30. * 移动端视频需要默认值
  31. * @var array
  32. */
  33. protected $videoDefault = [
  34. 'isMore' => false,
  35. 'state' => "pause",
  36. 'playIng' => false,
  37. 'isShowimage' => false,
  38. 'isShowProgressBarTime' => false,
  39. 'isplay' => true
  40. ];
  41. /**
  42. * VideoServices constructor.
  43. * @param VideoDao $dao
  44. */
  45. public function __construct(VideoDao $dao)
  46. {
  47. $this->dao = $dao;
  48. }
  49. /**
  50. * 获取短视频信息(获取不到抛出异常)
  51. * @param int $id
  52. * @param string $field
  53. * @return array
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. public function getVideoInfo(int $id, string $field = '*')
  59. {
  60. $videoInfo = $this->dao->getOne(['id' => $id], $field);
  61. if (!$videoInfo) {
  62. throw new ValidateException('获取短视频信息失败');
  63. }
  64. return $videoInfo->toArray();
  65. }
  66. /**
  67. * 获取视频详情
  68. * @param int $id
  69. * @return array
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public function getInfo(int $id)
  75. {
  76. $info = $this->dao->get($id);
  77. if (!$info) {
  78. throw new ValidateException('视频不存在');
  79. }
  80. $info = $info->toArray();
  81. $productInfo = [];
  82. $info['type_name'] = '';
  83. switch ($info['type']) {
  84. case 0:
  85. $info['type_name'] = '平台';
  86. break;
  87. case 1:
  88. if ($info['relation_id']) {
  89. /** @var SystemStoreServices $storeServices */
  90. $storeServices = app()->make(SystemStoreServices::class);
  91. $info['type_name'] = $storeServices->value(['id' => $info['relation_id']], 'name') ?? '';
  92. }
  93. break;
  94. }
  95. if ($info['product_id']) {
  96. $info['product_id'] = is_string($info['product_id']) ? explode(',', $info['product_id']) : $info['product_id'];
  97. /** @var StoreProductServices $productServices */
  98. $productServices = app()->make(StoreProductServices::class);
  99. $productInfo = $productServices->getColumn([['id', 'in', $info['product_id']]], 'id,type,product_type,price,image,store_name,cate_id,sales,stock');
  100. $cateIds = implode(',', array_column($productInfo, 'cate_id'));
  101. /** @var StoreProductCategoryServices $categoryService */
  102. $categoryService = app()->make(StoreProductCategoryServices::class);
  103. $cateList = $categoryService->getCateParentAndChildName($cateIds);
  104. foreach ($productInfo as $key => &$item) {
  105. $item['cate_name'] = '';
  106. if (isset($item['cate_id']) && $item['cate_id']) {
  107. $cate_ids = explode(',', $item['cate_id']);
  108. $cate_name = $categoryService->getCateName($cate_ids, $cateList);
  109. if ($cate_name) {
  110. $item['cate_name'] = is_array($cate_name) ? implode(',', $cate_name) : '';
  111. }
  112. }
  113. }
  114. }
  115. $info['productInfo'] = $productInfo;
  116. return $info;
  117. }
  118. /**
  119. * 后台获取视频列表
  120. * @param $where
  121. * @return array
  122. * @throws \think\db\exception\DataNotFoundException
  123. * @throws \think\db\exception\DbException
  124. * @throws \think\db\exception\ModelNotFoundException
  125. */
  126. public function sysPage($where)
  127. {
  128. [$page, $limit] = $this->getPageValue();
  129. $list = $this->dao->getList($where, '*', $page, $limit);
  130. $count = 0;
  131. if ($list) {
  132. /** @var SystemStoreServices $storeServices */
  133. $storeServices = app()->make(SystemStoreServices::class);
  134. $storeIds = array_unique(array_column($list, 'relation_id'));
  135. $storeInfos = $storeServices->getColumn([['id', 'in', $storeIds]], 'id,name,image', 'id');
  136. $site_name = sys_config('site_name');
  137. $site_image = sys_config('wap_login_logo');
  138. /** @var StoreProductServices $productServices */
  139. $productServices = app()->make(StoreProductServices::class);
  140. $product_where = ['is_del' => 0, 'is_show' => 1, 'is_verify' => 1];
  141. foreach ($list as &$item) {
  142. $item['product_id'] = is_string($item['product_id']) ? explode(',', $item['product_id']) : $item['product_id'];
  143. $item['product_info'] = [];
  144. $item['product_num'] = 0;
  145. if ($item['product_id']) {
  146. $item['product_info'] = $productServices->getSearchList($product_where + ['ids' => $item['product_id']], 0, 0, ['id', 'store_name', 'image', 'price'], '', []);
  147. $item['product_num'] = count($item['product_info']);
  148. }
  149. $item['type_name'] = $site_name;
  150. $item['type_image'] = $site_image;
  151. switch ($item['type']) {
  152. case 0:
  153. break;
  154. case 1:
  155. $item['type_name'] = $storeInfos[$item['relation_id']]['name'] ?? '';
  156. $item['type_image'] = $storeInfos[$item['relation_id']]['image'] ?? '';
  157. break;
  158. }
  159. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  160. }
  161. $count = $this->dao->count($where);
  162. }
  163. return compact('list', 'count');
  164. }
  165. /**
  166. * 获取短视频列表
  167. * @param int $uid
  168. * @param string $field
  169. * @param int $order_type
  170. * @param int $id
  171. * @return array
  172. * @throws \think\db\exception\DataNotFoundException
  173. * @throws \think\db\exception\DbException
  174. * @throws \think\db\exception\ModelNotFoundException
  175. */
  176. public function getVideoList(int $uid, string $field = '*', int $order_type = 1, int $id = 0)
  177. {
  178. //短视频未启用
  179. if (!sys_config('video_func_status', 1)) {
  180. return [];
  181. }
  182. $where = ['is_show' => 1, 'is_del' => 0, 'is_verify' => 1];
  183. [$page, $limit] = $this->getPageValue();
  184. //限制一次最多请求条数
  185. if ($limit > 10) $limit = 10;
  186. if ($id) {//指定视频进入
  187. $ids = array_merge([$id], $this->dao->getColumn($where, 'id'));
  188. $where['order_by_id'] = array_slice($ids, 0, $limit);
  189. $order = '';
  190. } elseif ($order_type == 1) {//最新
  191. $order = 'id desc,sort desc';
  192. } else if ($order_type == 2) {//推荐
  193. $where['is_recommend'] = 1;
  194. $order = 'is_recommend desc,sort desc,id desc';
  195. } else {
  196. $order = 'sort desc,id desc';
  197. }
  198. $list = $this->dao->getList($where, $field, $page, $limit, $order);
  199. if ($list) {
  200. /** @var LiveRoomServices $liveServices */
  201. $liveServices = app()->make(LiveRoomServices::class);
  202. $liveCount = $liveServices->count(['is_show' => 1, 'is_del' => 0, 'status' => 1, 'live_status' => [101, 105, 106]]);
  203. $is_live = (bool)$liveCount;
  204. /** @var SystemStoreServices $storeServices */
  205. $storeServices = app()->make(SystemStoreServices::class);
  206. $storeIds = array_unique(array_column($list, 'relation_id'));
  207. $storeInfos = $storeServices->getColumn([['id', 'in', $storeIds]], 'id,name,image', 'id');
  208. $site_name = sys_config('site_name');
  209. $site_image = sys_config('wap_login_logo');
  210. $userLike = $userCollect = [];
  211. $ids = array_column($list, 'id');
  212. if ($uid) {
  213. /** @var UserRelationServices $userRelationServices */
  214. $userRelationServices = app()->make(UserRelationServices::class);
  215. $userLike = $userRelationServices->getColumn([['uid', '=', $uid], ['relation_id', 'in', $ids], ['category', '=', 'video'], ['type', '=', 'like']], 'id,relation_id', 'relation_id');
  216. $userCollect = $userRelationServices->getColumn([['uid', '=', $uid], ['relation_id', 'in', $ids], ['category', '=', 'video'], ['type', '=', 'collect']], 'id,relation_id', 'relation_id');
  217. }
  218. /** @var StoreProductServices $storeProductServices */
  219. $storeProductServices = app()->make(StoreProductServices::class);
  220. $productWhere = ['is_verify' => 1, 'is_show' => 1, 'is_del' => 0];
  221. foreach ($list as &$item) {
  222. $item['product_id'] = is_string($item['product_id']) ? explode(',', $item['product_id']) : $item['product_id'];
  223. $item['product_num'] = count($item['product_id']);
  224. //过滤下架、审核中等商品
  225. $item['product_num'] = $item['product_num'] ? $storeProductServices->getCount($productWhere + ['ids' => $item['product_id']]) : 0;
  226. $item['type_name'] = $site_name;
  227. $item['type_image'] = $site_image;
  228. switch ($item['type']) {
  229. case 0://平台
  230. break;
  231. case 1://门店
  232. $item['type_name'] = $storeInfos[$item['relation_id']]['name'] ?? '';
  233. $item['type_image'] = $storeInfos[$item['relation_id']]['image'] ?? '';
  234. break;
  235. }
  236. $item['date'] = $item['add_time'] ? date('m月d日', $item['add_time']) : '';
  237. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  238. $item['is_like'] = isset($userLike[$item['id']]);
  239. $item['is_collect'] = isset($userCollect[$item['id']]);
  240. //app需要
  241. $item['id'] = (string)$item['id'];
  242. $item = array_merge($item, $this->videoDefault);
  243. $item['is_live'] = $is_live;
  244. }
  245. //增加浏览量
  246. VideoJob::dispatchDo('setVideoPlayNum', [$ids, $uid]);
  247. }
  248. return $list;
  249. }
  250. /**
  251. * diy获取短视频
  252. * @param int $uid
  253. * @param string $field
  254. * @param int $order_type
  255. * @return array
  256. * @throws \think\db\exception\DataNotFoundException
  257. * @throws \think\db\exception\DbException
  258. * @throws \think\db\exception\ModelNotFoundException
  259. */
  260. public function getDiyVideoList(int $uid, string $field = '*', int $order_type = 0)
  261. {
  262. //短视频未启用
  263. if (!sys_config('video_func_status', 1)) {
  264. return [];
  265. }
  266. $where = ['is_show' => 1, 'is_del' => 0, 'is_verify' => 1];
  267. [$page, $limit] = $this->getPageValue();
  268. //限制一次最多请求条数
  269. if ($limit > 10) $limit = 10;
  270. if ($order_type == 1) {//最新
  271. $order = 'id desc,sort desc';
  272. } else if ($order_type == 2) {//推荐
  273. $where['is_recommend'] = 1;
  274. $order = 'is_recommend desc,sort desc,id desc';
  275. } else {
  276. $order = 'sort desc,id desc';
  277. }
  278. $list = $this->dao->getList($where, $field, $page, $limit, $order);
  279. if ($list) {
  280. /** @var SystemStoreServices $storeServices */
  281. $storeServices = app()->make(SystemStoreServices::class);
  282. $storeIds = array_unique(array_column($list, 'relation_id'));
  283. $storeInfos = $storeServices->getColumn([['id', 'in', $storeIds]], 'id,name,image', 'id');
  284. $site_name = sys_config('site_name');
  285. $site_image = sys_config('wap_login_logo');
  286. /** @var StoreProductServices $productServices */
  287. $productServices = app()->make(StoreProductServices::class);
  288. $product_where = ['is_del' => 0, 'is_show' => 1, 'is_verify' => 1];
  289. foreach ($list as &$item) {
  290. $item['product_id'] = is_string($item['product_id']) ? explode(',', $item['product_id']) : $item['product_id'];
  291. $item['product_info'] = [];
  292. $item['product_num'] = 0;
  293. if ($item['product_id']) {
  294. $item['product_info'] = $productServices->getSearchList($product_where + ['ids' => $item['product_id']], 0, 0, ['id', 'store_name', 'image', 'price'], '', []);
  295. $item['product_num'] = count($item['product_info']);
  296. }
  297. $item['type_name'] = $site_name;
  298. $item['type_image'] = $site_image;
  299. switch ($item['type']) {
  300. case 0:
  301. break;
  302. case 1:
  303. $item['type_name'] = $storeInfos[$item['relation_id']]['name'] ?? '';
  304. $item['type_image'] = $storeInfos[$item['relation_id']]['image'] ?? '';
  305. break;
  306. }
  307. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  308. }
  309. $ids = array_column($list, 'id');
  310. //增加浏览量
  311. VideoJob::dispatchDo('setVideoPlayNum', [$ids, $uid]);
  312. }
  313. return $list;
  314. }
  315. /**
  316. * 用户点赞、收藏、分享、浏览播放视频
  317. * @param int $uid
  318. * @param int $id
  319. * @param string $type
  320. * @param int $num
  321. * @return bool
  322. * @throws \think\db\exception\DataNotFoundException
  323. * @throws \think\db\exception\DbException
  324. * @throws \think\db\exception\ModelNotFoundException
  325. */
  326. public function userRelationVideo(int $uid, int $id, string $type = 'like', int $num = 1)
  327. {
  328. $info = $this->getVideoInfo((int)$id);
  329. $data = ['uid' => $uid, 'relation_id' => $id, 'category' => UserRelationServices::CATEGORY_VIDEO, 'type' => $type];
  330. $typeArr = UserRelationServices::TYPE_NAME;
  331. if (!isset($typeArr[$type])) {
  332. throw new ValidateException('类型错误');
  333. }
  334. /** @var UserRelationServices $userRelationServices */
  335. $userRelationServices = app()->make(UserRelationServices::class);
  336. //播放 每一次都是增加数量
  337. if ($type != 'play') {
  338. $relation = $userRelationServices->get($data);
  339. } else {
  340. $relation = [];
  341. }
  342. $field = $type . '_num';
  343. $balance = $info[$field] ?? 0;
  344. if ($relation) {//取消
  345. $userRelationServices->delete($relation['id']);
  346. $new = (int)bcsub((string)$balance, (string)$num);
  347. } else {//增加
  348. $data['add_time'] = time();
  349. $userRelationServices->save($data);
  350. $new = (int)bcadd((string)$balance, (string)$num);
  351. }
  352. $new = max($new, 0);
  353. $this->dao->update($id, [$field => $new]);
  354. return true;
  355. }
  356. }