更新
This commit is contained in:
@@ -0,0 +1,250 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\adminapi\logic\auth;
|
||||
|
||||
|
||||
use app\common\enum\YesNoEnum;
|
||||
use app\common\logic\BaseLogic;
|
||||
use app\common\model\auth\Admin;
|
||||
use app\common\model\auth\SystemMenu;
|
||||
use app\common\model\auth\SystemRoleMenu;
|
||||
use app\common\service\qywx\QywxPromotionOperatorAccess;
|
||||
use app\adminapi\service\AdminAccessSyncService;
|
||||
use think\facade\Db;
|
||||
use app\common\service\ActiveMenuPermissions;
|
||||
|
||||
|
||||
/**
|
||||
* 系统菜单
|
||||
* Class MenuLogic
|
||||
* @package app\adminapi\logic\auth
|
||||
*/
|
||||
class MenuLogic extends BaseLogic
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取管理员对应的角色菜单
|
||||
* @param $adminId
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author 段誉
|
||||
* @date 2022/7/1 10:50
|
||||
*/
|
||||
public static function getMenuByAdminId($adminId)
|
||||
{
|
||||
$admin = Admin::findOrEmpty($adminId);
|
||||
|
||||
$where = [];
|
||||
$where[] = ['type', 'in', ['M', 'C']];
|
||||
$where[] = ['is_disable', '=', 0];
|
||||
$where[] = ['id', 'in', ActiveMenuPermissions::ids()];
|
||||
|
||||
if ($admin['root'] != 1) {
|
||||
$roleMenu = SystemRoleMenu::whereIn('role_id', $admin['role_id'])->column('menu_id');
|
||||
if (QywxPromotionOperatorAccess::hasSharedPagePermission((int) $adminId)) {
|
||||
$roleMenu = array_values(array_unique(array_merge(
|
||||
array_map('intval', $roleMenu),
|
||||
self::sharedPromotionMenuIds()
|
||||
)));
|
||||
}
|
||||
$where[] = ['id', 'in', $roleMenu];
|
||||
}
|
||||
|
||||
$menu = SystemMenu::where($where)
|
||||
->order(['sort' => 'desc', 'id' => 'asc'])
|
||||
->select();
|
||||
|
||||
return linear_to_tree($menu, 'children');
|
||||
}
|
||||
|
||||
|
||||
/** @return list<int> */
|
||||
private static function sharedPromotionMenuIds(): array
|
||||
{
|
||||
$menus = SystemMenu::where('is_disable', 0)
|
||||
->field('id,pid,perms')
|
||||
->select()
|
||||
->toArray();
|
||||
$byId = [];
|
||||
$pageIds = [];
|
||||
foreach ($menus as $menu) {
|
||||
$id = (int) ($menu['id'] ?? 0);
|
||||
if ($id <= 0) {
|
||||
continue;
|
||||
}
|
||||
$byId[$id] = $menu;
|
||||
if ((string) ($menu['perms'] ?? '') === QywxPromotionOperatorAccess::PAGE_PERMISSION) {
|
||||
$pageIds[] = $id;
|
||||
}
|
||||
}
|
||||
|
||||
$result = [];
|
||||
foreach ($pageIds as $pageId) {
|
||||
$chain = [];
|
||||
$seen = [];
|
||||
$currentId = $pageId;
|
||||
while ($currentId > 0) {
|
||||
if (isset($seen[$currentId]) || !isset($byId[$currentId])) {
|
||||
$chain = [];
|
||||
break;
|
||||
}
|
||||
$seen[$currentId] = true;
|
||||
$chain[] = $currentId;
|
||||
$currentId = (int) ($byId[$currentId]['pid'] ?? 0);
|
||||
}
|
||||
$result = array_merge($result, $chain);
|
||||
}
|
||||
|
||||
return array_values(array_unique($result));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 添加菜单
|
||||
* @param array $params
|
||||
* @return SystemMenu|\think\Model
|
||||
* @author 段誉
|
||||
* @date 2022/6/30 10:06
|
||||
*/
|
||||
public static function add(array $params)
|
||||
{
|
||||
return SystemMenu::create([
|
||||
'pid' => $params['pid'],
|
||||
'type' => $params['type'],
|
||||
'name' => $params['name'],
|
||||
'icon' => $params['icon'] ?? '',
|
||||
'sort' => $params['sort'],
|
||||
'perms' => $params['perms'] ?? '',
|
||||
'paths' => $params['paths'] ?? '',
|
||||
'component' => $params['component'] ?? '',
|
||||
'selected' => $params['selected'] ?? '',
|
||||
'params' => $params['params'] ?? '',
|
||||
'is_cache' => $params['is_cache'],
|
||||
'is_show' => $params['is_show'],
|
||||
'is_disable' => $params['is_disable'],
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 编辑菜单
|
||||
* @param array $params
|
||||
* @return SystemMenu
|
||||
* @author 段誉
|
||||
* @date 2022/6/30 10:07
|
||||
*/
|
||||
public static function edit(array $params)
|
||||
{
|
||||
return self::mutate((int) $params['id'], static fn () => SystemMenu::update([
|
||||
'id' => $params['id'],
|
||||
'pid' => $params['pid'],
|
||||
'type' => $params['type'],
|
||||
'name' => $params['name'],
|
||||
'icon' => $params['icon'] ?? '',
|
||||
'sort' => $params['sort'],
|
||||
'perms' => $params['perms'] ?? '',
|
||||
'paths' => $params['paths'] ?? '',
|
||||
'component' => $params['component'] ?? '',
|
||||
'selected' => $params['selected'] ?? '',
|
||||
'params' => $params['params'] ?? '',
|
||||
'is_cache' => $params['is_cache'],
|
||||
'is_show' => $params['is_show'],
|
||||
'is_disable' => $params['is_disable'],
|
||||
]));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 详情
|
||||
* @param $params
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/6/30 9:54
|
||||
*/
|
||||
public static function detail($params)
|
||||
{
|
||||
return SystemMenu::findOrEmpty($params['id'])->toArray();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 删除菜单
|
||||
* @param $params
|
||||
* @author 段誉
|
||||
* @date 2022/6/30 9:47
|
||||
*/
|
||||
public static function delete($params)
|
||||
{
|
||||
self::mutate((int) $params['id'], static function () use ($params): void {
|
||||
SystemMenu::destroy($params['id']);
|
||||
SystemRoleMenu::where(['menu_id' => $params['id']])->delete();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 更新状态
|
||||
* @param array $params
|
||||
* @return SystemMenu
|
||||
* @author 段誉
|
||||
* @date 2022/7/6 17:02
|
||||
*/
|
||||
public static function updateStatus(array $params)
|
||||
{
|
||||
return self::mutate((int) $params['id'], static fn () => SystemMenu::update([
|
||||
'id' => $params['id'],
|
||||
'is_disable' => $params['is_disable']
|
||||
]));
|
||||
}
|
||||
|
||||
private static function mutate(int $menuId, callable $operation)
|
||||
{
|
||||
$adminIds = [];
|
||||
$tokens = [];
|
||||
$result = Db::transaction(static function () use ($menuId, $operation, &$adminIds, &$tokens) {
|
||||
SystemMenu::where('id', $menuId)->lock(true)->findOrEmpty();
|
||||
$adminIds = AdminAccessSyncService::menuAdminIds($menuId);
|
||||
$tokens = AdminAccessSyncService::revoke($adminIds);
|
||||
return $operation();
|
||||
});
|
||||
AdminAccessSyncService::clearCommitted($adminIds, $tokens);
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 全部数据
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author 段誉
|
||||
* @date 2022/10/13 11:03
|
||||
*/
|
||||
public static function getAllData()
|
||||
{
|
||||
$data = SystemMenu::where(['is_disable' => YesNoEnum::NO])
|
||||
->field('id,pid,name')
|
||||
->order(['sort' => 'desc', 'id' => 'desc'])
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
return linear_to_tree($data, 'children');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user