CommentController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * 评论控制器.
  4. *
  5. * @author zhangjunbao <junbaoz1314@gmail.com>
  6. * @date 2020-01-28 20:18:54
  7. */
  8. declare(strict_types=1);
  9. namespace Modules\Api\Controllers;
  10. use App\Constants\CommentConstant;
  11. use App\Constants\CommonConstant;
  12. use App\Repositories\CommentRepository;
  13. use App\Repositories\Criterias\CommentCriteria;
  14. use App\Repositories\Criterias\OrderLimitCriteria;
  15. use App\Repositories\Service\CommonService;
  16. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  17. use Modules\Api\Responses\CommentPagesResponse;
  18. use Modules\Manage\Responses\CommonSuccessPkResponse;
  19. class CommentController extends BaseController
  20. {
  21. /**
  22. * 评论列表.
  23. * @return mixed|CommentPagesResponse
  24. */
  25. public function lists()
  26. {
  27. $inputData = $this->getJsonData();
  28. $rules = [
  29. 'page' => 'nullable|integer',
  30. 'page_size' => 'nullable|integer',
  31. 'module' => 'nullable|max:60',
  32. 'svalue' => 'nullable|max:64',
  33. 'user_id' => 'nullable|max:32',
  34. 'top_type' => 'nullable|string', // all, only_top, not_top
  35. 'parent_id' => 'nullable|integer',
  36. ];
  37. $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
  38. if ($validator->fails()) {
  39. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  40. return json_fail($validator->errors()->first());
  41. }
  42. $orders = [
  43. ['created_at', 'DESC'],
  44. ];
  45. $pageSize = $inputData['page_size'] ?? CommentConstant::PAGE_SIZE;
  46. if ($pageSize > 100) {
  47. $pageSize = CommentConstant::PAGE_SIZE;
  48. }
  49. $page = format_page($inputData['page']);
  50. $pageSize = intval($pageSize);
  51. if ($pageSize <= 0) {
  52. $pageSize = CommentConstant::PAGE_SIZE;
  53. }
  54. $userCode = $this->getUserId();
  55. $plusParams = [];
  56. $commentRepo = make(CommentRepository::class);
  57. $commentRepo2 = clone $commentRepo;
  58. $lists = $commentRepo->pushCriteria(new CommentCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
  59. $checkResult = $commentRepo2->checkPermission($userCode, $inputData['module']);
  60. $plusParams['check_permission'] = $checkResult;
  61. return new CommentPagesResponse($lists, $plusParams);
  62. }
  63. /**
  64. * 创建评论.
  65. * @return mixed|CommonSuccessPkResponse
  66. */
  67. public function write()
  68. {
  69. $inputData = $this->getJsonData();
  70. $rules = [
  71. 'module' => 'required|max:60',
  72. 'svalue' => 'required|max:64',
  73. 'subject' => 'nullable|max:255',
  74. 'content' => 'required',
  75. 'parent_id' => 'nullable|integer',
  76. ];
  77. $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
  78. if ($validator->fails()) {
  79. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  80. return json_fail($validator->errors()->first());
  81. }
  82. $userId = $this->getUserId();
  83. $checkResult = make(CommentRepository::class)->checkPermission($userId, 'video');
  84. if (!$checkResult['check_result']) {
  85. $failMessage = sprintf('今日已发布评论 %s/%s 条 会员充值 解锁更多评论权限', $checkResult['posted'], $checkResult['amount']);
  86. return json_fail($failMessage, 500, $checkResult);
  87. }
  88. $module = strval(data_get($inputData, 'module', ''));
  89. $svalue = strval(data_get($inputData, 'svalue', ''));
  90. $subject = strval(data_get($inputData, 'subject', ''));
  91. $content = strval(data_get($inputData, 'content', ''));
  92. $parent_id = intval(data_get($inputData, 'parent_id', 0));
  93. $commonService = make(CommonService::class);
  94. if (!empty($content)) {
  95. // 过滤敏感词.
  96. $content = $commonService->filterString($content);
  97. }
  98. $setarr = [
  99. 'user_id' => $userId,
  100. 'module' => $module,
  101. 'svalue' => $svalue,
  102. 'subject' => $subject,
  103. 'content' => $content,
  104. 'parent_id' => $parent_id,
  105. ];
  106. $item = make(CommentRepository::class)->createComment($setarr);
  107. if (empty($item->id)) {
  108. return json_fail('评论失败了');
  109. }
  110. $result = [
  111. 'result' => 'success',
  112. 'id' => $item->id,
  113. 'amount' => $checkResult['amount'],
  114. 'posted' => $checkResult['posted'] + 1,
  115. ];
  116. return json_success($result);
  117. }
  118. /**
  119. * 点赞支持.
  120. * @return mixed|CommonSuccessPkResponse
  121. */
  122. public function support()
  123. {
  124. $inputData = $this->getJsonData();
  125. $rules = [
  126. 'id' => 'required|integer',
  127. ];
  128. $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
  129. if ($validator->fails()) {
  130. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  131. return json_fail($validator->errors()->first());
  132. }
  133. $id = intval(data_get($inputData, 'id', 0));
  134. $item = make(CommentRepository::class)->find($id);
  135. if (!$item) {
  136. return json_fail('点赞失败哦');
  137. }
  138. $item->total_support = intval($item->total_support) + 1;
  139. $result = $item->save();
  140. if (!$result) {
  141. return json_fail('点赞失败了');
  142. }
  143. $logSubject = '评论点赞';
  144. $logSvalue = $id;
  145. $this->log($logSubject, $logSvalue);
  146. return new CommonSuccessPkResponse($id);
  147. }
  148. }