hyperfk8s/app/Logic/Admin/AdminLogic.php

192 lines
4.3 KiB
PHP
Executable File

<?php
declare(strict_types=1);
/**
* This file is part of the youyao/admin-api.
* (c) youyao <info@nuancebiotech.cn>
* This source file is subject to the license under the project that is bundled.
*/
namespace App\Logic\Admin;
//use App\Constants\Status;
use App\Exception\BusinessException;
//use App\Kernel\Password\Password;
//use App\Kernel\Jwt\Jwt;
//use App\Logic\Company\CompanyLogic;
use App\Model\Menu;
use App\Repository\Admin\UserRepository;
use App\Repository\Menu\MenuRepository;
use App\Repository\RoleMenu\RoleMenuRepository;
use Hyperf\Di\Annotation\Inject;
//use Youyao\Framework\ErrCode;
class AdminLogic
{
/**
* @Inject
* @var UserRepository
*/
private $user;
/**
* @Inject
* @var MenuRepository
*/
private $menu;
/**
* @Inject
* @var RoleMenuRepository
*/
private $roleMenu;
// /**
// * @Inject
// * @var Jwt
// */
// private $jwt;
// /**
// * @Inject
// * @var Password
// */
// private $password;
public function getUserInfo($id)
{
return [
'username' => '测试账户',
'name' => '测试账户',
'avatar' => 'avatar',
'introduction' => 'introduction',
'company_name' => 'company_name'
];
// return $this->user->getUserByIDS();
}
# 还差数据表 + 安装jwt
// public function login($username, $password): ?string
// {
// $user = $this->user->getByUsername($username);
//
// if (empty($user) || ! $this->password->verify($password, $user->password)) {
// throw new BusinessException('用户名或密码错误', ErrCode::Unauthorized);
// }
// if ($user->status !== Status::REGULAR) {
// throw new BusinessException('用户已被禁用,请联系管理员', ErrCode::Unauthorized);
// }
// if (!CompanyLogic::validateUserCompany($user)) {
// throw new BusinessException('公司帐户已被禁用或不在服务期限内,请联系平台', ErrCode::Unauthorized);
// }
//
// $token = $this->jwt->generate($user);
// if (! $token) {
// throw new BusinessException('登录失败,请稍后重试', ErrCode::Unauthorized);
// }
//
// return $token;
// }
// public function getUserMenuTree(User $user): array
public function getUserMenuTree(): array
{
// $menus = $this->getUserMenus($user);
$menus = $this->getUserMenus();
$vueMenus = $actions = [];
foreach ($menus as $menu) {
if ($menu['type'] === 3) { # 按钮
$actions[] = $menu['action'];
} else {
$vueMenus[] = $menu;
}
}
return [
'menus' => $this->getMenuTree($vueMenus),
'actions' => $actions,
];
}
// protected function getUserMenus(User $user)
protected function getUserMenus()
{
// $roles = $this->user->getUserValidRoles($user);
$roles = $this->user->getUserValidRoles();
$roleIds = $roles->pluck('id')->unique()->all();
$menuIds = $this->roleMenu->getMenuIdsByRoleIds($roleIds);
return $this->menu->getByIds($menuIds);
}
# -------------------------------------------
/**
* 构建菜单
* @param array $menus
* @return array
*/
public function getMenuTree(array $menus)
{
$data = [];
foreach ($menus as $menu) {
if ($menu['pid'] === 0) {
$data[] = $this->getChildren($menus, $menu);
}
}
return $data;
}
/**
* 构建子菜单
* @param $menus
* @param $menu
* @return mixed
*/
public function getChildren($menus, $menu)
{
$c = [];
foreach ($menus as $children) {
if ($menu['id'] === $children['pid']) {
$c[] = $this->getChildren($menus, $children);
}
}
$menu['children'] = $c;
$menu['api'] = $this->formatApi($menu['api']);
return $menu;
}
/**
* 构建菜单中api
* @param $api
* @return array|false|string[]
*/
public function formatApi($api)
{
if ($api) {
return explode(';', $api);
}
return [];
}
}