map(function ($item) { $item['groups'] = ServerGroup::whereIn('id', $item['group_ids'])->get(['name', 'id']); return $item; }); return $this->success($servers); } public function sort(Request $request) { ini_set('post_max_size', '1m'); $params = $request->validate([ '*.id' => 'numeric', '*.order' => 'numeric' ]); try { DB::beginTransaction(); collect($params)->each(function ($item) { if (isset($item['id']) && isset($item['order'])) { Server::where('id', $item['id'])->update(['sort' => $item['order']]); } }); DB::commit(); } catch (\Exception $e) { DB::rollBack(); \Log::error($e); return $this->fail([500, '保存失败']); } return $this->success(true); } public function save(ServerSave $request) { $params = $request->validated(); if ($request->input('id')) { $server = Server::find($request->input('id')); if (!$server) { return $this->fail([400202, '服务器不存在']); } try { $server->update($params); return $this->success(true); } catch (\Exception $e) { \Log::error($e); return $this->fail([500, '保存失败']); } } try { Server::create($params); return $this->success(true); } catch (\Exception $e) { \Log::error($e); return $this->fail([500, '创建失败']); } } public function update(Request $request) { $request->validate([ 'id' => 'required|integer', 'show' => 'integer', ]); if (Server::where('id', $request->id)->update(['show' => $request->show]) === false) { return $this->fail([500, '保存失败']); } return $this->success(true); } /** * 删除 * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function drop(Request $request) { $request->validate([ 'id' => 'required|integer', ]); if (Server::where('id', $request->id)->delete() === false) { return $this->fail([500, '删除失败']); } return $this->success(true); } /** * 复制节点 * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function copy(Request $request) { $server = Server::find($request->input('id')); $server->show = 0; $server->code = null; if (!$server) { return $this->fail([400202, '服务器不存在']); } Server::create($server->toArray()); return $this->success(true); } }