Menu.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2025 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. return $this->save($data);
  62. }
  63. /**
  64. * 设置菜单的API权限
  65. * @param array $data
  66. * @return bool
  67. */
  68. public function setApis(array $data): bool
  69. {
  70. if (empty($data['apiIds'])) {
  71. $this->error = 'API权限不能为空';
  72. return false;
  73. }
  74. // 根据菜单id批量更新API关联记录
  75. return (new MenuApiModel)->updateByMenuId($this['menu_id'], $data['apiIds']);
  76. }
  77. /**
  78. * 删除菜单
  79. * @return bool
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\DbException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. * @throws \Exception
  84. */
  85. public function remove(): bool
  86. {
  87. // 判断是否存在下级菜单
  88. if (self::detail(['parent_id' => $this['menu_id']])) {
  89. $this->error = '当前菜单下存在子菜单或操作,请先删除';
  90. return false;
  91. }
  92. // 清空菜单与API关联关系
  93. MenuApiModel::deleteAll(['menu_id' => $this['menu_id']]);
  94. // 删除API记录
  95. return $this->delete();
  96. }
  97. /**
  98. * 获取所有上级id集
  99. * @param int $menuId
  100. * @param null $menuList
  101. * @return array
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. private function getTopMenuIds(int $menuId, $menuList = null): array
  107. {
  108. static $ids = [];
  109. is_null($menuList) && $menuList = $this->getAll();
  110. foreach ($menuList as $item) {
  111. if ($item['menu_id'] == $menuId && $item['parent_id'] > 0) {
  112. $ids[] = $item['parent_id'];
  113. $this->getTopMenuIds($item['parent_id'], $menuList);
  114. }
  115. }
  116. return $ids;
  117. }
  118. }