mirror of
https://gitee.com/zongzhige/shopxo.git
synced 2024-11-30 02:49:03 +08:00
插件新增权限管理
This commit is contained in:
parent
c5c20dbb52
commit
921d28f9ce
@ -84,6 +84,20 @@ class Plugins extends Common
|
||||
}
|
||||
}
|
||||
|
||||
// 插件权限校验
|
||||
$power_plugins = MyCache(MyConfig('shopxo.cache_admin_power_plugins_key').$this->admin['id']);
|
||||
if(empty($power_plugins) || !array_key_exists($params['data_request']['pluginsname'], $power_plugins))
|
||||
{
|
||||
$msg = '无权限使用该插件';
|
||||
if(IS_AJAX)
|
||||
{
|
||||
return DataReturn($msg, -5000);
|
||||
} else {
|
||||
MyViewAssign('msg', $msg);
|
||||
return MyView('public/tips_error');
|
||||
}
|
||||
}
|
||||
|
||||
// 应用名称/控制器/方法
|
||||
$pluginsname = $params['data_request']['pluginsname'];
|
||||
$pluginscontrol = strtolower($params['data_request']['pluginscontrol']);
|
||||
|
@ -69,7 +69,7 @@ class Pluginsadmin extends Common
|
||||
if($this->view_type == 'home')
|
||||
{
|
||||
// 插件列表
|
||||
$ret = PluginsAdminService::PluginsList();
|
||||
$ret = PluginsAdminService::PluginsList(['is_power'=>true]);
|
||||
MyViewAssign('data_list', $ret['data']);
|
||||
|
||||
// 插件更新信息
|
||||
|
@ -136,7 +136,7 @@ class Role extends Common
|
||||
}
|
||||
}
|
||||
|
||||
// 菜单列表
|
||||
// 权限列表
|
||||
$power = AdminRoleService::RolePowerEditData($params);
|
||||
MyViewAssign('power', $power);
|
||||
|
||||
@ -148,6 +148,7 @@ class Role extends Common
|
||||
'is_backend' => true,
|
||||
'role_id' => isset($params['id']) ? $params['id'] : 0,
|
||||
'data' => &$data,
|
||||
'power' => &$power,
|
||||
'params' => &$params,
|
||||
]));
|
||||
|
||||
|
@ -31,7 +31,7 @@
|
||||
</select>
|
||||
</div>
|
||||
<div class="am-form-group">
|
||||
<label>跳转url地址<span class="am-form-group-label-tips">带http://或https://</span></label>
|
||||
<label>跳转url地址<span class="am-form-group-label-tips">带http://或https://,仅web端有效</span></label>
|
||||
<input type="url" name="jump_url" placeholder="跳转url地址" data-validation-message="跳转url地址格式有误" class="am-radius" {{if !empty($data)}} value="{{$data.jump_url}}"{{/if}} />
|
||||
</div>
|
||||
<div class="am-form-group">
|
||||
|
@ -14,7 +14,7 @@
|
||||
|
||||
<!-- operation start -->
|
||||
<div class="am-g">
|
||||
<button class="am-btn am-btn-secondary am-radius am-btn-xs am-icon-plus submit-add" data-am-modal="{target: '#power-save-win'}"> 新增</button>
|
||||
<button class="am-btn am-btn-secondary am-radius am-btn-xs am-icon-plus submit-add"> 新增</button>
|
||||
</div>
|
||||
<!-- operation end -->
|
||||
|
||||
@ -86,41 +86,41 @@
|
||||
<!-- save win end -->
|
||||
|
||||
<!-- content start -->
|
||||
<ul class="tree-list am-padding-left-0 am-margin-top-sm">
|
||||
{{foreach $data as $v}}
|
||||
<li id="data-list-{{$v.id}}" {{if $v['is_show'] eq 0}}class="bk-cr-ffd"{{/if}}>
|
||||
<div class="am-padding-vertical-sm">
|
||||
<ul class="tree-list am-padding-left-0">
|
||||
{{foreach $data as $v}}
|
||||
<li id="data-list-{{$v.id}}">
|
||||
<div class="item-content am-padding-sm am-margin-bottom-xs am-margin-right-xs {{if $v['is_show'] eq 0}}bk-cr-ffd{{/if}}">
|
||||
{{if !empty($v['item'])}}
|
||||
<i class="stretch am-icon-plus am-margin-right-xs"></i>
|
||||
{{/if}}
|
||||
<span>{{if !empty($v['icon'])}}<span class="iconfont {{$v.icon}}"></span>{{/if}} {{$v.name}}</span>
|
||||
<span class="am-margin-left-sm">
|
||||
<a href="javascript:;" class="am-icon-edit submit-edit" data-json='{{:json_encode($v)}}' data-tag="power-save-win" data-item="{{if !empty($v['item'])}}ok{{else /}}no{{/if}}"></a>
|
||||
{{if empty($v['item'])}}
|
||||
<a href="javascript:;" class="am-icon-trash-o am-padding-left-sm submit-delete" data-url="{{:MyUrl('admin/power/delete')}}" data-id="{{$v.id}}"></a>
|
||||
{{/if}}
|
||||
</span>
|
||||
</div>
|
||||
</li>
|
||||
{{if !empty($v['item'])}}
|
||||
<i class="am-icon-plus c-p am-margin-right-xs"></i>
|
||||
<label class="c-p">
|
||||
{{else /}}
|
||||
<label class="c-p am-padding-left-lg">
|
||||
<ul class="list-find am-avg-sm-2 am-avg-md-3 am-avg-lg-4 am-margin-top-xs am-margin-left-xl" style="display:none;">
|
||||
{{foreach $v.item as $vs}}
|
||||
<li id="data-list-{{$vs.id}}">
|
||||
<div class="item-content am-padding-sm am-margin-bottom-xs am-margin-right-xs {{if $vs['is_show'] eq 0}}bk-cr-ffd{{/if}}">
|
||||
<span>{{if !empty($vs['icon'])}}<span class="iconfont {{$vs.icon}}"></span>{{/if}} {{$vs.name}}</span>
|
||||
<span class="am-fr find-submit">
|
||||
<a href="javascript:;" class="am-icon-edit submit-edit" data-json='{{:json_encode($vs)}}' data-tag="power-save-win" data-item="no"></a>
|
||||
<a href="javascript:;" class="am-icon-trash-o am-margin-left-sm submit-delete" data-url="{{:MyUrl('admin/power/delete')}}" data-id="{{$vs.id}}"></a>
|
||||
</span>
|
||||
</div>
|
||||
</li>
|
||||
{{/foreach}}
|
||||
</ul>
|
||||
{{/if}}
|
||||
<span>{{if !empty($v['icon'])}}<span class="iconfont {{$v.icon}}"></span>{{/if}} {{$v.name}}</span>
|
||||
</label>
|
||||
<div class="am-fr m-r-20 list-submit">
|
||||
<span class="am-icon-edit am-icon-md c-p submit-edit {{if !empty($v['item'])}}am-margin-right-xs{{/if}}" data-am-modal="{target: '#power-save-win'}" data-json='{{:json_encode($v)}}' data-tag="power-save-win" data-item="{{if !empty($v['item'])}}ok{{else /}}no{{/if}}"></span>
|
||||
{{if empty($v['item'])}}
|
||||
<span class="am-icon-trash-o am-icon-md c-p am-padding-left-lg m-r-15 submit-delete" data-url="{{:MyUrl('admin/power/delete')}}" data-id="{{$v.id}}"></span>
|
||||
{{/if}}
|
||||
</div>
|
||||
</li>
|
||||
{{if !empty($v['item'])}}
|
||||
<ul class="list-find am-padding-left-0 am-margin-top-xs" style="display:none;">
|
||||
{{foreach $v.item as $vs}}
|
||||
<li id="data-list-{{$vs.id}}" {{if $vs['is_show'] eq 0}}class="bk-cr-ffd"{{/if}}>
|
||||
<label class="c-p">
|
||||
<span>{{if !empty($vs['icon'])}}<span class="iconfont {{$vs.icon}}"></span>{{/if}} {{$vs.name}}</span>
|
||||
</label>
|
||||
<div class="am-fr am-margin-left-sm find-submit">
|
||||
<span class="am-icon-edit am-icon-sm c-p submit-edit" data-am-modal="{target: '#power-save-win'}" data-json='{{:json_encode($vs)}}' data-tag="power-save-win" data-item="no"></span>
|
||||
<span class="am-icon-trash-o am-icon-sm c-p am-margin-left-sm am-margin-right-sm submit-delete" data-url="{{:MyUrl('admin/power/delete')}}" data-id="{{$vs.id}}"></span>
|
||||
</div>
|
||||
</li>
|
||||
{{/foreach}}
|
||||
</ul>
|
||||
{{/if}}
|
||||
{{/foreach}}
|
||||
</ul>
|
||||
{{/foreach}}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- right content end -->
|
||||
|
@ -6,14 +6,38 @@
|
||||
<!-- 父级内容 -->
|
||||
{__block__}
|
||||
|
||||
<!-- 权限项 -->
|
||||
<!-- 菜单权限 -->
|
||||
<div class="am-panel am-panel-default">
|
||||
<div class="am-panel-hd">权限项</div>
|
||||
<div class="am-panel-hd">菜单权限</div>
|
||||
<div class="am-panel-bd">
|
||||
<ul class="am-avg-sm-2 am-avg-md-4 am-avg-lg-6">
|
||||
{{foreach $data.items as $item}}
|
||||
<li class="am-padding-vertical-sm">{{$item}}</li>
|
||||
{{/foreach}}
|
||||
<ul class="am-avg-sm-2 am-avg-md-4 am-avg-lg-6 am-gallery-bordered">
|
||||
{{if !empty($data['power'])}}
|
||||
{{foreach $data.power as $item}}
|
||||
<li>
|
||||
<div class="am-gallery-item">{{$item}}</div>
|
||||
</li>
|
||||
{{/foreach}}
|
||||
{{else /}}
|
||||
{{include file="public/not_data" /}}
|
||||
{{/if}}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 插件权限 -->
|
||||
<div class="am-panel am-panel-default">
|
||||
<div class="am-panel-hd">插件权限</div>
|
||||
<div class="am-panel-bd">
|
||||
<ul class="am-avg-sm-2 am-avg-md-4 am-avg-lg-6 am-gallery-bordered">
|
||||
{{if !empty($data['power_plugins'])}}
|
||||
{{foreach $data.power_plugins as $item}}
|
||||
<li>
|
||||
<div class="am-gallery-item">{{$item}}</div>
|
||||
</li>
|
||||
{{/foreach}}
|
||||
{{else /}}
|
||||
{{include file="public/not_data" /}}
|
||||
{{/if}}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -15,33 +15,56 @@
|
||||
</div>
|
||||
|
||||
<div class="am-form-group">
|
||||
<label>拥有权限</label>
|
||||
<ul class="tree-list p-0">
|
||||
{{foreach $power as $v}}
|
||||
<li {{if $v['is_show'] eq 0}}class="bk-cr-ffd"{{/if}}>
|
||||
<label>菜单权限</label>
|
||||
<ul class="tree-list am-avg-sm-1 am-avg-md-1 am-avg-lg-1">
|
||||
{{if !empty($power['power'])}}
|
||||
{{foreach $power.power as $v}}
|
||||
<li>
|
||||
<div class="item-content am-padding-sm am-margin-bottom-xs am-margin-right-xs {{if $v['is_show'] eq 0}}bk-cr-ffd{{/if}}">
|
||||
<label>
|
||||
<input type="checkbox" name="power_id" value="{{$v.id}}" class="node-choice" {{if $v['is_power'] eq 'ok'}}checked{{/if}} />
|
||||
<span>{{$v.name}}</span>
|
||||
</label>
|
||||
</div>
|
||||
</li>
|
||||
{{if !empty($v['item'])}}
|
||||
<i class="am-icon-minus-square c-p am-margin-right-xs"></i>
|
||||
<label class="c-p">
|
||||
{{else /}}
|
||||
<label class="c-p m-l-2">
|
||||
<ul class="list-find am-avg-sm-2 am-avg-md-3 am-avg-lg-4 am-margin-top-xs am-margin-left-xl">
|
||||
{{foreach $v.item as $vs}}
|
||||
<li>
|
||||
<div class="item-content am-padding-sm am-margin-bottom-xs am-margin-right-xs {{if $vs['is_show'] eq 0}}bk-cr-ffd{{/if}}">
|
||||
<label>
|
||||
<input type="checkbox" name="power_id" value="{{$vs.id}}" {{if $vs['is_power'] eq 'ok'}}checked{{/if}} />
|
||||
<span>{{$vs.name}}</span>
|
||||
</label>
|
||||
</div>
|
||||
</li>
|
||||
{{/foreach}}
|
||||
</ul>
|
||||
{{/if}}
|
||||
<input type="checkbox" name="power_id" value="{{$v.id}}" class="node-choice" {{if $v['is_power'] eq 'ok'}}checked{{/if}} />
|
||||
<span>{{$v.name}}</span>
|
||||
</label>
|
||||
</li>
|
||||
{{if !empty($v['item'])}}
|
||||
<ul class="list-find p-0 am-margin-top-xs">
|
||||
{{foreach $v.item as $vs}}
|
||||
<li {{if $vs['is_show'] eq 0}}class="bk-cr-ffd"{{/if}}>
|
||||
<label class="c-p">
|
||||
<input type="checkbox" name="power_id" value="{{$vs.id}}" {{if $vs['is_power'] eq 'ok'}}checked{{/if}} />
|
||||
<span>{{$vs.name}}</span>
|
||||
</label>
|
||||
</li>
|
||||
{{/foreach}}
|
||||
</ul>
|
||||
{{/if}}
|
||||
{{/foreach}}
|
||||
{{/foreach}}
|
||||
{{else /}}
|
||||
<p class="am-text-warning">无菜单数据</p>
|
||||
{{/if}}
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div class="am-form-group">
|
||||
<label>插件权限<span class="am-text-warning am-margin-left-sm">插件还需在菜单权限中勾选[ <strong class="am-text-danger">应用中心 -> 应用管理+应用调用管理</strong> ]权限</span></label>
|
||||
<ul class="tree-list am-avg-sm-2 am-avg-md-3 am-avg-lg-4">
|
||||
{{if !empty($power['plugins'])}}
|
||||
{{foreach $power.plugins as $v}}
|
||||
<li>
|
||||
<div class="item-content am-padding-sm am-margin-bottom-xs am-margin-right-xs">
|
||||
<label>
|
||||
<input type="checkbox" name="plugins" value="{{$v.plugins}}" class="node-choice" {{if $v['is_power'] eq 'ok'}}checked{{/if}} />
|
||||
<span>{{$v.name}}</span>
|
||||
</label>
|
||||
</div>
|
||||
</li>
|
||||
{{/foreach}}
|
||||
{{else /}}
|
||||
<p class="am-text-warning">无插件数据</p>
|
||||
{{/if}}
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
|
@ -12,6 +12,7 @@ namespace app\service;
|
||||
|
||||
use think\facade\Db;
|
||||
use app\service\AdminService;
|
||||
use app\service\AdminRoleService;
|
||||
|
||||
/**
|
||||
* 权限菜单服务层
|
||||
@ -188,21 +189,23 @@ class AdminPowerService
|
||||
{
|
||||
foreach($admin as $id)
|
||||
{
|
||||
MyCache(MyConfig('shopxo.cache_admin_power_key').$id, null);
|
||||
MyCache(MyConfig('shopxo.cache_admin_left_menu_key').$id, null);
|
||||
MyCache(MyConfig('shopxo.cache_admin_power_key').$id, null);
|
||||
MyCache(MyConfig('shopxo.cache_admin_power_plugins_key').$id, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 管理员权限菜单初始化
|
||||
* @author Devil
|
||||
* @author Devil
|
||||
* @blog http://gong.gg/
|
||||
* @version 1.0.0
|
||||
* @date 2018-12-06
|
||||
* @date 2022-01-23
|
||||
* @desc description
|
||||
* @param [boolean] $is_refresh [是否强制刷新]
|
||||
*/
|
||||
public static function PowerMenuInit()
|
||||
public static function PowerMenuInit($is_refresh = false)
|
||||
{
|
||||
// 基础参数
|
||||
$admin = AdminService::LoginInfo();
|
||||
@ -212,9 +215,10 @@ class AdminPowerService
|
||||
// 读取缓存数据
|
||||
$admin_left_menu = MyCache(MyConfig('shopxo.cache_admin_left_menu_key').$admin_id);
|
||||
$admin_power = MyCache(MyConfig('shopxo.cache_admin_power_key').$admin_id);
|
||||
$admin_plugins = MyCache(MyConfig('shopxo.cache_admin_power_plugins_key').$admin_id);
|
||||
|
||||
// 缓存没数据则从数据库重新读取
|
||||
if((($role_id > 0 || $admin_id == 1) && empty($admin_left_menu)) || MyEnv('app_debug'))
|
||||
if((($role_id > 0 || $admin_id == 1) && empty($admin_left_menu)) || $is_refresh || MyEnv('app_debug'))
|
||||
{
|
||||
// 获取一级数据
|
||||
if($admin_id == 1 || $role_id == 1)
|
||||
@ -229,6 +233,7 @@ class AdminPowerService
|
||||
// 有数据,则处理子级数据
|
||||
if(!empty($admin_left_menu))
|
||||
{
|
||||
// 菜单权限
|
||||
foreach($admin_left_menu as $k=>$v)
|
||||
{
|
||||
// 是否存在控制器和方法
|
||||
@ -294,9 +299,19 @@ class AdminPowerService
|
||||
unset($admin_left_menu[$k]);
|
||||
}
|
||||
}
|
||||
|
||||
// 插件权限
|
||||
if($admin_id == 1 || $role_id == 1)
|
||||
{
|
||||
$plugins_data = AdminRoleService::PluginsList();
|
||||
$admin_plugins = empty($plugins_data) ? [] : array_column($plugins_data, 'name', 'plugins');
|
||||
} else {
|
||||
$admin_plugins = Db::name('RolePlugins')->where(['role_id'=>$role_id])->column('name', 'plugins');
|
||||
}
|
||||
}
|
||||
MyCache(MyConfig('shopxo.cache_admin_left_menu_key').$admin_id, $admin_left_menu);
|
||||
MyCache(MyConfig('shopxo.cache_admin_power_key').$admin_id, $admin_power);
|
||||
MyCache(MyConfig('shopxo.cache_admin_power_plugins_key').$admin_id, $admin_plugins);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ namespace app\service;
|
||||
|
||||
use think\facade\Db;
|
||||
use app\service\AdminPowerService;
|
||||
use app\service\PluginsAdminService;
|
||||
|
||||
/**
|
||||
* 角色服务层
|
||||
@ -42,29 +43,50 @@ class AdminRoleService
|
||||
$data = Db::name('Role')->field($field)->where($where)->order($order_by)->limit($m, $n)->select()->toArray();
|
||||
if(!empty($data))
|
||||
{
|
||||
// 获取对应权限数据
|
||||
$powers = [];
|
||||
// 获取对应菜单权限数据
|
||||
$power_list = [];
|
||||
$ids = array_column($data, 'id');
|
||||
$powers_data = Db::name('Role')->alias('r')->join('role_power rp', 'rp.role_id = r.id')->join('power p', 'rp.power_id = p.id')->where(['r.id'=>$ids])->field('rp.role_id, rp.power_id, p.name')->select()->toArray();
|
||||
if(!empty($powers_data))
|
||||
{
|
||||
foreach($powers_data as $p)
|
||||
{
|
||||
$powers[$p['role_id']][] = $p['name'];
|
||||
$power_list[$p['role_id']][] = $p['name'];
|
||||
}
|
||||
}
|
||||
|
||||
// 获取插件权限
|
||||
$power_plugins_list = [];
|
||||
$powers_data = Db::name('Role')->alias('r')->join('role_plugins rp', 'rp.role_id = r.id')->where(['r.id'=>$ids])->field('rp.role_id, rp.plugins, rp.name')->select()->toArray();
|
||||
if(!empty($powers_data))
|
||||
{
|
||||
foreach($powers_data as $p)
|
||||
{
|
||||
$power_plugins_list[$p['role_id']][] = $p['name'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 是否存在超级管理角色组
|
||||
// 超级管理员数据库中并没存储关联关系,所以这里直接读取全部权限菜单
|
||||
if(in_array(1, $ids))
|
||||
{
|
||||
$powers[1] = Db::name('Power')->column('name');
|
||||
// 全部菜单
|
||||
$power_list[1] = Db::name('Power')->column('name');
|
||||
|
||||
// 全部插件
|
||||
$plugins_data = self::PluginsList();
|
||||
$power_plugins_list[1] = empty($plugins_data) ? [] : array_column($plugins_data, 'name');
|
||||
}
|
||||
|
||||
// 循环处理数据
|
||||
foreach($data as &$v)
|
||||
{
|
||||
// 对应权限数据
|
||||
$v['items'] = array_key_exists($v['id'], $powers) ? $powers[$v['id']] : [];
|
||||
// 对应菜单权限数据
|
||||
$v['power'] = array_key_exists($v['id'], $power_list) ? $power_list[$v['id']] : [];
|
||||
|
||||
// 对应插件权限数据
|
||||
$v['power_plugins'] = array_key_exists($v['id'], $power_plugins_list) ? $power_plugins_list[$v['id']] : [];
|
||||
|
||||
// 时间
|
||||
$v['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
|
||||
@ -138,7 +160,6 @@ class AdminRoleService
|
||||
{
|
||||
// 当前角色关联的所有菜单id
|
||||
$action = empty($params['role_id']) ? [] : Db::name('RolePower')->where(['role_id'=>$params['role_id']])->column('power_id');
|
||||
|
||||
// 权限列表
|
||||
$power_field = 'id,name,is_show';
|
||||
$power = Db::name('Power')->field($power_field)->where(['pid'=>0])->order('sort')->select()->toArray();
|
||||
@ -161,7 +182,32 @@ class AdminRoleService
|
||||
}
|
||||
}
|
||||
}
|
||||
return $power;
|
||||
|
||||
// 插件权限
|
||||
$plugins = [];
|
||||
$action = empty($params['role_id']) ? [] : Db::name('RolePlugins')->where(['role_id'=>$params['role_id']])->column('plugins');
|
||||
// 插件列表
|
||||
$plugins_data = self::PluginsList();
|
||||
if(!empty($plugins_data))
|
||||
{
|
||||
foreach($plugins_data as $pv)
|
||||
{
|
||||
if(!empty($pv['plugins']) && !empty($pv['name']) && !empty($pv['logo']))
|
||||
{
|
||||
$plugins[] = [
|
||||
'plugins' => $pv['plugins'],
|
||||
'name' => $pv['name'],
|
||||
'logo' => $pv['logo'],
|
||||
'is_power' => (empty($action) || !in_array($pv['plugins'], $action)) ? 'no' : 'ok',
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'power' => $power,
|
||||
'plugins' => $plugins,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@ -201,79 +247,115 @@ class AdminRoleService
|
||||
return DataReturn($ret, -1);
|
||||
}
|
||||
|
||||
// 开启事务
|
||||
// 启动事务
|
||||
Db::startTrans();
|
||||
|
||||
// 角色数据更新
|
||||
$role_data = [
|
||||
'name' => $params['name'],
|
||||
'is_enable' => isset($params['is_enable']) ? intval($params['is_enable']) : 0,
|
||||
'upd_time' => time(),
|
||||
];
|
||||
// 捕获异常
|
||||
try {
|
||||
// 角色数据更新
|
||||
$role_data = [
|
||||
'name' => $params['name'],
|
||||
'is_enable' => isset($params['is_enable']) ? intval($params['is_enable']) : 0,
|
||||
'upd_time' => time(),
|
||||
];
|
||||
|
||||
// 不存在添加,则更新
|
||||
if(empty($params['id']))
|
||||
{
|
||||
$role_data['add_time'] = time();
|
||||
$role_id = Db::name('Role')->insertGetId($role_data);
|
||||
} else {
|
||||
// 是否包含删除超级管理员角色
|
||||
if($params['id'] == 1)
|
||||
// 不存在添加,则更新
|
||||
if(empty($params['id']))
|
||||
{
|
||||
return DataReturn('超级管理员角色不可编辑', -1);
|
||||
}
|
||||
|
||||
// 更新
|
||||
if(Db::name('Role')->where(['id'=>$params['id']])->update($role_data) !== false)
|
||||
{
|
||||
$role_id = $params['id'];
|
||||
}
|
||||
}
|
||||
if(empty($role_id))
|
||||
{
|
||||
Db::rollback();
|
||||
return DataReturn('角色数据保存失败', -2);
|
||||
}
|
||||
|
||||
// 权限关联数据删除
|
||||
if(Db::name('RolePower')->where(['role_id'=>$role_id])->delete() === false)
|
||||
{
|
||||
Db::rollback();
|
||||
return DataReturn('角色权限操作失败', -3);
|
||||
}
|
||||
|
||||
// 权限关联数据添加
|
||||
if(!empty($params['power_id']))
|
||||
{
|
||||
$rp_data = [];
|
||||
foreach(explode(',', $params['power_id']) as $power_id)
|
||||
{
|
||||
if(!empty($power_id))
|
||||
$role_data['add_time'] = time();
|
||||
$role_id = Db::name('Role')->insertGetId($role_data);
|
||||
} else {
|
||||
// 是否包含删除超级管理员角色
|
||||
if($params['id'] == 1)
|
||||
{
|
||||
$rp_data[] = [
|
||||
'role_id' => $role_id,
|
||||
'power_id' => $power_id,
|
||||
'add_time' => time(),
|
||||
];
|
||||
throw new \Exception('超级管理员角色不可编辑');
|
||||
}
|
||||
|
||||
// 更新
|
||||
if(Db::name('Role')->where(['id'=>$params['id']])->update($role_data) !== false)
|
||||
{
|
||||
$role_id = $params['id'];
|
||||
}
|
||||
}
|
||||
if(!empty($rp_data))
|
||||
if(empty($role_id))
|
||||
{
|
||||
if(Db::name('RolePower')->insertAll($rp_data) < count($rp_data))
|
||||
throw new \Exception('角色数据保存失败');
|
||||
}
|
||||
|
||||
// 菜单权限数据删除
|
||||
if(Db::name('RolePower')->where(['role_id'=>$role_id])->delete() === false)
|
||||
{
|
||||
throw new \Exception('角色菜单权限操作失败');
|
||||
}
|
||||
|
||||
// 菜单权限数据添加
|
||||
if(!empty($params['power_id']))
|
||||
{
|
||||
$rp_data = [];
|
||||
foreach(explode(',', $params['power_id']) as $power_id)
|
||||
{
|
||||
Db::rollback();
|
||||
return DataReturn('角色权限添加失败', -10);
|
||||
if(!empty($power_id))
|
||||
{
|
||||
$rp_data[] = [
|
||||
'role_id' => $role_id,
|
||||
'power_id' => $power_id,
|
||||
'add_time' => time(),
|
||||
];
|
||||
}
|
||||
}
|
||||
if(!empty($rp_data))
|
||||
{
|
||||
if(Db::name('RolePower')->insertAll($rp_data) < count($rp_data))
|
||||
{
|
||||
throw new \Exception('角色菜单权限添加失败');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 插件权限删除
|
||||
if(Db::name('RolePlugins')->where(['role_id'=>$role_id])->delete() === false)
|
||||
{
|
||||
throw new \Exception('角色插件权限操作失败');
|
||||
}
|
||||
|
||||
// 插件权限数据添加
|
||||
if(!empty($params['plugins']))
|
||||
{
|
||||
$plugins_data = self::PluginsList();
|
||||
$plugins_list = empty($plugins_data) ? [] : array_column($plugins_data, null, 'plugins');
|
||||
$rp_data = [];
|
||||
foreach(explode(',', $params['plugins']) as $plugins)
|
||||
{
|
||||
if(!empty($plugins) && array_key_exists($plugins, $plugins_list))
|
||||
{
|
||||
$rp_data[] = [
|
||||
'role_id' => $role_id,
|
||||
'name' => $plugins_list[$plugins]['name'],
|
||||
'plugins' => $plugins,
|
||||
'add_time' => time(),
|
||||
];
|
||||
}
|
||||
}
|
||||
if(!empty($rp_data))
|
||||
{
|
||||
if(Db::name('RolePlugins')->insertAll($rp_data) < count($rp_data))
|
||||
{
|
||||
throw new \Exception('角色插件权限添加失败');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 提交事务
|
||||
Db::commit();
|
||||
|
||||
// 清除用户权限数据
|
||||
AdminPowerService::PowerCacheDelete();
|
||||
|
||||
return DataReturn('操作成功', 0);
|
||||
} catch(\Exception $e) {
|
||||
Db::rollback();
|
||||
return DataReturn($e->getMessage(), -1);
|
||||
}
|
||||
|
||||
// 提交事务
|
||||
Db::commit();
|
||||
|
||||
// 清除用户权限数据
|
||||
AdminPowerService::PowerCacheDelete();
|
||||
|
||||
return DataReturn('操作成功', 0);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -307,7 +389,7 @@ class AdminRoleService
|
||||
Db::startTrans();
|
||||
|
||||
// 删除角色
|
||||
if(Db::name('Role')->where(['id'=>$params['ids']])->delete() !== false && Db::name('RolePower')->where(['role_id'=>$params['ids']])->delete() !== false)
|
||||
if(Db::name('Role')->where(['id'=>$params['ids']])->delete() !== false && Db::name('RolePower')->where(['role_id'=>$params['ids']])->delete() !== false && Db::name('RolePlugins')->where(['role_id'=>$params['ids']])->delete() !== false)
|
||||
{
|
||||
// 提交事务
|
||||
Db::commit();
|
||||
@ -321,5 +403,35 @@ class AdminRoleService
|
||||
Db::rollback();
|
||||
return DataReturn('删除失败', -100);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取插件列表
|
||||
* @author Devil
|
||||
* @blog http://gong.gg/
|
||||
* @version 1.0.0
|
||||
* @date 2022-01-23
|
||||
* @desc description
|
||||
* @param [array] $params [输入参数]
|
||||
*/
|
||||
public static function PluginsList($params = [])
|
||||
{
|
||||
$result = [];
|
||||
$res = PluginsAdminService::PluginsList();
|
||||
if(!empty($res['data']))
|
||||
{
|
||||
// 已安装的插件
|
||||
if(!empty($res['data']['db_data']))
|
||||
{
|
||||
$result = $res['data']['db_data'];
|
||||
}
|
||||
|
||||
// 未安装的插件
|
||||
if(!empty($res['data']['dir_data']))
|
||||
{
|
||||
$result = array_merge($result, $res['data']['dir_data']);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
?>
|
@ -508,6 +508,7 @@ class AdminService
|
||||
// 清空权限缓存数据
|
||||
MyCache(MyConfig('shopxo.cache_admin_left_menu_key').$admin['id'], null);
|
||||
MyCache(MyConfig('shopxo.cache_admin_power_key').$admin['id'], null);
|
||||
MyCache(MyConfig('shopxo.cache_admin_power_plugins_key').$admin['id'], null);
|
||||
|
||||
// 权限菜单初始化
|
||||
AdminPowerService::PowerMenuInit();
|
||||
|
@ -14,6 +14,8 @@ use think\facade\Db;
|
||||
use app\service\PluginsService;
|
||||
use app\service\ResourcesService;
|
||||
use app\service\SqlConsoleService;
|
||||
use app\service\AdminPowerService;
|
||||
use app\service\AdminService;
|
||||
|
||||
/**
|
||||
* 应用管理服务层
|
||||
@ -53,6 +55,23 @@ class PluginsAdminService
|
||||
{
|
||||
if($dh = opendir($plugins_dir))
|
||||
{
|
||||
// 是否需要校验权限
|
||||
$power_plugins = [];
|
||||
$is_power = (isset($params['is_power']) && $params['is_power'] == true);
|
||||
if($is_power)
|
||||
{
|
||||
// 获取当前登录账户的插件权限
|
||||
$admin = AdminService::LoginInfo();
|
||||
if(!empty($admin))
|
||||
{
|
||||
$res = MyCache(MyConfig('shopxo.cache_admin_power_plugins_key').$admin['id']);
|
||||
if(!empty($res))
|
||||
{
|
||||
$power_plugins = $res;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取数据库已安装插件
|
||||
$temp_data = Db::name('Plugins')->order(self::$plugins_order_by)->column('*', 'plugins');
|
||||
|
||||
@ -67,9 +86,18 @@ class PluginsAdminService
|
||||
{
|
||||
// 获取数据库配置信息
|
||||
$base = $config['base'];
|
||||
$db_config = array_key_exists($base['plugins'], $temp_data) ? $temp_data[$base['plugins']] : [];
|
||||
|
||||
// 是否需要判断权限
|
||||
if($is_power == true)
|
||||
{
|
||||
if(empty($power_plugins) || !array_key_exists($base['plugins'], $power_plugins))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// 数据组装
|
||||
$db_config = array_key_exists($base['plugins'], $temp_data) ? $temp_data[$base['plugins']] : [];
|
||||
$dir_data[$base['plugins']] = [
|
||||
'id' => empty($db_config['id']) ? 0 : $db_config['id'],
|
||||
'plugins' => $base['plugins'],
|
||||
@ -1026,6 +1054,9 @@ php;
|
||||
}
|
||||
$plugins = $ret['data'];
|
||||
|
||||
// 强制刷新用户权限缓存
|
||||
AdminPowerService::PowerMenuInit(true);
|
||||
|
||||
// 附件同步到数据库
|
||||
ResourcesService::AttachmentDiskFilesToDb('plugins_'.$plugins);
|
||||
|
||||
|
@ -36,12 +36,12 @@ return [
|
||||
// 默认编码
|
||||
'default_charset' => 'utf-8',
|
||||
|
||||
// 后端缓存key列表
|
||||
// 权限缓存存储key
|
||||
'cache_admin_power_key' => 'cache_admin_power_',
|
||||
|
||||
// 菜单列表
|
||||
// 后端菜单缓存key列表
|
||||
// 菜单权限缓存存储key
|
||||
// 插件权限缓存存储key
|
||||
'cache_admin_left_menu_key' => 'cache_admin_left_menu_',
|
||||
'cache_admin_power_key' => 'cache_admin_power_',
|
||||
'cache_admin_power_plugins_key' => 'cache_admin_power_plugins_',
|
||||
|
||||
|
||||
// 公共缓存key列表
|
||||
|
@ -1,16 +1,10 @@
|
||||
/**
|
||||
* 权限添加/编辑
|
||||
*/
|
||||
.tree-list li { list-style-type: none; padding-left: 5px !important; }
|
||||
.tree-list li label { display: inline; }
|
||||
.list-find { margin: 0px 0px 5px 35px; overflow: hidden; padding-top: 5px !important; }
|
||||
.tree-list i { width: 10px; }
|
||||
.list-find li { line-height: 26px; }
|
||||
.list-find span { font-weight: normal; }
|
||||
.tree-list li { border:1px dashed #d3ebf7; background: #f5fafd; padding: 6px 10px; }
|
||||
.list-find li { background: #ecf8ff; float: left; margin: 0px 10px 10px 0px; }
|
||||
.list-find li input[type="checkbox"] { vertical-align: text-top; }
|
||||
@media only screen and (min-width: 641px) {
|
||||
.list-submit, .find-submit { display: none; }
|
||||
.list-find li:hover .find-submit, .tree-list li:hover .list-submit { display: block !important; }
|
||||
.tree-list li {
|
||||
list-style-type: none;
|
||||
}
|
||||
.tree-list li .item-content {
|
||||
border:1px dashed #ceecff;
|
||||
background: #ddf2ff;
|
||||
}
|
||||
.tree-list li .stretch {
|
||||
cursor: pointer;
|
||||
}
|
@ -1,16 +1,19 @@
|
||||
/**
|
||||
* 权限添加/编辑
|
||||
*/
|
||||
.tree-list li { list-style-type: none; padding-left: 5px !important; }
|
||||
.tree-list li label { display: inline; }
|
||||
.list-find { margin: 0px 0px 5px 35px; overflow: hidden; padding-top: 5px !important; }
|
||||
.tree-list i { width: 10px; }
|
||||
.list-find li { line-height: 26px; }
|
||||
.list-find span { font-weight: normal; }
|
||||
.tree-list li { border:1px dashed #d3ebf7; background: #f5fafd; padding: 6px 10px; }
|
||||
.list-find li { background: #ecf8ff; float: left; margin: 0px 10px 10px 0px; }
|
||||
.list-find li input[type="checkbox"] { vertical-align: text-top; }
|
||||
@media only screen and (min-width: 641px) {
|
||||
.list-submit, .find-submit { display: none; }
|
||||
.list-find li:hover .find-submit, .tree-list li:hover .list-submit { display: block !important; }
|
||||
.tree-list li {
|
||||
list-style-type: none;
|
||||
}
|
||||
.tree-list li .item-content {
|
||||
border:1px dashed #ceecff;
|
||||
background: #ddf2ff;
|
||||
}
|
||||
.tree-list li label,
|
||||
.tree-list li label input[type="checkbox"] {
|
||||
cursor: pointer;
|
||||
margin: 0;
|
||||
}
|
||||
.tree-list li label input[type="checkbox"],
|
||||
.tree-list li label span {
|
||||
vertical-align: middle;
|
||||
}
|
||||
.list-find li label span {
|
||||
font-weight: normal;
|
||||
}
|
@ -3,7 +3,7 @@ $(function()
|
||||
/**
|
||||
* 展开/关闭
|
||||
*/
|
||||
$('.tree-list i').on('click', function()
|
||||
$('.tree-list i.stretch').on('click', function()
|
||||
{
|
||||
if($(this).hasClass('am-icon-plus'))
|
||||
{
|
||||
@ -13,7 +13,7 @@ $(function()
|
||||
$(this).removeClass('am-icon-minus-square');
|
||||
$(this).addClass('am-icon-plus');
|
||||
}
|
||||
$(this).parent().next('.list-find').toggle(100);
|
||||
$(this).parents('li').next('ul.list-find').toggle(100);
|
||||
});
|
||||
|
||||
/**
|
||||
@ -21,21 +21,23 @@ $(function()
|
||||
*/
|
||||
$('.submit-add').on('click', function()
|
||||
{
|
||||
// 更改窗口名称
|
||||
$title = $('#power-save-win').find('.am-popup-title');
|
||||
$title.text($title.data('add-title'));
|
||||
// 移除菜单禁止状态
|
||||
$('form select[name="pid"]').removeAttr('disabled');
|
||||
|
||||
// 清空表单
|
||||
FormDataFill({"id":"", "pid":0, "name":"", "control":"", "action":"", "icon":"", "sort":0, "is_show":1});
|
||||
|
||||
// 移除菜单禁止状态
|
||||
$('form select[name="pid"]').removeAttr('disabled');
|
||||
|
||||
// 校验成功状态增加失去焦点
|
||||
$('form').find('.am-field-valid').each(function()
|
||||
{
|
||||
$(this).blur();
|
||||
});
|
||||
|
||||
// 更改窗口名称
|
||||
var $popup = $('#power-save-win');
|
||||
$title = $popup.find('.am-popup-title');
|
||||
$title.text($title.data('add-title'));
|
||||
$popup.modal('open');
|
||||
});
|
||||
|
||||
/**
|
||||
@ -44,7 +46,8 @@ $(function()
|
||||
$('.submit-edit').on('click', function()
|
||||
{
|
||||
// 更改窗口名称
|
||||
$title = $('#power-save-win').find('.am-popup-title');
|
||||
var $popup = $('#power-save-win');
|
||||
$title = $popup.find('.am-popup-title');
|
||||
$title.text($title.data('edit-title'));
|
||||
|
||||
// 父级禁用菜单列表选择
|
||||
@ -54,5 +57,6 @@ $(function()
|
||||
} else {
|
||||
$('form select[name="pid"]').removeAttr('disabled');
|
||||
}
|
||||
$popup.modal('open');
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user