Video.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\api\controller;
  3. use app\model\CustomerClue;
  4. use think\facade\Request;
  5. use app\model\VideoType;
  6. use app\model\UserCollect;
  7. use app\model\Video as VideoModel;
  8. class Video extends Base
  9. {
  10. /**
  11. * 视频列表
  12. */
  13. public function list($page, $limit, $type = '', $label = '', $keyword = '', $order = 'uploadtime', $desc = 'desc')
  14. {
  15. $order = !$order ? 'uploadtime' : $order;
  16. $desc = !$desc ? 'desc' : $desc;
  17. // $order = $order.' '.$desc;
  18. $condition[] = ['root_id', '=', request()->token['root_org']];
  19. $condition[] = ['publish', '=', 1];
  20. $condition[] = ['delete_time', '=', 0];
  21. empty($label) ?: $condition[] = ['label', '=', $label];
  22. empty($type) ?: $condition[] = ['type', '=', $type];
  23. empty($keyword) ?: $condition[] = ['title', 'like', '%' . $keyword . '%'];
  24. // 获取数据
  25. $data = VideoModel::where($condition)->field('id,title,description,video_url,uploadtime,cover,shared_times,cover_share_img');
  26. if ($order == 'clue_number') {
  27. $data = $data->select()->toArray();
  28. } else {
  29. $data = $data->page($page, $limit)->order($order . ' ' . $desc)->select()->toArray();
  30. }
  31. $count = VideoModel::where($condition)->count();
  32. $user_id = $this->request->token['uid'];
  33. if (!empty($user_id)) {
  34. $video_id = array_column($data, 'id');
  35. $video_id = implode(',', $video_id);
  36. $user_foot = UserCollect::where([['content_type','=','video'], ['content_id','in',$video_id], ['user_id','=',$user_id]])->column('user_id', 'content_id');
  37. }
  38. foreach ($data as &$val) {
  39. $val['isCollection'] = false;
  40. if (!empty($user_id)) {
  41. $val['isCollection'] = isset($user_foot[$val['id']]) ? true : false;
  42. }
  43. $val['clue_number'] = CustomerClue::where([['pipe_type', '=', 'video'], ['pipe_id', '=', $val['id']]])->count();
  44. /** -----乐尚企业演示,线索数量进行调整按照分享量的三分之一展示,正式使用系统时删除此代码 ------*/
  45. if(request()->token['root_org'] == 1793) {
  46. $val['clue_number'] = floor($val['shared_times'] > 0 ? $val['shared_times']/3 : 0);
  47. }
  48. /*------------------------------------------------------------------------*/
  49. }
  50. // 排序
  51. if ($order == 'clue_number') {
  52. $sort = $desc == 'desc' ? SORT_DESC : SORT_ASC;
  53. array_multisort(array_column($data, $order), $sort, array_column($data, 'uploadtime'), SORT_DESC, $data);
  54. $data = array_slice($data, ($page-1) * $limit, $limit);
  55. }
  56. return json(['code' => 0, 'data' => $data, 'count' => $count, 'msg' => '获取成功']);
  57. }
  58. /**
  59. * 类型获取
  60. */
  61. public function type()
  62. {
  63. $type = VideoType::where(['pid' => 0, 'type' => 'video', 'root_id' => request()->token['root_org']])->select()->toArray();
  64. $label = VideoType::where([['pid', '>', 0], ['type', '=', 'video'], ['root_id', '=', request()->token['root_org']]])->select()->toArray();
  65. foreach ($type as &$val) {
  66. $val['sonLabel'] = [];
  67. foreach ($label as $typeVal) {
  68. if ($val['id'] == $typeVal['pid']) {
  69. $val['sonLabel'][] = $typeVal;
  70. }
  71. }
  72. }
  73. return json(['code' => 0, 'data' => $type, 'msg' => '获取成功']);
  74. }
  75. /**
  76. * 获取所有标签
  77. */
  78. public function get_all_label(){
  79. $label = VideoType::field('id,pid,name')->where([['pid', '>', 0], ['type', '=', 'video'], ['root_id', '=', request()->token['root_org']]])->select()->toArray();
  80. return json(['code' => 0, 'data' => $label, 'msg' => '获取成功']);
  81. }
  82. /**
  83. * 获取视频详情
  84. */
  85. public function info($video_id)
  86. {
  87. $video = VideoModel::where(['id' => $video_id, 'publish' => 1, 'delete_time' => 0])->find();
  88. $user_id = $this->request->token['uid'];
  89. $collect = UserCollect::where(['user_id' => $user_id, 'content_type' => 'video', 'content_id' => $video_id])->count();
  90. $video['collect'] = $collect > 0 ? true : false;
  91. return json(['code' => 0, 'msg' => '获取成功', 'data' => $video]);
  92. }
  93. /**
  94. * 收藏
  95. */
  96. public function collect($id)
  97. {
  98. $user_id = $this->request->token['uid'];
  99. $had = UserCollect::where(['user_id' => $user_id, 'content_type' => 'video', 'content_id' => $id])->count();
  100. if ($had > 0) return json(['code' => 1, 'msg' => '您已收藏']);
  101. UserCollect::insert(['user_id' => $user_id, 'content_type' => 'video', 'content_id' => $id]);
  102. return json(['code' => 0, 'msg' => '收藏成功']);
  103. }
  104. /**
  105. * 取消收藏
  106. */
  107. public function collectCancel($id)
  108. {
  109. $user_id = $this->request->token['uid'];
  110. UserCollect::where(['user_id' => $user_id, 'content_type' => 'video', 'content_id' => $id])->delete();
  111. return json(['code' => 0, 'msg' => '取消收藏']);
  112. }
  113. }