CommentController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. declare(strict_types=1);
  3. namespace Modules\Manage\Controllers;
  4. use App\Model\CommentModel;
  5. use App\Repositories\CommentRepository;
  6. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  7. /**
  8. * 评论管理
  9. */
  10. class CommentController extends BaseController
  11. {
  12. /**
  13. * 首页数据
  14. * @param ValidatorFactoryInterface $validatorFactory
  15. */
  16. public function lists(ValidatorFactoryInterface $validatorFactory)
  17. {
  18. $inputData = $this->getJsonData();
  19. $rules = [
  20. 'stime' => 'nullable|date',
  21. 'otime' => 'nullable|date',
  22. 'module' => 'nullable|string',
  23. 'like_content' => 'nullable|string',
  24. 'digest' => 'nullable|numeric',
  25. 'top' => 'nullable|numeric',
  26. 'user_uid' => 'nullable',
  27. 'page' => 'nullable|numeric',
  28. 'page_size' => 'nullable|numeric'
  29. ];
  30. $validator = $validatorFactory->make($inputData, $rules);
  31. if ($validator->fails()) {
  32. return json_fail($validator->errors()->first());
  33. }
  34. $commentModel = make(CommentRepository::class);
  35. $commentGroup = clone $commentModel;
  36. $module = $commentGroup->getModule();
  37. $commentList = make(CommentRepository::class)->getLists($inputData);
  38. $data = [
  39. 'dataList'=>$commentList,
  40. 'module'=>$module
  41. ];
  42. return json_success($data);
  43. }
  44. /**
  45. * 修改状态
  46. * @param ValidatorFactoryInterface $validatorFactory
  47. * @return mixed
  48. */
  49. public function editType(ValidatorFactoryInterface $validatorFactory)
  50. {
  51. $inputData = $this->getJsonData();
  52. $rules = [
  53. 'comment_id' => 'required|numeric',
  54. 'type' => 'required|string|in:is_digest,is_top',
  55. 'val' => 'required|numeric',
  56. ];
  57. $validator = $validatorFactory->make($inputData, $rules);
  58. if ($validator->fails()) {
  59. return json_fail($validator->errors()->first());
  60. }
  61. $res = CommentModel::query()
  62. ->where('id', $inputData['comment_id'])
  63. ->update([$inputData['type'] => $inputData['val']]);
  64. if (!$res) {
  65. return json_fail('修改失败');
  66. }
  67. return json_success('修改成功');
  68. }
  69. /**
  70. * 删除评论
  71. * @param ValidatorFactoryInterface $validatorFactory
  72. * @return mixed
  73. */
  74. public function delete(ValidatorFactoryInterface $validatorFactory)
  75. {
  76. $inputData = $this->getJsonData();
  77. $rules = [
  78. 'comment_id' => 'required|numeric'
  79. ];
  80. $validator = $validatorFactory->make($inputData, $rules);
  81. if ($validator->fails()) {
  82. return json_fail($validator->errors()->first());
  83. }
  84. $res = CommentModel::query()
  85. ->where('id', $inputData['comment_id'])
  86. ->delete();
  87. if (!$res) {
  88. return json_fail('修改失败');
  89. }
  90. return json_success('修改成功');
  91. }
  92. /**
  93. * 批量删除评论
  94. *
  95. * @param ValidatorFactoryInterface $validatorFactory
  96. * @return mixed
  97. */
  98. public function deleteLists(ValidatorFactoryInterface $validatorFactory)
  99. {
  100. $inputData = $this->getJsonData();
  101. $rules = [
  102. 'ids' => 'required'
  103. ];
  104. $validator = $validatorFactory->make($inputData, $rules);
  105. if ($validator->fails()) {
  106. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  107. return json_fail($validator->errors()->first());
  108. }
  109. $ids = $inputData['ids'];
  110. $aIds = explode(',', $ids);
  111. $res = CommentModel::query()->whereIn('id', $aIds)->delete();
  112. if ($res) {
  113. return json_success($res);
  114. } else {
  115. return json_fail('删除失败');
  116. }
  117. }
  118. }