Menu.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\common\model\store\Menu as MenuModel;
  14. use app\admin\model\store\MenuApi as MenuApiModel;
  15. /**
  16. * 商家后台菜单模型
  17. * Class Menu
  18. * @package app\admin\model\store
  19. */
  20. class Menu extends MenuModel
  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. return $this->getTreeData(static::getAll());
  32. }
  33. /**
  34. * 新增记录
  35. * @param array $data
  36. * @return bool
  37. */
  38. public function add(array $data): bool
  39. {
  40. return $this->save($data);
  41. }
  42. /**
  43. * 更新记录
  44. * @param array $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(array $data): bool
  51. {
  52. // 判断上级角色是否为当前子级
  53. if (isset($data['parent_id']) && $data['parent_id'] > 0) {
  54. // 获取所有上级id集
  55. $parentIds = $this->getTopMenuIds($data['parent_id']);
  56. if (in_array($this['menu_id'], $parentIds)) {
  57. $this->error = '上级菜单不允许设置为当前子菜单';
  58. return false;
  59. }
  60. }
  61. // 如果模块是操作, 不允许修改上级菜单id
  62. if ($this['module'] == 20 && isset($data['parent_id'])) {
  63. unset($data['parent_id']);
  64. }
  65. return $this->save($data);
  66. }
  67. /**
  68. * 设置菜单的API权限
  69. * @param array $data
  70. * @return bool
  71. */
  72. public function setApis(array $data): bool
  73. {
  74. if (empty($data['apiIds'])) {
  75. $this->error = 'API权限不能为空';
  76. return false;
  77. }
  78. // 根据菜单id批量更新API关联记录
  79. return (new MenuApiModel)->updateByMenuId($this['menu_id'], $data['apiIds']);
  80. }
  81. /**
  82. * 删除菜单
  83. * @return bool
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. * @throws \Exception
  88. */
  89. public function remove(): bool
  90. {
  91. // 判断是否存在下级菜单
  92. if (self::detail(['parent_id' => $this['menu_id']])) {
  93. $this->error = '当前菜单下存在子菜单或操作,请先删除';
  94. return false;
  95. }
  96. // 清空菜单与API关联关系
  97. MenuApiModel::deleteAll(['menu_id' => $this['menu_id']]);
  98. // 删除API记录
  99. return $this->delete();
  100. }
  101. /**
  102. * 获取所有上级id集
  103. * @param int $menuId
  104. * @param null $menuList
  105. * @return array
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. */
  110. private function getTopMenuIds(int $menuId, $menuList = null): array
  111. {
  112. static $ids = [];
  113. is_null($menuList) && $menuList = $this->getAll();
  114. foreach ($menuList as $item) {
  115. if ($item['menu_id'] == $menuId && $item['parent_id'] > 0) {
  116. $ids[] = $item['parent_id'];
  117. $this->getTopMenuIds($item['parent_id'], $menuList);
  118. }
  119. }
  120. return $ids;
  121. }
  122. }