Unit.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\store\controller\setting;
  4. use app\store\controller\Controller;
  5. use app\common\model\Unit as UnitModel;
  6. /**
  7. * 计量单位
  8. * Class Unit
  9. * @package app\store\controller\setting
  10. */
  11. class Unit extends Controller
  12. {
  13. /**
  14. * 列表
  15. * @return array
  16. * @throws \think\db\exception\DbException
  17. */
  18. public function list()
  19. {
  20. $model = new UnitModel;
  21. $list = $model->getList($this->request->param());
  22. return $this->renderSuccess(compact('list'));
  23. }
  24. /**
  25. * 获取所有记录
  26. * @return array
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\DbException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. */
  31. public function all()
  32. {
  33. $model = new UnitModel;
  34. $list = $model->getAll();
  35. return $this->renderSuccess(compact('list'));
  36. }
  37. /**
  38. * 获取详情记录
  39. * @param int $id
  40. * @return array
  41. */
  42. public function detail(int $id)
  43. {
  44. $detail = UnitModel::detail($id);
  45. return $this->renderSuccess(compact('detail'));
  46. }
  47. /**
  48. * 添加
  49. * @return array
  50. */
  51. public function add()
  52. {
  53. // 新增记录
  54. $model = new UnitModel;
  55. if ($model->add($this->postForm())) {
  56. return $this->renderSuccess('添加成功');
  57. }
  58. return $this->renderError($model->getError() ?: '添加失败');
  59. }
  60. /**
  61. * 编辑
  62. * @param int $id
  63. * @return array
  64. */
  65. public function edit(int $id)
  66. {
  67. // 详情
  68. $model = UnitModel::detail($id);
  69. // 更新记录
  70. if ($model->edit($this->postForm())) {
  71. return $this->renderSuccess('更新成功');
  72. }
  73. return $this->renderError($model->getError() ?: '更新失败');
  74. }
  75. /**
  76. * 删除
  77. * @param int $id
  78. * @return array
  79. */
  80. public function delete(int $id)
  81. {
  82. // 详情
  83. $model = UnitModel::detail($id);
  84. if (!$model->delete()) {
  85. return $this->renderError($model->getError() ?: '删除失败');
  86. }
  87. return $this->renderSuccess('删除成功');
  88. }
  89. }