This commit is contained in:
wuyanwen 2019-12-09 16:22:00 +08:00
parent e6b443043a
commit dfa045726b
16 changed files with 383 additions and 122 deletions

View File

@ -84,7 +84,7 @@
<a><i class="layui-icon layui-icon-set"></i>&emsp;<cite>权限管理</cite></a>
<dl class="layui-nav-child">
<dd><a lay-href="{:url('user')}">用户管理</a></dd>
<dd><a lay-href="page/system/role.html">角色管理</a></dd>
<dd><a lay-href="{:url('role')}">角色管理</a></dd>
<dd><a lay-href="page/system/authorities.html">菜单管理</a></dd>
<dd><a lay-href="page/system/loginRecord.html">登录日志</a></dd>
</dl>

View File

@ -1 +1,79 @@
<?php
namespace catchAdmin\permissions\controller;
use app\Request;
use catcher\base\BaseController;
use catcher\CatchForm;
use catcher\CatchResponse;
class Roles extends BaseController
{
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();
}
public function roles()
{
}
public function create()
{
$form = new CatchForm();
$form->formId('roles');
$form->text('name', '角色名称');
$form->formBtn('submitRoles');
return $this->fetch([
'form' => $form->render()
]);
}
public function save()
{}
public function read($id)
{
}
public function edit($id)
{
$form = new CatchForm();
$form->formId('roles');
$form->text('name', '角色名称');
$form->formBtn('submitRoles');
return $this->fetch([
'form' => $form->render()
]);
}
public function update($id, UpdateRequest $request)
{
}
public function delete($id)
{
return CatchResponse::success($this->role->deleteBy($id));
}
public function list(Request $request)
{
return CatchResponse::paginate($this->role->getList($request->param()));
}
}

View File

