validate([ 'page' => ['sometimes', 'integer', 'min:1'], 'limit' => ['sometimes', 'integer', 'min:1', 'max:200'], 'type' => ['nullable', 'in:pc,app'], ]); $page = request()->input('page', 1); $limit = request()->input('limit', 15); $type = request()->input('type', ''); $query = BannerModel::query(); if (!empty($type)) { $query->where('type', $type); } $count = $query->count(); $list = BannerVisibilityService::decorate($query->forPage($page, $limit) ->orderByDesc('sort')->orderByDesc('id')->get()); } catch (ValidationException $e) { return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first()); } catch (Exception $e) { return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage()); } return $this->success(['total' => $count, 'data' => $list]); } /** * @api {post} /admin/banner/update 修改 * @apiDescription 更新或者新增都用这个接口 * @apiGroup 轮播图管理 * @apiUse result * @apiUse header * @apiVersion 1.0.0 * */ public function update() { DB::beginTransaction(); try { $params = request()->validate([ 'id' => ['nullable', 'integer', 'min:1'], 'title' => ['required'], 'image' => ['required'], 'link' => ['nullable', function ($attribute, $value, $fail) { if ((!is_string($value) && !is_int($value)) || strlen((string) $value) > 191) { $fail('跳转ID或链接格式错误'); } }], 'type' => ['required', 'in:pc,app'], 'sort' => ['nullable', 'integer'], 'lang' => ['required'], 'status' => ['sometimes', 'integer', 'in:0,1'], ]); $id = $params['id'] ?? null; $banner = $id === null ? new BannerModel() : BannerModel::where('id', $id)->lockForUpdate()->first(); if (!$banner) throw new Exception('数据不存在', HttpStatus::CUSTOM_ERROR); $link = array_key_exists('link', $params) ? $params['link'] : $banner->link; $activityId = BannerVisibilityService::activityId($link); $previousActivityId = BannerVisibilityService::activityId($banner->link); if ($activityId !== null && (!$banner->exists || $activityId !== $previousActivityId)) { if (!DB::table('activity_rewards')->where('id', $activityId)->exists()) { throw ValidationException::withMessages(['link' => '绑定的活动不存在']); } } $banner->fill([ 'title' => $params['title'], 'image' => Util::replacePartInUrl($params['image']), 'link' => $activityId ?? ($link === null ? null : (string) $link), 'type' => $params['type'], 'sort' => array_key_exists('sort', $params) ? ($params['sort'] ?? 0) : ($banner->sort ?? 0), 'lang' => $params['lang'], 'status' => $params['status'] ?? ($banner->status ?? 1), ])->save(); DB::commit(); } catch (ValidationException $e) { DB::rollBack(); return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first()); } catch (Exception $e) { DB::rollBack(); return $this->error(intval($e->getCode()), $e->getMessage()); } return $this->success(); } /** Change manual publication without resubmitting the banner form. */ public function status() { try { $params = request()->validate([ 'id' => ['required', 'integer', 'min:1'], 'status' => ['required', 'integer', 'in:0,1'], ]); $banner = BannerModel::find($params['id']); if (!$banner) throw new Exception('数据不存在', HttpStatus::CUSTOM_ERROR); $banner->status = $params['status']; $banner->save(); } catch (ValidationException $e) { return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first()); } catch (Exception $e) { return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage()); } return $this->success(); } /** * @api {post} /admin/banner/delete 删除 * @apiGroup 轮播图管理 * @apiUse result * @apiUse header * @apiVersion 1.0.0 * * @apiParam {int} id 要删除的ID * */ function delete() { DB::beginTransaction(); try { request()->validate([ 'id' => ['required', 'integer', 'min:1', 'max:99999999'], ]); $id = request()->input('id', 0); $info = BannerModel::where('id', $id)->first(); if (!$info) throw new Exception("数据不存在", HttpStatus::CUSTOM_ERROR); $info->delete(); DB::commit(); } catch (ValidationException $e) { DB::rollBack(); return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first()); } catch (Exception $e) { DB::rollBack(); return $this->error(intval($e->getCode())); } return $this->success(); } }