redisCache->with()->set('123',1); $this->redisCache->with()->lua(RateLimit::class)->check('user:123', 10, 60); return $this->adminReturn->success( 'success', Arr::only( $this->getAdminUserInfo($this->adminId)?->toArray() ?: [], ['username', 'nickname', 'avatar', 'signed', 'backend_setting', 'phone', 'email'] ) ); } /** * @return array */ public function logout(): array { $this->tokenService->setJwt('admin')->getJwt()->addBlackList($this->getToken()); return $this->adminReturn->success(); } /** * @return array */ public function list(): array { return $this->adminReturn->success('success',$this->adminUserRepository->page( $this->getRequestData(), $this->getCurrentPage(), $this->getPageSize() )); } /** * @return array */ public function updateInfo(): array { $res = $this->adminUserRepository->updateById($this->adminId,Arr::except($this->getRequestData(),['password'])); if (!$res) throw new ErrException('修改失败'); return $this->adminReturn->success(); } /** * @return array */ public function resetPassword(): array { $adminUserInfo = $this->adminUserRepository->findById($this->adminId); if (!$adminUserInfo) throw new ErrException('用户异常'); $adminUserInfo->resetPassword(); if (!$adminUserInfo->save()) throw new ErrException('保存密码失败'); return $this->adminReturn->success(); } /** * @return array */ public function createUser(): array { if (! $this->adminUserRepository->create(array_merge( $this->getRequestData(), ['created_by' => $this->adminId] ))) throw new ErrException(); return $this->adminReturn->success(); } /** * @return array */ public function deleteUser(): array { if (! $this->adminUserRepository->deleteById($this->getRequestData())) throw new ErrException(); return $this->adminReturn->success(); } /** * @param int $userId * @return array */ public function saveUser(int $userId): array { if (! $this->adminUserRepository->updateById( $userId, array_merge( $this->getRequestData(), ['updated_by' => $this->adminId] ) )) throw new ErrException(); return $this->adminReturn->success(); } /** * @param int $userId * @return array */ public function getUserRole(int $userId): array { $userInfo = $this->adminUserRepository->findById($userId); if (!$userInfo) throw new ErrException('获取用户信息失败'); return $this->adminReturn->success( 'success', $userInfo->roles()->get()->map( static fn (AdminRole $adminRole) => $adminRole->only([ 'id','code','name' ]) )->toArray() ); } /** * @param int $userId * @return array */ public function batchGrantRoleForUser(int $userId): array { $userInfo = $this->adminUserRepository->findById($userId); if (!$userInfo) throw new ErrException('获取用户信息失败'); try { $userInfo->roles()->sync( $this->adminRoleRepository->list([ 'code' => $this->request->input('role_codes') ])->map(static function(AdminRole $adminRole) { return $adminRole->id; })->all() ); return $this->adminReturn->success(); } catch (\Throwable $e) { throw new ErrException($e->getMessage()); } } }