MenuApi.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\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 int
  26. */
  27. public function updateByMenuId(int $menuId, array $apiIds)
  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 bool
  38. * @throws \Exception
  39. */
  40. private function removeByMenuId(int $menuId)
  41. {
  42. return $this->where('menu_id', '=', $menuId)->delete();
  43. }
  44. /**
  45. * 根据菜单id批量新增记录
  46. * @param int $menuId
  47. * @param array $apiIds
  48. * @return bool
  49. */
  50. private function insertByMenuId(int $menuId, array $apiIds)
  51. {
  52. $data = [];
  53. foreach ($apiIds as $api) {
  54. $data[] = ['menu_id' => $menuId, 'api_id' => $api];
  55. }
  56. return (bool)$this->addAll($data);
  57. }
  58. }