Api.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\admin\model\store;
  13. use app\admin\model\store\MenuApi as MenuApiModel;
  14. use app\common\model\store\Api as ApiModel;
  15. /**
  16. * 商家后台API权限模型
  17. * Class Api
  18. * @package app\admin\model\store
  19. */
  20. class Api extends ApiModel
  21. {
  22. /**
  23. * 获取权限列表
  24. * @return array
  25. * @throws \think\db\exception\DataNotFoundException
  26. * @throws \think\db\exception\DbException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. */
  29. public function getList(): array
  30. {
  31. $all = static::getAll();
  32. return $this->getTreeData($all);
  33. }
  34. /**
  35. * 新增记录
  36. * @param array $data
  37. * @return bool|false
  38. */
  39. public function add(array $data): bool
  40. {
  41. return $this->allowField(['name', 'parent_id', 'url', 'sort'])->save($data);
  42. }
  43. /**
  44. * 更新记录
  45. * @param array $data
  46. * @return bool
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public function edit(array $data): bool
  52. {
  53. // 判断上级角色是否为当前子级
  54. if ($data['parent_id'] > 0) {
  55. // 获取所有上级id集
  56. $parentIds = $this->getTopApiIds($data['parent_id']);
  57. if (in_array($this['api_id'], $parentIds)) {
  58. $this->error = '上级权限不允许设置为当前子权限';
  59. return false;
  60. }
  61. }
  62. return $this->allowField(['name', 'parent_id', 'url', 'sort'])->save($data);
  63. }
  64. /**
  65. * 删除权限
  66. * @return bool
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\DbException
  69. * @throws \think\db\exception\ModelNotFoundException
  70. * @throws \Exception
  71. */
  72. public function remove(): bool
  73. {
  74. // 判断是否存在下级权限
  75. if (self::detail(['parent_id' => $this['api_id']])) {
  76. $this->error = '当前权限下存在子权限,请先删除';
  77. return false;
  78. }
  79. // 清空菜单与API关联关系
  80. MenuApiModel::deleteAll(['api_id' => $this['api_id']]);
  81. // 删除API记录
  82. return $this->delete();
  83. }
  84. /**
  85. * 获取所有上级id集
  86. * @param int $apiId
  87. * @param null $all
  88. * @return array
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\DbException
  91. * @throws \think\db\exception\ModelNotFoundException
  92. */
  93. private function getTopApiIds(int $apiId, $all = null): array
  94. {
  95. static $ids = [];
  96. is_null($all) && $all = $this->getAll();
  97. foreach ($all as $item) {
  98. if ($item['api_id'] == $apiId && $item['parent_id'] > 0) {
  99. $ids[] = $item['parent_id'];
  100. $this->getTopApiIds($item['parent_id'], $all);
  101. }
  102. }
  103. return $ids;
  104. }
  105. /**
  106. * 获取树状列表
  107. * @param array $list
  108. * @param int $parentId
  109. * @return array
  110. */
  111. private function getTreeData(array &$list, int $parentId = 0): array
  112. {
  113. $data = [];
  114. foreach ($list as $key => $item) {
  115. if ($item['parent_id'] == $parentId) {
  116. $children = $this->getTreeData($list, (int)$item['api_id']);
  117. !empty($children) && $item['children'] = $children;
  118. $data[] = $item;
  119. unset($list[$key]);
  120. }
  121. }
  122. return $data;
  123. }
  124. }