getJsonData(); // 针对layui table自动分页请求参数,优先GET值. $queryPage = request()->has('page') ? request()->input('page', 1) : null; $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null; if (!is_null($queryPage)) { $inputData['page'] = $queryPage; } if (!is_null($queryPageSize)) { $inputData['page_size'] = $queryPageSize; } $rules = [ 'page' => 'nullable|integer', 'page_size' => 'nullable|integer', 'type' => 'nullable|integer', 'title' => 'nullable', 'uid' => 'nullable', ]; $validator = Validator::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 > 500) { $pageSize = CommonConstant::PAGE_SIZE; } $page = format_page($inputData['page'] ?? 0); $items = $repo->pushCriteria(new AnnouncePagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page); return new AnnouncePagesResponse($items); } /** * 应用详情 * @return \Illuminate\Http\JsonResponse|NotIncrementIdDetailResponse * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function detail(Validator $validatorFactory, AnnounceRepository $repo) { $inputData = $this->getJsonData(); $rules = [ 'uid' => 'required|string', ]; $validator = Validator::make($inputData,$rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $video = $repo->findBy('uid', $inputData['uid']); return new NotIncrementIdDetailResponse($video); } /** * 添加应用 * @return \Illuminate\Http\JsonResponse|mixed * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function create(Validator $validatorFactory, AnnounceRepository $repo) { $inputData = $this->getJsonData(); $rules = [ 'title' => 'required|max:255', 'type' => 'required|integer|max:6', 'content' => 'required|string', 'status' => 'nullable|integer', ]; $validator = Validator::make($inputData,$rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $setarr = [ 'title' => $inputData['title'], 'content' => $inputData['content'], 'type' => $inputData['type'], 'status' => $inputData['status'] ?? 1, ]; $result = $repo->create($setarr); if (!$result) { return json_fail('创建失败'); } else { $logSubject = '添加一个公告, 名称:' . $result->title; $logSvalue = $result->uid; $logDbData = [ 'after_items' => [ $result->toArray(), ], 'before_items' => [ ], ]; $this->log($logSubject, $logSvalue, $logDbData); return new CommonSuccessUidResponse($result->uid); } } public function editStatus(Validator $validatorFactory, AnnounceRepository $repo) { $inputData = $this->getJsonData(); $rules = [ 'uid' => 'required|string' ]; $validator = Validator::make($inputData,$rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $app = $repo->findBy('uid', $inputData['uid']); if (empty($app)) { return json_fail(trans('common.err_resource_not_found')); } $oldApp = clone $app; $app->status = $app->status === 1 ? 2 : 1; $result = $app->save(); if (!$result) { return json_fail('修改失败'); } else { $logSubject = '编辑一个公告, 名称:' . $app->title; $logSvalue = $app->uid; $logDbData = [ 'after_items' => [ $app->toArray(), ], 'before_items' => [ $oldApp->toArray(), ], ]; $this->log($logSubject, $logSvalue, $logDbData); return new CommonSuccessUidResponse($app->uid); } } /** * 编辑应用 * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function modify(Validator $validatorFactory, AnnounceRepository $repo) { $inputData = $this->getJsonData(); $rules = [ 'title' => 'required|max:255', 'type' => 'required|integer|max:6', 'content' => 'required|string', 'status' => 'nullable|integer', ]; $validator = Validator::make($inputData,$rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $item = $repo->findBy('uid', $inputData['uid']); if (empty($item)) { return json_fail(trans('common.err_resource_not_found')); } $oldItem = clone $item; if (isset($inputData['title'])) { $item->title = trim($inputData['title']); } if (isset($inputData['content'])) { $item->content = rtrim($inputData['content']); } if (isset($inputData['type'])) { $item->type = intval($inputData['type']); } $item->status = $inputData['status']; $result = $item->save(); if (!$result) { return json_fail('修改失败'); } else { $logSubject = '编辑一个公告, 名称:' . $item->title; $logSvalue = $item->uid; $logDbData = [ 'after_items' => [ $item->toArray(), ], 'before_items' => [ $oldItem->toArray(), ], ]; $this->log($logSubject, $logSvalue, $logDbData); return new CommonSuccessUidResponse($item->uid); } } /** * 应用删除 * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse * @throws \Illuminate\Contracts\Container\BindingResolutionException */ public function delete(Validator $validatorFactory, AnnounceRepository $repo) { $inputData = $this->getJsonData(); $rules = [ 'uid' => 'required', ]; $validator = Validator::make($inputData,$rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $item = $repo->findBy('uid', $inputData['uid']); if (empty($item)) { return json_fail(trans('common.err_resource_not_found')); } $oldItem = clone $item; $result = $item->delete(); if (!$result) { return json_fail('删除失败'); } else { $logSubject = '删除一个应用,名称:' . $item->title; $logSvalue = $item->uid; $logDbData = [ 'after_items' => [ ], 'before_items' => [ $oldItem->toArray(), ], ]; $this->log($logSubject, $logSvalue, $logDbData); return new CommonSuccessUidResponse($item->uid); } } /** * 批量操作应用 * @return \Illuminate\Http\JsonResponse|mixed */ public function batch(Validator $validatorFactory, AnnounceRepository $repo) { $inputData = $this->getJsonData(); $operateName = $inputData['operate_name'] ?? ''; switch ($operateName) { case 'delete': $itemPks = $inputData['item_pks'] ?? []; if (!empty($itemPks)) { $resultItems = $repo->makeModel()->query()->whereIn('uid', $itemPks)->delete(); if ($resultItems) { $strPks = implode(',', $itemPks); $logSubject = '批量删除多个公告'; $logSvalue = $strPks; $logDbData = [ 'after_items' => [ ], 'before_items' => [ ], ]; $this->log($logSubject, $logSvalue, $logDbData); } } $data = [ 'result' => 'success', 'pks' => $itemPks, ]; return json_success($data); break; default: break; } return json_fail('未知操作'); } }