StoreSeckill.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\store;
  12. use think\App;
  13. use crmeb\basic\BaseController;
  14. use app\validate\admin\StoreSeckillValidate;
  15. use app\common\repositories\store\StoreSeckillTimeRepository as repository;
  16. /**
  17. * 秒杀场次配置
  18. */
  19. class StoreSeckill extends BaseController
  20. {
  21. /**
  22. * @var repository
  23. */
  24. protected $repository;
  25. /**
  26. * Express constructor.
  27. * @param App $app
  28. * @param repository $repository
  29. */
  30. public function __construct(App $app, repository $repository)
  31. {
  32. parent::__construct($app);
  33. $this->repository = $repository;
  34. }
  35. /**
  36. * 列表
  37. * @Author:Qinii
  38. * @Date: 2020/5/13
  39. * @return mixed
  40. */
  41. public function lst()
  42. {
  43. [$page, $limit] = $this->getPage();
  44. $where = $this->request->params(['title', 'status']);
  45. return app('json')->success($this->repository->getList($where, $page, $limit));
  46. }
  47. /**
  48. * 秒杀活动可选场次
  49. * @param $active_id 秒杀活动ID
  50. * @return \think\response\Json
  51. * FerryZhao 2024/4/15
  52. */
  53. public function select()
  54. {
  55. $activeId = $this->request->param('active_id') ?: null;
  56. $list = $this->repository->select($activeId);
  57. return app('json')->success($list);
  58. }
  59. /**
  60. * 添加
  61. * @Author:Qinii
  62. * @Date: 2020/5/13
  63. * @return mixed
  64. */
  65. public function create(StoreSeckillValidate $validate)
  66. {
  67. $data = $this->checkParams($validate);
  68. if (!$this->repository->checkTime($data, null))
  69. return app('json')->fail('时间段不可重叠');
  70. $this->repository->create($data);
  71. return app('json')->success('添加成功');
  72. }
  73. /**
  74. * 编辑
  75. * @param $id
  76. * @param StoreSeckillValidate $validate
  77. * @return mixed
  78. * @author Qinii
  79. * @day 2020-07-31
  80. */
  81. public function update($id, StoreSeckillValidate $validate)
  82. {
  83. $data = $this->checkParams($validate);
  84. if (!$this->repository->checkTime($data, $id))
  85. return app('json')->fail('时间段不可重叠');
  86. $this->repository->update($id, $data);
  87. return app('json')->success('编辑成功');
  88. }
  89. /**
  90. * 删除
  91. * @param $id
  92. * @return mixed
  93. * @author Qinii
  94. * @day 2020-07-31
  95. */
  96. public function delete($id)
  97. {
  98. if (!$this->repository->get($id))
  99. return app('json')->fail('数据不存在');
  100. $this->repository->delete($id);
  101. return app('json')->success('删除成功');
  102. }
  103. /**
  104. * 创建表单
  105. * @return mixed
  106. * @author Qinii
  107. * @day 2020-07-31
  108. */
  109. public function createForm()
  110. {
  111. return app('json')->success(formToData($this->repository->form()));
  112. }
  113. /**
  114. * 编辑表单
  115. * @param $id
  116. * @return mixed
  117. * @author Qinii
  118. * @day 2020-07-31
  119. */
  120. public function updateForm($id)
  121. {
  122. return app('json')->success(formToData($this->repository->updateForm($id)));
  123. }
  124. /**
  125. * 修改状态
  126. * @param $id
  127. * @return mixed
  128. * @author Qinii
  129. * @day 2020-07-31
  130. */
  131. public function switchStatus($id)
  132. {
  133. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  134. if (!$this->repository->get($id))
  135. return app('json')->fail('数据不存在');
  136. $this->repository->update($id, ['status' => $status]);
  137. return app('json')->success('修改成功');
  138. }
  139. /**
  140. * 检测参数
  141. * @param StoreSeckillValidate $validate
  142. * @return array|mixed|string|string[]
  143. * @author Qinii
  144. */
  145. public function checkParams(StoreSeckillValidate $validate)
  146. {
  147. $data = $this->request->params(['start_time', 'end_time', 'status', 'title', 'pic']);
  148. $validate->check($data);
  149. return $data;
  150. }
  151. }