StoreAttrTemplate.php 3.2 KB

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