Access.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\store\model\store;
  10. use app\common\model\store\Access as AccessModel;
  11. /**
  12. * 商家用户权限模型
  13. * Class Access
  14. * @package app\store\model\store
  15. */
  16. class Access extends AccessModel
  17. {
  18. /**
  19. * 获取权限列表 jstree格式
  20. * @param int $role_id 当前角色id
  21. * @return string
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. * @throws \think\exception\DbException
  25. */
  26. public function getJsTree($role_id = null)
  27. {
  28. $accessIds = is_null($role_id) ? [] : RoleAccess::getAccessIds($role_id);
  29. $jsTree = [];
  30. foreach ($this->getAll() as $item) {
  31. $jsTree[] = [
  32. 'id' => $item['access_id'],
  33. 'parent' => $item['parent_id'] > 0 ? $item['parent_id'] : '#',
  34. 'text' => $item['name'],
  35. 'state' => [
  36. 'selected' => (in_array($item['access_id'], $accessIds) && !$this->hasChildren($item['access_id']))
  37. ]
  38. ];
  39. }
  40. return json_encode($jsTree);
  41. }
  42. /**
  43. * 是否存在子集
  44. * @param $access_id
  45. * @return bool
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. * @throws \think\exception\DbException
  49. */
  50. private function hasChildren($access_id)
  51. {
  52. foreach (self::getAll() as $item) {
  53. if ($item['parent_id'] == $access_id)
  54. return true;
  55. }
  56. return false;
  57. }
  58. }