123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\services\activity\video;
- use app\dao\activity\video\VideoDao;
- use app\jobs\activity\VideoJob;
- use app\services\activity\live\LiveRoomServices;
- use app\services\BaseServices;
- use app\services\out\StoreProductServices;
- use app\services\product\category\StoreProductCategoryServices;
- use app\services\store\SystemStoreServices;
- use app\services\user\UserRelationServices;
- use think\exception\ValidateException;
- /**
- * Class VideoServices
- * @package app\services\activity\video
- * @mixin VideoDao
- */
- class VideoServices extends BaseServices
- {
- /**
- * 移动端视频需要默认值
- * @var array
- */
- protected $videoDefault = [
- 'isMore' => false,
- 'state' => "pause",
- 'playIng' => false,
- 'isShowimage' => false,
- 'isShowProgressBarTime' => false,
- 'isplay' => true
- ];
- /**
- * VideoServices constructor.
- * @param VideoDao $dao
- */
- public function __construct(VideoDao $dao)
- {
- $this->dao = $dao;
- }
- /**
- * 获取短视频信息(获取不到抛出异常)
- * @param int $id
- * @param string $field
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getVideoInfo(int $id, string $field = '*')
- {
- $videoInfo = $this->dao->getOne(['id' => $id], $field);
- if (!$videoInfo) {
- throw new ValidateException('获取短视频信息失败');
- }
- return $videoInfo->toArray();
- }
- /**
- * 获取视频详情
- * @param int $id
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getInfo(int $id)
- {
- $info = $this->dao->get($id);
- if (!$info) {
- throw new ValidateException('视频不存在');
- }
- $info = $info->toArray();
- $productInfo = [];
- $info['type_name'] = '';
- switch ($info['type']) {
- case 0:
- $info['type_name'] = '平台';
- break;
- case 1:
- if ($info['relation_id']) {
- /** @var SystemStoreServices $storeServices */
- $storeServices = app()->make(SystemStoreServices::class);
- $info['type_name'] = $storeServices->value(['id' => $info['relation_id']], 'name') ?? '';
- }
- break;
- }
- if ($info['product_id']) {
- $info['product_id'] = is_string($info['product_id']) ? explode(',', $info['product_id']) : $info['product_id'];
- /** @var StoreProductServices $productServices */
- $productServices = app()->make(StoreProductServices::class);
- $productInfo = $productServices->getColumn([['id', 'in', $info['product_id']]], 'id,type,product_type,price,image,store_name,cate_id,sales,stock');
- $cateIds = implode(',', array_column($productInfo, 'cate_id'));
- /** @var StoreProductCategoryServices $categoryService */
- $categoryService = app()->make(StoreProductCategoryServices::class);
- $cateList = $categoryService->getCateParentAndChildName($cateIds);
- foreach ($productInfo as $key => &$item) {
- $item['cate_name'] = '';
- if (isset($item['cate_id']) && $item['cate_id']) {
- $cate_ids = explode(',', $item['cate_id']);
- $cate_name = $categoryService->getCateName($cate_ids, $cateList);
- if ($cate_name) {
- $item['cate_name'] = is_array($cate_name) ? implode(',', $cate_name) : '';
- }
- }
- }
- }
- $info['productInfo'] = $productInfo;
- return $info;
- }
- /**
- * 后台获取视频列表
- * @param $where
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function sysPage($where)
- {
- [$page, $limit] = $this->getPageValue();
- $list = $this->dao->getList($where, '*', $page, $limit);
- $count = 0;
- if ($list) {
- /** @var SystemStoreServices $storeServices */
- $storeServices = app()->make(SystemStoreServices::class);
- $storeIds = array_unique(array_column($list, 'relation_id'));
- $storeInfos = $storeServices->getColumn([['id', 'in', $storeIds]], 'id,name,image', 'id');
- $site_name = sys_config('site_name');
- $site_image = sys_config('wap_login_logo');
- /** @var StoreProductServices $productServices */
- $productServices = app()->make(StoreProductServices::class);
- $product_where = ['is_del' => 0, 'is_show' => 1, 'is_verify' => 1];
- foreach ($list as &$item) {
- $item['product_id'] = is_string($item['product_id']) ? explode(',', $item['product_id']) : $item['product_id'];
- $item['product_info'] = [];
- $item['product_num'] = 0;
- if ($item['product_id']) {
- $item['product_info'] = $productServices->getSearchList($product_where + ['ids' => $item['product_id']], 0, 0, ['id', 'store_name', 'image', 'price'], '', []);
- $item['product_num'] = count($item['product_info']);
- }
- $item['type_name'] = $site_name;
- $item['type_image'] = $site_image;
- switch ($item['type']) {
- case 0:
- break;
- case 1:
- $item['type_name'] = $storeInfos[$item['relation_id']]['name'] ?? '';
- $item['type_image'] = $storeInfos[$item['relation_id']]['image'] ?? '';
- break;
- }
- $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
- }
- $count = $this->dao->count($where);
- }
- return compact('list', 'count');
- }
- /**
- * 获取短视频列表
- * @param int $uid
- * @param string $field
- * @param int $order_type
- * @param int $id
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getVideoList(int $uid, string $field = '*', int $order_type = 1, int $id = 0)
- {
- //短视频未启用
- if (!sys_config('video_func_status', 1)) {
- return [];
- }
- $where = ['is_show' => 1, 'is_del' => 0, 'is_verify' => 1];
- [$page, $limit] = $this->getPageValue();
- //限制一次最多请求条数
- if ($limit > 10) $limit = 10;
- if ($id) {//指定视频进入
- $ids = array_merge([$id], $this->dao->getColumn($where, 'id'));
- $where['order_by_id'] = array_slice($ids, 0, $limit);
- $order = '';
- } elseif ($order_type == 1) {//最新
- $order = 'id desc,sort desc';
- } else if ($order_type == 2) {//推荐
- $where['is_recommend'] = 1;
- $order = 'is_recommend desc,sort desc,id desc';
- } else {
- $order = 'sort desc,id desc';
- }
- $list = $this->dao->getList($where, $field, $page, $limit, $order);
- if ($list) {
- /** @var LiveRoomServices $liveServices */
- $liveServices = app()->make(LiveRoomServices::class);
- $liveCount = $liveServices->count(['is_show' => 1, 'is_del' => 0, 'status' => 1, 'live_status' => [101, 105, 106]]);
- $is_live = (bool)$liveCount;
- /** @var SystemStoreServices $storeServices */
- $storeServices = app()->make(SystemStoreServices::class);
- $storeIds = array_unique(array_column($list, 'relation_id'));
- $storeInfos = $storeServices->getColumn([['id', 'in', $storeIds]], 'id,name,image', 'id');
- $site_name = sys_config('site_name');
- $site_image = sys_config('wap_login_logo');
- $userLike = $userCollect = [];
- $ids = array_column($list, 'id');
- if ($uid) {
- /** @var UserRelationServices $userRelationServices */
- $userRelationServices = app()->make(UserRelationServices::class);
- $userLike = $userRelationServices->getColumn([['uid', '=', $uid], ['relation_id', 'in', $ids], ['category', '=', 'video'], ['type', '=', 'like']], 'id,relation_id', 'relation_id');
- $userCollect = $userRelationServices->getColumn([['uid', '=', $uid], ['relation_id', 'in', $ids], ['category', '=', 'video'], ['type', '=', 'collect']], 'id,relation_id', 'relation_id');
- }
- /** @var StoreProductServices $storeProductServices */
- $storeProductServices = app()->make(StoreProductServices::class);
- $productWhere = ['is_verify' => 1, 'is_show' => 1, 'is_del' => 0];
- foreach ($list as &$item) {
- $item['product_id'] = is_string($item['product_id']) ? explode(',', $item['product_id']) : $item['product_id'];
- $item['product_num'] = count($item['product_id']);
- //过滤下架、审核中等商品
- $item['product_num'] = $item['product_num'] ? $storeProductServices->getCount($productWhere + ['ids' => $item['product_id']]) : 0;
- $item['type_name'] = $site_name;
- $item['type_image'] = $site_image;
- switch ($item['type']) {
- case 0://平台
- break;
- case 1://门店
- $item['type_name'] = $storeInfos[$item['relation_id']]['name'] ?? '';
- $item['type_image'] = $storeInfos[$item['relation_id']]['image'] ?? '';
- break;
- }
- $item['date'] = $item['add_time'] ? date('m月d日', $item['add_time']) : '';
- $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
- $item['is_like'] = isset($userLike[$item['id']]);
- $item['is_collect'] = isset($userCollect[$item['id']]);
- //app需要
- $item['id'] = (string)$item['id'];
- $item = array_merge($item, $this->videoDefault);
- $item['is_live'] = $is_live;
- }
- //增加浏览量
- VideoJob::dispatchDo('setVideoPlayNum', [$ids, $uid]);
- }
- return $list;
- }
- /**
- * diy获取短视频
- * @param int $uid
- * @param string $field
- * @param int $order_type
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getDiyVideoList(int $uid, string $field = '*', int $order_type = 0)
- {
- //短视频未启用
- if (!sys_config('video_func_status', 1)) {
- return [];
- }
- $where = ['is_show' => 1, 'is_del' => 0, 'is_verify' => 1];
- [$page, $limit] = $this->getPageValue();
- //限制一次最多请求条数
- if ($limit > 10) $limit = 10;
- if ($order_type == 1) {//最新
- $order = 'id desc,sort desc';
- } else if ($order_type == 2) {//推荐
- $where['is_recommend'] = 1;
- $order = 'is_recommend desc,sort desc,id desc';
- } else {
- $order = 'sort desc,id desc';
- }
- $list = $this->dao->getList($where, $field, $page, $limit, $order);
- if ($list) {
- /** @var SystemStoreServices $storeServices */
- $storeServices = app()->make(SystemStoreServices::class);
- $storeIds = array_unique(array_column($list, 'relation_id'));
- $storeInfos = $storeServices->getColumn([['id', 'in', $storeIds]], 'id,name,image', 'id');
- $site_name = sys_config('site_name');
- $site_image = sys_config('wap_login_logo');
- /** @var StoreProductServices $productServices */
- $productServices = app()->make(StoreProductServices::class);
- $product_where = ['is_del' => 0, 'is_show' => 1, 'is_verify' => 1];
- foreach ($list as &$item) {
- $item['product_id'] = is_string($item['product_id']) ? explode(',', $item['product_id']) : $item['product_id'];
- $item['product_info'] = [];
- $item['product_num'] = 0;
- if ($item['product_id']) {
- $item['product_info'] = $productServices->getSearchList($product_where + ['ids' => $item['product_id']], 0, 0, ['id', 'store_name', 'image', 'price'], '', []);
- $item['product_num'] = count($item['product_info']);
- }
- $item['type_name'] = $site_name;
- $item['type_image'] = $site_image;
- switch ($item['type']) {
- case 0:
- break;
- case 1:
- $item['type_name'] = $storeInfos[$item['relation_id']]['name'] ?? '';
- $item['type_image'] = $storeInfos[$item['relation_id']]['image'] ?? '';
- break;
- }
- $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
- }
- $ids = array_column($list, 'id');
- //增加浏览量
- VideoJob::dispatchDo('setVideoPlayNum', [$ids, $uid]);
- }
- return $list;
- }
- /**
- * 用户点赞、收藏、分享、浏览播放视频
- * @param int $uid
- * @param int $id
- * @param string $type
- * @param int $num
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function userRelationVideo(int $uid, int $id, string $type = 'like', int $num = 1)
- {
- $info = $this->getVideoInfo((int)$id);
- $data = ['uid' => $uid, 'relation_id' => $id, 'category' => UserRelationServices::CATEGORY_VIDEO, 'type' => $type];
- $typeArr = UserRelationServices::TYPE_NAME;
- if (!isset($typeArr[$type])) {
- throw new ValidateException('类型错误');
- }
- /** @var UserRelationServices $userRelationServices */
- $userRelationServices = app()->make(UserRelationServices::class);
- //播放 每一次都是增加数量
- if ($type != 'play') {
- $relation = $userRelationServices->get($data);
- } else {
- $relation = [];
- }
- $field = $type . '_num';
- $balance = $info[$field] ?? 0;
- if ($relation) {//取消
- $userRelationServices->delete($relation['id']);
- $new = (int)bcsub((string)$balance, (string)$num);
- } else {//增加
- $data['add_time'] = time();
- $userRelationServices->save($data);
- $new = (int)bcadd((string)$balance, (string)$num);
- }
- $new = max($new, 0);
- $this->dao->update($id, [$field => $new]);
- return true;
- }
- }
|