| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- /**
- * 话题控制器.
- *
- * @author zhangjunbao <junbaoz1314@gmail.com>
- * @date 2020-01-29 10:13:09
- */
- declare(strict_types=1);
- namespace Modules\Api\Controllers;
- use App\Constants\CommonConstant;
- use App\Repositories\Criterias\OrderLimitCriteria;
- use App\Repositories\Criterias\TopicCriteria;
- use App\Repositories\TopicRepository;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- use Modules\Api\Responses\TopicPagesResponse;
- use Modules\Manage\Responses\CommonSuccessPkResponse;
- class TopicController extends BaseController
- {
- public function lists()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'page' => 'nullable|integer',
- 'page_size' => 'nullable|integer',
- 'category_id' => 'nullable|integer',
- 'top_type' => 'nullable|string', // all, only_top, not_top
- 'digest' => 'nullable|string', // all, only_digest, not_digest
- ];
- $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $orders = [
- ['created_at', 'DESC'],
- ];
- $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
- if ($pageSize > 100) {
- $pageSize = CommonConstant::PAGE_SIZE;
- }
- $page = format_page($inputData['page']);
- $pageSize = intval($pageSize);
- if ($pageSize <= 0) {
- $pageSize = CommonConstant::PAGE_SIZE;
- }
- $plusParams = [];
- $criteriaParams = $inputData;
- $criteriaParams['status'] = 1; // 追加状态条件.
- $lists = make(TopicRepository::class)->pushCriteria(new TopicCriteria($criteriaParams))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
- return new TopicPagesResponse($lists);
- }
- public function create()
- {
- $inputData = $this->getJsonData();
- $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', // 多图路径.
- ];
- $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $userId = $this->getUserId();
- $categoryId = intval(data_get($inputData, 'category_id', 0));
- $content = strval(data_get($inputData, 'content', ''));
- $ctType = intval(data_get($inputData, 'ct_type', 1));
- $videoId = strval(data_get($inputData, 'video_id', ''));
- $arrMulitPic = data_get($inputData, 'mulit_pic', []);
- $status = $ctType == 2 ? 3 : 1;
- $firstPic = '';
- $arrData = [];
- if (!empty($arrMulitPic)) {
- $arrData['mulit_pic'] = $arrMulitPic;
- reset($arrMulitPic);
- $firstPic = current($arrMulitPic); // 取第一张图.
- }
- // 封面.
- $coverPic = $firstPic;
- $sdata = json_encode($arrData, JSON_UNESCAPED_UNICODE);
- $setarr = [
- 'user_id' => $userId,
- 'category_id' => $categoryId,
- 'content' => $content,
- 'ct_type' => $ctType,
- 'video_id' => $videoId,
- 'status' => $status,
- 'cover_pic' => $coverPic,
- 'sdata' => $sdata,
- ];
- $item = make(TopicRepository::class)->createTopic($setarr);
- if (empty($item->id)) {
- return json_fail('话题失败了');
- }
- return new CommonSuccessPkResponse($item->id);
- }
- public function support()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'id' => 'required|integer',
- ];
- $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $id = intval(data_get($inputData, 'id', 0));
- $item = make(TopicRepository::class)->find($id);
- if (!$item) {
- return json_fail('点赞失败哦');
- }
- $item->total_support = intval($item->total_support) + 1;
- $result = $item->save();
- if (!$result) {
- return json_fail('点赞失败了');
- }
- $logSubject = '话题点赞';
- $logSvalue = $id;
- $this->log($logSubject, $logSvalue);
- return new CommonSuccessPkResponse($id);
- }
- }
|