Modules_Api_Controllers_CommentController.proxy.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. use \Hyperf\Di\Aop\ProxyTrait;
  22. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  23. function __construct()
  24. {
  25. if (method_exists(parent::class, '__construct')) {
  26. parent::__construct(...func_get_args());
  27. }
  28. $this->__handlePropertyHandler(__CLASS__);
  29. }
  30. /**
  31. * 评论列表.
  32. * @return mixed|CommentPagesResponse
  33. */
  34. public function lists()
  35. {
  36. $inputData = $this->getJsonData();
  37. $rules = [
  38. 'page' => 'nullable|integer',
  39. 'page_size' => 'nullable|integer',
  40. 'module' => 'nullable|max:60',
  41. 'svalue' => 'nullable|max:64',
  42. 'user_id' => 'nullable|max:32',
  43. 'top_type' => 'nullable|string',
  44. // all, only_top, not_top
  45. 'parent_id' => 'nullable|integer',
  46. ];
  47. $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
  48. if ($validator->fails()) {
  49. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  50. return json_fail($validator->errors()->first());
  51. }
  52. $orders = [['created_at', 'DESC']];
  53. $pageSize = $inputData['page_size'] ?? CommentConstant::PAGE_SIZE;
  54. if ($pageSize > 100) {
  55. $pageSize = CommentConstant::PAGE_SIZE;
  56. }
  57. $page = format_page($inputData['page']);
  58. $pageSize = intval($pageSize);
  59. if ($pageSize <= 0) {
  60. $pageSize = CommentConstant::PAGE_SIZE;
  61. }
  62. $userCode = $this->getUserId();
  63. $plusParams = [];
  64. $commentRepo = make(CommentRepository::class);
  65. $commentRepo2 = clone $commentRepo;
  66. $lists = $commentRepo->pushCriteria(new CommentCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
  67. $checkResult = $commentRepo2->checkPermission($userCode, $inputData['module']);
  68. $plusParams['check_permission'] = $checkResult;
  69. return new CommentPagesResponse($lists, $plusParams);
  70. }
  71. /**
  72. * 创建评论.
  73. * @return mixed|CommonSuccessPkResponse
  74. */
  75. public function write()
  76. {
  77. $inputData = $this->getJsonData();
  78. $rules = ['module' => 'required|max:60', 'svalue' => 'required|max:64', 'subject' => 'nullable|max:255', 'content' => 'required', 'parent_id' => 'nullable|integer'];
  79. $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
  80. if ($validator->fails()) {
  81. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  82. return json_fail($validator->errors()->first());
  83. }
  84. $userId = $this->getUserId();
  85. $checkResult = make(CommentRepository::class)->checkPermission($userId, 'video');
  86. if (!$checkResult['check_result']) {
  87. $failMessage = sprintf('今日已发布评论 %s/%s 条 会员充值 解锁更多评论权限', $checkResult['posted'], $checkResult['amount']);
  88. return json_fail($failMessage, 500, $checkResult);
  89. }
  90. $module = strval(data_get($inputData, 'module', ''));
  91. $svalue = strval(data_get($inputData, 'svalue', ''));
  92. $subject = strval(data_get($inputData, 'subject', ''));
  93. $content = strval(data_get($inputData, 'content', ''));
  94. $parent_id = intval(data_get($inputData, 'parent_id', 0));
  95. $commonService = make(CommonService::class);
  96. if (!empty($content)) {
  97. // 过滤敏感词.
  98. $content = $commonService->filterString($content);
  99. }
  100. $setarr = ['user_id' => $userId, 'module' => $module, 'svalue' => $svalue, 'subject' => $subject, 'content' => $content, 'parent_id' => $parent_id];
  101. $item = make(CommentRepository::class)->createComment($setarr);
  102. if (empty($item->id)) {
  103. return json_fail('评论失败了');
  104. }
  105. $result = ['result' => 'success', 'id' => $item->id, 'amount' => $checkResult['amount'], 'posted' => $checkResult['posted'] + 1];
  106. return json_success($result);
  107. }
  108. /**
  109. * 点赞支持.
  110. * @return mixed|CommonSuccessPkResponse
  111. */
  112. public function support()
  113. {
  114. $inputData = $this->getJsonData();
  115. $rules = ['id' => 'required|integer'];
  116. $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
  117. if ($validator->fails()) {
  118. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  119. return json_fail($validator->errors()->first());
  120. }
  121. $id = intval(data_get($inputData, 'id', 0));
  122. $item = make(CommentRepository::class)->find($id);
  123. if (!$item) {
  124. return json_fail('点赞失败哦');
  125. }
  126. $item->total_support = intval($item->total_support) + 1;
  127. $result = $item->save();
  128. if (!$result) {
  129. return json_fail('点赞失败了');
  130. }
  131. $logSubject = '评论点赞';
  132. $logSvalue = $id;
  133. $this->log($logSubject, $logSvalue);
  134. return new CommonSuccessPkResponse($id);
  135. }
  136. }