123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- namespace app\api\controller;
- use app\model\CustomerClue;
- use think\facade\Request;
- use app\model\VideoType;
- use app\model\UserCollect;
- use app\model\Video as VideoModel;
- class Video extends Base
- {
- /**
- * 视频列表
- */
- public function list($page, $limit, $type = '', $label = '', $keyword = '', $order = 'uploadtime', $desc = 'desc')
- {
- $order = !$order ? 'uploadtime' : $order;
- $desc = !$desc ? 'desc' : $desc;
- // $order = $order.' '.$desc;
- $condition[] = ['root_id', '=', request()->token['root_org']];
- $condition[] = ['publish', '=', 1];
- $condition[] = ['delete_time', '=', 0];
- empty($label) ?: $condition[] = ['label', '=', $label];
- empty($type) ?: $condition[] = ['type', '=', $type];
- empty($keyword) ?: $condition[] = ['title', 'like', '%' . $keyword . '%'];
- // 获取数据
- $data = VideoModel::where($condition)->field('id,title,description,video_url,uploadtime,cover,shared_times,cover_share_img');
- if ($order == 'clue_number') {
- $data = $data->select()->toArray();
- } else {
- $data = $data->page($page, $limit)->order($order . ' ' . $desc)->select()->toArray();
- }
- $count = VideoModel::where($condition)->count();
- $user_id = $this->request->token['uid'];
- if (!empty($user_id)) {
- $video_id = array_column($data, 'id');
- $video_id = implode(',', $video_id);
- $user_foot = UserCollect::where([['content_type','=','video'], ['content_id','in',$video_id], ['user_id','=',$user_id]])->column('user_id', 'content_id');
- }
- foreach ($data as &$val) {
- $val['isCollection'] = false;
- if (!empty($user_id)) {
- $val['isCollection'] = isset($user_foot[$val['id']]) ? true : false;
- }
- $val['clue_number'] = CustomerClue::where([['pipe_type', '=', 'video'], ['pipe_id', '=', $val['id']]])->count();
- /** -----乐尚企业演示,线索数量进行调整按照分享量的三分之一展示,正式使用系统时删除此代码 ------*/
- if(request()->token['root_org'] == 1793) {
- $val['clue_number'] = floor($val['shared_times'] > 0 ? $val['shared_times']/3 : 0);
- }
- /*------------------------------------------------------------------------*/
- }
- // 排序
- if ($order == 'clue_number') {
- $sort = $desc == 'desc' ? SORT_DESC : SORT_ASC;
- array_multisort(array_column($data, $order), $sort, array_column($data, 'uploadtime'), SORT_DESC, $data);
- $data = array_slice($data, ($page-1) * $limit, $limit);
- }
- return json(['code' => 0, 'data' => $data, 'count' => $count, 'msg' => '获取成功']);
- }
- /**
- * 类型获取
- */
- public function type()
- {
- $type = VideoType::where(['pid' => 0, 'type' => 'video', 'root_id' => request()->token['root_org']])->select()->toArray();
- $label = VideoType::where([['pid', '>', 0], ['type', '=', 'video'], ['root_id', '=', request()->token['root_org']]])->select()->toArray();
- foreach ($type as &$val) {
- $val['sonLabel'] = [];
- foreach ($label as $typeVal) {
- if ($val['id'] == $typeVal['pid']) {
- $val['sonLabel'][] = $typeVal;
- }
- }
- }
- return json(['code' => 0, 'data' => $type, 'msg' => '获取成功']);
- }
-
- /**
- * 获取所有标签
- */
- public function get_all_label(){
- $label = VideoType::field('id,pid,name')->where([['pid', '>', 0], ['type', '=', 'video'], ['root_id', '=', request()->token['root_org']]])->select()->toArray();
- return json(['code' => 0, 'data' => $label, 'msg' => '获取成功']);
- }
- /**
- * 获取视频详情
- */
- public function info($video_id)
- {
- $video = VideoModel::where(['id' => $video_id, 'publish' => 1, 'delete_time' => 0])->find();
- $user_id = $this->request->token['uid'];
- $collect = UserCollect::where(['user_id' => $user_id, 'content_type' => 'video', 'content_id' => $video_id])->count();
- $video['collect'] = $collect > 0 ? true : false;
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $video]);
- }
- /**
- * 收藏
- */
- public function collect($id)
- {
- $user_id = $this->request->token['uid'];
- $had = UserCollect::where(['user_id' => $user_id, 'content_type' => 'video', 'content_id' => $id])->count();
- if ($had > 0) return json(['code' => 1, 'msg' => '您已收藏']);
- UserCollect::insert(['user_id' => $user_id, 'content_type' => 'video', 'content_id' => $id]);
- return json(['code' => 0, 'msg' => '收藏成功']);
- }
- /**
- * 取消收藏
- */
- public function collectCancel($id)
- {
- $user_id = $this->request->token['uid'];
- UserCollect::where(['user_id' => $user_id, 'content_type' => 'video', 'content_id' => $id])->delete();
- return json(['code' => 0, 'msg' => '取消收藏']);
- }
- }
|