2026-02-26 21:10:34 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 文件功能:后台房间管理控制器
|
2026-03-03 14:36:09 +08:00
|
|
|
|
* 管理员可新增、编辑、删除房间信息(名称、介绍、公告等)
|
|
|
|
|
|
* 系统房间(room_keep = true)不允许删除
|
2026-02-26 21:10:34 +08:00
|
|
|
|
*
|
|
|
|
|
|
* @author ChatRoom Laravel
|
|
|
|
|
|
*
|
2026-03-03 14:36:09 +08:00
|
|
|
|
* @version 1.1.0
|
2026-02-26 21:10:34 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
|
use App\Models\Room;
|
|
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
use Illuminate\View\View;
|
|
|
|
|
|
|
|
|
|
|
|
class RoomManagerController extends Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 显示所有房间列表
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function index(): View
|
|
|
|
|
|
{
|
|
|
|
|
|
$rooms = Room::orderBy('id')->get();
|
|
|
|
|
|
|
|
|
|
|
|
return view('admin.rooms.index', compact('rooms'));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-03 14:36:09 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 新增房间
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function store(Request $request): RedirectResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
$data = $request->validate([
|
2026-03-12 15:26:54 +08:00
|
|
|
|
'room_name' => 'required|string|max:100|unique:rooms,room_name',
|
|
|
|
|
|
'room_des' => 'nullable|string|max:500',
|
|
|
|
|
|
'room_owner' => 'nullable|string|max:50',
|
2026-03-03 14:36:09 +08:00
|
|
|
|
'permit_level' => 'required|integer|min:0|max:15',
|
2026-03-12 15:26:54 +08:00
|
|
|
|
'door_open' => 'required|boolean',
|
2026-03-03 14:36:09 +08:00
|
|
|
|
], [
|
|
|
|
|
|
'room_name.unique' => '房间名称已存在,请换一个名称。',
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
// 设置新建房间的默认值
|
2026-03-12 15:26:54 +08:00
|
|
|
|
$data['room_keep'] = false; // 新建房间均为非系统房间,可删除
|
2026-03-03 14:36:09 +08:00
|
|
|
|
$data['build_time'] = now();
|
|
|
|
|
|
|
|
|
|
|
|
$room = Room::create($data);
|
|
|
|
|
|
|
|
|
|
|
|
return redirect()->route('admin.rooms.index')->with('success', "房间「{$room->room_name}」新建成功!");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-26 21:10:34 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 更新房间信息
|
2026-02-28 23:44:38 +08:00
|
|
|
|
*
|
|
|
|
|
|
* @param Room $room 路由模型自动注入
|
2026-02-26 21:10:34 +08:00
|
|
|
|
*/
|
2026-02-28 23:44:38 +08:00
|
|
|
|
public function update(Request $request, Room $room): RedirectResponse
|
2026-02-26 21:10:34 +08:00
|
|
|
|
{
|
|
|
|
|
|
$data = $request->validate([
|
2026-03-12 15:26:54 +08:00
|
|
|
|
'room_name' => 'required|string|max:100',
|
|
|
|
|
|
'room_des' => 'nullable|string|max:500',
|
2026-02-26 21:10:34 +08:00
|
|
|
|
'announcement' => 'nullable|string|max:500',
|
2026-03-12 15:26:54 +08:00
|
|
|
|
'room_owner' => 'nullable|string|max:50',
|
2026-02-26 21:10:34 +08:00
|
|
|
|
'permit_level' => 'required|integer|min:0|max:15',
|
2026-03-12 15:26:54 +08:00
|
|
|
|
'door_open' => 'required|boolean',
|
2026-02-26 21:10:34 +08:00
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
$room->update($data);
|
|
|
|
|
|
|
2026-03-03 14:36:09 +08:00
|
|
|
|
return redirect()->route('admin.rooms.index')->with('success', "房间「{$room->room_name}」信息已更新!");
|
2026-02-26 21:10:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-03 14:36:09 +08:00
|
|
|
|
* 删除房间(系统房间不允许删除)
|
2026-02-28 23:44:38 +08:00
|
|
|
|
*
|
|
|
|
|
|
* @param Room $room 路由模型自动注入
|
2026-02-26 21:10:34 +08:00
|
|
|
|
*/
|
2026-02-28 23:44:38 +08:00
|
|
|
|
public function destroy(Room $room): RedirectResponse
|
2026-02-26 21:10:34 +08:00
|
|
|
|
{
|
|
|
|
|
|
if ($room->room_keep) {
|
|
|
|
|
|
return redirect()->route('admin.rooms.index')->with('error', '系统房间不允许删除!');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-03 14:36:09 +08:00
|
|
|
|
$name = $room->room_name;
|
2026-02-26 21:10:34 +08:00
|
|
|
|
$room->delete();
|
|
|
|
|
|
|
2026-03-03 14:36:09 +08:00
|
|
|
|
return redirect()->route('admin.rooms.index')->with('success', "房间「{$name}」已删除!");
|
2026-02-26 21:10:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|