first commit
This commit is contained in:
28
catch/permissions/OperateLogListener.php
Normal file
28
catch/permissions/OperateLogListener.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace catchAdmin\permissions;
|
||||
|
||||
use catchAdmin\permissions\model\Permissions;
|
||||
use catcher\CatchAdmin;
|
||||
use think\facade\Db;
|
||||
|
||||
class OperateLogListener
|
||||
{
|
||||
public function handle($params)
|
||||
{
|
||||
$request = $params['request'];
|
||||
$permission = $params['permission'];
|
||||
|
||||
$parentPermission = Permissions::where('id', $permission->parent_id)->value('permission_name');
|
||||
Db::name('operate_log')->insert([
|
||||
'creator_id' => $request->user()->id,
|
||||
'module' => $parentPermission ? : '',
|
||||
'method' => $request->method(),
|
||||
'operate' => $permission->permission_name,
|
||||
'route' => $permission->route,
|
||||
'params' => json_encode($request->param()),
|
||||
'created_at' => time(),
|
||||
'ip' => $request->ip(),
|
||||
]);
|
||||
}
|
||||
}
|
98
catch/permissions/PermissionsMiddleware.php
Normal file
98
catch/permissions/PermissionsMiddleware.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
namespace catchAdmin\permissions;
|
||||
|
||||
use app\Request;
|
||||
use catchAdmin\permissions\model\Permissions;
|
||||
use catcher\exceptions\PermissionForbiddenException;
|
||||
use think\helper\Str;
|
||||
|
||||
class PermissionsMiddleware
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月12日
|
||||
* @param Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws PermissionForbiddenException
|
||||
*/
|
||||
public function handle(Request $request, \Closure $next)
|
||||
{
|
||||
$rule = $rule = $request->rule()->getName();
|
||||
|
||||
if (!$rule) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
[$module, $controller, $action] = $this->parseRule($rule);
|
||||
|
||||
if (in_array($module, $this->ignoreModule())) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
if (!$request->user()) {
|
||||
throw new PermissionForbiddenException('Login is invalid', 10006);
|
||||
}
|
||||
|
||||
// toad
|
||||
if (($permission = $this->getPermission($module, $controller, $action, $request))
|
||||
&& !in_array($permission->id, $request->user()->getPermissionsBy())) {
|
||||
throw new PermissionForbiddenException();
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
protected function parseRule($rule)
|
||||
{
|
||||
[$controller, $action] = explode(Str::contains($rule, '@') ? '@' : '/', $rule);
|
||||
|
||||
$controller = explode('\\', $controller);
|
||||
|
||||
$controllerName = strtolower(array_pop($controller));
|
||||
|
||||
array_pop($controller);
|
||||
|
||||
$module = array_pop($controller);
|
||||
|
||||
return [$module, $controllerName, $action];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月14日
|
||||
* @param $module
|
||||
* @param $controllerName
|
||||
* @param $action
|
||||
* @param $request
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return array|bool|\think\Model|null
|
||||
*/
|
||||
protected function getPermission($module, $controllerName, $action, $request)
|
||||
{
|
||||
$permissionMark = sprintf('%s:%s', $controllerName, $action);
|
||||
$permission = Permissions::where('module', $module)->where('permission_mark', $permissionMark)->find();
|
||||
|
||||
if (!$permission) {
|
||||
return false;
|
||||
}
|
||||
|
||||
event('operateLog', [
|
||||
'request' => $request,
|
||||
'permission' => $permission,
|
||||
]);
|
||||
|
||||
return $permission;
|
||||
}
|
||||
|
||||
protected function ignoreModule()
|
||||
{
|
||||
return ['login'];
|
||||
}
|
||||
}
|
166
catch/permissions/controller/Permission.php
Normal file
166
catch/permissions/controller/Permission.php
Normal file
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
namespace catchAdmin\permissions\controller;
|
||||
|
||||
|
||||
use app\Request;
|
||||
use catcher\base\CatchController;
|
||||
use catcher\CatchAdmin;
|
||||
use catcher\CatchForm;
|
||||
use catcher\CatchResponse;
|
||||
use catcher\exceptions\FailedException;
|
||||
use catcher\Tree;
|
||||
use catchAdmin\permissions\model\Permissions as Permissions;
|
||||
|
||||
class Permission extends CatchController
|
||||
{
|
||||
protected $permissions;
|
||||
|
||||
public function __construct(Permissions $permissions)
|
||||
{
|
||||
$this->permissions = $permissions;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月11日
|
||||
* @throws \Exception
|
||||
* @return string
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月11日
|
||||
* @param Request $request
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function list(Request $request)
|
||||
{
|
||||
return CatchResponse::success(Tree::done($this->permissions->getList($request->param())));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月11日
|
||||
* @throws \Exception
|
||||
* @return string
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$form = new CatchForm();
|
||||
$form->formId('permission');
|
||||
$form->text('permission_name', '菜单名称', true)->verify('required')->placeholder('请输入菜单名称');
|
||||
$form->hidden('parent_id')->default(\request()->param('id') ?? 0);
|
||||
$form->select('module', '模块', true)->verify('required')->options(CatchAdmin::getModulesInfo());
|
||||
$form->text('route', '路由')->placeholder('请输入路由');
|
||||
$form->radio('method', '请求方法', true)->default(Permissions::GET)->options([
|
||||
['value' => Permissions::GET, 'title' => 'get'],
|
||||
['value' => Permissions::POST, 'title' => 'post'],
|
||||
['value' => Permissions::PUT, 'title' => 'put'],
|
||||
['value' => Permissions::DELETE, 'title' => 'delete'],
|
||||
]);
|
||||
$form->text('permission_mark', '权限标识', true)->verify('required')->placeholder('请输入权限标识controller:action');
|
||||
$form->radio('type', '类型', true)->default(Permissions::BTN_TYPE)->options([
|
||||
['value' => Permissions::MENU_TYPE, 'title' => '菜单'],
|
||||
['value' => Permissions::BTN_TYPE, 'title' => '按钮'],
|
||||
]);
|
||||
$form->text('sort', '排序')->verify('numberX')->default(1)->placeholder('倒叙排序');
|
||||
$form->formBtn('submitPermission');
|
||||
|
||||
return $this->fetch(['form' => $form->render()]);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月11日
|
||||
* @param Request $request
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function save(Request $request)
|
||||
{
|
||||
return CatchResponse::success($this->permissions->storeBy($request->param()));
|
||||
}
|
||||
|
||||
public function read()
|
||||
{}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月11日
|
||||
* @param $id
|
||||
* @throws \Exception
|
||||
* @return string
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
$permission = $this->permissions->findBy($id);
|
||||
|
||||
$form = new CatchForm();
|
||||
$form->formId('permission');
|
||||
$form->text('permission_name', '菜单名称', true)
|
||||
->default($permission->permission_name)
|
||||
->verify('required')
|
||||
->placeholder('请输入菜单名称');
|
||||
$form->hidden('parent_id')->default($permission->parent_id);
|
||||
$form->select('module', '模块', true)->default($permission->module)->options(CatchAdmin::getModulesInfo());
|
||||
$form->text('route', '路由')->default($permission->route)->placeholder('请输入路由');
|
||||
$form->radio('method', '请求方法', true)->verify('required')->default($permission->method)->options([
|
||||
['value' => Permissions::GET, 'title' => 'get'],
|
||||
['value' => Permissions::POST, 'title' => 'post'],
|
||||
['value' => Permissions::PUT, 'title' => 'put'],
|
||||
['value' => Permissions::DELETE, 'title' => 'delete'],
|
||||
]);
|
||||
$form->text('permission_mark', '权限标识', true)
|
||||
->default($permission->permission_mark)
|
||||
->verify('required')->placeholder('请输入权限标识controller:action');
|
||||
$form->radio('type', '类型', true)->default($permission->type)->options([
|
||||
['value' => Permissions::MENU_TYPE, 'title' => '菜单'],
|
||||
['value' => Permissions::BTN_TYPE, 'title' => '按钮'],
|
||||
]);
|
||||
$form->text('sort', '排序')->verify('numberX')->default($permission->sort)->placeholder('倒叙排序');
|
||||
$form->formBtn('submitPermission');
|
||||
|
||||
return $this->fetch([
|
||||
'form' => $form->render(),
|
||||
'permission_id' => $permission->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月11日
|
||||
* @param $id
|
||||
* @param Request $request
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function update($id, Request $request)
|
||||
{
|
||||
return CatchResponse::success($this->permissions->updateBy($id, $request->param()));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月11日
|
||||
* @param $id
|
||||
* @throws FailedException
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
if ($this->permissions->where('parent_id', $id)->find()) {
|
||||
throw new FailedException('存在子菜单,无法删除');
|
||||
}
|
||||
|
||||
$this->permissions->findBy($id)->roles()->detach();
|
||||
|
||||
return CatchResponse::success($this->permissions->deleteBy($id));
|
||||
}
|
||||
}
|
||||
|
||||
|
197
catch/permissions/controller/Role.php
Normal file
197
catch/permissions/controller/Role.php
Normal file
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
namespace catchAdmin\permissions\controller;
|
||||
|
||||
use app\Request;
|
||||
use catcher\base\CatchController;
|
||||
use catcher\CatchForm;
|
||||
use catcher\CatchResponse;
|
||||
use catcher\exceptions\FailedException;
|
||||
use catcher\Tree;
|
||||
use think\response\Json;
|
||||
|
||||
class Role extends CatchController
|
||||
{
|
||||
protected $role;
|
||||
|
||||
public function __construct(\catchAdmin\permissions\model\Roles $role)
|
||||
{
|
||||
$this->role = $role;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月09日
|
||||
* @throws \Exception
|
||||
* @return string
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月11日
|
||||
* @throws \Exception
|
||||
* @return string
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$form = new CatchForm();
|
||||
$form->formId('role');
|
||||
$form->text('role_name', '角色名称', true)->verify('required')->placeholder('请输入角色名称');
|
||||
$form->hidden('parent_id')->default(\request()->param('id') ?? 0);
|
||||
$form->textarea('description', '角色描述')->placeholder('请输入角色描述');
|
||||
$form->dom('<div id="permissions"></div>', '权限');
|
||||
$form->formBtn('submitRole');
|
||||
|
||||
return $this->fetch([
|
||||
'form' => $form->render(),
|
||||
'parent_id' => \request()->param('id') ?? 0,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月11日
|
||||
* @param Request $request
|
||||
* @return Json
|
||||
* @throws \think\db\exception\DbException
|
||||
*/
|
||||
public function save(Request $request)
|
||||
{
|
||||
$this->role->storeBy($request->param());
|
||||
|
||||
if (!empty($request->param('permissionids'))) {
|
||||
$this->role->attach($request->param('permissionids'));
|
||||
}
|
||||
// 添加角色
|
||||
return CatchResponse::success();
|
||||
}
|
||||
|
||||
public function read($id)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月11日
|
||||
* @param $id
|
||||
* @throws \Exception
|
||||
* @return string
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
$role = $this->role->findBy($id);
|
||||
|
||||
$form = new CatchForm();
|
||||
$form->formId('role');
|
||||
$form->hidden('parent_id')->default($role->parent_id);
|
||||
$form->text('role_name', '角色名称', true)->default($role->name)->verify('required')->placeholder('请输入角色名称');
|
||||
$form->textarea('description', '角色描述')->default($role->description)->placeholder('请输入角色描述');
|
||||
$form->dom('<div id="permissions"></div>', '权限');
|
||||
$form->formBtn('submitRole');
|
||||
|
||||
return $this->fetch([
|
||||
'form' => $form->render(),
|
||||
'role_id' => $role->id,
|
||||
'parent_id' => $role->parent_id
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月11日
|
||||
* @param $id
|
||||
* @param Request $request
|
||||
* @return Json
|
||||
* @throws \think\db\exception\DbException
|
||||
*/
|
||||
public function update($id, Request $request)
|
||||
{
|
||||
$this->role->updateBy($id, $request->param());
|
||||
|
||||
$role = $this->role->findBy($id);
|
||||
|
||||
$role->detach();
|
||||
|
||||
if (!empty($request->param('permissionids'))) {
|
||||
$role->attach($request->param('permissionids'));
|
||||
}
|
||||
|
||||
return CatchResponse::success();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月11日
|
||||
* @param $id
|
||||
* @throws FailedException
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return Json
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
if ($this->role->where('parent_id', $id)->find()) {
|
||||
throw new FailedException('存在子角色,无法删除');
|
||||
}
|
||||
$role = $this->role->findBy($id);
|
||||
// 删除权限
|
||||
$role->detach();
|
||||
// 删除用户关联
|
||||
$role->users()->detach();
|
||||
// 删除
|
||||
$this->role->deleteBy($id);
|
||||
|
||||
return CatchResponse::success();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月11日
|
||||
* @param Request $request
|
||||
* @return Json
|
||||
*/
|
||||
public function list(Request $request)
|
||||
{
|
||||
return CatchResponse::success(Tree::done($this->role->getList($request->param())));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月11日
|
||||
* @param Request $request
|
||||
* @param \catchAdmin\permissions\model\Permissions $permission
|
||||
* @return Json
|
||||
*/
|
||||
public function getPermissions(Request $request, \catchAdmin\permissions\model\Permissions $permission): Json
|
||||
{
|
||||
$parentRoleHasPermissionIds = null;
|
||||
if ($request->param('parent_id')) {
|
||||
$permissions = $this->role->findBy($request->param('parent_id'))->getPermissions();
|
||||
foreach ($permissions as $_permission) {
|
||||
$parentRoleHasPermissionIds[] = $_permission->pivot->permission_id;
|
||||
}
|
||||
}
|
||||
|
||||
$permissions = Tree::done($permission->getList([
|
||||
'permission_ids' => $parentRoleHasPermissionIds
|
||||
]));
|
||||
|
||||
$permissionIds = [];
|
||||
if ($request->param('role_id')) {
|
||||
$roleHasPermissions = $this->role->findBy($request->param('role_id'))->getPermissions();
|
||||
foreach ($roleHasPermissions as $_permission) {
|
||||
$permissionIds[] = $_permission->pivot->permission_id;
|
||||
}
|
||||
}
|
||||
|
||||
return CatchResponse::success([
|
||||
'permissions' => $permissions,
|
||||
'hasPermissions' => $permissionIds,
|
||||
]);
|
||||
}
|
||||
}
|
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
use think\migration\Migrator;
|
||||
use think\migration\db\Column;
|
||||
|
||||
class Roles extends Migrator
|
||||
{
|
||||
/**
|
||||
* Change Method.
|
||||
*
|
||||
* Write your reversible migrations using this method.
|
||||
*
|
||||
* More information on writing migrations is available here:
|
||||
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
|
||||
*
|
||||
* The following commands can be used in this method and Phinx will
|
||||
* automatically reverse them when rolling back:
|
||||
*
|
||||
* createTable
|
||||
* renameTable
|
||||
* addColumn
|
||||
* renameColumn
|
||||
* addIndex
|
||||
* addForeignKey
|
||||
*
|
||||
* Remember to call "create()" or "update()" and NOT "save()" when working
|
||||
* with the Table class.
|
||||
*/
|
||||
public function change()
|
||||
{
|
||||
$table = $this->table('roles',['engine'=>'Innodb', 'comment' => '角色表', 'signed' => false]);
|
||||
$table->addColumn('role_name', 'string',['limit' => 15,'default'=>'','comment'=>'角色名'])
|
||||
->addColumn('parent_id', 'integer',['default'=>0,'comment'=>'父级ID', 'signed' => false])
|
||||
->addColumn('description', 'string',['default'=> '','comment'=>'角色备注'])
|
||||
->addColumn('created_at', 'integer', array('default'=>0,'comment'=>'创建时间', 'signed' => false ))
|
||||
->addColumn('updated_at', 'integer', array('default'=>0,'comment'=>'更新时间', 'signed' => false))
|
||||
->addColumn('deleted_at', 'integer', array('default'=>0,'comment'=>'删除状态,0未删除 >0 已删除', 'signed' => false))
|
||||
->create();
|
||||
}
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
use think\migration\Migrator;
|
||||
use think\migration\db\Column;
|
||||
|
||||
class Permissions extends Migrator
|
||||
{
|
||||
/**
|
||||
* Change Method.
|
||||
*
|
||||
* Write your reversible migrations using this method.
|
||||
*
|
||||
* More information on writing migrations is available here:
|
||||
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
|
||||
*
|
||||
* The following commands can be used in this method and Phinx will
|
||||
* automatically reverse them when rolling back:
|
||||
*
|
||||
* createTable
|
||||
* renameTable
|
||||
* addColumn
|
||||
* renameColumn
|
||||
* addIndex
|
||||
* addForeignKey
|
||||
*
|
||||
* Remember to call "create()" or "update()" and NOT "save()" when working
|
||||
* with the Table class.
|
||||
*/
|
||||
public function change()
|
||||
{
|
||||
$table = $this->table('permissions',['engine'=>'Innodb', 'comment' => '菜单表', 'signed' => false]);
|
||||
$table->addColumn('permission_name', 'string',['limit' => 15,'default'=>'','comment'=>'菜单名称'])
|
||||
->addColumn('parent_id', 'integer',['default'=>0,'comment'=>'父级ID', 'signed' => false])
|
||||
->addColumn('route', 'string', ['default' => '', 'comment' => '路由', 'limit' => 50])
|
||||
->addColumn('module', 'string', ['default' => '', 'comment' => '模块', 'limit' => 20])
|
||||
->addColumn('method', 'string', ['default' => 'get', 'comment' => '路由请求方法', 'limit' => 15])
|
||||
->addColumn('permission_mark', 'string', ['null' => false, 'comment' => '权限标识', 'limit' => 50])
|
||||
->addColumn('type', 'integer',['limit' => \Phinx\Db\Adapter\MysqlAdapter::INT_TINY,'default'=> 1,'comment'=>'1 菜单 2 按钮'])
|
||||
->addColumn('sort', 'integer',['default'=> 0,'comment'=>'排序字段'])
|
||||
->addColumn('created_at', 'integer', array('default'=>0,'comment'=>'创建时间', 'signed' => false ))
|
||||
->addColumn('updated_at', 'integer', array('default'=>0,'comment'=>'更新时间', 'signed' => false))
|
||||
->addColumn('deleted_at', 'integer', array('default'=>0,'comment'=>'删除状态,null 未删除 timestamp 已删除', 'signed' => false))
|
||||
->create();
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use think\migration\Migrator;
|
||||
use think\migration\db\Column;
|
||||
|
||||
class UserHasRoles extends Migrator
|
||||
{
|
||||
/**
|
||||
* Change Method.
|
||||
*
|
||||
* Write your reversible migrations using this method.
|
||||
*
|
||||
* More information on writing migrations is available here:
|
||||
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
|
||||
*
|
||||
* The following commands can be used in this method and Phinx will
|
||||
* automatically reverse them when rolling back:
|
||||
*
|
||||
* createTable
|
||||
* renameTable
|
||||
* addColumn
|
||||
* renameColumn
|
||||
* addIndex
|
||||
* addForeignKey
|
||||
*
|
||||
* Remember to call "create()" or "update()" and NOT "save()" when working
|
||||
* with the Table class.
|
||||
*/
|
||||
public function change()
|
||||
{
|
||||
$table = $this->table('user_has_roles',['engine'=>'Innodb', 'comment' => '用户角色表', 'signed' => false]);
|
||||
$table->addColumn('uid', 'integer',['comment'=>'用户ID', 'signed' => false])
|
||||
->addColumn('role_id', 'integer', ['comment'=>'角色ID', 'signed' => false])
|
||||
->create();
|
||||
}
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use think\migration\Migrator;
|
||||
use think\migration\db\Column;
|
||||
|
||||
class RoleHasPermissions extends Migrator
|
||||
{
|
||||
/**
|
||||
* Change Method.
|
||||
*
|
||||
* Write your reversible migrations using this method.
|
||||
*
|
||||
* More information on writing migrations is available here:
|
||||
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
|
||||
*
|
||||
* The following commands can be used in this method and Phinx will
|
||||
* automatically reverse them when rolling back:
|
||||
*
|
||||
* createTable
|
||||
* renameTable
|
||||
* addColumn
|
||||
* renameColumn
|
||||
* addIndex
|
||||
* addForeignKey
|
||||
*
|
||||
* Remember to call "create()" or "update()" and NOT "save()" when working
|
||||
* with the Table class.
|
||||
*/
|
||||
public function change()
|
||||
{
|
||||
$table = $this->table('role_has_permissions',['engine'=>'Innodb', 'comment' => '角色权限表', 'signed' => false]);
|
||||
$table->addColumn('role_id', 'integer',['comment'=>'角色ID', 'signed' => false])
|
||||
->addColumn('permission_id', 'integer', ['comment'=>'权限ID', 'signed' => false])
|
||||
->create();
|
||||
}
|
||||
}
|
603
catch/permissions/database/seeds/PermissionSeed.php
Normal file
603
catch/permissions/database/seeds/PermissionSeed.php
Normal file
@@ -0,0 +1,603 @@
|
||||
<?php
|
||||
|
||||
use think\migration\Seeder;
|
||||
|
||||
class PermissionSeed extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run Method.
|
||||
*
|
||||
* Write your database seeder using this method.
|
||||
*
|
||||
* More information on writing seeders is available here:
|
||||
* http://docs.phinx.org/en/latest/seeding.html
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$this->roles();
|
||||
$this->createPermissions();
|
||||
}
|
||||
|
||||
protected function roles()
|
||||
{
|
||||
\catchAdmin\permissions\model\Roles::create([
|
||||
'role_name' => '超级管理员',
|
||||
'description' => 'super user',
|
||||
]);
|
||||
|
||||
\think\facade\Db::name('user_has_roles')->insert([
|
||||
'role_id' => 1,
|
||||
'uid' => 1,
|
||||
]);
|
||||
|
||||
\think\facade\Db::name('role_has_permissions')->insertAll(array (
|
||||
0 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 4,
|
||||
),
|
||||
1 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 6,
|
||||
),
|
||||
2 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 7,
|
||||
),
|
||||
3 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 8,
|
||||
),
|
||||
4 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 9,
|
||||
),
|
||||
5 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 10,
|
||||
),
|
||||
6 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 11,
|
||||
),
|
||||
7 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 12,
|
||||
),
|
||||
8 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 13,
|
||||
),
|
||||
9 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 14,
|
||||
),
|
||||
10 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 15,
|
||||
),
|
||||
11 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 16,
|
||||
),
|
||||
12 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 17,
|
||||
),
|
||||
13 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 18,
|
||||
),
|
||||
14 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 19,
|
||||
),
|
||||
15 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 20,
|
||||
),
|
||||
16 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 21,
|
||||
),
|
||||
17 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 22,
|
||||
),
|
||||
18 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 23,
|
||||
),
|
||||
19 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 24,
|
||||
),
|
||||
20 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 25,
|
||||
),
|
||||
21 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 26,
|
||||
),
|
||||
22 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 27,
|
||||
),
|
||||
23 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 28,
|
||||
),
|
||||
24 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 29,
|
||||
),
|
||||
25 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 30,
|
||||
),
|
||||
26 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 31,
|
||||
),
|
||||
27 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 32,
|
||||
),
|
||||
28 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 33,
|
||||
),
|
||||
29 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 34,
|
||||
),
|
||||
30 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 35,
|
||||
),
|
||||
31 =>
|
||||
array (
|
||||
'role_id' => 1,
|
||||
'permission_id' => 36,
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
protected function createPermissions()
|
||||
{
|
||||
foreach (array (
|
||||
0 =>
|
||||
array (
|
||||
'id' => 4,
|
||||
'permission_name' => 'Dashboard',
|
||||
'parent_id' => 0,
|
||||
'module' => 'index',
|
||||
'route' => 'dashboard',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'index:dashboard',
|
||||
'type' => 1,
|
||||
'sort' => 10000,
|
||||
),
|
||||
1 =>
|
||||
array (
|
||||
'id' => 5,
|
||||
'permission_name' => '主题',
|
||||
'parent_id' => 4,
|
||||
'module' => '',
|
||||
'route' => 'themes',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'index:theme',
|
||||
'type' => 2,
|
||||
'sort' => 0,
|
||||
),
|
||||
2 =>
|
||||
array (
|
||||
'id' => 6,
|
||||
'permission_name' => '用户管理',
|
||||
'parent_id' => 16,
|
||||
'module' => 'user',
|
||||
'route' => 'user',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'user:index',
|
||||
'type' => 1,
|
||||
'sort' => 9999,
|
||||
),
|
||||
3 =>
|
||||
array (
|
||||
'id' => 7,
|
||||
'permission_name' => '创建',
|
||||
'parent_id' => 6,
|
||||
'module' => 'user',
|
||||
'route' => 'user',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'user:create',
|
||||
'type' => 2,
|
||||
'sort' => 0,
|
||||
),
|
||||
4 =>
|
||||
array (
|
||||
'id' => 8,
|
||||
'permission_name' => '保存',
|
||||
'parent_id' => 6,
|
||||
'module' => 'user',
|
||||
'route' => 'user',
|
||||
'method' => 'post',
|
||||
'permission_mark' => 'user:save',
|
||||
'type' => 2,
|
||||
'sort' => 0,
|
||||
),
|
||||
5 =>
|
||||
array (
|
||||
'id' => 9,
|
||||
'permission_name' => '查看',
|
||||
'parent_id' => 6,
|
||||
'module' => 'user',
|
||||
'route' => 'user/<id>/edit',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'user:edit',
|
||||
'type' => 2,
|
||||
'sort' => 0,
|
||||
),
|
||||
6 =>
|
||||
array (
|
||||
'id' => 10,
|
||||
'permission_name' => '编辑',
|
||||
'parent_id' => 6,
|
||||
'module' => 'user',
|
||||
'route' => 'user/<id>',
|
||||
'method' => 'put',
|
||||
'permission_mark' => 'user:update',
|
||||
'type' => 2,
|
||||
'sort' => 0,
|
||||
),
|
||||
7 =>
|
||||
array (
|
||||
'id' => 11,
|
||||
'permission_name' => '删除',
|
||||
'parent_id' => 6,
|
||||
'module' => 'user',
|
||||
'route' => 'user/<id>',
|
||||
'method' => 'delete',
|
||||
'permission_mark' => 'user:delete',
|
||||
'type' => 2,
|
||||
'sort' => 0,
|
||||
),
|
||||
8 =>
|
||||
array (
|
||||
'id' => 12,
|
||||
'permission_name' => '列表',
|
||||
'parent_id' => 6,
|
||||
'module' => 'user',
|
||||
'route' => 'users',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'user:list',
|
||||
'type' => 2,
|
||||
'sort' => 0,
|
||||
),
|
||||
9 =>
|
||||
array (
|
||||
'id' => 13,
|
||||
'permission_name' => '禁用/启用',
|
||||
'parent_id' => 6,
|
||||
'module' => 'user',
|
||||
'route' => 'user/switch/status/<id>',
|
||||
'method' => 'put',
|
||||
'permission_mark' => 'user:switchStatus',
|
||||
'type' => 2,
|
||||
'sort' => 0,
|
||||
),
|
||||
10 =>
|
||||
array (
|
||||
'id' => 14,
|
||||
'permission_name' => '恢复',
|
||||
'parent_id' => 6,
|
||||
'module' => 'user',
|
||||
'route' => 'user/recover/<id>',
|
||||
'method' => 'put',
|
||||
'permission_mark' => 'user:recover',
|
||||
'type' => 2,
|
||||
'sort' => 0,
|
||||
),
|
||||
11 =>
|
||||
array (
|
||||
'id' => 15,
|
||||
'permission_name' => '角色管理',
|
||||
'parent_id' => 16,
|
||||
'module' => 'permissions',
|
||||
'route' => 'role',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'role:index',
|
||||
'type' => 1,
|
||||
'sort' => 1000,
|
||||
),
|
||||
12 =>
|
||||
array (
|
||||
'id' => 16,
|
||||
'permission_name' => '权限管理',
|
||||
'parent_id' => 0,
|
||||
'module' => 'permissions',
|
||||
'route' => '',
|
||||
'method' => 'get',
|
||||
'permission_mark' => '@:@',
|
||||
'type' => 1,
|
||||
'sort' => 1,
|
||||
),
|
||||
13 =>
|
||||
array (
|
||||
'id' => 17,
|
||||
'permission_name' => '菜单管理',
|
||||
'parent_id' => 16,
|
||||
'module' => 'permissions',
|
||||
'route' => 'permission',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'permission:index',
|
||||
'type' => 1,
|
||||
'sort' => 1,
|
||||
),
|
||||
14 =>
|
||||
array (
|
||||
'id' => 18,
|
||||
'permission_name' => '创建',
|
||||
'parent_id' => 15,
|
||||
'module' => 'permissions',
|
||||
'route' => 'role/create',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'role:create',
|
||||
'type' => 2,
|
||||
'sort' => 1,
|
||||
),
|
||||
15 =>
|
||||
array (
|
||||
'id' => 19,
|
||||
'permission_name' => '保存',
|
||||
'parent_id' => 15,
|
||||
'module' => 'permissions',
|
||||
'route' => 'role',
|
||||
'method' => 'post',
|
||||
'permission_mark' => 'role:save',
|
||||
'type' => 2,
|
||||
'sort' => 1,
|
||||
),
|
||||
16 =>
|
||||
array (
|
||||
'id' => 20,
|
||||
'permission_name' => '查看',
|
||||
'parent_id' => 15,
|
||||
'module' => 'permissions',
|
||||
'route' => 'role/<id>/edit',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'role:edit',
|
||||
'type' => 2,
|
||||
'sort' => 1,
|
||||
),
|
||||
17 =>
|
||||
array (
|
||||
'id' => 21,
|
||||
'permission_name' => '更新',
|
||||
'parent_id' => 15,
|
||||
'module' => 'permissions',
|
||||
'route' => 'role/<id>',
|
||||
'method' => 'put',
|
||||
'permission_mark' => 'role:update',
|
||||
'type' => 2,
|
||||
'sort' => 1,
|
||||
),
|
||||
18 =>
|
||||
array (
|
||||
'id' => 22,
|
||||
'permission_name' => '删除',
|
||||
'parent_id' => 15,
|
||||
'module' => 'permissions',
|
||||
'route' => 'role/<id>',
|
||||
'method' => 'delete',
|
||||
'permission_mark' => 'role:delete',
|
||||
'type' => 2,
|
||||
'sort' => 1,
|
||||
),
|
||||
19 =>
|
||||
array (
|
||||
'id' => 23,
|
||||
'permission_name' => '列表',
|
||||
'parent_id' => 15,
|
||||
'module' => 'permissions',
|
||||
'route' => 'roles',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'role:list',
|
||||
'type' => 2,
|
||||
'sort' => 1,
|
||||
),
|
||||
20 =>
|
||||
array (
|
||||
'id' => 24,
|
||||
'permission_name' => '获取权限',
|
||||
'parent_id' => 15,
|
||||
'module' => 'permissions',
|
||||
'route' => 'role/get/permissions',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'role:getPermissions',
|
||||
'type' => 2,
|
||||
'sort' => 1,
|
||||
),
|
||||
21 =>
|
||||
array (
|
||||
'id' => 25,
|
||||
'permission_name' => '删除',
|
||||
'parent_id' => 17,
|
||||
'module' => 'permissions',
|
||||
'route' => 'permission/<id>',
|
||||
'method' => 'delete',
|
||||
'permission_mark' => 'permissions:delete',
|
||||
'type' => 2,
|
||||
'sort' => 1,
|
||||
),
|
||||
22 =>
|
||||
array (
|
||||
'id' => 26,
|
||||
'permission_name' => '更新',
|
||||
'parent_id' => 17,
|
||||
'module' => 'permissions',
|
||||
'route' => 'permission/<id>',
|
||||
'method' => 'put',
|
||||
'permission_mark' => 'permission:update',
|
||||
'type' => 2,
|
||||
'sort' => 1,
|
||||
),
|
||||
23 =>
|
||||
array (
|
||||
'id' => 27,
|
||||
'permission_name' => '查看',
|
||||
'parent_id' => 17,
|
||||
'module' => 'permissions',
|
||||
'route' => 'permissions/<id>/edit',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'permission:edit',
|
||||
'type' => 2,
|
||||
'sort' => 1,
|
||||
),
|
||||
24 =>
|
||||
array (
|
||||
'id' => 28,
|
||||
'permission_name' => '创建',
|
||||
'parent_id' => 17,
|
||||
'module' => 'permissions',
|
||||
'route' => 'permission/create',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'permission:create',
|
||||
'type' => 2,
|
||||
'sort' => 1,
|
||||
),
|
||||
25 =>
|
||||
array (
|
||||
'id' => 29,
|
||||
'permission_name' => '保存',
|
||||
'parent_id' => 17,
|
||||
'module' => 'permissions',
|
||||
'route' => 'permission',
|
||||
'method' => 'post',
|
||||
'permission_mark' => 'permission',
|
||||
'type' => 2,
|
||||
'sort' => 1,
|
||||
|
||||
),
|
||||
26 =>
|
||||
array (
|
||||
'id' => 30,
|
||||
'permission_name' => '列表',
|
||||
'parent_id' => 17,
|
||||
'module' => 'permissions',
|
||||
'route' => 'permissions',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'permission:list',
|
||||
'type' => 2,
|
||||
'sort' => 1,
|
||||
),
|
||||
27 =>
|
||||
array (
|
||||
'id' => 31,
|
||||
'permission_name' => '系统管理',
|
||||
'parent_id' => 0,
|
||||
'module' => 'system',
|
||||
'route' => '',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 's:s',
|
||||
'type' => 1,
|
||||
'sort' => 1,
|
||||
),
|
||||
28 =>
|
||||
array (
|
||||
'id' => 32,
|
||||
'permission_name' => '日志管理',
|
||||
'parent_id' => 31,
|
||||
'module' => 'system',
|
||||
'route' => '',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'log:log',
|
||||
'type' => 1,
|
||||
'sort' => 1,
|
||||
),
|
||||
29 =>
|
||||
array (
|
||||
'id' => 33,
|
||||
'permission_name' => '登录日志',
|
||||
'parent_id' => 32,
|
||||
'module' => 'system',
|
||||
'route' => 'loginLog/index',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'operateLog:index',
|
||||
'type' => 1,
|
||||
'sort' => 1,
|
||||
),
|
||||
30 =>
|
||||
array (
|
||||
'id' => 34,
|
||||
'permission_name' => '操作日志',
|
||||
'parent_id' => 32,
|
||||
'module' => 'index',
|
||||
'route' => 'operateLog/index',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'loginLog:index',
|
||||
'type' => 1,
|
||||
'sort' => 1,
|
||||
),
|
||||
31 =>
|
||||
array (
|
||||
'id' => 35,
|
||||
'permission_name' => '数据字典',
|
||||
'parent_id' => 31,
|
||||
'module' => 'system',
|
||||
'route' => 'data/dictionary',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'datadictory:index',
|
||||
'type' => 1,
|
||||
'sort' => 1,
|
||||
),
|
||||
32 =>
|
||||
array (
|
||||
'id' => 36,
|
||||
'permission_name' => '查看表',
|
||||
'parent_id' => 35,
|
||||
'module' => 'system',
|
||||
'route' => 'table/view/<table>',
|
||||
'method' => 'get',
|
||||
'permission_mark' => 'datadictionary:view',
|
||||
'type' => 2,
|
||||
'sort' => 1,
|
||||
),
|
||||
) as $permission) {
|
||||
\catchAdmin\permissions\model\Permissions::create($permission);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
57
catch/permissions/model/HasRolesTrait.php
Normal file
57
catch/permissions/model/HasRolesTrait.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
namespace catchAdmin\permissions\model;
|
||||
|
||||
trait HasRolesTrait
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月08日
|
||||
* @return mixed
|
||||
*/
|
||||
public function roles()
|
||||
{
|
||||
return $this->belongsToMany(Roles::class, 'user_has_roles', 'role_id', 'uid');
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月08日
|
||||
* @return mixed
|
||||
*/
|
||||
public function getRoles()
|
||||
{
|
||||
return $this->roles()->select();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月08日
|
||||
* @param array $roles
|
||||
* @return mixed
|
||||
*/
|
||||
public function attach(array $roles)
|
||||
{
|
||||
if (empty($roles)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
sort($roles);
|
||||
|
||||
return $this->roles()->attach($roles);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月08日
|
||||
* @param array $roles
|
||||
* @return mixed
|
||||
*/
|
||||
public function detach(array $roles = [])
|
||||
{
|
||||
if (empty($roles)) {
|
||||
return $this->roles()->detach();
|
||||
}
|
||||
|
||||
return $this->roles()->detach($roles);
|
||||
}
|
||||
}
|
56
catch/permissions/model/Permissions.php
Normal file
56
catch/permissions/model/Permissions.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
namespace catchAdmin\permissions\model;
|
||||
|
||||
use catcher\base\CatchModel;
|
||||
|
||||
class Permissions extends CatchModel
|
||||
{
|
||||
protected $name = 'permissions';
|
||||
|
||||
protected $field = [
|
||||
'id', //
|
||||
'permission_name', // 菜单名称
|
||||
'parent_id', // 父级ID
|
||||
'module', // 模块
|
||||
'route', // 路由
|
||||
'method', // 请求方法
|
||||
'permission_mark', // 权限标识
|
||||
'type', // 1 菜单 2 按钮
|
||||
'sort', // 排序字段
|
||||
'created_at', // 创建时间
|
||||
'updated_at', // 更新时间
|
||||
'deleted_at', // 删除状态,null 未删除 timestamp 已删除
|
||||
|
||||
];
|
||||
|
||||
public const MENU_TYPE = 1;
|
||||
public const BTN_TYPE = 2;
|
||||
|
||||
public const GET = 'get';
|
||||
public const POST = 'post';
|
||||
public const PUT = 'put';
|
||||
public const DELETE = 'delete';
|
||||
|
||||
public function getList($search = [])
|
||||
{
|
||||
return $this->when($search['name'] ?? false, function ($query) use ($search){
|
||||
$query->whereLike('name', $search['name']);
|
||||
})
|
||||
->when($search['id'] ?? false, function ($query) use ($search){
|
||||
$query->where('parent_id', $search['id'])
|
||||
->whereOr('id', $search['id']);
|
||||
})
|
||||
->when($search['permission_ids'] ?? false, function ($query) use ($search){
|
||||
$query->whereIn('id', $search['permission_ids']);
|
||||
})
|
||||
->order('sort', 'desc')
|
||||
->order('id', 'desc')
|
||||
->select()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
public function roles(): \think\model\relation\BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Roles::class, 'role_has_permissions', 'role_id', 'permission_id');
|
||||
}
|
||||
}
|
107
catch/permissions/model/Roles.php
Normal file
107
catch/permissions/model/Roles.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
namespace catchAdmin\permissions\model;
|
||||
|
||||
use catchAdmin\user\model\Users;
|
||||
use catcher\base\CatchModel;
|
||||
|
||||
class Roles extends CatchModel
|
||||
{
|
||||
protected $name = 'roles';
|
||||
|
||||
protected $field = [
|
||||
'id', //
|
||||
'role_name', // 角色名
|
||||
'parent_id', // 父级ID
|
||||
'description', // 角色备注
|
||||
'created_at', // 创建时间
|
||||
'updated_at', // 更新时间
|
||||
'deleted_at', // 删除状态,0未删除 >0 已删除
|
||||
|
||||
];
|
||||
|
||||
public function getList($search = [])
|
||||
{
|
||||
return $this->when($search['name'] ?? false, function ($query) use ($search){
|
||||
$query->whereLike('name', $search['name']);
|
||||
})
|
||||
->when($search['id'] ?? false, function ($query) use ($search){
|
||||
$query->where('parent_id', $search['id'])
|
||||
->whereOr('id', $search['id']);
|
||||
})
|
||||
->order('id', 'desc')
|
||||
->select()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月08日
|
||||
* @return \think\model\relation\BelongsToMany
|
||||
*/
|
||||
public function users(): \think\model\relation\BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Users::class, 'user_has_roles', 'uid', 'role_id');
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月09日
|
||||
* @return \think\model\relation\BelongsToMany
|
||||
*/
|
||||
public function permissions(): \think\model\relation\BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Permissions::class, 'role_has_permissions', 'permission_id', 'role_id');
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月08日
|
||||
* @param array $condition
|
||||
* @param array $field
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPermissions($condition = [], $field = [])
|
||||
{
|
||||
return $this->permissions()
|
||||
->when(!empty($field), function ($query) use ($field){
|
||||
$query->field($field);
|
||||
})
|
||||
->when(!empty($condition), function ($query) use ($condition){
|
||||
$query->where($condition);
|
||||
})
|
||||
->select();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月08日
|
||||
* @param array $permissions
|
||||
* @return mixed
|
||||
* @throws \think\db\exception\DbException
|
||||
*/
|
||||
public function attach(array $permissions)
|
||||
{
|
||||
if (empty($permissions)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
sort($permissions);
|
||||
|
||||
return $this->permissions()->attach($permissions);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月08日
|
||||
* @param array $roles
|
||||
* @return mixed
|
||||
*/
|
||||
public function detach(array $roles = [])
|
||||
{
|
||||
if (empty($roles)) {
|
||||
return $this->permissions()->detach();
|
||||
}
|
||||
|
||||
return $this->permissions()->detach($roles);
|
||||
}
|
||||
}
|
11
catch/permissions/module.json
Normal file
11
catch/permissions/module.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "权限管理",
|
||||
"alias": "permissions",
|
||||
"description": "",
|
||||
"keywords": [],
|
||||
"order": 2,
|
||||
"services": [],
|
||||
"aliases": {},
|
||||
"files": [],
|
||||
"requires": []
|
||||
}
|
11
catch/permissions/route.php
Normal file
11
catch/permissions/route.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
// 角色
|
||||
$router->resource('role', '\catchAdmin\permissions\controller\Role');
|
||||
// 角色列表
|
||||
$router->get('roles', '\catchAdmin\permissions\controller\Role@list');
|
||||
$router->get('/role/get/permissions', '\catchAdmin\permissions\controller\Role@getPermissions');
|
||||
|
||||
// 权限
|
||||
$router->resource('permission', '\catchAdmin\permissions\controller\Permission');
|
||||
// 权限列表
|
||||
$router->get('permissions', '\catchAdmin\permissions\controller\Permission@list');
|
22
catch/permissions/view/permission/create.html
Normal file
22
catch/permissions/view/permission/create.html
Normal file
@@ -0,0 +1,22 @@
|
||||
{$form|raw}
|
||||
<script>
|
||||
layui.use(['layer', 'form', 'admin', 'formX'], function () {
|
||||
var $ = layui.jquery;
|
||||
var layer = layui.layer;
|
||||
var form = layui.form;
|
||||
var admin = layui.admin;
|
||||
var mUser = admin.getLayerData('#permission'); // 列表页面传递的数据,#modelUserForm这个只要写弹窗内任意一个元素的id即可
|
||||
form.render();
|
||||
// 回显数据
|
||||
form.val('role', mUser);
|
||||
// 表单提交事件
|
||||
form.on('submit(submitPermission)', function (data) {
|
||||
admin.req('{:url("permission")}', data.field, function (response) {
|
||||
layer.msg(response.msg, {icon: 1});
|
||||
admin.putLayerData('formOk', true, '#permission'); // 设置操作成功的标识,#modelUserForm这个只要写弹窗内任意一个元素的id即可
|
||||
admin.closeDialog('#permission'); // 关闭页面层弹窗
|
||||
}, 'post');
|
||||
return false;
|
||||
});
|
||||
});
|
||||
</script>
|
22
catch/permissions/view/permission/edit.html
Normal file
22
catch/permissions/view/permission/edit.html
Normal file
@@ -0,0 +1,22 @@
|
||||
{$form|raw}
|
||||
<script>
|
||||
layui.use(['layer', 'form', 'admin', 'formX'], function () {
|
||||
var $ = layui.jquery;
|
||||
var layer = layui.layer;
|
||||
var form = layui.form;
|
||||
var admin = layui.admin;
|
||||
var mUser = admin.getLayerData('#permission'); // 列表页面传递的数据,#modelUserForm这个只要写弹窗内任意一个元素的id即可
|
||||
form.render();
|
||||
// 回显数据
|
||||
form.val('permission', mUser);
|
||||
// 表单提交事件
|
||||
form.on('submit(submitPermission)', function (data) {
|
||||
admin.req('permission/'+ "{$permission_id}", data.field, function (response) {
|
||||
layer.msg(response.msg, {icon: 1});
|
||||
admin.putLayerData('formOk', true, '#permission'); // 设置操作成功的标识,#modelUserForm这个只要写弹窗内任意一个元素的id即可
|
||||
admin.closeDialog('#permission'); // 关闭页面层弹窗
|
||||
}, 'put');
|
||||
return false;
|
||||
});
|
||||
});
|
||||
</script>
|
200
catch/permissions/view/permission/index.html
Normal file
200
catch/permissions/view/permission/index.html
Normal file
@@ -0,0 +1,200 @@
|
||||
{extend name="$layout"}
|
||||
{block name="title"}角色管理{/block}
|
||||
{block name="search"}
|
||||
<div class="layui-form toolbar">
|
||||
<div class="layui-form-item">
|
||||
<!--<div class="layui-inline">
|
||||
<div class="layui-input-inline mr0">
|
||||
<input id="edtSearchAuth" class="layui-input" type="text" placeholder="输入角色名称"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-inline">
|
||||
<button id="btnSearchAuth" class="layui-btn icon-btn"><i class="layui-icon"></i>搜索
|
||||
</button>-->
|
||||
<button id="btnAddAuth" class="layui-btn icon-btn"><i class="layui-icon"></i>添加</button>
|
||||
<!--</div>-->
|
||||
</div>
|
||||
</div>
|
||||
{/block}
|
||||
{block name="table"}
|
||||
<table class="layui-table" id="tablePermission" lay-filter="tablePermission"></table>
|
||||
<!-- 表格操作列 -->
|
||||
<script type="text/html" id="tableBarAuth">
|
||||
{:editButton()}
|
||||
{:addButton('新增子菜单')}
|
||||
{:deleteButton()}
|
||||
</script>
|
||||
{/block}
|
||||
{block name="script"}
|
||||
<script>
|
||||
layui.use(['layer', 'form', 'table', 'admin', 'util', 'treeTable'], function () {
|
||||
var $ = layui.jquery;
|
||||
var layer = layui.layer;
|
||||
var form = layui.form;
|
||||
var table = layui.table;
|
||||
var util = layui.util;
|
||||
var admin = layui.admin;
|
||||
var treeTable = layui.treeTable;
|
||||
|
||||
var treeTb = treeTable.render({
|
||||
tree: {
|
||||
arrowType: 'arrow2',
|
||||
iconIndex: 1, // 折叠图标显示在第几列
|
||||
idName: 'id', // 自定义id字段的名称
|
||||
childName: 'children', // 自定义标识是否还有子节点的字段名称
|
||||
pidName: 'parent_id',
|
||||
isPidData: true,
|
||||
getIcon: function(d) { // 自定义图标
|
||||
// d是当前行的数据
|
||||
if (d.children.length) { // 判断是否有子集
|
||||
return '<i class="ew-tree-icon ew-tree-icon-folder"></i>';
|
||||
} else {
|
||||
return '<i class="ew-tree-icon ew-tree-icon-file"></i>';
|
||||
}
|
||||
}
|
||||
},
|
||||
elem: '#tablePermission',
|
||||
cellMinWidth: 100,
|
||||
cols: [
|
||||
{type: 'numbers', title: '#'},
|
||||
{field: 'permission_name', title: '菜单名称', minWidth: 200},
|
||||
{field: 'route', title: '菜单路由', templet: function (d) {
|
||||
return escapeChars(d.route);
|
||||
}
|
||||
},
|
||||
{field: 'method', title: '请求方法', width: 90},
|
||||
{field: 'permission_mark', title: '权限标识'},
|
||||
{field: 'type', title: '菜单类型', templet: function (d) {
|
||||
return d.type == 1 ? '<button class="layui-btn layui-btn-primary layui-btn-xs">菜单</button>' :
|
||||
'<button class="layui-btn layui-btn-primary layui-btn-xs">按钮</button>'
|
||||
}, width: 90, align: 'center'
|
||||
},
|
||||
{field: 'sort', title: '排序', width:60, align: 'center'},
|
||||
{
|
||||
field: 'created_at', sort: true, templet: function (d) {
|
||||
return util.toDateString(d.created_at);
|
||||
}, title: '创建时间', maxWidth: 100
|
||||
},
|
||||
{templet: '#tableBarAuth', title: '操作', align: 'center', minWidth: 150}
|
||||
],
|
||||
reqData: function(data, callback) {
|
||||
// 在这里写ajax请求,通过callback方法回调数据
|
||||
$.get('{:url("permissions")}', function (res) {
|
||||
callback(res.data); // 参数是数组类型
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 添加按钮点击事件
|
||||
$('#btnAddAuth').click(function () {
|
||||
showEditModel();
|
||||
});
|
||||
function escapeChars(str) {
|
||||
str = str.replace(/&/g, '&');
|
||||
str = str.replace(/</g, '<');
|
||||
str = str.replace(/>/g, '>');
|
||||
str = str.replace(/'/g, '´');
|
||||
str = str.replace(/"/g, '"');
|
||||
str = str.replace(/\|/g, '¦');
|
||||
return str;
|
||||
}
|
||||
// 工具条点击事件
|
||||
treeTable.on('tool(tablePermission)', function (obj) {
|
||||
var data = obj.data;
|
||||
var layEvent = obj.event;
|
||||
if (layEvent === 'edit') { // 修改
|
||||
showEditModel(data);
|
||||
} else if (layEvent === 'del') { // 删除
|
||||
doDel(obj);
|
||||
} else {
|
||||
showEditModel(data, true);
|
||||
}
|
||||
});
|
||||
treeTable.on('row(tablePermission)', function(obj){
|
||||
console.log(obj.value); //得到修改后的值
|
||||
console.log(obj.field); //当前编辑的字段名
|
||||
console.log(obj.data); //所在行的所有相关数据
|
||||
});
|
||||
// 删除
|
||||
function doDel(obj) {
|
||||
layer.confirm('确定要删除“' + obj.data.permission_name + '”吗?', {
|
||||
skin: 'layui-layer-admin',
|
||||
shade: .1
|
||||
}, function (index) {
|
||||
layer.close(index);
|
||||
admin.req('/permission/'+ obj.data.id, {}, function (response) {
|
||||
if (response.code === 10000) {
|
||||
layer.msg(response.msg, {icon: 1});
|
||||
obj.del()
|
||||
} else {
|
||||
layer.msg(response.msg, {icon: 2});
|
||||
}
|
||||
}, 'delete');
|
||||
});
|
||||
}
|
||||
|
||||
// 显示表单弹窗
|
||||
// 显示表单弹窗
|
||||
function showEditModel(permission, addPermission= false) {
|
||||
var layIndex = admin.open({
|
||||
title: addPermission ? '新增子菜单' : ((permission ? '修改' : '添加') + '菜单'),
|
||||
url: addPermission ? '/permission/create' + '?id='+permission.id : (permission ? '/permission/'+permission.id + '/edit': '/permission/create'),
|
||||
data: addPermission ? '' : permission, // 传递数据到表单页面
|
||||
end: function () {
|
||||
if (admin.getLayerData(layIndex, 'formOk')) { // 判断表单操作成功标识
|
||||
if (addPermission) {
|
||||
treeTb.reload();
|
||||
setTimeout(function () {
|
||||
treeTb.expand(permission.id)
|
||||
}, 200)
|
||||
} else {
|
||||
if (permission) {
|
||||
treeTb.reload();
|
||||
setTimeout(function () {
|
||||
treeTb.expand(permission.id)
|
||||
}, 200)
|
||||
} else {
|
||||
treeTb.reload(); // 成功刷新表格
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
success: function (layero, dIndex) {
|
||||
// 弹窗超出范围不出现滚动条
|
||||
$(layero).children('.layui-layer-content').css('overflow', 'visible');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 搜索按钮点击事件
|
||||
$('#btnSearchAuth').click(function () {
|
||||
$('#edtSearchAuth').removeClass('layui-form-danger');
|
||||
var keyword = $('#edtSearchAuth').val();
|
||||
var $tds = $('#tableAuth').next('.treeTable').find('.layui-table-body tbody tr td');
|
||||
$tds.css('background-color', 'transparent');
|
||||
if (!keyword) {
|
||||
layer.tips('请输入关键字', '#edtSearchAuth', {tips: [1, '#ff4c4c']});
|
||||
$('#edtSearchAuth').addClass('layui-form-danger');
|
||||
$('#edtSearchAuth').focus();
|
||||
return;
|
||||
}
|
||||
var searchCount = 0;
|
||||
$tds.each(function () {
|
||||
if ($(this).text().indexOf(keyword) >= 0) {
|
||||
$(this).css('background-color', '#FAE6A0');
|
||||
if (searchCount == 0) {
|
||||
$('body,html').stop(true);
|
||||
$('body,html').animate({scrollTop: $(this).offset().top - 150}, 500);
|
||||
}
|
||||
searchCount++;
|
||||
}
|
||||
});
|
||||
if (searchCount == 0) {
|
||||
layer.msg("没有匹配结果", {icon: 5, anim: 6});
|
||||
} else {
|
||||
treetable.expandAll('#tableAuth');
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{/block}
|
35
catch/permissions/view/role/create.html
Normal file
35
catch/permissions/view/role/create.html
Normal file
@@ -0,0 +1,35 @@
|
||||
{$form|raw}
|
||||
<script>
|
||||
layui.use(['layer', 'form', 'admin', 'formX', 'authtree'], function () {
|
||||
var $ = layui.jquery;
|
||||
var layer = layui.layer;
|
||||
var form = layui.form;
|
||||
var admin = layui.admin;
|
||||
var authtree = layui.authtree;
|
||||
var mUser = admin.getLayerData('#role'); // 列表页面传递的数据,#modelUserForm这个只要写弹窗内任意一个元素的id即可
|
||||
// 回显数据
|
||||
form.val('role', mUser);
|
||||
// 表单提交事件
|
||||
form.on('submit(submitRole)', function (data) {
|
||||
admin.req('{:url("role")}', data.field, function (response) {
|
||||
layer.msg(response.msg, {icon: 1});
|
||||
admin.putLayerData('formOk', true, '#role'); // 设置操作成功的标识,#modelUserForm这个只要写弹窗内任意一个元素的id即可
|
||||
admin.closeDialog('#role'); // 关闭页面层弹窗
|
||||
}, 'post');
|
||||
return false;
|
||||
});
|
||||
admin.req('{:url("/role/get/permissions")}',{parent_id:"{$parent_id}"}, function (response) {
|
||||
authtree.render('#permissions', response.data.permissions,{
|
||||
inputname: 'permissionids[]',
|
||||
layfilter: 'lay-check-auth',
|
||||
autowidth: true,
|
||||
nameKey: 'permission_name',
|
||||
valueKey: 'id',
|
||||
childKey: 'children',
|
||||
collapseLeafNode: true,
|
||||
theme: 'auth-skin-default',
|
||||
});
|
||||
})
|
||||
|
||||
});
|
||||
</script>
|
36
catch/permissions/view/role/edit.html
Normal file
36
catch/permissions/view/role/edit.html
Normal file
@@ -0,0 +1,36 @@
|
||||
{$form|raw}
|
||||
<script>
|
||||
layui.use(['layer', 'form', 'admin', 'formX', 'authtree'], function () {
|
||||
var $ = layui.jquery;
|
||||
var layer = layui.layer;
|
||||
var form = layui.form;
|
||||
var admin = layui.admin;
|
||||
var mUser = admin.getLayerData('#role'); // 列表页面传递的数据,#modelUserForm这个只要写弹窗内任意一个元素的id即可
|
||||
var authtree = layui.authtree;
|
||||
|
||||
// 回显数据
|
||||
form.val('role', mUser);
|
||||
admin.req('{:url("/role/get/permissions")}',{role_id:"{$role_id}", parent_id:"{$parent_id}"}, function (response) {
|
||||
authtree.render('#permissions', response.data.permissions,{
|
||||
inputname: 'permissionids[]',
|
||||
layfilter: 'lay-check-auth',
|
||||
autowidth: true,
|
||||
nameKey: 'permission_name',
|
||||
valueKey: 'id',
|
||||
childKey: 'children',
|
||||
collapseLeafNode: true,
|
||||
theme: 'auth-skin-default',
|
||||
checkedKey: response.data.hasPermissions
|
||||
});
|
||||
})
|
||||
// 表单提交事件
|
||||
form.on('submit(submitRole)', function (data) {
|
||||
admin.req('role/'+ "{$role_id}", data.field, function (response) {
|
||||
layer.msg(response.msg, {icon: 1});
|
||||
admin.putLayerData('formOk', true, '#role'); // 设置操作成功的标识,#modelUserForm这个只要写弹窗内任意一个元素的id即可
|
||||
admin.closeDialog('#role'); // 关闭页面层弹窗
|
||||
}, 'put');
|
||||
return false;
|
||||
});
|
||||
});
|
||||
</script>
|
178
catch/permissions/view/role/index.html
Normal file
178
catch/permissions/view/role/index.html
Normal file
@@ -0,0 +1,178 @@
|
||||
{extend name="$layout"}
|
||||
{block name="title"}角色管理{/block}
|
||||
{block name="search"}
|
||||
<div class="layui-form toolbar">
|
||||
<div class="layui-form-item">
|
||||
<!--<div class="layui-inline">
|
||||
<div class="layui-input-inline mr0">
|
||||
<input id="edtSearchAuth" class="layui-input" type="text" placeholder="输入角色名称"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-inline">
|
||||
<button id="btnSearchAuth" class="layui-btn icon-btn"><i class="layui-icon"></i>搜索
|
||||
</button>-->
|
||||
<button id="btnAddAuth" class="layui-btn icon-btn"><i class="layui-icon"></i>添加</button>
|
||||
<!--</div>-->
|
||||
</div>
|
||||
</div>
|
||||
{/block}
|
||||
{block name="table"}
|
||||
<table class="layui-table" id="tableRole"></table>
|
||||
<!-- 表格操作列 -->
|
||||
<script type="text/html" id="tableBarAuth">
|
||||
{:editButton()}
|
||||
{:addButton('新增子角色')}
|
||||
{:deleteButton()}
|
||||
</script>
|
||||
{/block}
|
||||
{block name="script"}
|
||||
<script>
|
||||
layui.use(['layer', 'form', 'table', 'admin', 'util', 'treeTable'], function () {
|
||||
var $ = layui.jquery;
|
||||
var layer = layui.layer;
|
||||
var form = layui.form;
|
||||
var table = layui.table;
|
||||
var util = layui.util;
|
||||
var admin = layui.admin;
|
||||
var treeTable = layui.treeTable;
|
||||
|
||||
var treeTb = treeTable.render({
|
||||
tree: {
|
||||
arrowType: 'arrow2',
|
||||
iconIndex: 1, // 折叠图标显示在第几列
|
||||
idName: 'id', // 自定义id字段的名称
|
||||
childName: 'children', // 自定义标识是否还有子节点的字段名称
|
||||
pidName: 'parent_id',
|
||||
isPidData: true,
|
||||
getIcon: function(d) { // 自定义图标
|
||||
// d是当前行的数据
|
||||
if (d.children.length) { // 判断是否有子集
|
||||
return '<i class="ew-tree-icon ew-tree-icon-folder"></i>';
|
||||
} else {
|
||||
return '<i class="ew-tree-icon ew-tree-icon-file"></i>';
|
||||
}
|
||||
}
|
||||
},
|
||||
elem: '#tableRole',
|
||||
cellMinWidth: 100,
|
||||
cols: [
|
||||
{type: 'numbers', title: '#'},
|
||||
{field: 'role_name', title: '角色名称', minWidth: 100},
|
||||
{field: 'description', title: '角色描述'},
|
||||
{
|
||||
field: 'created_at', sort: true, templet: function (d) {
|
||||
return util.toDateString(d.created_at);
|
||||
}, title: '创建时间', maxWidth: 100
|
||||
},
|
||||
{templet: '#tableBarAuth', title: '操作', align: 'center', minWidth: 120}
|
||||
],
|
||||
reqData: function(data, callback) {
|
||||
// 在这里写ajax请求,通过callback方法回调数据
|
||||
$.get('{:url("roles")}', function (res) {
|
||||
callback(res.data); // 参数是数组类型
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 添加按钮点击事件
|
||||
$('#btnAddAuth').click(function () {
|
||||
showEditModel();
|
||||
});
|
||||
|
||||
// 工具条点击事件
|
||||
treeTable.on('tool(tableRole)', function (obj) {
|
||||
var data = obj.data;
|
||||
var layEvent = obj.event;
|
||||
if (layEvent === 'edit') { // 修改
|
||||
showEditModel(data);
|
||||
} else if (layEvent === 'del') { // 删除
|
||||
doDel(obj);
|
||||
} else {
|
||||
showEditModel(data, true);
|
||||
}
|
||||
});
|
||||
|
||||
// 删除
|
||||
function doDel(obj) {
|
||||
layer.confirm('确定要删除“' + obj.data.role_name + '”吗?', {
|
||||
skin: 'layui-layer-admin',
|
||||
shade: .1
|
||||
}, function (index) {
|
||||
layer.close(index);
|
||||
admin.req('/role/'+ obj.data.id, {}, function (response) {
|
||||
if (response.code === 10000) {
|
||||
layer.msg(response.msg, {icon: 1});
|
||||
obj.del()
|
||||
} else {
|
||||
layer.msg(response.msg, {icon: 2});
|
||||
}
|
||||
}, 'delete');
|
||||
});
|
||||
}
|
||||
|
||||
// 显示表单弹窗
|
||||
// 显示表单弹窗
|
||||
function showEditModel(mRole, addRole = false) {
|
||||
var layIndex = admin.open({
|
||||
title: addRole ? '新增子角色' : ((mRole ? '修改' : '添加') + '角色'),
|
||||
url: addRole ? '/role/create' + '?id='+mRole.id : (mRole ? '/role/'+mRole.id + '/edit': '/role/create'),
|
||||
data: addRole ? '' : mRole, // 传递数据到表单页面
|
||||
area: '700px',
|
||||
end: function () {
|
||||
if (admin.getLayerData(layIndex, 'formOk')) { // 判断表单操作成功标识
|
||||
if (addRole) {
|
||||
treeTb.reload();
|
||||
setTimeout(function () {
|
||||
treeTb.expand(mRole.id)
|
||||
}, 200)
|
||||
} else {
|
||||
if (mRole) {
|
||||
treeTb.reload();
|
||||
setTimeout(function () {
|
||||
treeTb.expand(mRole.id)
|
||||
}, 200)
|
||||
} else {
|
||||
treeTb.reload(); // 成功刷新表格
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
success: function (layero, dIndex) {
|
||||
// 弹窗超出范围不出现滚动条
|
||||
$(layero).children('.layui-layer-content').css('overflow', 'visible');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 搜索按钮点击事件
|
||||
$('#btnSearchAuth').click(function () {
|
||||
$('#edtSearchAuth').removeClass('layui-form-danger');
|
||||
var keyword = $('#edtSearchAuth').val();
|
||||
var $tds = $('#tableAuth').next('.treeTable').find('.layui-table-body tbody tr td');
|
||||
$tds.css('background-color', 'transparent');
|
||||
if (!keyword) {
|
||||
layer.tips('请输入关键字', '#edtSearchAuth', {tips: [1, '#ff4c4c']});
|
||||
$('#edtSearchAuth').addClass('layui-form-danger');
|
||||
$('#edtSearchAuth').focus();
|
||||
return;
|
||||
}
|
||||
var searchCount = 0;
|
||||
$tds.each(function () {
|
||||
if ($(this).text().indexOf(keyword) >= 0) {
|
||||
$(this).css('background-color', '#FAE6A0');
|
||||
if (searchCount == 0) {
|
||||
$('body,html').stop(true);
|
||||
$('body,html').animate({scrollTop: $(this).offset().top - 150}, 500);
|
||||
}
|
||||
searchCount++;
|
||||
}
|
||||
});
|
||||
if (searchCount == 0) {
|
||||
layer.msg("没有匹配结果", {icon: 5, anim: 6});
|
||||
} else {
|
||||
treetable.expandAll('#tableAuth');
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{/block}
|
Reference in New Issue
Block a user