Guarantee.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\controller\admin\store;
  3. use app\common\repositories\store\GuaranteeRepository;
  4. use app\validate\admin\GuaranteeValidate;
  5. use think\App;
  6. use ln\basic\BaseController;
  7. class Guarantee extends BaseController
  8. {
  9. /**
  10. * @var GuaranteeRepository
  11. */
  12. protected $repository;
  13. /**
  14. * City constructor.
  15. * @param App $app
  16. * @param GuaranteeRepository $repository
  17. */
  18. public function __construct(App $app, GuaranteeRepository $repository)
  19. {
  20. parent::__construct($app);
  21. $this->repository = $repository;
  22. }
  23. public function lst()
  24. {
  25. [$page, $limit] = $this->getPage();
  26. $where = $this->request->params(['date','keyword']);
  27. $where['is_del'] = 0;
  28. $data = $this->repository->getList($where,$page, $limit);
  29. return app('json')->success($data);
  30. }
  31. public function createForm()
  32. {
  33. return app('json')->success(formToData($this->repository->form(null)));
  34. }
  35. public function create(GuaranteeValidate $validate)
  36. {
  37. $data = $this->checkParams($validate);
  38. $this->repository->create($data);
  39. return app('json')->success('添加成功');
  40. }
  41. public function updateForm($id)
  42. {
  43. return app('json')->success(formToData($this->repository->updateForm($id)));
  44. }
  45. public function update($id,GuaranteeValidate $validate)
  46. {
  47. $ret = $this->repository->get($id);
  48. if(!$ret) return app('json')->fail('数据不存在');
  49. $data = $this->checkParams($validate);
  50. $this->repository->update($id,$data);
  51. return app('json')->success('编辑成功');
  52. }
  53. public function sort($id)
  54. {
  55. $ret = $this->repository->get($id);
  56. if(!$ret) return app('json')->fail('数据不存在');
  57. $data = [
  58. 'sort' => $this->request->param('sort'),
  59. ];
  60. $this->repository->update($id,$data);
  61. return app('json')->success('修改成功');
  62. }
  63. public function switchStatus($id)
  64. {
  65. $ret = $this->repository->get($id);
  66. if(!$ret) return app('json')->fail('数据不存在');
  67. $data = [
  68. 'status' => $this->request->param('status') == 1 ?: 0,
  69. ];
  70. $this->repository->update($id,$data);
  71. return app('json')->success('修改成功');
  72. }
  73. public function detail($id)
  74. {
  75. $ret = $this->repository->get($id);
  76. if(!$ret) return app('json')->fail('数据不存在');
  77. return app('json')->success($ret);
  78. }
  79. public function delete($id)
  80. {
  81. $ret = $this->repository->get($id);
  82. if(!$ret) return app('json')->fail('数据不存在');
  83. $this->repository->update($id,['is_del' => 1]);
  84. return app('json')->success('删除成功');
  85. }
  86. public function checkParams(GuaranteeValidate $validate)
  87. {
  88. $params = [
  89. "guarantee_name", "guarantee_info", "image", "status", "sort",
  90. ];
  91. $data = $this->request->params($params);
  92. $validate->check($data);
  93. return $data;
  94. }
  95. }