Api.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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\library\helper;
  14. use app\common\model\store\Api as ApiModel;
  15. /**
  16. * 商家用户权限模型
  17. * Class Api
  18. * @package app\store\model\store
  19. */
  20. class Api extends ApiModel
  21. {
  22. /**
  23. * 获取权限列表 jstree格式
  24. * @param int|null $roleId 当前角色id
  25. * @return string
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\DbException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. */
  30. public function getJsTree(int $roleId = null): string
  31. {
  32. $apiIds = is_null($roleId) ? [] : RoleAccess::getAccessIds($roleId);
  33. $jsTree = [];
  34. foreach ($this->getAll() as $item) {
  35. $jsTree[] = [
  36. 'id' => $item['api_id'],
  37. 'parent' => $item['parent_id'] > 0 ? $item['parent_id'] : '#',
  38. 'text' => $item['name'],
  39. 'state' => [
  40. 'selected' => (in_array($item['api_id'], $apiIds) && !$this->hasChildren($item['api_id']))
  41. ]
  42. ];
  43. }
  44. return helper::jsonEncode($jsTree);
  45. }
  46. /**
  47. * 是否存在子集
  48. * @param $apiId
  49. * @return bool
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. private function hasChildren($apiId): bool
  55. {
  56. foreach (self::getAll() as $item) {
  57. if ($item['parent_id'] == $apiId)
  58. return true;
  59. }
  60. return false;
  61. }
  62. }