This commit is contained in:
LHY 2020-03-09 00:02:50 +08:00
parent 87dab38a5c
commit 10cf82992d
10 changed files with 374 additions and 13 deletions

View File

@ -28,6 +28,7 @@ CREATE TABLE IF NOT EXISTS `__PREFIX__admin_role` (
`sort` int(4) unsigned DEFAULT '0' COMMENT '排序',
`name` varchar(25) NOT NULL DEFAULT '' COMMENT '角色名称',
`description` varchar(100) DEFAULT '' COMMENT '描述',
`tags` varchar(500) DEFAULT '' COMMENT '标记',
`create_time` datetime NOT NULL DEFAULT '2020-01-01 00:00:00' COMMENT '添加时间',
`update_time` datetime NOT NULL DEFAULT '2020-01-01 00:00:00' COMMENT '更新时间',
PRIMARY KEY (`id`)
@ -54,6 +55,18 @@ CREATE TABLE IF NOT EXISTS `__PREFIX__admin_role_menu` (
INDEX (`role_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='角色权限表';
CREATE TABLE IF NOT EXISTS `__PREFIX__admin_group` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
`parent_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '上级id',
`description` varchar(100) DEFAULT '' COMMENT '描述',
`sort` int(4) NOT NULL DEFAULT '0' COMMENT '排序',
`name` varchar(25) NOT NULL DEFAULT '' COMMENT '标题',
`tags` varchar(500) DEFAULT '' COMMENT '标记',
`create_time` datetime NOT NULL DEFAULT '2020-01-01 00:00:00' COMMENT '添加时间',
`update_time` datetime NOT NULL DEFAULT '2020-01-01 00:00:00' COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='用户分组表';
CREATE TABLE IF NOT EXISTS `__PREFIX__admin_user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
`role_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '角色组',
@ -66,12 +79,16 @@ CREATE TABLE IF NOT EXISTS `__PREFIX__admin_user` (
`email` varchar(55) DEFAULT '' COMMENT '邮箱',
`errors` int(10) unsigned DEFAULT '0' COMMENT '错误次数',
`enable` tinyint(1) unsigned DEFAULT '1' COMMENT '启用',
`tags` varchar(500) DEFAULT '' COMMENT '标记',
`group_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '分组',
`login_time` datetime NOT NULL DEFAULT '2020-01-01 00:00:00' COMMENT '登录时间',
`create_time` datetime NOT NULL DEFAULT '2020-01-01 00:00:00' COMMENT '添加时间',
`update_time` datetime NOT NULL DEFAULT '2020-01-01 00:00:00' COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='角色权限表';
UNIQUE (`username`),
INDEX (`role_id`),
INDEX (`group_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='管理员用户表';
CREATE TABLE IF NOT EXISTS `__PREFIX__admin_operation_log` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
@ -90,9 +107,14 @@ CREATE TABLE IF NOT EXISTS `__PREFIX__admin_operation_log` (
INSERT INTO `__PREFIX__admin_role` (`id`, `sort`, `name`, `description`, `create_time`, `update_time`) VALUES
(1, 1, '超级管理员', '超级管理员,拥有所有权限', '2020-03-01 20:11:43', '2020-03-01 20:16:03');
-- 默认组
INSERT INTO `admin_group` (`id`, `parent_id`, `description`, `sort`, `name`, `tags`, `create_time`, `update_time`) VALUES
(1, 0, '', 1, '默认分组', '', '2020-03-01 20:11:43', '2020-03-01 20:11:43');
-- 默认管理员 admin tpextadmin
INSERT INTO `__PREFIX__admin_user` (`id`, `role_id`, `username`, `password`, `salt`, `name`, `avatar`, `phone`, `email`, `errors`, `login_time`, `create_time`, `update_time`) VALUES
(1, 1, 'admin', '0796647d241a5014670013b324a155ee', '15fb7db', '管理员', '/assets/lightyearadmin/images/no-avatar.jpg', '', '', 0, '2020-01-01 00:00:00', '2020-03-01 20:19:11', '2020-03-01 20:19:11');
INSERT INTO `admin_user` (`id`, `role_id`, `username`, `password`, `salt`, `name`, `avatar`, `phone`, `email`, `errors`, `enable`, `tags`, `group_id`, `login_time`, `create_time`, `update_time`) VALUES
(1, 1, 'admin', '0796647d241a5014670013b324a155ee', '15fb7db', '管理员', '/assets/lightyearadmin/images/no-avatar.jpg', '', '', 0, 1, '', 1, '2020-01-01 00:00:00', '2020-03-01 20:19:11', '2020-03-08 23:37:36');
-- 菜单
INSERT INTO `__PREFIX__admin_menu` (`id`, `parent_id`, `sort`, `title`, `url`, `icon`, `create_time`, `update_time`) VALUES
@ -102,7 +124,8 @@ INSERT INTO `__PREFIX__admin_menu` (`id`, `parent_id`, `sort`, `title`, `url`, `
(4, 2, 1, '权限设置', '/admin/permission/index', 'mdi mdi-account-key', '2020-03-03 20:28:35', '2020-03-03 20:28:35'),
(5, 2, 1, '管理员', '/admin/admin/index', 'mdi mdi-account-card-details', '2020-03-03 20:29:07', '2020-03-03 20:34:25'),
(6, 2, 1, '角色管理', '/admin/role/index', 'mdi mdi-account-multiple', '2020-03-03 20:31:22', '2020-03-03 20:31:22'),
(7, 2, 1, '操作记录', '/admin/operationlog/index', 'mdi mdi-playlist-check', '2020-03-03 20:32:06', '2020-03-03 20:32:06'),
(8, 0, 1, '系统管理', '#', 'mdi mdi-settings', '2020-03-03 20:35:11', '2020-03-03 20:35:11'),
(9, 8, 1, '扩展管理', '/admin/extension/index', 'mdi mdi-blur', '2020-03-03 20:36:54', '2020-03-03 20:36:54'),
(10, 8, 1, '平台设置', '/admin/config/index', 'mdi mdi-settings-box', '2020-03-03 20:37:29', '2020-03-03 20:37:29');
(7, 2, 1, '用户分组', '/admin/group/index', 'mdi mdi-account-network', '2020-03-03 20:31:22', '2020-03-03 20:31:22'),
(8, 2, 1, '操作记录', '/admin/operationlog/index', 'mdi mdi-playlist-check', '2020-03-03 20:32:06', '2020-03-03 20:32:06'),
(9, 0, 1, '系统管理', '#', 'mdi mdi-settings', '2020-03-03 20:35:11', '2020-03-03 20:35:11'),
(10, 9, 1, '扩展管理', '/admin/extension/index', 'mdi mdi-blur', '2020-03-03 20:36:54', '2020-03-03 20:36:54'),
(11, 9, 1, '平台设置', '/admin/config/index', 'mdi mdi-settings-box', '2020-03-03 20:37:29', '2020-03-03 20:37:29');

View File

@ -8,6 +8,8 @@ DROP TABLE IF EXISTS `__PREFIX__admin_role_permission` ;
DROP TABLE IF EXISTS `__PREFIX__admin_role_menu` ;
DROP TABLE IF EXISTS `__PREFIX__admin_group` ;
DROP TABLE IF EXISTS `__PREFIX__admin_user` ;
DROP TABLE IF EXISTS `__PREFIX__admin_operation_log`;

View File

@ -4,17 +4,20 @@ namespace tpext\myadmin\admin\controller;
use think\Controller;
use tpext\builder\common\Builder;
use tpext\myadmin\admin\model\AdminRole;
use tpext\myadmin\admin\model\AdminGroup;
use tpext\myadmin\admin\model\AdminUser;
class Admin extends Controller
{
protected $dataModel;
protected $roleModel;
protected $groupModel;
protected function initialize()
{
$this->dataModel = new AdminUser;
$this->roleModel = new AdminRole;
$this->groupModel = new AdminGroup;
}
public function index()
@ -37,11 +40,12 @@ class Admin extends Controller
$table->show('username', '登录帐号');
$table->text('name', '姓名')->autoPost()->getWapper()->addStyle('max-width:80px');
$table->show('role_name', '角色');
$table->show('group_name', '分组');
$table->show('email', '电子邮箱')->default('无');
$table->show('phone', '手机号')->default('无');
$table->show('errors', '登录失败');
$table->show('login_time', '登录时间')->getWapper()->addStyle('width:180px');
$table->show('create_time', '添加时间')->getWapper()->addStyle('width:180px');
$table->show('update_time', '修改时间')->getWapper()->addStyle('width:180px');
$pagezise = 10;
@ -95,8 +99,11 @@ class Admin extends Controller
$d['__h_del__'] = $d['id'] == 1;
$d['__h_en__'] = $d['enable'] == 1;
$d['__h_dis__'] = $d['enable'] != 1 || $d['id'] == 1;
$d['__h_clr__'] = $d['errors'] < 1;
}
unset($d);
$table->data($data);
$table->paginator($this->dataModel->where($where)->count(), $pagezise);
@ -112,10 +119,12 @@ class Admin extends Controller
->btnEnable()
->btnDisable()
->btnDelete()
->btnPostRowid('clear_errors', url('clearErrors'), '', 'btn-info', 'mdi-backup-restore', 'title="重置登录失败次数"')
->mapClass([
'delete' => ['hidden' => '__h_del__'],
'enable' => ['hidden' => '__h_en__'],
'disable' => ['hidden' => '__h_dis__'],
'clear_errors' => ['hidden' => '__h_clr__'],
]);
if (request()->isAjax()) {
@ -148,6 +157,31 @@ class Admin extends Controller
}
}
public function clearErrors()
{
$ids = input('ids', '');
$ids = array_filter(explode(',', $ids), 'strlen');
if (empty($ids)) {
$this->error('参数有误');
}
$res = 0;
foreach ($ids as $id) {
if ($this->dataModel->where(['id' => $id])->update(['errors' => 0])) {
$res += 1;
}
}
if ($res) {
$this->success('成功重置' . $res . '个账号的登录失败次数');
} else {
$this->error('重置失败');
}
}
private function save($id = 0)
{
if ($id == 1 && session('admin_id') != 1) {
@ -157,11 +191,13 @@ class Admin extends Controller
$data = request()->only([
'name',
'role_id',
'group_id',
'avatar',
'username',
'password',
'email',
'phone',
'tags',
], 'post');
if ($id == 1) {
@ -178,6 +214,7 @@ class Admin extends Controller
'name|姓名' => 'require',
'email|电子邮箱' => 'email',
'phone|手机号' => 'mobile',
'errors|失败次数' => 'number',
]);
if (true !== $result) {
@ -236,6 +273,15 @@ class Admin extends Controller
return $roles;
}
private function getGroupList()
{
$tree = [0 => '未分组'];
$tree += $this->groupModel->buildTree(); //数组合并不要用 array_merge , 会重派数组键 作为options导致bug
return $tree;
}
private function form($title, $data = [])
{
$isEdit = isset($data['id']);
@ -245,12 +291,14 @@ class Admin extends Controller
$form = $builder->form();
$form->text('username', '登录帐号')->required()->beforSymbol('<i class="mdi mdi-account-key"></i>');
$form->select('role_id', '角色')->required()->options($this->getRoleList())->disabled($isEdit && $data['id'] == 1);
$form->select('role_id', '角色')->required()->options($this->getRoleList())->disabled($isEdit && $data['id'] == 1);
$form->password('password', '密码')->required(!$isEdit)->beforSymbol('<i class="mdi mdi-lock"></i>')->help($isEdit ? '不修改则留空620位' : '添加用户密码必填620位');
$form->text('name', '姓名')->required()->beforSymbol('<i class="mdi mdi-rename-box"></i>');
$form->select('group_id', '用户组')->options($this->getGroupList());
$form->image('avatar', '头像')->default('/assets/lightyearadmin/images/no-avatar.jpg');
$form->text('email', '电子邮箱')->beforSymbol('<i class="mdi mdi-email-variant"></i>');
$form->text('phone', '手机号')->beforSymbol('<i class="mdi mdi-cellphone-iphone"></i>');
$form->tags('tags', '标签');
if ($isEdit) {

View File

@ -0,0 +1,181 @@
<?php
namespace tpext\myadmin\admin\controller;
use think\Controller;
use tpext\builder\common\Builder;
use tpext\myadmin\admin\model\AdminGroup;
class Group extends Controller
{
protected $dataModel;
protected function initialize()
{
$this->dataModel = new AdminGroup;
}
public function index()
{
$builder = Builder::getInstance('分组管理', '列表');
$table = $builder->table();
$table->show('id', 'ID');
$table->raw('title_show', '名称')->getWapper()->addStyle('text-align:left;');
$table->show('users', '用户数');
$table->show('description', '描述')->default('无描述');
$table->text('name', '名称')->autoPost()->getWapper()->addStyle('max-width:80px');
$table->text('sort', '排序')->autoPost()->getWapper()->addStyle('max-width:40px');
$table->show('create_time', '添加时间')->getWapper()->addStyle('width:180px');
$table->show('update_time', '修改时间')->getWapper()->addStyle('width:180px');
$table->sortable([]);
$data = $this->dataModel->buildList(0, 0);
$table->data($data);
if (request()->isAjax()) {
return $table->partial()->render();
}
return $builder->render();
}
public function add()
{
if (request()->isPost()) {
return $this->save();
} else {
return $this->form('添加');
}
}
public function edit($id)
{
if (request()->isPost()) {
return $this->save($id);
} else {
$data = $this->dataModel->get($id);
if (!$data) {
$this->error('数据不存在');
}
return $this->form('编辑', $data);
}
}
private function save($id = 0)
{
$data = request()->only([
'name',
'tags',
'sort',
'parent_id',
], 'post');
$result = $this->validate($data, [
'name|名称' => 'require',
'sort|排序' => 'require|number',
'parent_id|上级' => 'require',
]);
if (true !== $result) {
$this->error($result);
}
if ($id) {
if ($data['parent_id'] == $id) {
$this->error('上级不能是自己');
}
$data['update_time'] = date('Y-m-d H:i:s');
$res = $this->dataModel->where(['id' => $id])->update($data);
} else {
$res = $this->dataModel->create($data);
}
if (!$res) {
$this->error('保存失败');
}
return Builder::getInstance()->layer()->closeRefresh(1, '保存成功');
}
private function form($title, $data = [])
{
$isEdit = isset($data['id']);
$builder = Builder::getInstance('分组管理', $title);
$form = $builder->form();
$tree = [0 => '顶级分组'];
$tree += $this->dataModel->buildTree(0, 0, $isEdit ? $data['id'] : 0); //数组合并不要用 array_merge , 会重排数组键 作为options导致bug
$form->text('name', '名称')->required();
$form->select('parent_id', '上级')->required()->options($tree);
$form->tags('tags', '标签');
$form->text('sort', '排序')->default(1)->required();
if ($isEdit) {
$form->show('create_time', '添加时间');
$form->show('update_time', '修改时间');
}
$form->fill($data);
return $builder->render();
}
public function autopost()
{
$id = input('id/d', '');
$name = input('name', '');
$value = input('value', '');
if (empty($id) || empty($name)) {
$this->error('参数有误');
}
$allow = ['name', 'sort'];
if (!in_array($name, $allow)) {
$this->error('不允许的操作');
}
$res = $this->dataModel->where(['id' => $id])->update([$name => $value]);
if ($res) {
$this->success('修改成功');
} else {
$this->error('修改失败');
}
}
public function delete()
{
$ids = input('ids');
$ids = array_filter(explode(',', $ids), 'strlen');
if (empty($ids)) {
$this->error('参数有误');
}
$res = 0;
foreach ($ids as $id) {
if ($this->dataModel->destroy($id)) {
$this->dataModel->where(['parent_id' => $id])->update(['parent_id' => 0]);
$res += 1;
}
}
if ($res) {
$this->success('成功删除' . $res . '条数据');
} else {
$this->error('删除失败');
}
}
}

View File

@ -80,7 +80,7 @@ class Menu extends Controller
'title|名称' => 'require',
'url|url' => 'require',
'icon|图标' => 'require',
'sort|排序' => 'require',
'sort|排序' => 'require|number',
'parent_id|上级' => 'require',
]);
@ -117,7 +117,7 @@ class Menu extends Controller
$tree = [0 => '根菜单'];
$tree += $this->dataModel->buildTree(0, 0, $isEdit ? $data['id'] : 0); //数组合并不要用 array_merge , 会重数组键 作为options导致bug
$tree += $this->dataModel->buildTree(0, 0, $isEdit ? $data['id'] : 0); //数组合并不要用 array_merge , 会重数组键 作为options导致bug
$modControllers = $this->permModel->getControllers();

View File

@ -40,6 +40,7 @@ class Role extends Controller
$table->show('id', 'ID');
$table->show('name', '名称');
$table->show('users', '用户数');
$table->show('description', '描述')->default('无描述');
$table->text('sort', '排序')->autoPost()->getWapper()->addStyle('max-width:40px');
$table->show('create_time', '添加时间')->getWapper()->addStyle('width:180px');
@ -132,6 +133,7 @@ class Role extends Controller
'name',
'description',
'sort',
'tags'
], 'post');
$result = $this->validate($data, [
@ -267,6 +269,7 @@ class Role extends Controller
$form->text('name', '名称')->maxlength(25)->required();
$form->textarea('description', '描述')->maxlength(100);
$form->text('sort', '排序')->required()->default(1);
$form->tags('tags', '标签');
if ($isEdit) {
$form->show('create_time', '添加时间');

View File

@ -0,0 +1,85 @@
<?php
namespace tpext\myadmin\admin\model;
use think\Model;
class AdminGroup extends Model
{
protected $autoWriteTimestamp = 'dateTime';
public function getUsersAttr($value, $data)
{
$count = AdminUser::where('group_id', $data['id'])->count();
return $count ? $count : 0;
}
public function buildList($parent = 0, $deep = 0)
{
$roots = static::where(['parent_id' => $parent])->order('sort')->select();
$data = [];
$deep += 1;
foreach ($roots as $root) {
if ($deep == 1) {
$root['title_show'] = '├─' . $root['name'];
} else if ($deep == 2) {
$root['title_show'] = str_repeat('&nbsp;', 8) . '├─' . $root['name'];
} else if ($deep == 3) {
$root['title_show'] = str_repeat('&nbsp;', 16) . '├─' . $root['name'];
} else if ($deep == 4) {
$root['title_show'] = str_repeat('&nbsp;', 24) . '├─' . $root['name'];
} else if ($deep == 5) {
$root['title_show'] = str_repeat('&nbsp;', 32) . '├─' . $root['name'];
} else if ($deep == 5) {
$root['title_show'] = str_repeat('&nbsp;', 40) . '├─' . $root['name'];
}
$root['title_show'];
$data[] = $root;
$data = array_merge($data, $this->buildList($root->id, $deep));
}
return $data;
}
public function buildTree($parent = 0, $deep = 0, $except = 0)
{
$roots = static::where(['parent_id' => $parent])->order('sort')->field('id,name,parent_id')->select();
$data = [];
$deep += 1;
foreach ($roots as $root) {
if ($deep == 1) {
$root['title_show'] = '├─' . $root['name'];
} else if ($deep == 2) {
$root['title_show'] = '──├─' . $root['name'];
} else if ($deep == 3) {
$root['title_show'] = '────├─' . $root['name'];
} else if ($deep == 4) {
$root['title_show'] = '──────├─' . $root['name'];
} else if ($deep == 5) {
$root['title_show'] = '────────├─' . $root['name'];
} else if ($deep == 5) {
$root['title_show'] = '──────────├─' . $root['name'];
}
if ($root['id'] == $except) {
continue;
}
$root['title_show'];
$data[$root['id']] = $root['title_show'];
$data += $this->buildTree($root->id, $deep);
}
return $data;
}
}

View File

@ -7,4 +7,10 @@ use think\Model;
class AdminRole extends Model
{
protected $autoWriteTimestamp = 'dateTime';
public function getUsersAttr($value, $data)
{
$count = AdminUser::where('role_id', $data['id'])->count();
return $count ? $count : 0;
}
}

View File

@ -8,6 +8,13 @@ class AdminUser extends Model
{
protected $autoWriteTimestamp = 'dateTime';
public static function current()
{
$admin_id = session('admin_id');
return static::get($admin_id);
}
/**
* Undocumented function
*
@ -44,6 +51,12 @@ class AdminUser extends Model
return $role ? $role['name'] : '--';
}
public function getGroupNameAttr($value, $data)
{
$group = AdminGroup::get($data['group_id']);
return $group ? $group['name'] : '--';
}
public function checkPermission($admin_id, $controller, $action)
{
$data = static::get($admin_id);

View File

@ -17,7 +17,7 @@ class Module extends baseModule
protected $root = __DIR__ . '/../../';
protected $modules = [
'admin' => ['index', 'permission', 'role', 'admin', 'menu', 'operationlog'],
'admin' => ['index', 'permission', 'role', 'admin', 'group', 'menu', 'operationlog'],
];
public function install()