116 lines
3.9 KiB
PHP
116 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Admin;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class AdminUserController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
return view('admin.system.admins');
|
|
}
|
|
|
|
public function data(Request $request)
|
|
{
|
|
$q = Admin::query();
|
|
$username = trim((string) $request->query('username', ''));
|
|
if ($username !== '') {
|
|
$q->where('username', 'like', '%'.$username.'%');
|
|
}
|
|
|
|
$sortable = ['id', 'username', 'is_super', 'created_at', 'updated_at'];
|
|
$field = (string) $request->query('field', 'id');
|
|
$order = strtolower((string) $request->query('order', 'desc')) === 'asc' ? 'asc' : 'desc';
|
|
if (! in_array($field, $sortable, true)) {
|
|
$field = 'id';
|
|
}
|
|
$q->orderBy($field, $order);
|
|
|
|
$limit = max(1, min(100, (int) $request->query('limit', 15)));
|
|
$page = max(1, (int) $request->query('page', 1));
|
|
$paginator = $q->paginate($limit, ['*'], 'page', $page);
|
|
|
|
$selfId = (int) auth('admin')->id();
|
|
$data = collect($paginator->items())->map(function (Admin $a) use ($selfId) {
|
|
return [
|
|
'id' => $a->id,
|
|
'username' => $a->username,
|
|
'is_super' => (int) $a->is_super,
|
|
'created_at' => optional($a->created_at)->format('Y-m-d H:i:s'),
|
|
'updated_at' => optional($a->updated_at)->format('Y-m-d H:i:s'),
|
|
'is_self' => $a->id === $selfId,
|
|
];
|
|
})->values();
|
|
|
|
return response()->json([
|
|
'code' => 0,
|
|
'msg' => '',
|
|
'count' => $paginator->total(),
|
|
'data' => $data,
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'username' => ['required', 'string', 'max:64', 'alpha_dash', Rule::unique('admins', 'username')],
|
|
'password' => ['required', 'string', 'min:6', 'max:128'],
|
|
'is_super' => ['nullable', 'integer', Rule::in([0, 1])],
|
|
]);
|
|
|
|
$admin = Admin::query()->create([
|
|
'username' => $data['username'],
|
|
'password' => $data['password'],
|
|
'is_super' => (int) ($data['is_super'] ?? 0),
|
|
]);
|
|
|
|
return response()->json(['code' => 0, 'msg' => 'ok', 'data' => ['id' => $admin->id]]);
|
|
}
|
|
|
|
public function update(Request $request, Admin $adminUser)
|
|
{
|
|
$data = $request->validate([
|
|
'password' => ['nullable', 'string', 'min:6', 'max:128'],
|
|
'is_super' => ['nullable', 'integer', Rule::in([0, 1])],
|
|
]);
|
|
|
|
if (array_key_exists('is_super', $data) && $data['is_super'] !== null) {
|
|
$newSuper = (int) $data['is_super'];
|
|
if ($adminUser->isSuper() && $newSuper === 0 && $this->superCount() <= 1) {
|
|
return response()->json(['code' => 1, 'msg' => '至少保留一名超级管理员'], 422);
|
|
}
|
|
$adminUser->is_super = $newSuper;
|
|
}
|
|
|
|
if (! empty($data['password'])) {
|
|
$adminUser->password = $data['password'];
|
|
}
|
|
$adminUser->save();
|
|
|
|
return response()->json(['code' => 0, 'msg' => 'ok']);
|
|
}
|
|
|
|
public function destroy(Admin $adminUser)
|
|
{
|
|
if ((int) $adminUser->id === (int) auth('admin')->id()) {
|
|
return response()->json(['code' => 1, 'msg' => '不能删除当前登录账号'], 422);
|
|
}
|
|
if ($adminUser->isSuper() && $this->superCount() <= 1) {
|
|
return response()->json(['code' => 1, 'msg' => '不能删除最后一名超级管理员'], 422);
|
|
}
|
|
|
|
$adminUser->delete();
|
|
|
|
return response()->json(['code' => 0, 'msg' => 'ok']);
|
|
}
|
|
|
|
private function superCount(): int
|
|
{
|
|
return (int) Admin::query()->where('is_super', 1)->count();
|
|
}
|
|
}
|