* @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); } }