VideoController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. namespace app\controller\api\v1\activity;
  12. use app\Request;
  13. use app\services\activity\video\VideoCommentServices;
  14. use app\services\activity\video\VideoServices;
  15. use app\services\product\product\StoreProductServices;
  16. use app\services\user\UserRelationServices;
  17. /**
  18. * 短视频类
  19. * Class VideoController
  20. * @package app\controller\api\activity
  21. */
  22. class VideoController
  23. {
  24. protected $services;
  25. public function __construct(VideoServices $services)
  26. {
  27. $this->services = $services;
  28. }
  29. /**
  30. * 获取短视频列表
  31. * @return mixed
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public function list(Request $request)
  37. {
  38. [$order_type, $id] = $request->getMore([
  39. ['order_type', ''],
  40. ['id', 0]
  41. ], true);
  42. return app('json')->success($this->services->getVideoList((int)$request->uid(), '*', (int)$order_type, (int)$id));
  43. }
  44. /**
  45. * 获取短视频评价列表
  46. * @param Request $request
  47. * @param VideoCommentServices $commentServices
  48. * @param $id
  49. * @return mixed
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function commentList(Request $request, VideoCommentServices $commentServices, $id)
  55. {
  56. if (!(int)$id) return app('json')->fail('缺少参数');
  57. return app('json')->success($commentServices->getVideoCommentList((int)$request->uid(), (int)$id));
  58. }
  59. /**
  60. * 获取短视频评价回复列表
  61. * @param Request $request
  62. * @param VideoCommentServices $commentServices
  63. * @param $pid
  64. * @return mixed
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. public function commentReplyList(Request $request, VideoCommentServices $commentServices, $pid)
  70. {
  71. if (!(int)$pid) return app('json')->fail('缺少参数');
  72. return app('json')->success($commentServices->getVideoCommentList((int)$request->uid(), 0, (int)$pid));
  73. }
  74. /**
  75. * 短视频关联商品
  76. * @param Request $request
  77. * @param StoreProductServices $productServices
  78. * @param $id
  79. * @return mixed
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\DbException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. */
  84. public function productList(Request $request, StoreProductServices $productServices, $id)
  85. {
  86. if (!$id) return app('json')->fail('缺少参数');
  87. $video = $this->services->getVideoInfo((int)$id);
  88. $list = [];
  89. $count = 0;
  90. if ($video['product_id']) {
  91. $ids = is_string($video['product_id']) ? explode(',', $video['product_id']) : $video['product_id'];
  92. $where = ['ids' => $ids];
  93. $list = $productServices->getGoodsList($where, (int)$request->uid());
  94. $newList = [];
  95. foreach ($list as $key => $item) {
  96. $item['promotions'] = !isset($item['promotions']) || !$item['promotions'] ? (object)[] : $item['promotions'];
  97. if ($item['relation_id'] && $item['type'] == 1) {
  98. $item['store_id'] = $item['relation_id'];
  99. } else {
  100. $item['store_id'] = 0;
  101. }
  102. $newList[$key] = $item;
  103. }
  104. $where['is_verify'] = 1;
  105. $where['is_show'] = 1;
  106. $where['is_del'] = 0;
  107. $count = $productServices->getCount($where);
  108. $list = get_thumb_water($newList, 'small');
  109. }
  110. return app('json')->success(compact('list', 'count'));
  111. }
  112. /**
  113. * 短视频点赞、收藏、分享(再次点击取消)
  114. * @param Request $request
  115. * @param $id
  116. * @param $type
  117. * @return mixed
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\DbException
  120. * @throws \think\db\exception\ModelNotFoundException
  121. */
  122. public function relation(Request $request, $id, $type = 'like')
  123. {
  124. if (!$id) return app('json')->fail('缺少参数');
  125. if (!in_array($type, ['like', 'collect', 'share'])) {
  126. return app('json')->fail('类型错误');
  127. }
  128. $uid = (int)$request->uid();
  129. $this->services->userRelationVideo($uid, $id, $type);
  130. return app('json')->success();
  131. }
  132. /**
  133. * 保存评价
  134. * @param Request $request
  135. * @param VideoCommentServices $commentServices
  136. * @param $video_id
  137. * @param $id
  138. * @return mixed
  139. * @throws \think\db\exception\DataNotFoundException
  140. * @throws \think\db\exception\DbException
  141. * @throws \think\db\exception\ModelNotFoundException
  142. */
  143. public function saveComment(Request $request, VideoCommentServices $commentServices, $id, $pid = 0)
  144. {
  145. if (!$id) return app('json')->fail('缺少参数');
  146. [$content] = $request->getMore([
  147. ['content', '']
  148. ], true);
  149. if (!$content) {
  150. return app('json')->fail('请输入评论内容');
  151. }
  152. $uid = (int)$request->uid();
  153. $comment = $commentServices->saveComment($uid, (int)$id, (int)$pid, ['content' => $content, 'ip' => $request->ip()]);
  154. return app('json')->success('评价成功', $comment->toArray());
  155. }
  156. /**
  157. * 评论点赞(再次点击取消)
  158. * @param Request $request
  159. * @param UserRelationServices $userRelationServices
  160. * @param $id
  161. * @param $type
  162. * @return mixed
  163. * @throws \think\db\exception\DataNotFoundException
  164. * @throws \think\db\exception\DbException
  165. * @throws \think\db\exception\ModelNotFoundException
  166. */
  167. public function commentRelation(Request $request, UserRelationServices $userRelationServices, VideoCommentServices $commentServices, $id, $type = 'like')
  168. {
  169. if (!$id) return app('json')->fail('缺少参数');
  170. if (!in_array($type, ['like', 'collect', 'share'])) {
  171. return app('json')->fail('类型错误');
  172. }
  173. $comment = $commentServices->get($id);
  174. if (!$comment) {
  175. return app('json')->fail('评论不存在或已删除');
  176. }
  177. $uid = (int)$request->uid();
  178. $data = ['uid' => $uid, 'relation_id' => $id, 'category' => UserRelationServices::CATEGORY_VIDEO_COMMENT, 'type' => $type];
  179. $relation = $userRelationServices->get($data);
  180. if ($relation) {//取消
  181. $userRelationServices->delete($relation['id']);
  182. $commentServices->bcDec($id, $type . '_num', 1);
  183. $status = 0;
  184. } else {
  185. $data['add_time'] = time();
  186. $userRelationServices->save($data);
  187. $commentServices->bcInc($id, $type . '_num', 1);
  188. $status = 1;
  189. }
  190. return app('json')->success(($status == 1 ? '取消' : '') . (UserRelationServices::TYPE_NAME[$type] ?? '收藏') . '成功');
  191. }
  192. /**
  193. * 撤销评价
  194. * @param Request $request
  195. * @param VideoCommentServices $commentServices
  196. * @param $id
  197. * @return mixed
  198. */
  199. public function commentDelete(Request $request, VideoCommentServices $commentServices, $id)
  200. {
  201. if (!$id) return app('json')->fail('缺少参数');
  202. $comment = $commentServices->get($id);
  203. if (!$comment) {
  204. return app('json')->fail('评论不存在或已删除');
  205. }
  206. $uid = (int)$request->uid();
  207. if ($comment['uid'] != $uid) {
  208. return app('json')->fail('只能撤销自己的评价');
  209. }
  210. $commentServices->update($id, ['is_del' => 1]);
  211. $this->services->bcDec($comment['video_id'], 'comment_num', 1);
  212. return app('json')->success('撤销成功');
  213. }
  214. }