Coupon.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\coupon\api\controller;
  11. use app\api\controller\BaseApi;
  12. use addon\coupon\model\Coupon as CouponModel;
  13. use addon\coupon\model\CouponType as CouponTypeModel;
  14. use addon\coupon\model\MemberCoupon;
  15. use think\facade\Db;
  16. /**
  17. * 优惠券
  18. */
  19. class Coupon extends BaseApi
  20. {
  21. /**
  22. * 优惠券类型信息
  23. */
  24. public function typeinfo()
  25. {
  26. $coupon_type_id = isset($this->params[ 'coupon_type_id' ]) ? $this->params[ 'coupon_type_id' ] : 0;
  27. if (empty($coupon_type_id)) {
  28. return $this->response($this->error('', 'REQUEST_COUPON_TYPE_ID'));
  29. }
  30. $app_type = isset($this->params[ 'app_type' ]) ? $this->params[ 'app_type' ] : 'h5';
  31. $coupon_model = new CouponModel();
  32. $condition = [
  33. [ 'coupon_type_id', '=', $coupon_type_id ],
  34. [ 'is_show', '=', 1 ],
  35. [ 'site_id', '=', $this->site_id ]
  36. ];
  37. $coupon_type_model = new CouponTypeModel();
  38. $qrcode = $coupon_type_model->qrcode($coupon_type_id, $app_type, $this->site_id)[ 'data' ];
  39. $info = $coupon_model->getCouponTypeInfo($condition);
  40. if (!empty($info[ 'data' ]) && !empty($qrcode)) {
  41. $info[ 'data' ][ 'qrcode' ] = $qrcode[ 'path' ];
  42. }
  43. return $this->response($info);
  44. }
  45. /**
  46. * 列表信息
  47. */
  48. public function memberpage()
  49. {
  50. $token = $this->checkToken();
  51. if ($token[ 'code' ] < 0) return $this->response($token);
  52. $page = isset($this->params[ 'page' ]) ? $this->params[ 'page' ] : 1;
  53. $page_size = isset($this->params[ 'page_size' ]) ? $this->params[ 'page_size' ] : PAGE_LIST_ROWS;
  54. $state = isset($this->params[ 'state' ]) ? $this->params[ 'state' ] : 1;//优惠券状态 1已领用(未使用) 2已使用 3已过期
  55. $coupon_model = new CouponModel();
  56. $condition = [
  57. [ 'npc.member_id', '=', $token[ 'data' ][ 'member_id' ] ],
  58. [ 'npc.state', '=', $state ]
  59. ];
  60. //按类型筛选
  61. $type = isset($this->params[ 'type' ]) ? $this->params[ 'type' ] : '';
  62. $related_id = isset($this->params[ 'related_id' ]) ? $this->params[ 'related_id' ] : 0;
  63. switch ( $type ) {
  64. case "reward"://满减
  65. $condition[] = [ "npc.type", "=", "reward" ];
  66. break;
  67. case "discount"://折扣
  68. $condition[] = [ "npc.type", "=", "discount" ];
  69. break;
  70. case "no_threshold"://无门槛
  71. $condition[] = [ "npc.at_least", "=", 0 ];
  72. break;
  73. }
  74. if (!empty($related_id)) {
  75. $condition[] = [ 'related_id', '=', $related_id ];
  76. }
  77. $list = $coupon_model->getMemberCouponPageList($condition, $page, $page_size);
  78. return $this->response($list);
  79. }
  80. /**
  81. * 优惠券类型列表
  82. */
  83. public function typelists()
  84. {
  85. $num = isset($this->params[ 'num' ]) ? $this->params[ 'num' ] : 0;
  86. $coupon_type_id_arr = isset($this->params[ 'coupon_type_id_arr' ]) ? $this->params[ 'coupon_type_id_arr' ] : '';//coupon_type_id数组
  87. $can_receive = isset($this->params[ 'can_receive' ]) ? $this->params[ 'can_receive' ] : 0;// 是否只查询可领取的
  88. $token = $this->checkToken();
  89. $coupon_model = new CouponModel();
  90. $condition = [
  91. [ 'status', '=', 1 ],
  92. [ 'is_show', '=', 1 ],
  93. [ 'site_id', '=', $this->site_id ]
  94. ];
  95. //按类型查询
  96. $type = isset($this->params[ 'type' ]) ? $this->params[ 'type' ] : '';
  97. switch ( $type ) {
  98. case "reward"://满减
  99. $condition[] = [ "type", "=", "reward" ];
  100. break;
  101. case "discount"://折扣
  102. $condition[] = [ "type", "=", "discount" ];
  103. break;
  104. case "no_threshold"://无门槛
  105. $condition[] = [ "at_least", "=", 0 ];
  106. break;
  107. }
  108. if (!empty($coupon_type_id_arr)) {
  109. $condition[] = [ 'coupon_type_id', 'in', $coupon_type_id_arr ];
  110. }
  111. $field = 'coupon_type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,image,validity_type,fixed_term,status,is_show,goods_type,discount_limit,count,lead_count,IF(count < 0 or count - lead_count > 0, 1, 0) as is_remain';
  112. if ($can_receive == 1) {
  113. $condition[] = [ [ 'count', '<>', Db::raw('lead_count') ] ];
  114. }
  115. $order = Db::raw('IF(count < 0 or count - lead_count > 0, 1, 0) DESC,sort ASC');
  116. $list = $coupon_model->getCouponTypeList($condition, $field, $order, $num);
  117. if (!empty($list[ 'data' ]) && $this->member_id) {
  118. foreach ($list[ 'data' ] as $k => $v) {
  119. $list[ 'data' ][ $k ][ 'member_coupon_num' ] = $coupon_model->getCouponCount([
  120. [ 'get_type', '=', 2 ],
  121. [ 'member_id', '=', $this->member_id ],
  122. [ 'coupon_type_id', '=', $v[ 'coupon_type_id' ] ]
  123. ])[ 'data' ];
  124. }
  125. }
  126. return $this->response($list);
  127. }
  128. /**
  129. * 优惠券类型分页列表
  130. */
  131. public function typepagelists()
  132. {
  133. $page = isset($this->params[ 'page' ]) ? $this->params[ 'page' ] : 1;
  134. $page_size = isset($this->params[ 'page_size' ]) ? $this->params[ 'page_size' ] : PAGE_LIST_ROWS;
  135. $coupon_type_id_arr = isset($this->params[ 'coupon_type_id_arr' ]) ? $this->params[ 'coupon_type_id_arr' ] : '';//coupon_type_id数组
  136. $can_receive = isset($this->params[ 'can_receive' ]) ? $this->params[ 'can_receive' ] : 0;// 是否只查询可领取的
  137. $token = $this->checkToken();
  138. $coupon_model = new CouponModel();
  139. $condition = [
  140. [ 'status', '=', 1 ],
  141. [ 'is_show', '=', 1 ],
  142. [ 'site_id', '=', $this->site_id ]
  143. ];
  144. //按类型查询
  145. $type = isset($this->params[ 'type' ]) ? $this->params[ 'type' ] : '';
  146. switch ( $type ) {
  147. case "reward"://满减
  148. $condition[] = [ "type", "=", "reward" ];
  149. break;
  150. case "discount"://折扣
  151. $condition[] = [ "type", "=", "discount" ];
  152. break;
  153. case "no_threshold"://无门槛
  154. $condition[] = [ "at_least", "=", 0 ];
  155. break;
  156. }
  157. if (!empty($coupon_type_id_arr)) {
  158. $condition[] = [ 'coupon_type_id', 'in', $coupon_type_id_arr ];
  159. }
  160. $field = 'coupon_type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,image,validity_type,fixed_term,status,is_show,goods_type,discount_limit,count,lead_count,IF(count < 0 or count - lead_count > 0, 1, 0) as is_remain';
  161. if ($can_receive == 1) {
  162. $condition[] = [ [ 'count', '<>', Db::raw('lead_count') ] ];
  163. }
  164. if ($this->member_id) {
  165. $prefix = config('database.connections.mysql.prefix');
  166. $field .= ', (select count(coupon_id) from ' . $prefix . 'promotion_coupon pc where pc.coupon_type_id = ct.coupon_type_id and pc.get_type=2 and pc.member_id=' . $this->member_id . ') as member_coupon_num';
  167. }
  168. $order = Db::raw('IF(count < 0 or count - lead_count > 0, 1, 0) DESC,sort ASC');
  169. $list = $coupon_model->getCouponTypePageList($condition, $page, $page_size, $order, $field, 'ct');
  170. return $this->response($list);
  171. }
  172. /**
  173. * 获取优惠券
  174. */
  175. public function receive()
  176. {
  177. $token = $this->checkToken();
  178. if ($token[ 'code' ] < 0) return $this->response($token);
  179. $site_id = $this->site_id;
  180. $coupon_type_id = isset($this->params[ 'coupon_type_id' ]) ? $this->params[ 'coupon_type_id' ] : 0;
  181. $get_type = isset($this->params[ 'get_type' ]) ? $this->params[ 'get_type' ] : 2;//获取方式:1订单2.直接领取3.活动领取
  182. if (empty($coupon_type_id)) {
  183. return $this->response($this->error('', 'REQUEST_COUPON_TYPE_ID'));
  184. }
  185. $coupon_model = new CouponModel();
  186. $res = $coupon_model->receiveCoupon($coupon_type_id, $site_id, $token[ 'data' ][ 'member_id' ], $get_type);
  187. $res[ 'data' ] = [];
  188. //判断一下用户是否拥有当前优惠券
  189. $coupon_result = $coupon_model->getCouponInfo([ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'site_id', '=', $site_id ], [ 'member_id', '=', $token[ 'data' ][ 'member_id' ] ] ], 'coupon_id');
  190. $coupon = $coupon_result[ 'data' ];
  191. $res[ 'data' ][ 'is_exist' ] = empty($coupon) ? 0 : 1;
  192. return $this->response($res);
  193. }
  194. /**
  195. * 会员优惠券数量
  196. * @return string
  197. */
  198. public function num()
  199. {
  200. $token = $this->checkToken();
  201. if ($token[ 'code' ] < 0) return $this->response($token);
  202. $state = $this->params[ 'state' ] ?? 1;
  203. $coupon_model = new MemberCoupon();
  204. $count = $coupon_model->getMemberCouponNum($token[ 'data' ][ 'member_id' ], $state);
  205. return $this->response($count);
  206. }
  207. /**
  208. * 是否可以领取
  209. */
  210. public function receivedNum()
  211. {
  212. $token = $this->checkToken();
  213. if ($token[ 'code' ] < 0) return $this->response($token);
  214. $coupon_type_id = isset($this->params[ 'coupon_type_id' ]) ? $this->params[ 'coupon_type_id' ] : 0;
  215. $coupon_model = new MemberCoupon();
  216. $res = $coupon_model->receivedNum($coupon_type_id, $this->member_id);
  217. return $this->response($res);
  218. }
  219. /**
  220. * 查询商品可用的优惠券
  221. * @param int $id
  222. * @return false|string
  223. */
  224. public function goodsCoupon($id = 0)
  225. {
  226. $this->checkToken();
  227. $coupon_model = new CouponModel();
  228. $goods_id = $this->params[ 'goods_id' ] ?? 0;
  229. if (!empty($id)) {
  230. $goods_id = $id;
  231. }
  232. $condition = [
  233. [ 'site_id', '=', $this->site_id ],
  234. [ 'status', '=', 1 ],
  235. [ 'is_show', '=', 1 ],
  236. [ 'goods_type', '=', 1 ]
  237. ];
  238. $field = 'count,lead_count,coupon_type_id,coupon_type_id as type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,validity_type,fixed_term,goods_type,discount_limit';
  239. if ($this->member_id) {
  240. $prefix = config('database.connections.mysql.prefix');
  241. $field .= ',(select count(coupon_id) from ' . $prefix . 'promotion_coupon pc where pc.coupon_type_id = type_id and pc.get_type=2 and pc.member_id=' . $this->member_id . ') as member_coupon_num';
  242. }
  243. $list = $coupon_model->getCouponTypeList($condition, $field, "money desc", null, 'ct');
  244. $goods_condition = [
  245. [ 'site_id', '=', $this->site_id ],
  246. [ 'status', '=', 1 ],
  247. [ 'is_show', '=', 1 ],
  248. [ 'goods_type', '=', 2 ],
  249. [ 'goods_ids', 'like', "%,$goods_id,%" ]
  250. ];
  251. $goods_coupon = $coupon_model->getCouponTypeList($goods_condition, $field, "money desc", null, 'ct');
  252. if (!empty($goods_coupon[ 'data' ])) {
  253. $list[ 'data' ] = array_merge($list[ 'data' ], $goods_coupon[ 'data' ]);
  254. }
  255. if ($list[ 'data' ] && $this->member_id) {
  256. foreach ($list[ 'data' ] as $k => $v) {
  257. // 已抢光
  258. if ($v[ 'count' ] == $v[ 'lead_count' ]) {
  259. unset($list[ 'data' ][ $k ]);
  260. } elseif ($v[ 'max_fetch' ] != 0 && $v[ 'member_coupon_num' ] > 0 && $v[ 'member_coupon_num' ] >= $v[ 'max_fetch' ]) {
  261. // 已领取
  262. unset($list[ 'data' ][ $k ]);
  263. }
  264. }
  265. $list[ 'data' ] = array_values($list[ 'data' ]);
  266. }
  267. return $this->response($list);
  268. }
  269. /**
  270. * 查询优惠券通过优惠券类型id
  271. */
  272. public function couponById()
  273. {
  274. $id = $this->params[ 'id' ] ?? 0;
  275. $coupon_model = new CouponModel();
  276. $condition = [
  277. [ 'site_id', '=', $this->site_id ],
  278. [ 'status', '=', 1 ],
  279. [ 'coupon_type_id', 'in', $id ]
  280. ];
  281. $list = $coupon_model->getCouponTypeList($condition, "coupon_type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,validity_type,fixed_term,goods_type,discount_limit", "money desc", "");
  282. return $this->response($list);
  283. }
  284. }