Menu.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\controller\store;
  13. use app\admin\controller\Controller;
  14. use app\admin\model\store\Menu as MenuModel;
  15. /**
  16. * 商家后台菜单控制器
  17. * Class Menu
  18. * @package app\store\controller
  19. */
  20. class Menu extends Controller
  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 index()
  30. {
  31. $model = new MenuModel;
  32. $list = $model->getList();
  33. return $this->renderSuccess(compact('list'));
  34. }
  35. /**
  36. * 菜单详情
  37. * @param int $menuId
  38. * @return array
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function info(int $menuId)
  44. {
  45. // 菜单详情
  46. $model = MenuModel::detail($menuId);
  47. return $this->renderSuccess(['info' => $model]);
  48. }
  49. /**
  50. * 添加菜单
  51. * @return array
  52. */
  53. public function add()
  54. {
  55. // 新增记录
  56. $model = new MenuModel;
  57. if ($model->add($this->postForm())) {
  58. return $this->renderSuccess('添加成功');
  59. }
  60. return $this->renderError($model->getError() ?: '添加失败');
  61. }
  62. /**
  63. * 更新菜单
  64. * @param $menuId
  65. * @return array|string
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. */
  70. public function edit(int $menuId)
  71. {
  72. // 菜单详情
  73. $model = MenuModel::detail($menuId);
  74. // 更新记录
  75. if ($model->edit($this->postForm())) {
  76. return $this->renderSuccess('更新成功');
  77. }
  78. return $this->renderError($model->getError() ?: '更新失败');
  79. }
  80. /**
  81. * 设置菜单绑定的Api
  82. * @param int $menuId
  83. * @return array
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. */
  88. public function setApis(int $menuId)
  89. {
  90. // 菜单详情
  91. $model = MenuModel::detail($menuId);
  92. // 更新记录
  93. if ($model->setApis($this->postForm())) {
  94. return $this->renderSuccess('操作成功');
  95. }
  96. return $this->renderError($model->getError() ?: '操作失败');
  97. }
  98. /**
  99. * 删除菜单
  100. * @param $menuId
  101. * @return array
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. public function delete(int $menuId)
  107. {
  108. // 菜单详情
  109. $model = MenuModel::detail($menuId);
  110. if (!$model->remove()) {
  111. return $this->renderError($model->getError() ?: '删除失败');
  112. }
  113. return $this->renderSuccess('删除成功');
  114. }
  115. }