超管后台 - 编辑管理员
This commit is contained in:
@@ -11,20 +11,6 @@ class Administrator extends Model
|
||||
// 设置数据表名
|
||||
protected $name = 'administrators';
|
||||
|
||||
// 设置数据表前缀
|
||||
protected $prefix = 'tk_';
|
||||
|
||||
// 设置主键
|
||||
protected $pk = 'id';
|
||||
|
||||
// 自动写入时间戳
|
||||
protected $autoWriteTimestamp = true;
|
||||
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createTime';
|
||||
protected $updateTime = 'updateTime';
|
||||
protected $deleteTime = 'deleteTime';
|
||||
|
||||
// 隐藏字段
|
||||
protected $hidden = [
|
||||
'password'
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
namespace app\superadmin\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 超级管理员权限配置模型类
|
||||
*/
|
||||
class AdministratorPermissions extends Model
|
||||
{
|
||||
// 设置数据表名
|
||||
protected $name = 'administrator_permissions';
|
||||
|
||||
// 设置主键
|
||||
protected $pk = 'id';
|
||||
|
||||
// 自动写入时间戳
|
||||
protected $autoWriteTimestamp = true;
|
||||
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createTime';
|
||||
protected $updateTime = 'updateTime';
|
||||
protected $deleteTime = 'deleteTime';
|
||||
|
||||
// 定义字段类型
|
||||
protected $type = [
|
||||
'id' => 'integer',
|
||||
'adminId' => 'integer',
|
||||
'permissions' => 'json',
|
||||
'createTime' => 'integer',
|
||||
'updateTime' => 'integer',
|
||||
'deleteTime' => 'integer'
|
||||
];
|
||||
|
||||
/**
|
||||
* 保存管理员权限
|
||||
* @param int $adminId 管理员ID
|
||||
* @param array $permissionIds 权限ID数组
|
||||
* @return bool
|
||||
*/
|
||||
public static function savePermissions($adminId, $permissionIds)
|
||||
{
|
||||
// 检查是否已有记录
|
||||
$record = self::where('adminId', $adminId)->find();
|
||||
|
||||
// 准备权限数据
|
||||
$permissionData = [
|
||||
'ids' => is_array($permissionIds) ? implode(',', $permissionIds) : $permissionIds
|
||||
];
|
||||
|
||||
if ($record) {
|
||||
// 更新已有记录
|
||||
return $record->save([
|
||||
'permissions' => json_encode($permissionData),
|
||||
'updateTime' => time()
|
||||
]);
|
||||
} else {
|
||||
// 创建新记录
|
||||
return self::create([
|
||||
'adminId' => $adminId,
|
||||
'permissions' => json_encode($permissionData),
|
||||
'createTime' => time(),
|
||||
'updateTime' => time(),
|
||||
'deleteTime' => 0
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取管理员权限
|
||||
* @param int $adminId 管理员ID
|
||||
* @return array 权限ID数组
|
||||
*/
|
||||
public static function getPermissions($adminId)
|
||||
{
|
||||
$record = self::where('adminId', $adminId)->find();
|
||||
|
||||
if (!$record || empty($record->permissions)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$permissions = $record->permissions ? json_decode($record->permissions, true) : [];
|
||||
|
||||
if (isset($permissions['ids']) && !empty($permissions['ids'])) {
|
||||
return is_string($permissions['ids']) ? explode(',', $permissions['ids']) : $permissions['ids'];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -12,19 +12,6 @@ class Menu extends Model
|
||||
// 设置数据表名
|
||||
protected $name = 'menus';
|
||||
|
||||
// 设置数据表前缀
|
||||
protected $prefix = 'tk_';
|
||||
|
||||
// 设置主键
|
||||
protected $pk = 'id';
|
||||
|
||||
// 自动写入时间戳
|
||||
protected $autoWriteTimestamp = true;
|
||||
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'create_time';
|
||||
protected $updateTime = 'update_time';
|
||||
|
||||
/**
|
||||
* 获取所有菜单,并组织成树状结构
|
||||
* @param bool $onlyEnabled 是否只获取启用的菜单
|
||||
@@ -35,11 +22,6 @@ class Menu extends Model
|
||||
{
|
||||
$cacheKey = 'superadmin_menu_tree' . ($onlyEnabled ? '_enabled' : '_all');
|
||||
|
||||
// 如果使用缓存并且缓存中有数据,则直接返回缓存数据
|
||||
// if ($useCache && Cache::has($cacheKey)) {
|
||||
// return Cache::get($cacheKey);
|
||||
// }
|
||||
|
||||
// 查询条件
|
||||
$where = [];
|
||||
if ($onlyEnabled) {
|
||||
@@ -62,7 +44,12 @@ class Menu extends Model
|
||||
|
||||
return $menuTree;
|
||||
}
|
||||
|
||||
|
||||
public static function getMenusNameByIds($ids)
|
||||
{
|
||||
return self::whereIn('id', $ids)->column('title');
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建菜单树
|
||||
* @param array $menus 所有菜单
|
||||
@@ -85,60 +72,60 @@ class Menu extends Model
|
||||
|
||||
return $tree;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 清除菜单缓存
|
||||
* 根据权限ID获取相应的菜单树
|
||||
* @param array $permissionIds 权限ID数组
|
||||
* @param bool $onlyEnabled 是否只获取启用的菜单
|
||||
* @return array
|
||||
*/
|
||||
public static function clearMenuCache()
|
||||
public static function getMenuTreeByPermissions($permissionIds, $onlyEnabled = true)
|
||||
{
|
||||
Cache::delete('superadmin_menu_tree_enabled');
|
||||
Cache::delete('superadmin_menu_tree_all');
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加或更新菜单
|
||||
* @param array $data 菜单数据
|
||||
* @return bool
|
||||
*/
|
||||
public static function saveMenu($data)
|
||||
{
|
||||
if (isset($data['id']) && $data['id'] > 0) {
|
||||
// 更新
|
||||
$menu = self::find($data['id']);
|
||||
if (!$menu) {
|
||||
return false;
|
||||
// 如果没有权限,返回空数组
|
||||
if (empty($permissionIds)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// 查询条件
|
||||
$where = [];
|
||||
if ($onlyEnabled) {
|
||||
$where[] = ['status', '=', 1];
|
||||
}
|
||||
|
||||
// 获取所有一级菜单(用户拥有权限的)
|
||||
$topMenus = self::where($where)
|
||||
->where('parent_id', 0)
|
||||
->whereIn('id', $permissionIds)
|
||||
->order('sort', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 菜单ID集合,用于获取子菜单
|
||||
$menuIds = array_column($topMenus, 'id');
|
||||
|
||||
// 获取所有子菜单
|
||||
$childMenus = self::where($where)
|
||||
->where('parent_id', 'in', $menuIds)
|
||||
->order('sort', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 将子菜单按照父ID进行分组
|
||||
$childMenusGroup = [];
|
||||
foreach ($childMenus as $menu) {
|
||||
$childMenusGroup[$menu['parent_id']][] = $menu;
|
||||
}
|
||||
|
||||
// 构建菜单树
|
||||
$menuTree = [];
|
||||
foreach ($topMenus as $topMenu) {
|
||||
// 添加子菜单
|
||||
if (isset($childMenusGroup[$topMenu['id']])) {
|
||||
$topMenu['children'] = $childMenusGroup[$topMenu['id']];
|
||||
}
|
||||
$result = $menu->save($data);
|
||||
} else {
|
||||
// 新增
|
||||
$menu = new self();
|
||||
$result = $menu->save($data);
|
||||
$menuTree[] = $topMenu;
|
||||
}
|
||||
|
||||
// 清除缓存
|
||||
self::clearMenuCache();
|
||||
|
||||
return $result !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除菜单
|
||||
* @param int $id 菜单ID
|
||||
* @return bool
|
||||
*/
|
||||
public static function deleteMenu($id)
|
||||
{
|
||||
// 查找子菜单
|
||||
$childCount = self::where('parent_id', $id)->count();
|
||||
if ($childCount > 0) {
|
||||
return false; // 有子菜单不能删除
|
||||
}
|
||||
|
||||
$result = self::destroy($id);
|
||||
|
||||
// 清除缓存
|
||||
self::clearMenuCache();
|
||||
|
||||
return $result !== false;
|
||||
return $menuTree;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user