@ -28,7 +28,7 @@ class Permissions extends Migrator
*/
public function change()
{
$table = $this->table('roles',['engine'=>'Innodb', 'comment' => '菜单表', 'signed' => false]);
$table = $this->table('permissions',['engine'=>'Innodb', 'comment' => '菜单表', 'signed' => false]);
$table->addColumn('name', 'string',['limit' => 15,'default'=>'','comment'=>'菜单名称'])
->addColumn('parent_id', 'integer',['default'=>0,'comment'=>'父级ID', 'signed' => false])
->addColumn('route', 'string', ['default' => '', 'comment' => '路由', 'limit' => 50])

View File

@ -1,7 +1,7 @@
<?php
namespace catchAdmin\permissions\model;
trait UserTrait
trait HasRolesTrait
{
/**
*

View File

@ -0,0 +1,28 @@
<?php
namespace catchAdmin\permissions\model;
use catcher\base\BaseModel;
class Permissions extends BaseModel
{
protected $name = 'permissions';
protected $field = [
'id', //
'name', // 菜单名称
'parent_id', // 父级ID
'route', // 路由
'permission_mark', // 权限标识
'type', // 1 菜单 2 按钮
'sort', // 排序字段
'created_at', // 创建时间
'updated_at', // 更新时间
'deleted_at', // 删除状态null 未删除 timestamp 已删除
];
public function roles(): \think\model\relation\BelongsToMany
{
return $this->belongsToMany(Roles::class, 'role_has_permissions');
}
}

View File

@ -0,0 +1,83 @@
<?php
namespace catchAdmin\permissions\model;
use catchAdmin\user\model\Users;
use catcher\base\BaseModel;
class Roles extends BaseModel
{
protected $name = 'roles';
protected $field = [
'id', //
'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']);
})->paginate($search['limit'] ?? $this->limit);
}
/**
*
* @time 2019年12月08日
* @return \think\model\relation\BelongsToMany
*/
public function users(): \think\model\relation\BelongsToMany
{
return $this->belongsToMany(Users::class, 'user_has_roles');
}
/**
*
* @time 2019年12月09日
* @return \think\model\relation\BelongsToMany
*/
public function permissions(): \think\model\relation\BelongsToMany
{
return $this->belongsToMany(Permissions::class, 'role_has_permissions', 'role_id', 'permission_id');
}
/**
*
* @time 2019年12月08日
* @param $rid
* @return mixed
*/
public function getRoles($rid)
{
return $this->findBy($rid)->permissions()->get();
}
/**
*
* @time 2019年12月08日
* @param $rid
* @param array $roles
* @return mixed
*/
public function attach($rid, array $roles)
{
return $this->findBy($rid)->permissions()->attach($roles);
}
/**
*
* @time 2019年12月08日
* @param $rid
* @param array $roles
* @return mixed
*/
public function detach($rid, array $roles)
{
return $this->findBy($rid)->permissions()->detach($roles);
}
}

View File

@ -1 +1,4 @@
<?php
$router->resource('role', '\catchAdmin\permissions\controller\Roles');
// 用户列表
$router->get('roles', '\catchAdmin\permissions\controller\Roles/list');

View File

@ -1,10 +1 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>
{$form|raw}

View File

@ -1,10 +1,159 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{extend name="../../../view/layout"}
{block name="title"}角色管理{/block}
{block name="search"}
<div class="layui-form toolbar">
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label w-auto">搜索:</label>
<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">&#xe615;</i>搜索
</button>
<button id="btnAddAuth" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
<button id="btnExpandAuth" class="layui-btn icon-btn">全部展开</button>
</div>
<div class="layui-inline">
<button id="btnFoldAuth" class="layui-btn icon-btn">全部折叠</button>
</div>
</div>
</div>
{/block}
{block name="table"}
<table class="layui-table" id="tableRole" lay-filter="tableAuth"></table>
<!-- 表格操作列 -->
<script type="text/html" id="tableBarAuth">
<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
{/block}
{block name="script"}
<script>
layui.use(['layer', 'form', 'table', 'admin', 'treetable'], function () {
var $ = layui.jquery;
var layer = layui.layer;
var form = layui.form;
var table = layui.table;
var admin = layui.admin;
var treetable = layui.treetable;
</body>
</html>
// 渲染表格
function renderTable() {
treetable.render({
treeColIndex: 1,
treeSpid: -1,
treeIdName: 'authorityId',
treePidName: 'parentId',
elem: '#tableRole',
url: '{:url("roles")}',
cellMinWidth: 100,
cols: [[
{type: 'id', title: '#', field: 'id'},
{field: 'name', title: '角色名称', minWidth: 200},
{templet: '#tableBarAuth', title: '操作', align: 'center', minWidth: 120}
]]
});
}
renderTable();
// 添加按钮点击事件
$('#btnAddAuth').click(function () {
showEditModel();
});
// 工具条点击事件
table.on('tool(tableAuth)', function (obj) {
var data = obj.data;
var layEvent = obj.event;
if (layEvent === 'edit') { // 修改
showEditModel(data);
} else if (layEvent === 'del') { // 删除
doDel(data.authorityId, data.authorityName);
}
});
// 删除
function doDel(authorityId, authorityName) {
layer.confirm('确定要删除“' + authorityName + '”吗?', {
skin: 'layui-layer-admin',
shade: .1
}, function (index) {
layer.close(index);
layer.load(2);
$.get('../../json/ok.json', {
authorityId: authorityId
}, function (res) {
layer.closeAll('loading');
if (res.code == 200) {
layer.msg(res.msg, {icon: 1});
renderTable();
} else {
layer.msg(res.msg, {icon: 2});
}
}, 'json');
});
}
// 显示表单弹窗
// 显示表单弹窗
function showEditModel(mRole) {
var layIndex = admin.open({
title: (mRole ? '修改' : '添加') + '用户',
url: mRole ? '/role/'+mRole.id + '/edit':'/role/create',
data: mRole, // 传递数据到表单页面
end: function () {
if (admin.getLayerData(layIndex, 'formOk')) { // 判断表单操作成功标识
insTb.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');
}
});
$('#btnExpandAuth').click(function () {
treetable.expandAll('#tableAuth');
});
$('#btnFoldAuth').click(function () {
treetable.foldAll('#tableAuth');
});
});
</script>
{/block}

View File

@ -1,10 +1,13 @@
<?php
namespace catchAdmin\user\model;
use catchAdmin\permissions\model\HasRolesTrait;
use catcher\base\BaseModel;
class Users extends BaseModel
{
use HasRolesTrait;
protected $name = 'users';
protected $field = [
@ -33,7 +36,15 @@ class Users extends BaseModel
return password_hash($value, PASSWORD_DEFAULT);
}
public function getList($search)
/**
* 用户列表
*
* @time 2019年12月08日
* @param $search
* @throws \think\db\exception\DbException
* @return \think\Paginator
*/
public function getList($search): \think\Paginator
{
return (($search['trash'] ?? false) ? static::onlyTrashed() : $this)->when($search['username'] ?? false, function ($query) use ($search){
return $query->whereLike('username', $search['username']);

View File

@ -7,11 +7,11 @@ return [
// pathinfo分隔符
'pathinfo_depr' => '/',
// URL伪静态后缀
'url_html_suffix' => 'html',
'url_html_suffix' => '',
// URL普通方式参数 用于自动生成
'url_common_param' => true,
// 是否开启路由延迟解析
'url_lazy_route' => false,
'url_lazy_route' => true,
// 是否强制使用路由
'url_route_must' => false,
// 合并路由规则

View File

@ -1,33 +0,0 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class AnyClassNameYouWant 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()
{
}
}

View File

@ -1,19 +0,0 @@
<?php
use think\migration\Seeder;
class Abc 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()
{
}
}

View File

@ -205,7 +205,7 @@ class CatchAdmin
*/
public static function getRoutes(): string
{
if (file_exists(self::getCacheViewsFile())) {
if (file_exists(self::getCacheRoutesFile())) {
return self::getCacheRoutesFile();
}

View File

@ -1,6 +1,7 @@
<?php
namespace catcher\command;
use catcher\CatchAdmin;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
@ -97,9 +98,9 @@ class InstallCommand extends Command
* 安装第一步
*
* @time 2019年11月29日
* @return bool
* @return mixed
*/
protected function firstStep(): bool
protected function firstStep()
{
if (file_exists($this->app->getRootPath() . '.env')) {
return false;
@ -140,33 +141,16 @@ class InstallCommand extends Command
*/
protected function secondStep(): void
{
$modulePaths = glob(root_path('module') . '*');
$php = getenv('_');
$this->checkRootDatabase();
$think = root_path() . DIRECTORY_SEPARATOR . 'think';
foreach ($modulePaths as $path) {
if (is_dir($path)) {
$moduleDatabasePath = $path . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR;
if (is_dir($moduleDatabasePath)) {
if (is_dir($moduleDatabasePath . 'migrations' . DIRECTORY_SEPARATOR)) {
$migrationFiles = glob($moduleDatabasePath . 'migrations' . DIRECTORY_SEPARATOR . '*.php');
foreach ($migrationFiles as $file) {
copy($file,
root_path('database') . 'migrations'. DIRECTORY_SEPARATOR .
pathinfo($file, PATHINFO_BASENAME));
}
}
foreach (CatchAdmin::getModulesDirectory() as $directory) {
$moduleInfo = CatchAdmin::getModuleInfo($directory);
if (is_dir($moduleDatabasePath . 'seeds' . DIRECTORY_SEPARATOR)) {
$seedFiles = glob($moduleDatabasePath . 'seeds' . DIRECTORY_SEPARATOR . '*.php');
foreach ($seedFiles as $file) {
copy($file,
root_path('database') . 'seeds' . DIRECTORY_SEPARATOR .
pathinfo($file, PATHINFO_BASENAME));
}
}
}
}
$this->output->info(
sprintf('module [%s] migrations ', $moduleInfo['alias']) .
exec(sprintf('%s %s catch-migrate:run %s', $php, $think, $moduleInfo['alias'])));
}
}
@ -190,9 +174,7 @@ class InstallCommand extends Command
protected function finished(): void
{
// todo something
if ($this->dataInstall) {
rmdir($this->app->getRootPath() . 'database');
}
}
/**
@ -241,23 +223,11 @@ class InstallCommand extends Command
if ($this->getEnvFile()) {
$this->output->info('env file has been generated');
}
if ((new \mysqli($host, $username, $password, null, $port))->query(sprintf('CREATE DATABASE IF NOT EXISTS %s DEFAULT CHARSET %s COLLATE %s_general_ci;',
$database, $charset, $charset))) {
$this->output->info(sprintf('🎉 create database %s successfully', $database));
exec(sprintf('%s %s migrate:run', getenv('_'), root_path() . DIRECTORY_SEPARATOR . 'think'));
$this->output->info('🎉 database table install successfully');
exec(sprintf('%s %s seed:run', getenv('_'),root_path() . DIRECTORY_SEPARATOR . 'think'));
$this->output->info('🎉 Fill database table successfully ');
} else {
$this->dataInstall = false;
$this->output->warning(sprintf('create database %s failed, you should create it by yourself', $database));
$this->output->warning('you should use `php think migrate:run` to create tables');
$this->output->warning('you should use `php think seed:run` to fill tables data');
$this->output->warning(sprintf('create database %s failedyou need create database first by yourself', $database));
}
}

View File

@ -80,9 +80,9 @@ class ModelGeneratorCommand extends Command
<?php
namespace catchAdmin\{Module}\model;
use cather\Model;
use cather\base\BaseModel;
class {Class} extends Model
class {Class} extends BaseModel
{
protected \$name = '{Name}';