VideoCommentServices.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. declare (strict_types=1);
  12. namespace app\services\activity\video;
  13. use app\dao\activity\video\VideoCommentDao;
  14. use app\services\BaseServices;
  15. use app\services\store\SystemStoreServices;
  16. use app\services\user\UserRelationServices;
  17. use app\services\user\UserServices;
  18. use crmeb\exceptions\AdminException;
  19. use crmeb\services\FormBuilder as Form;
  20. use think\exception\ValidateException;
  21. use think\facade\Route as Url;
  22. /**
  23. * Class VideoCommentServices
  24. * @package app\services\activity\video
  25. * @mixin VideoCommentDao
  26. */
  27. class VideoCommentServices extends BaseServices
  28. {
  29. /**
  30. * VideoCommentServices constructor.
  31. * @param VideoCommentDao $dao
  32. */
  33. public function __construct(VideoCommentDao $dao)
  34. {
  35. $this->dao = $dao;
  36. }
  37. /**
  38. * 视频评价列表
  39. * @param array $where
  40. * @return array
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public function sysPage(array $where)
  46. {
  47. $where['pid'] = 0;
  48. $where['is_del'] = 0;
  49. [$page, $limit] = $this->getPageValue();
  50. $with = ['video', 'reply' => function($query) {
  51. $query->field('id,pid,content')->bind(['reply' => 'content']);
  52. }];
  53. $list = $this->dao->getList($where, '*', $with, $page, $limit);
  54. $count = $this->dao->count($where);
  55. return compact('list', 'count');
  56. }
  57. /**
  58. * 回复评论
  59. * @param int $id
  60. * @param string $content
  61. */
  62. public function setReply(int $id, string $content)
  63. {
  64. if ($content == '') throw new AdminException('请输入回复内容');
  65. $comment = $this->dao->get($id);
  66. if (!$comment) {
  67. throw new AdminException('回复的评论不存在');
  68. }
  69. $save = [];
  70. $save['type'] = $comment['type'];
  71. $save['relation_id'] = $comment['relation_id'];
  72. $save['pid'] = $comment['pid'] ? $comment['pid'] : $id;
  73. $save['video_id'] = $comment['video_id'];
  74. $save['content'] = $content;
  75. $nickname = $avatar = '';
  76. switch ($comment['type']) {
  77. case 0:
  78. $nickname = sys_config('site_name');
  79. $avatar = sys_config('wap_login_logo');
  80. break;
  81. case 1://门店
  82. $store_id = (int)$comment['relation_id'];
  83. if ($store_id) {
  84. /** @var SystemStoreServices $storeServices */
  85. $storeServices = app()->make(SystemStoreServices::class);
  86. $store = $storeServices->get($store_id);
  87. $nickname = $store['name'];
  88. $avatar = $store['image'];
  89. }
  90. break;
  91. }
  92. $save['nickname'] = $nickname;
  93. $save['avatar'] = $avatar;
  94. $where = ['video_id' => $comment['video_id'], 'uid' => 0, 'pid' => $id,'is_del' => 0];
  95. if ($this->dao->count($where)) {
  96. $res = $this->dao->update($where, ['content' => $content, 'update_time' => time()]);
  97. } else {
  98. $save['add_time'] = time();
  99. $res = $this->dao->save($save);
  100. }
  101. if (!$res) throw new AdminException('回复失败,请稍后再试');
  102. $this->dao->update($id, ['is_reply' => 1]);
  103. return true;
  104. }
  105. /**
  106. * 获取评论回复列表
  107. * @param int $id
  108. * @param array $where
  109. * @return array
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\DbException
  112. * @throws \think\db\exception\ModelNotFoundException
  113. */
  114. public function getCommentReplyList(int $id, array $where)
  115. {
  116. $comment = $this->dao->get($id);
  117. if (!$comment) {
  118. throw new AdminException('回复的评论不存在');
  119. }
  120. $where['pid'] = $id;
  121. $where['is_del'] = 0;
  122. [$page, $limit] = $this->getPageValue();
  123. $list = $this->dao->getList($where, '*', ['video', 'user', 'children' => function ($query) {
  124. $query->with([
  125. 'user' => function ($query) {
  126. $query->field('uid,avatar,nickname,is_money_level');
  127. }
  128. ]);
  129. }], $page, $limit);
  130. $count = $this->dao->count($where);
  131. return compact('list', 'count');
  132. }
  133. /**
  134. * 创建自评表单
  135. * @param int $video_id
  136. * @param $store_id
  137. * @return mixed
  138. */
  139. public function createForm(int $video_id, $store_id = 0)
  140. {
  141. if ($video_id == 0) {
  142. $field[] = Form::frameImage('video', '视频', Url::buildUrl($store_id ? config('admin.store_prefix') . '/video.shortVideo/index' : config('admin.admin_prefix') . '/video.shortVideo/index', array('fodder' => 'video')))->icon('ios-add')->width('960px')->height('560px')->modal(['footer-hide' => true])->Props(['srcKey' => 'image']);
  143. } else {
  144. $field[] = Form::hidden('video_id', $video_id);
  145. }
  146. $field[] = Form::frameImage('avatar', '用户头像', Url::buildUrl($store_id ? config('admin.store_prefix') . '/widget.images/index' : config('admin.admin_prefix') . '/widget.images/index', array('fodder' => 'avatar')))->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true]);
  147. $field[] = Form::input('nickname', '用户名称')->col(24);
  148. $field[] = Form::input('content', '评价文字')->type('textarea');
  149. $field[] = Form::dateTime('add_time', '评论时间', '')->placeholder('请选择评论时间(不选择默认当前添加时间)');
  150. return create_form('添加自评', $field, Url::buildUrl('/marketing/video/comment/save_fictitious'), 'POST');
  151. }
  152. /**
  153. * 保存评价、回复
  154. * @param int $uid
  155. * @param int $video_id
  156. * @param $id
  157. * @param array $data
  158. * @return \crmeb\basic\BaseModel|\think\Model
  159. * @throws \think\db\exception\DataNotFoundException
  160. * @throws \think\db\exception\DbException
  161. * @throws \think\db\exception\ModelNotFoundException
  162. */
  163. public function saveComment(int $uid, int $video_id, $id, array $data)
  164. {
  165. /** @var VideoServices $videoServices */
  166. $videoServices = app()->make(VideoServices::class);
  167. $video = $videoServices->get($video_id);
  168. if (!$video) {
  169. throw new AdminException('评论的视频不存在');
  170. }
  171. $pid = $id;
  172. if ($id) {//回复评价
  173. $comment = $this->dao->get($id);
  174. if (!$comment) {
  175. return app('json')->fail('评论不存在或已删除');
  176. }
  177. if ($comment['pid']) $pid = $comment['pid'];
  178. }
  179. if ($uid && $uid != -1) {
  180. /** @var UserServices $userServices */
  181. $userServices = app()->make(UserServices::class);
  182. $userInfo = $userServices->getUserInfo($uid);
  183. if (!$userInfo) {
  184. return app('json')->fail('用户不存在');
  185. }
  186. $data['nickname'] = $userInfo['nickname'] ?? '';
  187. $data['avatar'] = $userInfo['avatar'] ?? '';
  188. }
  189. //ip转城市信息
  190. if (isset($data['ip']) && $data['ip']) {
  191. $data['city'] = $this->convertIp($data['ip']);
  192. }
  193. $data['uid'] = $uid;
  194. $data['video_id'] = $video_id;
  195. $data['pid'] = $pid;
  196. $data['type'] = $video['type'];
  197. $data['relation_id'] = $video['relation_id'];
  198. $time = time();
  199. if (isset($data['add_time']) && $data['add_time']) {
  200. if ($data['add_time'] > $time) {
  201. throw new AdminException('评论时间应小于当前时间');
  202. }
  203. $data['add_time'] = strtotime($data['add_time']);
  204. } else {
  205. $data['add_time'] = $time;
  206. }
  207. $res = $this->dao->save($data);
  208. /** @var VideoServices $videoServices */
  209. $videoServices = app()->make(VideoServices::class);
  210. $videoServices->bcInc($video_id, 'comment_num', 1);
  211. if (!$res) throw new AdminException('保存评论失败');
  212. return $res;
  213. }
  214. /**
  215. * 获取短视频评价列表
  216. * @param int $uid
  217. * @param int $id
  218. * @param int $pid
  219. * @return array
  220. * @throws \think\db\exception\DataNotFoundException
  221. * @throws \think\db\exception\DbException
  222. * @throws \think\db\exception\ModelNotFoundException
  223. */
  224. public function getVideoCommentList(int $uid, int $id, int $pid = 0)
  225. {
  226. $where = ['video_id' => $id, 'is_del' => 0];
  227. [$page, $limit] = $this->getPageValue();
  228. if ($limit > 20) $limit = 20;
  229. if ($pid) {
  230. $comment = $this->dao->get($pid);
  231. if (!$comment) {
  232. throw new ValidateException('评论不存在或已删除');
  233. }
  234. $where['video_id'] = $comment['video_id'];
  235. }
  236. $where['pid'] = $pid;
  237. $list = $this->dao->getList($where, 'id,pid,video_id,uid,nickname,avatar,content,like_num,city,add_time', ['children' => function($query) {
  238. $query->field("id,pid")->with([
  239. 'user' => function ($query) {
  240. $query->field('uid,avatar,nickname,is_money_level')->bind(['is_money_level']);
  241. }
  242. ]);
  243. }, 'user' => function ($query) {
  244. $query->field('uid,avatar,nickname,is_money_level')->bind(['is_money_level']);
  245. }], $page, $limit);
  246. if ($list) {
  247. $userLike = [];
  248. if ($uid) {
  249. $ids = array_column($list, 'id');
  250. /** @var UserRelationServices $userRelationServices */
  251. $userRelationServices = app()->make(UserRelationServices::class);
  252. $userLike = $userRelationServices->getColumn([['uid', '=', $uid], ['relation_id', 'in', $ids], ['category', '=', 'video_comment'], ['type', '=', 'like']], 'id,relation_id', 'relation_id');
  253. }
  254. foreach ($list as &$item) {
  255. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  256. $item['is_like'] = isset($userLike[$item['id']]);
  257. $item['reply'] = [];
  258. $item['reply_count'] = isset($item['children']) ? (int)count($item['children']) : 0;
  259. $item['city'] = $this->addressHandle($item['city'])['city'] ?? '';
  260. unset($item['children']);
  261. }
  262. }
  263. return $list;
  264. }
  265. }