TopicController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. public function lists()
  20. {
  21. $inputData = $this->getJsonData();
  22. $rules = [
  23. 'page' => 'nullable|integer',
  24. 'page_size' => 'nullable|integer',
  25. 'category_id' => 'nullable|integer',
  26. 'top_type' => 'nullable|string', // all, only_top, not_top
  27. 'digest' => 'nullable|string', // all, only_digest, not_digest
  28. ];
  29. $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
  30. if ($validator->fails()) {
  31. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  32. return json_fail($validator->errors()->first());
  33. }
  34. $orders = [
  35. ['created_at', 'DESC'],
  36. ];
  37. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  38. if ($pageSize > 100) {
  39. $pageSize = CommonConstant::PAGE_SIZE;
  40. }
  41. $page = format_page($inputData['page']);
  42. $pageSize = intval($pageSize);
  43. if ($pageSize <= 0) {
  44. $pageSize = CommonConstant::PAGE_SIZE;
  45. }
  46. $plusParams = [];
  47. $criteriaParams = $inputData;
  48. $criteriaParams['status'] = 1; // 追加状态条件.
  49. $lists = make(TopicRepository::class)->pushCriteria(new TopicCriteria($criteriaParams))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
  50. return new TopicPagesResponse($lists);
  51. }
  52. public function create()
  53. {
  54. $inputData = $this->getJsonData();
  55. $rules = [
  56. 'category_id' => 'required|integer',
  57. 'content' => 'required|max:1000',
  58. 'ct_type' => 'required|integer|in:1,2',
  59. 'video_id' => 'required_if:ct_type,2',
  60. 'mulit_pic' => 'nullable|array', // 多图路径.
  61. ];
  62. $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
  63. if ($validator->fails()) {
  64. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  65. return json_fail($validator->errors()->first());
  66. }
  67. $userId = $this->getUserId();
  68. $categoryId = intval(data_get($inputData, 'category_id', 0));
  69. $content = strval(data_get($inputData, 'content', ''));
  70. $ctType = intval(data_get($inputData, 'ct_type', 1));
  71. $videoId = strval(data_get($inputData, 'video_id', ''));
  72. $arrMulitPic = data_get($inputData, 'mulit_pic', []);
  73. $status = $ctType == 2 ? 3 : 1;
  74. $firstPic = '';
  75. $arrData = [];
  76. if (!empty($arrMulitPic)) {
  77. $arrData['mulit_pic'] = $arrMulitPic;
  78. reset($arrMulitPic);
  79. $firstPic = current($arrMulitPic); // 取第一张图.
  80. }
  81. // 封面.
  82. $coverPic = $firstPic;
  83. $sdata = json_encode($arrData, JSON_UNESCAPED_UNICODE);
  84. $setarr = [
  85. 'user_id' => $userId,
  86. 'category_id' => $categoryId,
  87. 'content' => $content,
  88. 'ct_type' => $ctType,
  89. 'video_id' => $videoId,
  90. 'status' => $status,
  91. 'cover_pic' => $coverPic,
  92. 'sdata' => $sdata,
  93. ];
  94. $item = make(TopicRepository::class)->createTopic($setarr);
  95. if (empty($item->id)) {
  96. return json_fail('话题失败了');
  97. }
  98. return new CommonSuccessPkResponse($item->id);
  99. }
  100. public function support()
  101. {
  102. $inputData = $this->getJsonData();
  103. $rules = [
  104. 'id' => 'required|integer',
  105. ];
  106. $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
  107. if ($validator->fails()) {
  108. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  109. return json_fail($validator->errors()->first());
  110. }
  111. $id = intval(data_get($inputData, 'id', 0));
  112. $item = make(TopicRepository::class)->find($id);
  113. if (!$item) {
  114. return json_fail('点赞失败哦');
  115. }
  116. $item->total_support = intval($item->total_support) + 1;
  117. $result = $item->save();
  118. if (!$result) {
  119. return json_fail('点赞失败了');
  120. }
  121. $logSubject = '话题点赞';
  122. $logSvalue = $id;
  123. $this->log($logSubject, $logSvalue);
  124. return new CommonSuccessPkResponse($id);
  125. }
  126. }