Menu.php 3.8 KB

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