CouponreceiveController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace app\index\controller;
  3. use think\exception\ValidateException;
  4. use app\model\CouponReceive;
  5. use app\model\Coupon;
  6. class CouponreceiveController extends Base
  7. {
  8. public function submitorder()
  9. {
  10. $price = input('get.price', '', 'serach_in');
  11. $where['weid'] = weid();
  12. $where['uid'] = UID();
  13. $query = CouponReceive::where($where);
  14. $query->where(['is_use' => 0]);
  15. $query->where('end_time', '>=', time());
  16. if (!empty($price)) {
  17. $query->where('min_price', '<=', $price);
  18. }
  19. $data = $query->select()->toArray();
  20. foreach ($data as &$cvo) {
  21. /*
  22. if ($cvo['expire_type'] == 10) {
  23. $cvo['end_time'] = strtotime("+" . $cvo['expire_day'] . " day", $cvo['create_time']);
  24. CouponReceive::where('id', $cvo['id'])->update(['end_time'=>$cvo['end_time']]);
  25. }*/
  26. $cvo['reduce_price'] = number_format($cvo['reduce_price'], 0);
  27. $cvo['min_price'] = number_format($cvo['min_price'], 0);
  28. $cvo['start_time'] = time_ymd($cvo['start_time']);
  29. $cvo['end_time'] = time_ymd($cvo['end_time']);
  30. }
  31. return $this->json(['data' => $data]);
  32. }
  33. public function mylist()
  34. {
  35. $ptype = input('get.ptype', '', 'serach_in');
  36. $price = input('get.price', '', 'serach_in');
  37. $where['weid'] = weid();
  38. $where['uid'] = UID();
  39. $query = CouponReceive::where($where);
  40. if (!empty($price)) {
  41. $query->where('min_price', '<=', $price);
  42. }
  43. if (empty($ptype)) {
  44. $query->where(['is_use' => 0]);
  45. } elseif ($ptype == 1) {
  46. $query->where(['is_use' => 1]);
  47. } elseif ($ptype == 2) {
  48. $query->where('end_time', '<', time());
  49. }
  50. $data = $query->select()->toArray();
  51. foreach ($data as &$cvo) {
  52. $cvo['reduce_price'] = number_format($cvo['reduce_price'], 0);
  53. $cvo['min_price'] = number_format($cvo['min_price'], 0);
  54. $cvo['start_time'] = time_ymd($cvo['start_time']);
  55. $cvo['end_time'] = time_ymd($cvo['end_time']);
  56. }
  57. return $this->json(['data' => $data]);
  58. }
  59. //返回可领取的优惠券
  60. public function couponlist()
  61. {
  62. global $_W;
  63. $errno = 0;
  64. $message = '返回消息';
  65. $uid = UID();
  66. $CouponReceive = CouponReceive::where(['uid' => $uid])->select()->toArray();
  67. $coupon_ids = array();
  68. for ($i = 0; $i < count($CouponReceive); $i++) {
  69. array_push($coupon_ids, $CouponReceive[$i]['coupon_id']);
  70. }
  71. $data = Coupon::where(['weid' => weid(), 'ptype' => 1])->select()->toArray();
  72. foreach ($data as &$cvo) {
  73. $cvo['reduce_price'] = number_format($cvo['reduce_price'], 0);
  74. $cvo['min_price'] = number_format($cvo['min_price'], 0);
  75. $cvo['start_time'] = time_ymd($cvo['start_time']);
  76. $cvo['end_time'] = time_ymd($cvo['end_time']);
  77. if (in_array($cvo['id'], $coupon_ids)) {
  78. $cvo['fetch'] = 1;
  79. }
  80. }
  81. return $this->json(['data' => $data]);
  82. }
  83. public function add()
  84. {
  85. $id = input('post.coupon_id', '', 'serach_in');
  86. $Coupondata = Coupon::where(['weid' => weid(), 'id' => $id])->find();
  87. if (!empty($Coupondata)) {
  88. $Coupondata = $Coupondata->toArray();
  89. if ($Coupondata['total_num'] == -1 || $Coupondata['total_num'] > $Coupondata['receive_num']) {
  90. unset($Coupondata['id']);
  91. $Coupondata['uid'] = UID();
  92. $Coupondata['coupon_id'] = $id;
  93. $r = CouponReceive::create($Coupondata);
  94. if ($r) {
  95. $message = "领取成功";
  96. $receive_num = $Coupondata['receive_num'] + 1;
  97. Coupon::update(['id' => $id, 'receive_num' => $receive_num]);
  98. } else {
  99. $message = "领取失败";
  100. }
  101. } else {
  102. $message = "领取失败,你来晚了";
  103. }
  104. }
  105. return $this->json(['message' => $message, 'data' => $data]);
  106. }
  107. //领取优惠券
  108. public function fetch()
  109. {
  110. $errno = 0;
  111. $message = '返回消息';
  112. $uid = UID();
  113. $id = input('post.id', '', 'intval');
  114. $Coupondata = Coupon::where(['weid' => weid(), 'id' => $id])->find();
  115. $havecoupon = CouponReceive::where(['coupon_id' => $id, 'uid' => $uid])->find();
  116. if (empty($havecoupon)) {
  117. if (!empty($Coupondata)) {
  118. $Coupondata = $Coupondata->toArray();
  119. //领取优惠券前,判断发放数量
  120. if ($Coupondata['total_num'] == -1 || $Coupondata['total_num'] > $Coupondata['receive_num']) {
  121. unset($Coupondata['id']);
  122. $Coupondata['uid'] = UID() ? UID() : 0;
  123. $Coupondata['coupon_id'] = $id;
  124. if ($Coupondata['expire_type'] == 10) {
  125. $Coupondata['end_time'] = strtotime("+" . $Coupondata['expire_day'] . " day");
  126. }
  127. unset($Coupondata['create_time']);
  128. unset($Coupondata['update_time']);
  129. $r = CouponReceive::create($Coupondata);
  130. if ($r) {
  131. $message = "领取成功";
  132. //领取成功后更新优惠券领取数量
  133. $receive_num = $Coupondata['receive_num'] + 1;
  134. Coupon::update(['id' => $id, 'receive_num' => $receive_num]);
  135. } else {
  136. $message = "领取失败";
  137. }
  138. } else {
  139. $errno = '2001';
  140. $message = '领取失败,你来晚了';
  141. }
  142. }
  143. } else {
  144. $errno = '2003';
  145. $message = '已经领取过了!';
  146. }
  147. return $this->json(['message' => $message, 'errno' => $errno, 'data' => $data]);
  148. }
  149. }