You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.7 KiB

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\AdminUser;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
class AdminUserController extends Controller
{
use ApiResponse;
public function index(Request $request): JsonResponse
{
$query = AdminUser::query()->with('roles');
if ($kw = $request->query('keyword')) {
$query->where(function ($q) use ($kw) {
$q->where('username', 'like', "%{$kw}%")
->orWhere('real_name', 'like', "%{$kw}%")
->orWhere('mobile', 'like', "%{$kw}%");
});
}
if ($request->filled('status')) {
$query->where('status', (int) $request->query('status'));
}
$paginator = $query
->orderByDesc('id')
->paginate((int) $request->query('page_size', 20))
->withQueryString();
$paginator->getCollection()->transform(function (AdminUser $u) {
return [
'id' => $u->id,
'username' => $u->username,
'real_name' => $u->real_name,
'mobile' => $u->mobile,
'email' => $u->email,
'status' => (int) $u->status,
'last_login_at' => $u->last_login_at?->toIso8601String(),
'last_login_ip' => $u->last_login_ip,
'roles' => $u->roles->map(fn ($r) => ['id' => $r->id, 'name' => $r->name, 'code' => $r->code]),
'created_at' => $u->created_at?->toIso8601String(),
];
});
return $this->paginated($paginator);
}
public function store(Request $request): JsonResponse
{
$data = $request->validate([
'username' => ['required', 'string', 'max:64', 'unique:admin_users,username'],
'password' => ['required', 'string', 'min:6', 'max:255'],
'real_name' => ['nullable', 'string', 'max:64'],
'mobile' => ['nullable', 'string', 'max:20'],
'email' => ['nullable', 'email', 'max:128'],
'status' => ['required', 'integer', 'in:0,1'],
'role_ids' => ['nullable', 'array'],
'role_ids.*' => ['integer', 'exists:roles,id'],
]);
$admin = AdminUser::query()->create([
'username' => $data['username'],
'password_hash' => Hash::make($data['password']),
'real_name' => $data['real_name'] ?? null,
'mobile' => $data['mobile'] ?? null,
'email' => $data['email'] ?? null,
'status' => (int) $data['status'],
]);
if (! empty($data['role_ids'])) {
$admin->roles()->sync($data['role_ids']);
}
return $this->ok(['id' => $admin->id], '已创建');
}
public function update(Request $request, int $adminUser): JsonResponse
{
$model = AdminUser::query()->findOrFail($adminUser);
$data = $request->validate([
'real_name' => ['nullable', 'string', 'max:64'],
'mobile' => ['nullable', 'string', 'max:20'],
'email' => ['nullable', 'email', 'max:128'],
'status' => ['sometimes', 'integer', 'in:0,1'],
'role_ids' => ['nullable', 'array'],
'role_ids.*' => ['integer', 'exists:roles,id'],
]);
$model->fill([
'real_name' => $data['real_name'] ?? $model->real_name,
'mobile' => array_key_exists('mobile', $data) ? $data['mobile'] : $model->mobile,
'email' => array_key_exists('email', $data) ? $data['email'] : $model->email,
'status' => isset($data['status']) ? (int) $data['status'] : $model->status,
]);
$model->save();
if (array_key_exists('role_ids', $data)) {
$model->roles()->sync($data['role_ids'] ?? []);
}
return $this->ok(null, '已保存');
}
public function resetPassword(Request $request, int $adminUser): JsonResponse
{
$data = $request->validate([
'password' => ['required', 'string', 'min:6', 'max:255'],
]);
$model = AdminUser::query()->findOrFail($adminUser);
$model->forceFill([
'password_hash' => Hash::make($data['password']),
])->save();
return $this->ok(null, '密码已重置');
}
public function destroy(Request $request, int $adminUser): JsonResponse
{
$model = AdminUser::query()->findOrFail($adminUser);
if ($model->id === $request->user()->id) {
return $this->fail('不能删除当前登录账号', 422);
}
$model->delete();
return $this->ok(null, '已删除');
}
}