catchAdmin/catch/permissions/model/Permissions.php

78 lines
2.3 KiB
PHP
Raw Normal View History

2019-12-09 16:22:00 +08:00
<?php
namespace catchAdmin\permissions\model;
2020-01-13 21:23:24 +08:00
use catchAdmin\permissions\model\search\PermissionsSearch;
2019-12-11 21:00:14 +08:00
use catcher\base\CatchModel;
2019-12-09 16:22:00 +08:00
2019-12-11 21:00:14 +08:00
class Permissions extends CatchModel
2019-12-09 16:22:00 +08:00
{
2020-01-13 21:23:24 +08:00
use PermissionsSearch;
2019-12-09 16:22:00 +08:00
protected $name = 'permissions';
protected $field = [
2020-04-14 19:16:28 +08:00
'id', //
'permission_name', // 菜单名称
'parent_id', // 父级ID
'icon',
'component', // 组件
'redirect',
'keep_alive',
'hide_children_in_menu',
'creator_id',
'module', // 模块
'route', // 路由
'method', // 请求方法
'permission_mark', // 权限标识
'type', // 1 菜单 2 按钮
'sort', // 排序字段
'created_at', // 创建时间
'updated_at', // 更新时间
'deleted_at', // 删除状态null 未删除 timestamp 已删除
2019-12-09 16:22:00 +08:00
];
2019-12-11 21:00:14 +08:00
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';
2020-04-14 20:31:38 +08:00
public function getList($isMenu = false)
2019-12-11 21:00:14 +08:00
{
2020-01-13 21:23:24 +08:00
return $this->catchSearch()
->order('sort', 'desc')
->order('id', 'desc')
2020-04-14 20:31:38 +08:00
->when($isMenu, function ($query){
$query->where('type', self::MENU_TYPE);
})
->select();
2019-12-11 21:00:14 +08:00
}
2019-12-09 16:22:00 +08:00
public function roles(): \think\model\relation\BelongsToMany
{
2019-12-12 09:13:29 +08:00
return $this->belongsToMany(Roles::class, 'role_has_permissions', 'role_id', 'permission_id');
2019-12-09 16:22:00 +08:00
}
2020-01-14 08:23:29 +08:00
/**
* 获取当前用户权限
*
* @time 2020年01月14日
2020-01-17 11:29:33 +08:00
* @param array $permissionIds
* @return \think\Collection
2020-01-14 08:23:29 +08:00
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
2020-01-17 11:29:33 +08:00
* @throws \think\db\exception\DataNotFoundException
2020-01-14 08:23:29 +08:00
*/
2020-01-17 11:29:33 +08:00
public static function getCurrentUserPermissions(array $permissionIds): \think\Collection
2020-01-14 08:23:29 +08:00
{
2020-01-17 11:29:33 +08:00
return parent::whereIn('id', $permissionIds)
2020-04-16 17:14:56 +08:00
->field(['permission_name as title', 'id', 'parent_id',
'route', 'icon', 'component', 'redirect',
'keep_alive as keepAlive', 'hide_children_in_menu', 'type'
])
2020-01-14 08:23:29 +08:00
->select();
}
2019-12-27 09:52:03 +08:00
}