order('sort', 'asc') ->field('id, title, desc, url, image, sort, create_time, update_time') ->select() ->toArray(); // 记录操作日志 $this->logSuccess('Banner管理', '获取所有Banner', ['data' => $banners]); return json([ 'code' => 200, 'msg' => '获取成功', 'data' => $banners ]); } catch (DbException $e) { // 记录失败日志 $this->logFail('Banner管理', '获取所有Banner', $e->getMessage()); return json([ 'code' => 500, 'msg' => '获取失败:' . $e->getMessage(), 'data' => [] ]); } } /** * 创建Banner * @return Json */ public function createBanner() { try { // 获取请求参数 $data = $this->request->param(); // 验证参数 $this->validate($data, [ 'title|标题' => 'require|max:255', 'desc|简介' => 'max:65535', 'url|跳转地址' => 'max:65535', 'image|图片地址' => 'max:65535', 'sort|排序号' => 'integer', ]); // 准备Banner数据 $bannerData = [ 'title' => $data['title'], 'desc' => $data['desc'] ?? '', 'url' => $data['url'] ?? '', 'image' => $data['image'] ?? '', 'sort' => $data['sort'] ?? 0, 'create_time' => date('Y-m-d H:i:s'), ]; // 创建Banner $result = Banner::insertGetId($bannerData); if ($result === false) { return json([ 'code' => 500, 'msg' => '创建失败' ]); } // 记录操作日志 $this->logSuccess('Banner管理', '创建Banner', ['id' => $result]); return json([ 'code' => 200, 'msg' => '创建成功', 'data' => ['id' => $result] ]); } catch (ValidateException $e) { // 记录失败日志 $this->logFail('Banner管理', '创建Banner', $e->getMessage()); return json([ 'code' => 400, 'msg' => $e->getError() ]); } catch (DbException $e) { // 记录失败日志 $this->logFail('Banner管理', '创建Banner', $e->getMessage()); return json([ 'code' => 500, 'msg' => '创建失败:' . $e->getMessage() ]); } } /** * 编辑Banner * @param int $id Banner ID * @return Json */ public function editBanner(int $id) { try { // 获取请求参数 $data = $this->request->param(); // 验证参数 $this->validate($data, [ 'title|标题' => 'require|max:255', 'desc|简介' => 'max:65535', 'url|跳转地址' => 'max:65535', 'image|图片地址' => 'max:65535', 'sort|排序号' => 'integer', ]); // 检查Banner是否存在 $banner = Banner::where('id', $id) ->where('delete_time', null) ->find(); if (!$banner) { return json([ 'code' => 404, 'msg' => 'Banner不存在' ]); } // 准备更新数据 $updateData = [ 'title' => $data['title'], 'desc' => $data['desc'] ?? null, 'url' => $data['url'] ?? null, 'image' => $data['image'] ?? null, 'sort' => $data['sort'] ?? 0, 'update_time' => date('Y-m-d H:i:s'), ]; // 执行更新 Banner::where('id', $id)->update($updateData); // 获取更新后的Banner信息 $updatedBanner = Banner::where('id', $id)->find(); // 记录操作日志 $this->logSuccess('Banner管理', '更新Banner', ['id' => $id]); return json([ 'code' => 200, 'msg' => '更新成功', 'data' => $updatedBanner ? $updatedBanner->toArray() : [] ]); } catch (ValidateException $e) { // 记录失败日志 $this->logFail('Banner管理', '更新Banner', $e->getMessage()); return json([ 'code' => 400, 'msg' => $e->getError() ]); } catch (DbException $e) { // 记录失败日志 $this->logFail('Banner管理', '更新Banner', $e->getMessage()); return json([ 'code' => 500, 'msg' => '更新失败:' . $e->getMessage() ]); } } /** * 删除Banner * @param int $id Banner ID * @return Json */ public function deleteBanner(int $id) { try { // 检查Banner是否存在 $banner = Banner::where('id', $id) ->where('delete_time', null) ->find(); if (!$banner) { return json([ 'code' => 404, 'msg' => 'Banner不存在' ]); } // 逻辑删除Banner $result = Banner::where('id', $id) ->where('delete_time', null) ->update([ 'delete_time' => time(), 'update_time' => time() ]); if ($result === false) { return json([ 'code' => 500, 'msg' => '删除失败' ]); } // 记录操作日志 $this->logSuccess('Banner管理', '删除Banner', ['id' => $id]); return json([ 'code' => 200, 'msg' => '删除成功' ]); } catch (DbException $e) { // 记录失败日志 $this->logFail('Banner管理', '删除Banner', $e->getMessage()); return json([ 'code' => 500, 'msg' => '删除失败:' . $e->getMessage() ]); } } }