request->param('page', 1); $limit = (int)$this->request->param('limit', 10); $keyword = $this->request->param('keyword', ''); $query = Services::where('delete_time', null) ->where('tid', $this->getTenantId()); // 关键词搜索 if (!empty($keyword)) { $query->where('name', 'like', '%' . $keyword . '%'); } $total = $query->count(); $list = $query->order('sort', 'asc') ->order('id', 'desc') ->page($page, $limit) ->select() ->toArray(); return json([ 'code' => 200, 'msg' => 'success', 'data' => [ 'list' => $list, 'total' => $total ] ]); } catch (\Exception $e) { return json([ 'code' => 500, 'msg' => '获取失败:' . $e->getMessage(), 'data' => [] ]); } } /** * 添加特色服务 * @return Json */ public function addServices(): Json { try { $data = $this->request->param(); $service = new Services(); $service->tid = $this->getTenantId(); $service->title = $data['title']; $service->url = $data['url']; $service->thumb = $data['thumb'] ?? ''; $service->desc = $data['desc'] ?? ''; $service->sort = $data['sort'] ?? 0; $service->create_time = date('Y-m-d H:i:s'); $service->save(); $this->logSuccess('特色服务', '添加服务', ['id' => $service->id]); return json([ 'code' => 200, 'msg' => '添加成功', 'data' => $service->toArray() ]); } catch (ValidateException $e) { return json([ 'code' => 400, 'msg' => $e->getError() ]); } catch (\Exception $e) { $this->logFail('特色服务', '添加服务', $e->getMessage()); return json([ 'code' => 500, 'msg' => '添加失败:' . $e->getMessage() ]); } } /** * 更新特色服务 * @param int $id * @return Json */ public function editServices(int $id): Json { try { $data = $this->request->param(); $service = Services::where('id', $id) ->where('tid', $this->getTenantId()) ->where('delete_time', null) ->find(); if (!$service) { return json([ 'code' => 404, 'msg' => '服务不存在' ]); } if (isset($data['title'])) $service->title = $data['title']; if (isset($data['url'])) $service->url = $data['url']; if (isset($data['thumb'])) $service->thumb = $data['thumb']; if (isset($data['desc'])) $service->desc = $data['desc']; if (isset($data['sort'])) $service->sort = $data['sort']; $service->update_time = date('Y-m-d H:i:s'); $service->save(); $this->logSuccess('特色服务', '更新服务', ['id' => $id]); return json([ 'code' => 200, 'msg' => '更新成功', 'data' => $service->toArray() ]); } catch (ValidateException $e) { return json([ 'code' => 400, 'msg' => $e->getError() ]); } catch (\Exception $e) { $this->logFail('特色服务', '更新服务', $e->getMessage()); return json([ 'code' => 500, 'msg' => '更新失败:' . $e->getMessage() ]); } } /** * 删除特色服务 * @param int $id * @return Json */ public function deleteServices(int $id): Json { try { $service = Services::where('id', $id) ->where('tid', $this->getTenantId()) ->where('delete_time', null) ->find(); if (!$service) { return json([ 'code' => 404, 'msg' => '服务不存在' ]); } $service->delete(); $this->logSuccess('特色服务', '删除服务', ['id' => $id]); return json([ 'code' => 200, 'msg' => '删除成功' ]); } catch (\Exception $e) { $this->logFail('特色服务', '删除服务', $e->getMessage()); return json([ 'code' => 500, 'msg' => '删除失败:' . $e->getMessage() ]); } } }