123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?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',
- 'icon',
- 'component',
- 'redirect',
- 'keepalive',
- 'creator_id',
- 'hidden',
- 'module',
- 'route',
- 'permission_mark',
- 'type',
- 'sort',
- 'created_at',
- 'updated_at',
- 'deleted_at',
- ];
- 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()
- ->catchOrder()
- ->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');
- }
-
- public static function getCurrentUserPermissions(array $permissionIds): \think\Collection
- {
- return parent::whereIn('id', $permissionIds)
- ->field(['permission_name as title', 'id', 'parent_id',
- 'route', 'icon', 'component', 'redirect', 'module',
- 'keepalive as keepAlive', 'type', 'permission_mark', 'hidden'
- ])
- ->catchOrder()
- ->select();
- }
-
- public static function onAfterInsert(Model $model)
- {
- $model = self::where('id', $model->id)->find();
- if ($model && $model->parent_id) {
- $parent = self::where('id', $model->parent_id)->find();
- $level = $parent->level ? $parent->level . '-' . $parent->id : $parent->id;
- return $model->where('id', $model->id)->update([
- 'level' => $level
- ]);
- }
- return true;
- }
- public function show($id)
- {
- $permission = $this->findBy($id);
-
-
- $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(),
- ]);
- }
-
- 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;
- }
- }
|