StoreAttrTemplate.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace app\controller\merchant\store;
  3. use ln\basic\BaseController;
  4. use app\common\repositories\store\StoreAttrTemplateRepository;
  5. use app\validate\merchant\StoreAttrTemplateValidate;
  6. use think\App;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. /**
  11. * Class StoreAttrTemplate
  12. * @package app\controller\merchant\store
  13. * @author zfy
  14. * @day 2020-05-06
  15. */
  16. class StoreAttrTemplate extends BaseController
  17. {
  18. /**
  19. * @var StoreAttrTemplateRepository
  20. */
  21. protected $repository;
  22. /**
  23. * StoreAttrTemplate constructor.
  24. * @param App $app
  25. * @param StoreAttrTemplateRepository $repository
  26. */
  27. public function __construct(App $app, StoreAttrTemplateRepository $repository)
  28. {
  29. parent::__construct($app);
  30. $this->repository = $repository;
  31. }
  32. /**
  33. * @return mixed
  34. * @throws DbException
  35. * @throws DataNotFoundException
  36. * @throws ModelNotFoundException
  37. * @author zfy
  38. * @day 2020-05-06
  39. */
  40. public function lst()
  41. {
  42. [$page, $limit] = $this->getPage();
  43. $data = $this->repository->getList($this->request->merId(), [], $page, $limit);
  44. return app('json')->success($data);
  45. }
  46. public function getlist()
  47. {
  48. return app('json')->success($this->repository->list($this->request->merId()));
  49. }
  50. /**
  51. * @param StoreAttrTemplateValidate $validate
  52. * @return mixed
  53. * @author zfy
  54. * @day 2020-05-06
  55. */
  56. public function create(StoreAttrTemplateValidate $validate)
  57. {
  58. $data = $this->checkParams($validate);
  59. $data['mer_id'] = $this->request->merId();
  60. $this->repository->create($data);
  61. return app('json')->success('添加成功');
  62. }
  63. /**
  64. * @param $id
  65. * @param StoreAttrTemplateValidate $validate
  66. * @return mixed
  67. * @throws DbException
  68. * @author zfy
  69. * @day 2020-05-06
  70. */
  71. public function update($id, StoreAttrTemplateValidate $validate)
  72. {
  73. $merId = $this->request->merId();
  74. if (!$this->repository->merExists($merId, $id))
  75. return app('json')->fail('数据不存在');
  76. $data = $this->checkParams($validate);
  77. $data['mer_id'] = $merId;
  78. $this->repository->update($id, $data);
  79. return app('json')->success('编辑成功');
  80. }
  81. /**
  82. * @param $id
  83. * @return mixed
  84. * @throws DbException
  85. * @author zfy
  86. * @day 2020-05-06
  87. */
  88. public function delete($id)
  89. {
  90. $merId = $this->request->merId();
  91. if (!$this->repository->merExists($merId, $id))
  92. return app('json')->fail('数据不存在');
  93. $this->repository->delete($id, $merId);
  94. return app('json')->success('删除成功');
  95. }
  96. /**
  97. * @param StoreAttrTemplateValidate $validate
  98. * @return array
  99. * @author zfy
  100. * @day 2020-05-06
  101. */
  102. public function checkParams(StoreAttrTemplateValidate $validate)
  103. {
  104. $data = $this->request->params(['template_name', ['template_value', []]]);
  105. $validate->check($data);
  106. return $data;
  107. }
  108. }