MenuApi.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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\MenuApi as MenuApiModel;
  14. /**
  15. * 商家后台用户角色与菜单权限关系表模型
  16. * Class MenuApi
  17. * @package app\admin\model\store
  18. */
  19. class MenuApi extends MenuApiModel
  20. {
  21. /**
  22. * 根据菜单id批量更新记录
  23. * @param int $menuId
  24. * @param array $apiIds
  25. * @return bool
  26. */
  27. public function updateByMenuId(int $menuId, array $apiIds): bool
  28. {
  29. return $this->transaction(function () use ($menuId, $apiIds) {
  30. $this->removeByMenuId($menuId);
  31. return $this->insertByMenuId($menuId, $apiIds);
  32. });
  33. }
  34. /**
  35. * 根据菜单id批量删除记录
  36. * @param int $menuId
  37. * @return void
  38. */
  39. private function removeByMenuId(int $menuId): void
  40. {
  41. static::deleteAll(['menu_id' => $menuId]);
  42. }
  43. /**
  44. * 根据菜单id批量新增记录
  45. * @param int $menuId
  46. * @param array $apiIds
  47. * @return bool
  48. */
  49. private function insertByMenuId(int $menuId, array $apiIds): bool
  50. {
  51. $data = [];
  52. foreach ($apiIds as $api) {
  53. $data[] = ['menu_id' => $menuId, 'api_id' => $api];
  54. }
  55. return (bool)$this->addAll($data);
  56. }
  57. }