Api.php 3.9 KB

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