SystemRouteCate.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  7. * +----------------------------------------------------------------------
  8. * | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  9. * +----------------------------------------------------------------------
  10. * | Author: CRMEB Team <admin@crmeb.com>
  11. * +----------------------------------------------------------------------
  12. */
  13. namespace app\adminapi\controller\v1\setting;
  14. use app\adminapi\controller\AuthController;
  15. use app\services\system\SystemRouteCateServices;
  16. use app\services\system\SystemRouteServices;
  17. use think\facade\App;
  18. use think\Request;
  19. /**
  20. * Class SystemRouteCate
  21. * @author 等风来
  22. * @email 136327134@qq.com
  23. * @date 2023/4/6
  24. * @package app\adminapi\controller\v1\setting
  25. */
  26. class SystemRouteCate extends AuthController
  27. {
  28. /**
  29. * SystemRouteCate constructor.
  30. * @param App $app
  31. * @param SystemRouteCateServices $services
  32. */
  33. public function __construct(App $app, SystemRouteCateServices $services)
  34. {
  35. parent::__construct($app);
  36. $this->services = $services;
  37. }
  38. /**
  39. * @return \think\Response
  40. * @author 等风来
  41. * @email 136327134@qq.com
  42. * @date 2023/4/6
  43. */
  44. public function index()
  45. {
  46. return app('json')->success($this->services->getAllList());
  47. }
  48. /**
  49. * @return \think\Response
  50. * @author 等风来
  51. * @email 136327134@qq.com
  52. * @date 2023/4/6
  53. */
  54. public function create()
  55. {
  56. return app('json')->success($this->services->getFrom(0, $this->request->get('app_name', 'adminapi')));
  57. }
  58. /**
  59. * @param Request $request
  60. * @return \think\Response
  61. * @author 等风来
  62. * @email 136327134@qq.com
  63. * @date 2023/4/6
  64. */
  65. public function save(Request $request)
  66. {
  67. $data = $request->postMore([
  68. ['pid', ''],
  69. ['name', ''],
  70. ['sort', 0],
  71. ['app_name', ''],
  72. ]);
  73. if (!$data['name']) {
  74. return app('json')->fail('缺少分类名称');
  75. }
  76. $data['add_time'] = time();
  77. $pathAttr = explode('/', $data['pid']);
  78. $pathData = [];
  79. foreach ($pathAttr as $item) {
  80. if (!$item) {
  81. $pathData[] = $item;
  82. }
  83. }
  84. $data['pid'] = $pathData[count($pathData) - 1] ?? 0;
  85. $res = $this->services->save($data);
  86. $path = $this->services->setPathValue($data['pid'], $res->id);
  87. $res->path = $path;
  88. $res->save();
  89. return app('json')->success('保存成功');
  90. }
  91. /**
  92. * @param $id
  93. * @return \think\Response
  94. * @author 等风来
  95. * @email 136327134@qq.com
  96. * @date 2023/4/6
  97. */
  98. public function edit($id)
  99. {
  100. return app('json')->success($this->services->getFrom($id));
  101. }
  102. /**
  103. * @param Request $request
  104. * @param $id
  105. * @return \think\Response
  106. * @author 等风来
  107. * @email 136327134@qq.com
  108. * @date 2023/4/6
  109. */
  110. public function update(Request $request, $id)
  111. {
  112. $data = $request->postMore([
  113. ['pid', 0],
  114. ['name', ''],
  115. ['sort', 0],
  116. ['app_name', ''],
  117. ]);
  118. if (!$data['name']) {
  119. return app('json')->fail('缺少分类名称');
  120. }
  121. $pid = $this->services->value($id, 'pid');
  122. if ($data['pid'] != $pid) {
  123. $path = $this->services->getPathValue($data['pid']);
  124. $data['path'] = $this->services->setPathValue($path, $id);
  125. }
  126. $this->services->update($id, $data);
  127. return app('json')->success('修改成功');
  128. }
  129. /**
  130. * @param SystemRouteServices $service
  131. * @param $id
  132. * @return \think\Response
  133. * @author 等风来
  134. * @email 136327134@qq.com
  135. * @date 2023/4/6
  136. */
  137. public function delete(SystemRouteServices $service, $id)
  138. {
  139. if (!$id) {
  140. return app('json')->fail('缺少参数');
  141. }
  142. if ($service->count(['cate_id' => $id])) {
  143. return app('json')->fail('请先删除分类下的接口');
  144. }
  145. $this->services->delete($id);
  146. return app('json')->success('删除成功');
  147. }
  148. }