Api.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\Api as ApiModel;
  16. /**
  17. * 商家后台API权限控制器
  18. * Class Api
  19. * @package app\store\controller
  20. */
  21. class Api 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 ApiModel;
  33. $list = $model->getList();
  34. return $this->renderSuccess(compact('list'));
  35. }
  36. /**
  37. * 新增权限
  38. * @return Json
  39. */
  40. public function add(): Json
  41. {
  42. // 新增记录
  43. $model = new ApiModel;
  44. if ($model->add($this->postForm())) {
  45. return $this->renderSuccess('添加成功');
  46. }
  47. return $this->renderError($model->getError() ?: '添加失败');
  48. }
  49. /**
  50. * 编辑权限
  51. * @param int $apiId
  52. * @return Json
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public function edit(int $apiId): Json
  58. {
  59. // 权限详情
  60. $model = ApiModel::detail($apiId);
  61. // 更新记录
  62. if ($model->edit($this->postForm())) {
  63. return $this->renderSuccess('更新成功');
  64. }
  65. return $this->renderError($model->getError() ?: '更新失败');
  66. }
  67. /**
  68. * 删除权限
  69. * @param int $apiId
  70. * @return Json
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\DbException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. */
  75. public function delete(int $apiId): Json
  76. {
  77. // 权限详情
  78. $model = ApiModel::detail($apiId);
  79. if (!$model->remove()) {
  80. return $this->renderError($model->getError() ?: '删除失败');
  81. }
  82. return $this->renderSuccess('删除成功');
  83. }
  84. }