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.
219 lines
7.9 KiB
219 lines
7.9 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\AdminUser;
|
|
use App\Models\ResearchDirection;
|
|
use App\Models\Role;
|
|
use App\Models\University;
|
|
use App\Support\ApiResponse;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
class GridMemberController extends Controller
|
|
{
|
|
use ApiResponse;
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$roleId = Role::query()->where('code', 'grid_member')->value('id');
|
|
|
|
$query = AdminUser::query()
|
|
->with(['roles', 'universities', 'researchDirections'])
|
|
->when($roleId, fn ($q) => $q->whereHas('roles', fn ($rq) => $rq->where('roles.id', $roleId)));
|
|
|
|
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(fn (AdminUser $u) => $this->serializeList($u));
|
|
|
|
return $this->paginated($paginator);
|
|
}
|
|
|
|
public function show(int $gridMember): JsonResponse
|
|
{
|
|
$user = $this->findGridMember($gridMember);
|
|
$user->load(['roles', 'universities', 'researchDirections']);
|
|
|
|
return $this->ok($this->serializeDetail($user));
|
|
}
|
|
|
|
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'],
|
|
'university_ids' => ['required', 'array', 'min:1'],
|
|
'university_ids.*' => ['integer', 'exists:universities,id'],
|
|
'research_direction_ids' => ['required', 'array', 'min:1'],
|
|
'research_direction_ids.*' => ['integer', 'exists:research_directions,id'],
|
|
]);
|
|
|
|
$role = Role::query()->where('code', 'grid_member')->first();
|
|
if (! $role) {
|
|
return $this->fail('网格员角色未配置,请执行 SystemExtensionSeeder', 422);
|
|
}
|
|
|
|
$admin = DB::transaction(function () use ($data, $role) {
|
|
$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'],
|
|
]);
|
|
|
|
$admin->roles()->sync([$role->id]);
|
|
$this->syncScope($admin, $data['university_ids'], $data['research_direction_ids']);
|
|
|
|
return $admin;
|
|
});
|
|
|
|
return $this->ok(['id' => $admin->id], '已创建');
|
|
}
|
|
|
|
public function update(Request $request, int $gridMember): JsonResponse
|
|
{
|
|
$admin = $this->findGridMember($gridMember);
|
|
|
|
$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'],
|
|
'university_ids' => ['sometimes', 'array', 'min:1'],
|
|
'university_ids.*' => ['integer', 'exists:universities,id'],
|
|
'research_direction_ids' => ['sometimes', 'array', 'min:1'],
|
|
'research_direction_ids.*' => ['integer', 'exists:research_directions,id'],
|
|
]);
|
|
|
|
DB::transaction(function () use ($admin, $data) {
|
|
$admin->fill([
|
|
'real_name' => $data['real_name'] ?? $admin->real_name,
|
|
'mobile' => array_key_exists('mobile', $data) ? $data['mobile'] : $admin->mobile,
|
|
'email' => array_key_exists('email', $data) ? $data['email'] : $admin->email,
|
|
'status' => isset($data['status']) ? (int) $data['status'] : $admin->status,
|
|
]);
|
|
$admin->save();
|
|
|
|
if (array_key_exists('university_ids', $data) || array_key_exists('research_direction_ids', $data)) {
|
|
$uniIds = $data['university_ids'] ?? $admin->universities()->pluck('universities.id')->all();
|
|
$dirIds = $data['research_direction_ids'] ?? $admin->researchDirections()->pluck('research_directions.id')->all();
|
|
$this->syncScope($admin, $uniIds, $dirIds);
|
|
}
|
|
});
|
|
|
|
return $this->ok(null, '已保存');
|
|
}
|
|
|
|
public function resetPassword(Request $request, int $gridMember): JsonResponse
|
|
{
|
|
$data = $request->validate([
|
|
'password' => ['required', 'string', 'min:6', 'max:255'],
|
|
]);
|
|
|
|
$admin = $this->findGridMember($gridMember);
|
|
$admin->forceFill([
|
|
'password_hash' => Hash::make($data['password']),
|
|
])->save();
|
|
|
|
return $this->ok(null, '密码已重置');
|
|
}
|
|
|
|
public function destroy(Request $request, int $gridMember): JsonResponse
|
|
{
|
|
$admin = $this->findGridMember($gridMember);
|
|
|
|
if ($admin->id === $request->user()->id) {
|
|
return $this->fail('不能删除当前登录账号', 422);
|
|
}
|
|
|
|
$admin->universities()->detach();
|
|
$admin->researchDirections()->detach();
|
|
$admin->roles()->detach();
|
|
$admin->delete();
|
|
|
|
return $this->ok(null, '已删除');
|
|
}
|
|
|
|
protected function findGridMember(int $id): AdminUser
|
|
{
|
|
$roleId = Role::query()->where('code', 'grid_member')->value('id');
|
|
|
|
return AdminUser::query()
|
|
->when($roleId, fn ($q) => $q->whereHas('roles', fn ($rq) => $rq->where('roles.id', $roleId)))
|
|
->findOrFail($id);
|
|
}
|
|
|
|
/**
|
|
* @param array<int> $universityIds
|
|
* @param array<int> $directionIds
|
|
*/
|
|
protected function syncScope(AdminUser $admin, array $universityIds, array $directionIds): void
|
|
{
|
|
$uniIds = University::query()->whereIn('id', $universityIds)->where('status', 1)->pluck('id');
|
|
$dirIds = ResearchDirection::query()->whereIn('id', $directionIds)->where('status', 1)->pluck('id');
|
|
|
|
$admin->universities()->sync($uniIds);
|
|
$admin->researchDirections()->sync($dirIds);
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function serializeList(AdminUser $u): array
|
|
{
|
|
return [
|
|
'id' => $u->id,
|
|
'username' => $u->username,
|
|
'real_name' => $u->real_name,
|
|
'mobile' => $u->mobile,
|
|
'email' => $u->email,
|
|
'status' => (int) $u->status,
|
|
'universities' => $u->universities->map(fn ($uni) => [
|
|
'id' => $uni->id,
|
|
'name' => $uni->name,
|
|
])->values()->all(),
|
|
'research_directions' => $u->researchDirections->map(fn ($d) => [
|
|
'id' => $d->id,
|
|
'name' => $d->name,
|
|
])->values()->all(),
|
|
'created_at' => $u->created_at?->toIso8601String(),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function serializeDetail(AdminUser $u): array
|
|
{
|
|
$row = $this->serializeList($u);
|
|
$row['university_ids'] = $u->universities->pluck('id')->values()->all();
|
|
$row['research_direction_ids'] = $u->researchDirections->pluck('id')->values()->all();
|
|
|
|
return $row;
|
|
}
|
|
}
|