Coupon.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\coupon\shopapi\controller;
  11. use addon\coupon\model\Coupon as CouponModel;
  12. use addon\coupon\model\CouponType as CouponTypeModel;
  13. use addon\coupon\model\MemberCoupon;
  14. use app\shopapi\controller\BaseApi;
  15. /**
  16. * 优惠券
  17. */
  18. class Coupon extends BaseApi
  19. {
  20. public function __construct()
  21. {
  22. //执行父类构造函数
  23. parent::__construct();
  24. $token = $this->checkToken();
  25. if ($token[ 'code' ] < 0) {
  26. echo $this->response($token);
  27. exit;
  28. }
  29. }
  30. /**
  31. * 活动列表
  32. */
  33. public function lists()
  34. {
  35. $coupon_type_model = new CouponTypeModel();
  36. $page = isset($this->params[ 'page' ]) ? $this->params[ 'page' ] : 1;
  37. $page_size = isset($this->params[ 'page_size' ]) ? $this->params[ 'page_size' ] : PAGE_LIST_ROWS;
  38. $coupon_name = isset($this->params[ 'coupon_name' ]) ? $this->params[ 'coupon_name' ] : '';
  39. $status = isset($this->params[ 'status' ]) ? $this->params[ 'status' ] : '';
  40. $condition = [];
  41. if ($status !== "") {
  42. $condition[] = [ 'status', '=', $status ];
  43. }
  44. $type = isset($this->params[ 'type' ]) ? $this->params[ 'type' ] : '';
  45. if ($type) {
  46. $condition[] = [ 'type', '=', $type ];
  47. }
  48. //类型
  49. $validity_type = isset($this->params[ 'validity_type' ]) ? $this->params[ 'validity_type' ] : '';
  50. if ($validity_type) {
  51. $start_time = isset($this->params[ 'start_time' ]) ? $this->params[ 'start_time' ] : '';
  52. $end_time = isset($this->params[ 'end_time' ]) ? $this->params[ 'end_time' ] : '';
  53. switch ( $validity_type ) {
  54. case 1: //固定
  55. $condition[] = [ 'end_time', 'between', [ $start_time, $end_time ] ];
  56. break;
  57. case 2:
  58. $condition[] = [ 'fixed_term', 'between', [ $start_time, $end_time ] ];
  59. break;
  60. }
  61. }
  62. $condition[] = [ 'site_id', '=', $this->site_id ];
  63. $condition[] = [ 'coupon_name', 'like', '%' . $coupon_name . '%' ];
  64. $order = 'create_time desc';
  65. $field = '*';
  66. $res = $coupon_type_model->getCouponTypePageList($condition, $page, $page_size, $order, $field);
  67. //获取优惠券状态
  68. $coupon_type_status_arr = $coupon_type_model->getCouponTypeStatus();
  69. foreach ($res[ 'data' ][ 'list' ] as $key => $val) {
  70. $res[ 'data' ][ 'list' ][ $key ][ 'status_name' ] = $coupon_type_status_arr[ $val[ 'status' ] ];
  71. }
  72. return $this->response($res);
  73. }
  74. /**
  75. * 发送优惠券
  76. */
  77. public function send()
  78. {
  79. $member_id = isset($this->params[ 'member_id' ]) ? $this->params[ 'member_id' ] : 0;
  80. $coupon_type_ids = isset($this->params[ 'parent' ]) ? $this->params[ 'parent' ] : '';
  81. $get_type = isset($this->params[ 'get_type' ]) ? $this->params[ 'get_type' ] : 4;
  82. $site_id = $this->site_id;
  83. $parent = $coupon_type_ids;
  84. if (empty($parent)) {
  85. return $this->error('', 'REQUEST_COUPON_TYPE_ID');
  86. }
  87. $parent = explode(",", $parent);
  88. if (count($parent) == 1) {
  89. $coupon_model = new CouponModel();
  90. $res = $coupon_model->receiveCoupon($parent[ 0 ], $site_id, $member_id, $get_type);
  91. } else {
  92. $membercoupon_model = new MemberCoupon();
  93. $res = $membercoupon_model->sendCoupon(explode(',',$coupon_type_ids), $site_id, $member_id, $get_type);
  94. }
  95. return $this->response($res);
  96. }
  97. }