Menu.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Repositories\Models\Base;
  3. use App\Repositories\Models\Model;
  4. use Illuminate\Support\Facades\DB;
  5. class Menu extends Model
  6. {
  7. /**
  8. * @var string
  9. */
  10. protected $table = 'base_menus';
  11. protected $guarded = [];
  12. /**
  13. * The attributes excluded from the model's JSON form.
  14. *
  15. * @var array
  16. */
  17. protected $hidden = [];
  18. protected $casts = [
  19. 'meta' => 'json'
  20. ];
  21. const TYPE_MENU = 1;
  22. const TYPE_BTN = 2;
  23. public static function checkNameIsUnique($name, $ignore_id = 0)
  24. {
  25. return self::query()->where('name', $name)->where('type', self::TYPE_MENU)->where('id', '<>', $ignore_id)->exists();
  26. }
  27. public static function checkBtnPathIsUnique($path, $ignore_id = 0)
  28. {
  29. return self::query()->where('path', $path)->where('type', self::TYPE_BTN)->where('id', '<>', $ignore_id)->exists();
  30. }
  31. /**
  32. * 是否存在这个安全权限
  33. * @param $user_id
  34. * @param $btn_id
  35. * @return bool
  36. */
  37. public static function checkUserIsBtn($btn_id = 150, $admin_id = 0)
  38. {
  39. if (!$admin_id) $admin_id = login_admin_id();
  40. if (in_array($admin_id, config('site.superAdmin_ids', [1]))) return true;
  41. $roleIds = DB::table('base_roles_menus')->where('menu_id', $btn_id)->pluck('role_id');
  42. if (!count($roleIds)) return false;
  43. return DB::table('base_model_has_roles')->where('model_id', $admin_id)->whereIn('role_id', $roleIds)->exists();
  44. }
  45. }