catchAdmin/catch/permissions/model/Permissions.php

70 lines
1.8 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 = [
'id', //
2019-12-11 21:00:14 +08:00
'permission_name', // 菜单名称
2019-12-09 16:22:00 +08:00
'parent_id', // 父级ID
2019-12-28 21:21:05 +08:00
'icon',
2020-01-08 22:02:16 +08:00
'creator_id',
2019-12-28 21:21:05 +08:00
'module', // 模块
2019-12-09 16:22:00 +08:00
'route', // 路由
2019-12-28 21:21:05 +08:00
'method', // 请求方法
2019-12-09 16:22:00 +08:00
'permission_mark', // 权限标识
'type', // 1 菜单 2 按钮
'sort', // 排序字段
'created_at', // 创建时间
'updated_at', // 更新时间
'deleted_at', // 删除状态null 未删除 timestamp 已删除
];
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-01-13 21:23:24 +08:00
public function getList()
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')
->select()
->toArray();
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-01-14 08:23:29 +08:00
->field(['permission_name as title', 'route', 'icon'])
->select();
}
2019-12-27 09:52:03 +08:00
}