Menu.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\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. * @param $data
  25. * @return false|int
  26. */
  27. public function add(array $data)
  28. {
  29. return $this->save($data);
  30. }
  31. /**
  32. * 更新记录
  33. * @param array $data
  34. * @return bool
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. public function edit(array $data)
  40. {
  41. // 判断上级角色是否为当前子级
  42. if (isset($data['parent_id']) && $data['parent_id'] > 0) {
  43. // 获取所有上级id集
  44. $parentIds = $this->getTopMenuIds($data['parent_id']);
  45. if (in_array($this['menu_id'], $parentIds)) {
  46. $this->error = '上级菜单不允许设置为当前子菜单';
  47. return false;
  48. }
  49. }
  50. // 如果模块是操作, 不允许修改上级菜单id
  51. if ($this['module'] == 20 && isset($data['parent_id'])) {
  52. unset($data['parent_id']);
  53. }
  54. return $this->save($data) !== false;
  55. }
  56. /**
  57. * 设置菜单的API权限
  58. * @param array $data
  59. * @return bool|int
  60. */
  61. public function setApis(array $data)
  62. {
  63. if (!isset($data['apiIds']) || empty($data['apiIds'])) {
  64. $this->error = 'API权限不能为空';
  65. return false;
  66. }
  67. // 根据菜单id批量更新API关联记录
  68. return (new MenuApiModel)->updateByMenuId($this['menu_id'], $data['apiIds']);
  69. }
  70. /**
  71. * 删除菜单
  72. * @return bool
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. * @throws \Exception
  77. */
  78. public function remove()
  79. {
  80. // 判断是否存在下级菜单
  81. if (self::detail(['parent_id' => $this['menu_id']])) {
  82. $this->error = '当前菜单下存在子菜单或操作,请先删除';
  83. return false;
  84. }
  85. return $this->delete();
  86. }
  87. /**
  88. * 获取所有上级id集
  89. * @param int $menuId
  90. * @param null $menuList
  91. * @return array
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\DbException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. */
  96. private function getTopMenuIds(int $menuId, $menuList = null)
  97. {
  98. static $ids = [];
  99. is_null($menuList) && $menuList = $this->getAll();
  100. foreach ($menuList as $item) {
  101. if ($item['menu_id'] == $menuId && $item['parent_id'] > 0) {
  102. $ids[] = $item['parent_id'];
  103. $this->getTopMenuIds($item['parent_id'], $menuList);
  104. }
  105. }
  106. return $ids;
  107. }
  108. }