|
|
@@ -0,0 +1,486 @@
|
|
|
+<?php
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | 版权所有 2017~2021 LARAVEL研发中心
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | 官方网站: http://www.laravel.cn
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | Author: laravel开发员 <laravel.qq.com>
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+
|
|
|
+namespace App\Services\Api;
|
|
|
+
|
|
|
+use App\Models\AgentModel;
|
|
|
+use App\Models\GoodsModel;
|
|
|
+use App\Models\MemberModel;
|
|
|
+use App\Models\MerchantModel;
|
|
|
+use App\Models\OrderModel;
|
|
|
+use App\Models\ShopModel;
|
|
|
+use App\Models\TradeModel;
|
|
|
+use App\Models\VideoCollectModel;
|
|
|
+use App\Models\VideoModel;
|
|
|
+use App\Services\BaseService;
|
|
|
+use App\Services\ConfigService;
|
|
|
+use App\Services\RedisService;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 短视频管理-服务类
|
|
|
+ * @author laravel开发员
|
|
|
+ * @since 2020/11/11
|
|
|
+ * @package App\Services\Api
|
|
|
+ */
|
|
|
+class VideoService extends BaseService
|
|
|
+{
|
|
|
+ // 静态对象
|
|
|
+ protected static $instance = null;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ * @author laravel开发员
|
|
|
+ * @since 2020/11/11
|
|
|
+ * GoodsService constructor.
|
|
|
+ */
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ $this->model = new VideoModel();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 静态入口
|
|
|
+ * @return static|null
|
|
|
+ */
|
|
|
+ public static function make()
|
|
|
+ {
|
|
|
+ if (!self::$instance) {
|
|
|
+ self::$instance = (new static());
|
|
|
+ }
|
|
|
+ return self::$instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 列表数据
|
|
|
+ * @param $params
|
|
|
+ * @param int $pageSize
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getDataList($params, $pageSize = 18, $field = '', $userId=0)
|
|
|
+ {
|
|
|
+
|
|
|
+ $where = ['a.mark' => 1,'b.mark'=>1];
|
|
|
+ $field = $field? $field : 'lev_a.*';
|
|
|
+ $order = 'lev_a.id desc';
|
|
|
+ $sortType = isset($params['sort_type']) ? $params['sort_type'] : 0;
|
|
|
+ $list = $this->model->with(['member'])->from('video as a')
|
|
|
+ ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
|
|
|
+ ->where($where)
|
|
|
+ ->where(function ($query) use ($params) {
|
|
|
+ $type = isset($params['type']) ? $params['type'] : 0;
|
|
|
+ if ($type > 0) {
|
|
|
+ $query->where('a.type', $type);
|
|
|
+ }
|
|
|
+
|
|
|
+ $uid = isset($params['user_id']) ? $params['user_id'] : 0;
|
|
|
+ if ($uid > 0) {
|
|
|
+ $query->where('a.user_id', $uid);
|
|
|
+ }else{
|
|
|
+ $query->where('a.status', 2);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ ->where(function ($query) use ($params) {
|
|
|
+ $keyword = isset($params['kw']) ? $params['kw'] : '';
|
|
|
+ if ($keyword) {
|
|
|
+ $query->where('a.title', 'like', "%{$keyword}%")
|
|
|
+ ->orWhere('a.tags', 'like', "%{$keyword}%")
|
|
|
+ ->orWhere('a.description', 'like', "%{$keyword}%")
|
|
|
+ ->orWhere('b.nickname', 'like', "%{$keyword}%");
|
|
|
+ }
|
|
|
+ })
|
|
|
+ ->selectRaw($field)
|
|
|
+ ->orderByRaw($order)
|
|
|
+ ->paginate($pageSize > 0 ? $pageSize : 9999999);
|
|
|
+
|
|
|
+ $list = $list ? $list->toArray() : [];
|
|
|
+ if ($list && $list['data']) {
|
|
|
+ foreach ($list['data'] as &$item) {
|
|
|
+ $item['create_time'] = isset($item['create_time']) ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
|
|
|
+ $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
|
|
|
+ if(isset($item['albums'])){
|
|
|
+ $albums = $item['albums']? json_decode($item['albums'], true):[];
|
|
|
+ $item['albums'] = $albums? get_images_preview($albums) : [];
|
|
|
+ }
|
|
|
+
|
|
|
+ if(isset($item['file_url'])){
|
|
|
+ $item['file_url'] = $item['file_url']? get_image_url($item['file_url']) : '';
|
|
|
+ }
|
|
|
+
|
|
|
+ if(isset($item['music_url'])){
|
|
|
+ $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
|
|
|
+ }
|
|
|
+
|
|
|
+ $member = isset($item['member'])? $item['member'] : [];
|
|
|
+ if($member){
|
|
|
+ $member['avatar'] = isset($member['avatar'])? get_image_url($member['avatar']) : '';
|
|
|
+ }
|
|
|
+ $item['tags'] = isset($item['tags']) && $item['tags']? explode(',', $item['tags']) : [];
|
|
|
+ $item['like_num'] = isset($item['like_num']) && $item['like_num']? format_num($item['like_num']) : 0;
|
|
|
+ $item['collect_num'] = isset($item['collect_num']) && $item['collect_num']? format_num($item['collect_num']) : 0;
|
|
|
+ $item['views'] = isset($item['views']) && $item['views']? format_num($item['views']) : 0;
|
|
|
+ $item['member'] = $member;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'pageSize' => $pageSize,
|
|
|
+ 'total' => isset($list['total']) ? $list['total'] : 0,
|
|
|
+ 'list' => isset($list['data']) ? $list['data'] : []
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 列表数据
|
|
|
+ * @param $params
|
|
|
+ * @param int $pageSize
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getIndexList($params, $pageSize = 6, $field='', $userId=0)
|
|
|
+ {
|
|
|
+ $where = ['a.mark' => 1,'a.status'=>2,'b.mark'=>1];
|
|
|
+ $field = $field? $field : 'lev_a.*';
|
|
|
+ $order = 'rand()';
|
|
|
+ $model = $this->model->with(['member'])->from('video as a')
|
|
|
+ ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
|
|
|
+ ->where($where)
|
|
|
+ ->where(function ($query) use ($params) {
|
|
|
+ $type = isset($params['type']) ? $params['type'] : 0;
|
|
|
+ if ($type > 0) {
|
|
|
+ $query->where('a.type', $type);
|
|
|
+ }
|
|
|
+
|
|
|
+ $uid = isset($params['user_id']) ? $params['user_id'] : 0;
|
|
|
+ if ($uid > 0) {
|
|
|
+ $query->where('a.user_id', $uid);
|
|
|
+ }else{
|
|
|
+ $query->where('a.status', 2);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ ->where(function ($query) use ($params) {
|
|
|
+ $keyword = isset($params['kw']) ? $params['kw'] : '';
|
|
|
+ if ($keyword) {
|
|
|
+ $query->where('a.title', 'like', "%{$keyword}%")
|
|
|
+ ->orWhere('a.tags', 'like', "%{$keyword}%")
|
|
|
+ ->orWhere('a.description', 'like', "%{$keyword}%")
|
|
|
+ ->orWhere('b.nickname', 'like', "%{$keyword}%");
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 推荐的数据
|
|
|
+ $countModel = clone $model;
|
|
|
+ $total = $countModel->where(function($query) use($params, $userId){
|
|
|
+ // 推荐视频数据
|
|
|
+ $isRecommend = isset($params['is_recommend']) ? $params['is_recommend'] : 0;
|
|
|
+ if ($isRecommend > 0) {
|
|
|
+ $recommendData = VideoCollectService::make()->getRecommendData($userId);
|
|
|
+ $uids = isset($recommendData['uids'])? $recommendData['uids'] : []; // 按用户推荐
|
|
|
+ $tags = isset($recommendData['tags'])? $recommendData['tags'] : []; // 按标签推荐
|
|
|
+ if($uids){
|
|
|
+ $query->orWhere(function($query) use($uids){
|
|
|
+ $query->whereIn('a.user_id', $uids);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ if($tags){
|
|
|
+ $query->orWhere(function($query) use($tags){
|
|
|
+ foreach($tags as $tag){
|
|
|
+ $query->where('a.tags', 'like',"%{$tag}%")
|
|
|
+ ->orWhere('a.description','like',"%{$tag}%");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })->count('lev_a.id');
|
|
|
+
|
|
|
+ if($total > 0){
|
|
|
+ // 关联推荐数据
|
|
|
+ $list = $countModel->selectRaw($field)
|
|
|
+ ->orderByRaw($order)
|
|
|
+ ->paginate($pageSize > 0 ? $pageSize : 9999999);
|
|
|
+ }else{
|
|
|
+ // 默认推荐数据
|
|
|
+ $list = $model->selectRaw($field)
|
|
|
+ ->orderByRaw($order)
|
|
|
+ ->paginate($pageSize > 0 ? $pageSize : 9999999);
|
|
|
+ }
|
|
|
+
|
|
|
+ $list = $list ? $list->toArray() : [];
|
|
|
+ if ($list && $list['data']) {
|
|
|
+ foreach ($list['data'] as &$item) {
|
|
|
+ $item['create_time'] = isset($item['create_time']) ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
|
|
|
+ $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
|
|
|
+ if(isset($item['albums'])){
|
|
|
+ $albums = $item['albums']? json_decode($item['albums'], true):[];
|
|
|
+ $item['albums'] = $albums? get_images_preview($albums) : [];
|
|
|
+ }
|
|
|
+
|
|
|
+ if(isset($item['file_url'])){
|
|
|
+ $item['file_url'] = $item['file_url']? get_image_url($item['file_url']) : '';
|
|
|
+ }
|
|
|
+
|
|
|
+ if(isset($item['music_url'])){
|
|
|
+ $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
|
|
|
+ }
|
|
|
+
|
|
|
+ $member = isset($item['member'])? $item['member'] : [];
|
|
|
+ if($member){
|
|
|
+ $member['avatar'] = isset($member['avatar'])? get_image_url($member['avatar']) : '';
|
|
|
+ }
|
|
|
+ $item['tags'] = isset($item['tags']) && $item['tags']? explode(',', $item['tags']) : [];
|
|
|
+ $item['like_num'] = isset($item['like_num']) && $item['like_num']? format_num($item['like_num']) : 0;
|
|
|
+ $item['collect_num'] = isset($item['collect_num']) && $item['collect_num']? format_num($item['collect_num']) : 0;
|
|
|
+ $item['views'] = isset($item['views']) && $item['views']? format_num($item['views']) : 0;
|
|
|
+ $item['member'] = $member;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'pageSize' => $pageSize,
|
|
|
+ 'total' => isset($list['total']) ? $list['total'] : 0,
|
|
|
+ 'list' => isset($list['data']) ? $list['data'] : []
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 详情
|
|
|
+ * @param $id
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getInfo($id, $userId=0, $field=[])
|
|
|
+ {
|
|
|
+ $field = $field? $field : ['a.*'];
|
|
|
+ $info = $this->model->from('video as a')->with(['member'])
|
|
|
+ ->leftJoin('member as b','b.id','=','a.user_id')
|
|
|
+ ->where(['a.id'=> $id,'a.mark'=>1,'b.mark'=>1])
|
|
|
+ ->select($field)
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ $info = $info? $info->toArray() : [];
|
|
|
+ if($info){
|
|
|
+ if(isset($info['thumb'])){
|
|
|
+ $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
|
|
|
+ }
|
|
|
+ if(isset($info['file_url'])){
|
|
|
+ $info['file_url'] = $info['file_url']? get_image_url($info['file_url']) : '';
|
|
|
+ }
|
|
|
+ if(isset($item['music_url'])){
|
|
|
+ $item['music_url'] = $item['music_url']? get_image_url($item['music_url']) : '';
|
|
|
+ }
|
|
|
+ if(isset($info['albums'])){
|
|
|
+ $info['albums'] = $info['albums']? json_decode($info['albums'], true) : [];
|
|
|
+ $info['albums'] = $info['albums']? get_images_preview($info['albums']) : [];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 浏览历史
|
|
|
+ if(!VideoCollectService::make()->getCollectCacheInfo($userId, $id, 1)){
|
|
|
+ $data = [
|
|
|
+ 'user_id'=> $userId,
|
|
|
+ 'type'=> 1,
|
|
|
+ 'collect_id'=> $id,
|
|
|
+ 'collect_uid'=> isset($info['user_id'])? $info['user_id'] : 0,
|
|
|
+ 'tags'=> isset($info['tags'])? $info['tags'] : '',
|
|
|
+ 'create_time'=> time(),
|
|
|
+ 'status'=> 1,
|
|
|
+ ];
|
|
|
+ VideoCollectModel::insert($data);
|
|
|
+ RedisService::set("caches:videos:collect:temp_{$userId}_{$id}_1", $data, rand(10,30));
|
|
|
+ RedisService::clear("caches:videos:recommend:{$userId}");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 浏览量
|
|
|
+ $this->updateView($userId, $id);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $info;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新浏览量
|
|
|
+ * @param $userId
|
|
|
+ * @param $dynamicId
|
|
|
+ * @return array|mixed
|
|
|
+ */
|
|
|
+ public function updateView($userId, $id)
|
|
|
+ {
|
|
|
+ $cacheKey = "caches:videos:views:u{$userId}_d{$id}";
|
|
|
+ $data = RedisService::get($cacheKey);
|
|
|
+ if($data){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $data = $this->model->where(['id'=> $id])->update(['views'=>DB::raw('views + 1'),'update_time'=>time()]);
|
|
|
+ RedisService::set($cacheKey, $id, rand(1,3)*3600);
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发布
|
|
|
+ * @param $goodsId
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function publish($userId, $params)
|
|
|
+ {
|
|
|
+ $goodsId = isset($params['goods_id'])? intval($params['goods_id']) : 0;
|
|
|
+ $type = isset($params['type'])? intval($params['type']) : 2;
|
|
|
+ $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])
|
|
|
+ ->select(['id','nickname','status'])
|
|
|
+ ->first();
|
|
|
+ $status = isset($userInfo['status'])? $userInfo['status'] : 0;
|
|
|
+ $nickname = isset($userInfo['nickname'])? $userInfo['nickname'] : '';
|
|
|
+ if(empty($userInfo) || $status != 1){
|
|
|
+ $this->error = 2017;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $thumb = isset($params['thumb'])? trim($params['thumb']) : '';
|
|
|
+ if($thumb){
|
|
|
+ $result = upload_base64($thumb);
|
|
|
+ $thumb = isset($result['file_path'])? $result['file_path'] : '';
|
|
|
+ if(empty($thumb)){
|
|
|
+ $this->error = 2208;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 音乐
|
|
|
+ $musicUrl = isset($params['music_url'])? trim($params['music_url']) : '';
|
|
|
+ if($musicUrl){
|
|
|
+ $result = upload_remote_image($musicUrl,'mp3','music');
|
|
|
+ $musicUrl = isset($result['file_path'])? $result['file_path'] : '';
|
|
|
+ if(empty($musicUrl)){
|
|
|
+ $this->error = 2209;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 视频
|
|
|
+ $fileUrl = isset($params['file_url'])? trim($params['file_url']) : '';
|
|
|
+ $albums = isset($params['albums'])? $params['albums'] : [];
|
|
|
+ if($type == 1) {
|
|
|
+ if($fileUrl){
|
|
|
+ $result = upload_base64($fileUrl);
|
|
|
+ $fileUrl = isset($result['file_path'])? $result['file_path'] : '';
|
|
|
+ if(empty($fileUrl)){
|
|
|
+ $this->error = 2212;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ $this->error = 2213;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 相册
|
|
|
+ else if($type == 2){
|
|
|
+ if(empty($albums) || !is_array($albums)){
|
|
|
+ $this->error = 2211;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $files = [];
|
|
|
+ foreach ($albums as $album){
|
|
|
+ $result = upload_base64($album);
|
|
|
+ $url = isset($result['file_path'])? $result['file_path'] : '';
|
|
|
+ if(empty($url)){
|
|
|
+ $this->error = 2212;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $files[] = ['url'=> $url];
|
|
|
+ }
|
|
|
+
|
|
|
+ $albums = $files;
|
|
|
+ }
|
|
|
+
|
|
|
+ $title = isset($params['title']) && $params['title']? trim($params['title']) : "{$nickname} 发布的短视频";
|
|
|
+ $description = isset($params['description']) && $params['description']? trim($params['description']) : "{$nickname} 发布的短视频";
|
|
|
+ $publishCheck = ConfigService::make()->getConfigByCode('video_publish_check',0);
|
|
|
+ $publishCheck = $publishCheck>0? $publishCheck : 0;
|
|
|
+ $data = [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'title' => $title,
|
|
|
+ 'type' => $type,
|
|
|
+ 'description' => $description,
|
|
|
+ 'tags' => isset($params['tags']) && $params['tags']? implode(',', $params['tags']) : '',
|
|
|
+ 'thumb' => $thumb,
|
|
|
+ 'albums' => $albums? json_encode($albums) : '',
|
|
|
+ 'music_hash' => isset($params['music_hash'])? trim($params['music_hash']) : '',
|
|
|
+ 'music_url' => $musicUrl,
|
|
|
+ 'goods_id' => $goodsId,
|
|
|
+ 'is_comment' => isset($params['is_comment'])? intval($params['is_comment']) : 1,
|
|
|
+ 'status' => $publishCheck? 1 : 2,
|
|
|
+ 'mark' => 1,
|
|
|
+ 'publish_at' => date('Y-m-d H:i:s'),
|
|
|
+ 'create_time' => time(),
|
|
|
+ ];
|
|
|
+
|
|
|
+ if($this->model->insertGetId($data)){
|
|
|
+ $this->error = 1023;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->error = 1024;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 状态设置
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function status()
|
|
|
+ {
|
|
|
+ $id = request()->post('id', 0);
|
|
|
+ $status = request()->post('status', 1);
|
|
|
+ if ($id && !$this->model->where(['id' => $id, 'mark' => 1])->value('id')) {
|
|
|
+ $this->error = 2981;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($this->model->where(['id'=> $id,'mark'=>1])->update(['status'=>$status, 'update_time'=> time()])){
|
|
|
+ $this->error = 1002;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->error = 1003;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function delete()
|
|
|
+ {
|
|
|
+ // 参数
|
|
|
+ $param = request()->all();
|
|
|
+ $id = getter($param, "id");
|
|
|
+ if (empty($id)) {
|
|
|
+ $this->error = 2014;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!$this->model->where(['id'=> $id])->value('id')){
|
|
|
+ $this->error = 1039;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if($this->model->where(['id'=> $id])->update(['mark'=>0,'update_time'=>time()])){
|
|
|
+ $this->model->where(['mark'=> 0])->where('update_time','<=', time() - 3 * 86400)->delete();
|
|
|
+ $this->error = 1025;
|
|
|
+ return true;
|
|
|
+ }else{
|
|
|
+ $this->error = 1026;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|