Coupon.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace app\supplier\controller\coupon;
  3. use app\supplier\controller\Controller;
  4. use app\supplier\model\coupon\Coupon as CouponModel;
  5. use app\common\model\product\Category as CategoryModel;
  6. use app\supplier\model\product\Product as ProductModel;
  7. /**
  8. * 优惠券控制器
  9. */
  10. class Coupon extends Controller
  11. {
  12. /**
  13. * 优惠券列表
  14. */
  15. public function index()
  16. {
  17. $list = (new CouponModel)->getList($this->postData(),$this->getSupplierId());
  18. return $this->renderSuccess('', compact('list'));
  19. }
  20. /**
  21. * 添加优惠券
  22. */
  23. public function add()
  24. {
  25. $data = $this->postData();
  26. $data['shop_supplier_id'] = $this->getSupplierId();
  27. // 新增记录
  28. if ((new CouponModel)->add($data)) {
  29. return $this->renderSuccess('添加成功');
  30. }
  31. return $this->renderError('添加失败');
  32. }
  33. /**
  34. * 优惠券详情
  35. */
  36. public function couponDetail()
  37. {
  38. $coupon_id = $this->postData('coupon_id/i');
  39. // 优惠券详情
  40. $detail = CouponModel::detail($coupon_id)->toArray();
  41. if($detail['expire_type']==20){
  42. $detail['active_time'][0]=date('Y-m-d H:i:s',$detail['start_time']['value']);
  43. $detail['active_time'][1]=date('Y-m-d H:i:s',$detail['end_time']['value']);
  44. }
  45. if ($detail['product_ids'] != '') {
  46. $ProductModel = new ProductModel();
  47. $product = $ProductModel->getProduct($detail['product_ids']);
  48. $detail['product_list'] = $product->toArray();
  49. $detail['product'] = explode(',', $detail['product_ids']);
  50. }
  51. // 所有一级分类
  52. $category = CategoryModel::getFirstCategory();
  53. return $this->renderSuccess('', compact('detail', 'category'));
  54. }
  55. /**
  56. * 更新优惠券
  57. */
  58. public function edit($coupon_id)
  59. {
  60. $data = $this->postData();
  61. $model = CouponModel::detail($coupon_id);
  62. // 更新记录
  63. if ($model->edit($data)) {
  64. return $this->renderSuccess('更新成功');
  65. }
  66. return $this->renderError('更新失败');
  67. }
  68. /**
  69. * 删除优惠券
  70. */
  71. public function delete($coupon_id)
  72. {
  73. $coupon_id = $this->postData('coupon_id/i');
  74. // 优惠券详情
  75. $model = new CouponModel;
  76. // 更新记录
  77. if ($model->setDelete(['coupon_id' => $coupon_id])) {
  78. return $this->renderSuccess('删除成功');
  79. }
  80. return $this->renderError('删除失败');
  81. }
  82. }