Discounts.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\merchant\store\product;
  12. use app\common\repositories\store\product\StoreDiscountRepository;
  13. use app\validate\merchant\StoreDiscountsValidate;
  14. use crmeb\basic\BaseController;
  15. use think\App;
  16. use think\exception\ValidateException;
  17. class Discounts extends BaseController
  18. {
  19. protected $repository ;
  20. /**
  21. * Product constructor.
  22. * @param App $app
  23. * @param StoreDiscountRepository $repository
  24. */
  25. public function __construct(App $app ,StoreDiscountRepository $repository)
  26. {
  27. parent::__construct($app);
  28. $this->repository = $repository;
  29. }
  30. public function lst()
  31. {
  32. [$page, $limit] = $this->getPage();
  33. $status = $this->request->param('status');
  34. $where = $this->request->params(['keyword','store_name','title','type']);
  35. $where['is_show'] = $status;
  36. $where['mer_id'] = $this->request->merId();
  37. $data = $this->repository->getMerlist($where, $page, $limit);
  38. return app('json')->success($data);
  39. }
  40. public function create()
  41. {
  42. $data = $this->checkParams();
  43. $this->repository->save($data);
  44. return app('json')->success('添加成功');
  45. }
  46. public function update($id)
  47. {
  48. $data = $this->checkParams();
  49. if (!$this->repository->getWhere(['mer_id' => $data['mer_id'], $this->repository->getPk() => $id]))
  50. return app('json')->fail('数据不存在');
  51. $data['discount_id'] = $id;
  52. $this->repository->save($data);
  53. return app('json')->success('编辑成功');
  54. }
  55. public function detail($id)
  56. {
  57. $data = $this->repository->detail($id, $this->request->merId());
  58. if (!$data ) return app('json')->fail('数据不存在');
  59. return app('json')->success($data);
  60. }
  61. public function switchStatus($id)
  62. {
  63. $status = $this->request->param('status') == 1 ?: 0;
  64. if (!$this->repository->getWhere([$this->repository->getPk() => $id,'mer_id' => $this->request->merId()]))
  65. return app('json')->fail('数据不存在');
  66. $this->repository->update($id, ['is_show' => $status]);
  67. return app('json')->success('修改成功');
  68. }
  69. public function delete($id)
  70. {
  71. if (!$this->repository->getWhere([$this->repository->getPk() => $id,'mer_id' => $this->request->merId()]))
  72. return app('json')->fail('数据不存在');
  73. $this->repository->update($id, ['is_del' => 1]);
  74. return app('json')->success('删除成功');
  75. }
  76. public function checkParams()
  77. {
  78. $params = [
  79. ['title', ''],
  80. ['image', ''],
  81. ['type', 0],
  82. ['is_limit', 0],
  83. ['limit_num', 0],
  84. ['is_time', 0],
  85. ['time', []],
  86. ['sort', 0],
  87. ['free_shipping', 0],
  88. ['status', 0],
  89. ['is_show', 1],
  90. ['products', []],
  91. ['temp_id',0],
  92. ];
  93. $data = $this->request->params($params);
  94. app()->make(StoreDiscountsValidate::class)->check($data);
  95. if ($data['is_time'] && is_array($data['time'])) {
  96. if (empty($data['time'])) throw new ValidateException('开始时间必须填写');
  97. [$start, $end] = $data['time'];
  98. $start = strtotime($start);
  99. $end = strtotime($end);
  100. if($start > $end){
  101. throw new ValidateException('开始时间必须小于结束时间');
  102. }
  103. if($start < time() || $end < time()){
  104. throw new ValidateException('套餐时间不能小于当前时间');
  105. }
  106. }
  107. foreach ($data['products'] as $item) {
  108. if (!isset($item['items']))
  109. return app('json')->fail('请选择' . $item['store_name'] . '的规格');
  110. foreach ($item['attr'] as $attr) {
  111. if($attr['active_price'] > $attr['price']) return app('json')->fail('套餐价格高于原价');
  112. }
  113. }
  114. $data['mer_id'] = $this->request->merId();
  115. return $data;
  116. }
  117. }