Api.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\store\model\store;
  13. use app\common\model\store\Api as ApiModel;
  14. use app\common\library\helper;
  15. /**
  16. * 商家用户权限模型
  17. * Class Api
  18. * @package app\store\model\store
  19. */
  20. class Api extends ApiModel
  21. {
  22. /**
  23. * 获取权限列表 jstree格式
  24. * @param int $role_id 当前角色id
  25. * @return string
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. */
  29. public function getJsTree($role_id = null)
  30. {
  31. $apiIds = is_null($role_id) ? [] : RoleAccess::getAccessIds($role_id);
  32. $jsTree = [];
  33. foreach ($this->getAll() as $item) {
  34. $jsTree[] = [
  35. 'id' => $item['api_id'],
  36. 'parent' => $item['parent_id'] > 0 ? $item['parent_id'] : '#',
  37. 'text' => $item['name'],
  38. 'state' => [
  39. 'selected' => (in_array($item['api_id'], $apiIds) && !$this->hasChildren($item['api_id']))
  40. ]
  41. ];
  42. }
  43. return helper::jsonEncode($jsTree);
  44. }
  45. /**
  46. * 是否存在子集
  47. * @param $api_id
  48. * @return bool
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. private function hasChildren($api_id)
  53. {
  54. foreach (self::getAll() as $item) {
  55. if ($item['parent_id'] == $api_id)
  56. return true;
  57. }
  58. return false;
  59. }
  60. }