Modules_Manage_Controllers_CommentController.proxy.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. use \Hyperf\Di\Aop\ProxyTrait;
  13. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  14. function __construct()
  15. {
  16. if (method_exists(parent::class, '__construct')) {
  17. parent::__construct(...func_get_args());
  18. }
  19. $this->__handlePropertyHandler(__CLASS__);
  20. }
  21. /**
  22. * 首页数据
  23. * @param ValidatorFactoryInterface $validatorFactory
  24. */
  25. public function lists(ValidatorFactoryInterface $validatorFactory)
  26. {
  27. $inputData = $this->getJsonData();
  28. $rules = ['stime' => 'nullable|date', 'otime' => 'nullable|date', 'module' => 'nullable|string', 'like_content' => 'nullable|string', 'digest' => 'nullable|numeric', 'top' => 'nullable|numeric', 'user_uid' => 'nullable', 'page' => 'nullable|numeric', 'page_size' => 'nullable|numeric'];
  29. $validator = $validatorFactory->make($inputData, $rules);
  30. if ($validator->fails()) {
  31. return json_fail($validator->errors()->first());
  32. }
  33. $commentModel = make(CommentRepository::class);
  34. $commentGroup = clone $commentModel;
  35. $module = $commentGroup->getModule();
  36. $commentList = make(CommentRepository::class)->getLists($inputData);
  37. $data = ['dataList' => $commentList, 'module' => $module];
  38. return json_success($data);
  39. }
  40. /**
  41. * 修改状态
  42. * @param ValidatorFactoryInterface $validatorFactory
  43. * @return mixed
  44. */
  45. public function editType(ValidatorFactoryInterface $validatorFactory)
  46. {
  47. $inputData = $this->getJsonData();
  48. $rules = ['comment_id' => 'required|numeric', 'type' => 'required|string|in:is_digest,is_top', 'val' => 'required|numeric'];
  49. $validator = $validatorFactory->make($inputData, $rules);
  50. if ($validator->fails()) {
  51. return json_fail($validator->errors()->first());
  52. }
  53. $res = CommentModel::query()->where('id', $inputData['comment_id'])->update([$inputData['type'] => $inputData['val']]);
  54. if (!$res) {
  55. return json_fail('修改失败');
  56. }
  57. return json_success('修改成功');
  58. }
  59. /**
  60. * 删除评论
  61. * @param ValidatorFactoryInterface $validatorFactory
  62. * @return mixed
  63. */
  64. public function delete(ValidatorFactoryInterface $validatorFactory)
  65. {
  66. $inputData = $this->getJsonData();
  67. $rules = ['comment_id' => 'required|numeric'];
  68. $validator = $validatorFactory->make($inputData, $rules);
  69. if ($validator->fails()) {
  70. return json_fail($validator->errors()->first());
  71. }
  72. $res = CommentModel::query()->where('id', $inputData['comment_id'])->delete();
  73. if (!$res) {
  74. return json_fail('修改失败');
  75. }
  76. return json_success('修改成功');
  77. }
  78. /**
  79. * 批量删除评论
  80. *
  81. * @param ValidatorFactoryInterface $validatorFactory
  82. * @return mixed
  83. */
  84. public function deleteLists(ValidatorFactoryInterface $validatorFactory)
  85. {
  86. $inputData = $this->getJsonData();
  87. $rules = ['ids' => 'required'];
  88. $validator = $validatorFactory->make($inputData, $rules);
  89. if ($validator->fails()) {
  90. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  91. return json_fail($validator->errors()->first());
  92. }
  93. $ids = $inputData['ids'];
  94. $aIds = explode(',', $ids);
  95. $res = CommentModel::query()->whereIn('id', $aIds)->delete();
  96. if ($res) {
  97. return json_success($res);
  98. } else {
  99. return json_fail('删除失败');
  100. }
  101. }
  102. }