request->param('page', 1); $limit = (int)$this->request->param('limit', 10); $keyword = $this->request->param('keyword', ''); $status = $this->request->param('status', ''); $query = Services::where('delete_time', null) ->where('tid', $this->getTenantId()); // 关键词搜索 if (!empty($keyword)) { $query->where('name', 'like', '%' . $keyword . '%'); } // 状态筛选 if ($status !== '') { $query->where('status', (int)$status); } $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 add(): Json { try { $data = $this->request->param(); // 验证参数 $this->validate($data, [ 'name|服务名称' => 'require|max:100', 'url|服务地址' => 'require|url|max:255', 'sort|排序' => 'integer', 'status|状态' => 'in:0,1' ]); $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->status = $data['status'] ?? 1; $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 update(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['name'])) { $this->validate($data, [ 'name|服务名称' => 'require|max:100' ]); } if (isset($data['url'])) { $this->validate($data, [ 'url|服务地址' => 'require|url|max:255' ]); } 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']; if (isset($data['status'])) $service->status = $data['status']; $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 delete(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() ]); } } /** * 批量删除特色服务 * @return Json */ public function batchDelete(): Json { try { $ids = $this->request->param('ids', []); if (empty($ids)) { return json([ 'code' => 400, 'msg' => '请选择要删除的服务' ]); } Services::whereIn('id', $ids) ->where('tid', $this->getTenantId()) ->where('delete_time', null) ->update(['delete_time' => date('Y-m-d H:i:s')]); $this->logSuccess('特色服务', '批量删除服务', ['ids' => $ids]); return json([ 'code' => 200, 'msg' => '批量删除成功' ]); } catch (\Exception $e) { $this->logFail('特色服务', '批量删除服务', $e->getMessage()); return json([ 'code' => 500, 'msg' => '批量删除失败:' . $e->getMessage() ]); } } }