| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- /**
- * 评论控制器.
- *
- * @author zhangjunbao <junbaoz1314@gmail.com>
- * @date 2020-01-28 20:18:54
- */
- declare(strict_types=1);
- namespace Modules\Api\Controllers;
- use App\Constants\CommentConstant;
- use App\Constants\CommonConstant;
- use App\Repositories\CommentRepository;
- use App\Repositories\Criterias\CommentCriteria;
- use App\Repositories\Criterias\OrderLimitCriteria;
- use App\Repositories\Service\CommonService;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- use Modules\Api\Responses\CommentPagesResponse;
- use Modules\Manage\Responses\CommonSuccessPkResponse;
- class CommentController extends BaseController
- {
- /**
- * 评论列表.
- * @return mixed|CommentPagesResponse
- */
- public function lists()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'page' => 'nullable|integer',
- 'page_size' => 'nullable|integer',
- 'module' => 'nullable|max:60',
- 'svalue' => 'nullable|max:64',
- 'user_id' => 'nullable|max:32',
- 'top_type' => 'nullable|string', // all, only_top, not_top
- 'parent_id' => 'nullable|integer',
- ];
- $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $orders = [
- ['created_at', 'DESC'],
- ];
- $pageSize = $inputData['page_size'] ?? CommentConstant::PAGE_SIZE;
- if ($pageSize > 100) {
- $pageSize = CommentConstant::PAGE_SIZE;
- }
- $page = format_page($inputData['page']);
- $pageSize = intval($pageSize);
- if ($pageSize <= 0) {
- $pageSize = CommentConstant::PAGE_SIZE;
- }
- $userCode = $this->getUserId();
- $plusParams = [];
- $commentRepo = make(CommentRepository::class);
- $commentRepo2 = clone $commentRepo;
- $lists = $commentRepo->pushCriteria(new CommentCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
- $checkResult = $commentRepo2->checkPermission($userCode, $inputData['module']);
- $plusParams['check_permission'] = $checkResult;
- return new CommentPagesResponse($lists, $plusParams);
- }
- /**
- * 创建评论.
- * @return mixed|CommonSuccessPkResponse
- */
- public function write()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'module' => 'required|max:60',
- 'svalue' => 'required|max:64',
- 'subject' => 'nullable|max:255',
- 'content' => 'required',
- 'parent_id' => 'nullable|integer',
- ];
- $validator = make(ValidatorFactoryInterface::class)->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $userId = $this->getUserId();
- $checkResult = make(CommentRepository::class)->checkPermission($userId, 'video');
- if (!$checkResult['check_result']) {
- $failMessage = sprintf('今日已发布评论 %s/%s 条 会员充值 解锁更多评论权限', $checkResult['posted'], $checkResult['amount']);
- return json_fail($failMessage, 500, $checkResult);
- }
- $module = strval(data_get($inputData, 'module', ''));
- $svalue = strval(data_get($inputData, 'svalue', ''));
- $subject = strval(data_get($inputData, 'subject', ''));
- $content = strval(data_get($inputData, 'content', ''));
- $parent_id = intval(data_get($inputData, 'parent_id', 0));
- $commonService = make(CommonService::class);
- if (!empty($content)) {
- // 过滤敏感词.
- $content = $commonService->filterString($content);
- }
- $setarr = [
- 'user_id' => $userId,
- 'module' => $module,
- 'svalue' => $svalue,
- 'subject' => $subject,
- 'content' => $content,
- 'parent_id' => $parent_id,
- ];
- $item = make(CommentRepository::class)->createComment($setarr);
- if (empty($item->id)) {
- return json_fail('评论失败了');
- }
- $result = [
- 'result' => 'success',
- 'id' => $item->id,
- 'amount' => $checkResult['amount'],
- 'posted' => $checkResult['posted'] + 1,
- ];
- return json_success($result);
- }
- /**
- * 点赞支持.
- * @return mixed|CommonSuccessPkResponse
- */
- 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(CommentRepository::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);
- }
- }
|