catchAdmin/catch/permissions/model/Permissions.php

192 lines
5.2 KiB
PHP
Raw Normal View History

2020-04-29 17:37:45 +08:00
<?php
namespace catchAdmin\permissions\model;
use catchAdmin\permissions\model\search\PermissionsSearch;
use catcher\base\CatchModel;
use think\Model;
class Permissions extends CatchModel
{
use PermissionsSearch;
protected $name = 'permissions';
protected $field = [
'id', //
'permission_name', // 菜单名称
'parent_id', // 父级ID
2021-05-13 20:22:07 +08:00
'level', // 层级
2020-04-29 17:37:45 +08:00
'icon',
'component', // 组件
'redirect',
'keepalive',
'creator_id',
2020-09-05 16:01:18 +08:00
'hidden',
2020-04-29 17:37:45 +08:00
'module', // 模块
'route', // 路由
'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($isMenu = false)
{
return $this->catchSearch()
2020-06-17 13:24:02 +08:00
->catchOrder()
2020-04-29 17:37:45 +08:00
->when($isMenu, function ($query){
$query->where('type', self::MENU_TYPE);
})
->select();
}
public function roles(): \think\model\relation\BelongsToMany
{
return $this->belongsToMany(Roles::class, 'role_has_permissions', 'role_id', 'permission_id');
}
/**
* 获取当前用户权限
*
* @time 2020年01月14日
* @param array $permissionIds
* @return \think\Collection
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\DataNotFoundException
*/
public static function getCurrentUserPermissions(array $permissionIds): \think\Collection
{
return parent::whereIn('id', $permissionIds)
->field(['permission_name as title', 'id', 'parent_id',
2020-07-29 07:30:05 +08:00
'route', 'icon', 'component', 'redirect', 'module',
2020-09-05 16:01:18 +08:00
'keepalive as keepAlive', 'type', 'permission_mark', 'hidden'
2020-04-29 17:37:45 +08:00
])
2020-06-17 13:24:02 +08:00
->catchOrder()
2020-04-29 17:37:45 +08:00
->select();
}
/**
* 插入后回调 更新 level
*
* @time 2020年04月22日
* @param Model $model
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return array|bool|Model|void|null
*/
public static function onAfterInsert(Model $model)
{
2021-05-10 18:34:18 +08:00
$modelData = $model->getData();
2021-05-13 20:22:07 +08:00
$restful = intval($modelData['restful'] ?? 0);
2020-04-29 17:37:45 +08:00
2021-05-13 20:22:07 +08:00
$model = self::where('id', $model->id)->find();
2021-05-13 20:22:07 +08:00
if ($model && $model->parent_id) {
$parent = self::where('id', $model->parent_id)->find();
2021-05-13 20:22:07 +08:00
$level = $parent->level ? $parent->level . '-' . $parent->id : $parent->id;
2021-05-13 20:22:07 +08:00
$restful && self::createRestful($model, $level);
2020-04-29 17:37:45 +08:00
2021-05-13 20:22:07 +08:00
$model->updateBy('id', [
'level' => $level
]);
2021-05-10 18:34:18 +08:00
}
2021-05-13 20:22:07 +08:00
return true;
2020-04-29 17:37:45 +08:00
}
2020-09-06 11:18:12 +08:00
/**
* 创建 restful 菜单
*
* @time 2021年04月20日
* @param Model $model
* @param $level
* @return void
*/
protected static function createRestful(Model $model, $level)
{
$restful = [
'index' => '列表',
'save' => '保存',
'update' => '更新',
'delete' => '删除',
];
foreach ($restful as $k => $r) {
self::insert([
'parent_id' => $model->id,
'permission_name' => $r,
'level' => $level . '-' . $model->id,
'module' => $model->getData('module'),
'creator_id' => $model->getData('creator_id'),
'permission_mark' => $model->getData('permission_mark') . '@' . $k,
'type' => self::BTN_TYPE,
'created_at' => time(),
'updated_at' => time(),
'sort' => 1,
]);
}
}
2021-05-13 20:22:07 +08:00
/**
* 展示
*
* @time 2021年05月13日
* @param $id
* @return Permissions
*/
2020-09-06 11:18:12 +08:00
public function show($id)
{
$permission = $this->findBy($id);
// 不能使用改属性判断,模型有该属性,使用数组方式
// $permission->hidden
$hidden = $permission['hidden'] == Permissions::ENABLE ? Permissions::DISABLE : Permissions::ENABLE;
$nextLevelIds = $this->getNextLevel([$id]);
$nextLevelIds[] = $id;
return $this->whereIn('id', $nextLevelIds)->update([
'hidden' => $hidden,
'updated_at' => time(),
]);
}
/**
* 获取 level ids
*
* @time 2020年09月06日
* @param array $id
* @param array $ids
* @return array
*/
protected function getNextLevel(array $id, &$ids = [])
{
$_ids = $this->whereIn('parent_id', $id)
->where('type', self::MENU_TYPE)
->column('id');
if (count($_ids)) {
$ids = array_merge($_ids, $this->getNextLevel($_ids, $ids));
}
return $ids;
}
2020-04-29 17:37:45 +08:00
}