BroadcastGoods.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020/7/30
  7. *
  8. *
  9. */
  10. namespace app\controller\merchant\store\broadcast;
  11. use app\common\repositories\store\broadcast\BroadcastGoodsRepository;
  12. use app\validate\merchant\BroadcastGoodsValidate;
  13. use crmeb\basic\BaseController;
  14. use think\App;
  15. class BroadcastGoods extends BaseController
  16. {
  17. protected $repository;
  18. public function __construct(App $app, BroadcastGoodsRepository $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(['status_tag', 'keyword']);
  27. return app('json')->success($this->repository->getList($this->request->merId(), $where, $page, $limit));
  28. }
  29. public function detail($id)
  30. {
  31. if (!$this->repository->merExists($id, $this->request->merId()))
  32. return app('json')->fail('数据不存在');
  33. return app('json')->success($this->repository->get($id)->append(['product'])->toArray());
  34. }
  35. public function createForm()
  36. {
  37. return app('json')->success(formToData($this->repository->createForm()));
  38. }
  39. protected function checkParams(BroadcastGoodsValidate $validate)
  40. {
  41. $data = $this->request->params(['name', 'cover_img', 'product_id', 'price']);
  42. $validate->check($data);
  43. $data['product_id'] = $data['product_id']['id'];
  44. return $data;
  45. }
  46. public function create(BroadcastGoodsValidate $validate)
  47. {
  48. $this->repository->create($this->request->merId(), $this->checkParams($validate));
  49. return app('json')->success('创建成功');
  50. }
  51. public function batchCreate(BroadcastGoodsValidate $validate)
  52. {
  53. $goods = $this->request->param('goods', []);
  54. if (!count($goods)) return app('json')->fail('请选中商品');
  55. $validate->isBatch();
  56. foreach ($goods as $item) {
  57. $validate->check((array)$item);
  58. }
  59. $this->repository->batchCreate($this->request->merId(), $goods);
  60. return app('json')->success('创建成功');
  61. }
  62. public function updateForm($id)
  63. {
  64. if (!$this->repository->merExists($id, $this->request->merId()))
  65. return app('json')->fail('数据不存在');
  66. return app('json')->success(formToData($this->repository->updateForm($id)));
  67. }
  68. public function update($id, BroadcastGoodsValidate $validate)
  69. {
  70. if (!$this->repository->merExists($id, $this->request->merId()))
  71. return app('json')->fail('数据不存在');
  72. $this->repository->update($id, $this->checkParams($validate));
  73. return app('json')->success('编辑成功');
  74. }
  75. public function mark($id)
  76. {
  77. $mark = (string)$this->request->param('mark');
  78. if (!$this->repository->merExists($id, $this->request->merId()))
  79. return app('json')->fail('数据不存在');
  80. $this->repository->mark($id, $mark);
  81. return app('json')->success('修改成功');
  82. }
  83. public function changeStatus($id)
  84. {
  85. $isShow = $this->request->param('is_show') == 1 ? 1 : 0;
  86. if (!$this->repository->merExists($id, $this->request->merId()))
  87. return app('json')->fail('数据不存在');
  88. $this->repository->isShow($id, $isShow);
  89. return app('json')->success('修改成功');
  90. }
  91. public function delete($id)
  92. {
  93. if (!$this->repository->merExists($id, $this->request->merId()))
  94. return app('json')->fail('数据不存在');
  95. $this->repository->merDelete((int)$id);
  96. return app('json')->success('删除成功');
  97. }
  98. }