mirror of
https://gitee.com/ctexthuang/hyperf-micro-svc.git
synced 2026-02-08 10:20:16 +08:00
132 lines
3.5 KiB
PHP
132 lines
3.5 KiB
PHP
<?php
|
|
/**
|
|
* This service file is part of item.
|
|
*
|
|
* @author ctexthuang
|
|
* @contact ctexthuang@qq.com
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service\Admin\AdminUser;
|
|
|
|
use App\Common\Repository\AdminMenuRepository;
|
|
use App\Common\Repository\AdminRoleRepository;
|
|
use App\Common\Trait\AdminUserTrait;
|
|
use App\Constants\Model\AdminUser\AdminMenuStatusCode;
|
|
use App\Constants\Model\AdminUser\AdminRoleStatusCode;
|
|
use App\Constants\ResultCode;
|
|
use App\Exception\ErrException;
|
|
use App\Service\Admin\BaseAdminService;
|
|
use Hyperf\Collection\Arr;
|
|
|
|
class PermissionService extends BaseAdminService
|
|
{
|
|
use AdminUserTrait;
|
|
|
|
/**
|
|
* @var AdminRoleRepository
|
|
*/
|
|
protected AdminRoleRepository $adminRoleRepository;
|
|
|
|
/**
|
|
* @var AdminMenuRepository
|
|
*/
|
|
protected AdminMenuRepository $adminMenuRepository;
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function handle(): array
|
|
{
|
|
return $this->adminReturn->success(
|
|
'success',
|
|
$this->getAdminUserInfo($this->adminId)->isSuperAdmin() ?
|
|
$this->getAdminMenuBySuperAdmin() :
|
|
$this->getAdminMenuByAdminId()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
private function getAdminMenuByAdminId(): array
|
|
{
|
|
$permissions = $this->getAdminUserInfo($this->adminId)->getPermissions()->pluck('name')->unique();
|
|
|
|
$menuList = $permissions->isEmpty() ?
|
|
[] :
|
|
$this->adminMenuRepository->list([
|
|
'status' => AdminMenuStatusCode::Normal,
|
|
'name' => $permissions->toArray()
|
|
])->toArray();
|
|
|
|
$tree = [];
|
|
$map = [];
|
|
|
|
foreach ($menuList as &$menu) {
|
|
$menu['children'] = [];
|
|
$map[$menu['id']] = &$menu;
|
|
}
|
|
unset($menu);
|
|
|
|
foreach ($menuList as &$menu) {
|
|
$pid = $menu['parent_id'];
|
|
if ($pid === 0 || !isset($map[$pid])) {
|
|
$tree[] = &$menu;
|
|
} else {
|
|
$map[$pid]['children'][] = &$menu;
|
|
}
|
|
}
|
|
unset($menu);
|
|
|
|
return $tree;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
private function getAdminMenuBySuperAdmin(): array
|
|
{
|
|
return $this->adminMenuRepository->list([
|
|
'status' => AdminMenuStatusCode::Normal,
|
|
'children' => true,
|
|
'parent_id' => 0,
|
|
])?->toArray() ?? [];
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function getRoleByAdminUser(): array
|
|
{
|
|
return $this->adminReturn->success(
|
|
'success',
|
|
$this->getAdminUserInfo($this->adminId)->isSuperAdmin() ?
|
|
$this->adminRoleRepository->list(['status' => AdminRoleStatusCode::Normal])->toArray() :
|
|
$this->getAdminUserInfo($this->adminId)->getRoles(['name', 'code', 'remark'])->toArray()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function update(): array
|
|
{
|
|
$userInfo = $this->getAdminUserInfo($this->adminId);
|
|
if (!$userInfo) throw new ErrException('用户不存在');
|
|
|
|
$data = $this->getRequestData();
|
|
|
|
if (Arr::exists($data, 'new_password')) {
|
|
if (!$userInfo->verifyPassword(Arr::get($data,'old_password')))
|
|
throw new ErrException('旧密码错误',ResultCode::OLD_PASSWORD_ERROR);
|
|
|
|
$data['password'] = $data['new_password'];
|
|
}
|
|
|
|
if (!$this->adminUserRepository->updateById($userInfo->id, $data)) throw new ErrException('更新失败');
|
|
|
|
return $this->adminReturn->success();
|
|
}
|
|
} |