Modules_Api_Controllers_TopicController.proxy.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * 话题控制器.
  4. *
  5. * @author zhangjunbao <junbaoz1314@gmail.com>
  6. * @date 2020-01-29 10:13:09
  7. */
  8. declare (strict_types=1);
  9. namespace Modules\Api\Controllers;
  10. use App\Constants\CommonConstant;
  11. use App\Repositories\Criterias\OrderLimitCriteria;
  12. use App\Repositories\Criterias\TopicCriteria;
  13. use App\Repositories\TopicRepository;
  14. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  15. use Modules\Api\Responses\TopicPagesResponse;
  16. use Modules\Manage\Responses\CommonSuccessPkResponse;
  17. class TopicController extends BaseController
  18. {
  19. use \Hyperf\Di\Aop\ProxyTrait;
  20. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  21. function __construct()
  22. {
  23. if (method_exists(parent::class, '__construct')) {
  24. parent::__construct(...func_get_args());
  25. }
  26. $this->__handlePropertyHandler(__CLASS__);
  27. }
  28. public function lists()
  29. {
  30. $inputData = $this->getJsonData();
  31. $rules = [
  32. 'page' => 'nullable|integer',
  33. 'page_size' => 'nullable|integer',
  34. 'category_id' => 'nullable|integer',
  35. 'top_type' => 'nullable|string',
  36. // all, only_top, not_top
  37. 'digest' => 'nullable|string',
  38. ];
  39. $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
  40. if ($validator->fails()) {
  41. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  42. return json_fail($validator->errors()->first());
  43. }
  44. $orders = [['created_at', 'DESC']];
  45. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  46. if ($pageSize > 100) {
  47. $pageSize = CommonConstant::PAGE_SIZE;
  48. }
  49. $page = format_page($inputData['page']);
  50. $pageSize = intval($pageSize);
  51. if ($pageSize <= 0) {
  52. $pageSize = CommonConstant::PAGE_SIZE;
  53. }
  54. $plusParams = [];
  55. $criteriaParams = $inputData;
  56. $criteriaParams['status'] = 1;
  57. // 追加状态条件.
  58. $lists = make(TopicRepository::class)->pushCriteria(new TopicCriteria($criteriaParams))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
  59. return new TopicPagesResponse($lists);
  60. }
  61. public function create()
  62. {
  63. $inputData = $this->getJsonData();
  64. $rules = ['category_id' => 'required|integer', 'content' => 'required|max:1000', 'ct_type' => 'required|integer|in:1,2', 'video_id' => 'required_if:ct_type,2', 'mulit_pic' => 'nullable|array'];
  65. $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
  66. if ($validator->fails()) {
  67. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  68. return json_fail($validator->errors()->first());
  69. }
  70. $userId = $this->getUserId();
  71. $categoryId = intval(data_get($inputData, 'category_id', 0));
  72. $content = strval(data_get($inputData, 'content', ''));
  73. $ctType = intval(data_get($inputData, 'ct_type', 1));
  74. $videoId = strval(data_get($inputData, 'video_id', ''));
  75. $arrMulitPic = data_get($inputData, 'mulit_pic', []);
  76. $status = $ctType == 2 ? 3 : 1;
  77. $firstPic = '';
  78. $arrData = [];
  79. if (!empty($arrMulitPic)) {
  80. $arrData['mulit_pic'] = $arrMulitPic;
  81. reset($arrMulitPic);
  82. $firstPic = current($arrMulitPic);
  83. // 取第一张图.
  84. }
  85. // 封面.
  86. $coverPic = $firstPic;
  87. $sdata = json_encode($arrData, JSON_UNESCAPED_UNICODE);
  88. $setarr = ['user_id' => $userId, 'category_id' => $categoryId, 'content' => $content, 'ct_type' => $ctType, 'video_id' => $videoId, 'status' => $status, 'cover_pic' => $coverPic, 'sdata' => $sdata];
  89. $item = make(TopicRepository::class)->createTopic($setarr);
  90. if (empty($item->id)) {
  91. return json_fail('话题失败了');
  92. }
  93. return new CommonSuccessPkResponse($item->id);
  94. }
  95. public function support()
  96. {
  97. $inputData = $this->getJsonData();
  98. $rules = ['id' => 'required|integer'];
  99. $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
  100. if ($validator->fails()) {
  101. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  102. return json_fail($validator->errors()->first());
  103. }
  104. $id = intval(data_get($inputData, 'id', 0));
  105. $item = make(TopicRepository::class)->find($id);
  106. if (!$item) {
  107. return json_fail('点赞失败哦');
  108. }
  109. $item->total_support = intval($item->total_support) + 1;
  110. $result = $item->save();
  111. if (!$result) {
  112. return json_fail('点赞失败了');
  113. }
  114. $logSubject = '话题点赞';
  115. $logSvalue = $id;
  116. $this->log($logSubject, $logSvalue);
  117. return new CommonSuccessPkResponse($id);
  118. }
  119. }