SearchCouponController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/9/7
  8. * Time: 16:27
  9. */
  10. namespace app\frontend\modules\coupon\controllers;
  11. use app\common\components\ApiController;
  12. use app\common\exceptions\AppException;
  13. use app\common\facades\Setting;
  14. use app\common\models\coupon\CouponSlideShow;
  15. use app\common\models\GoodsCategory;
  16. use app\common\modules\coupon\models\PreMemberCoupon;
  17. use app\frontend\models\Goods;
  18. use app\frontend\models\Member;
  19. use app\frontend\modules\coupon\models\Coupon;
  20. use app\frontend\modules\coupon\models\MemberCoupon;
  21. use app\common\models\MemberShopInfo;
  22. use Carbon\Carbon;
  23. use Yunshop\Hotel\common\models\CouponHotel;
  24. use Yunshop\StoreCashier\common\models\Store;
  25. class SearchCouponController extends ApiController
  26. {
  27. //"优惠券中心"的优惠券
  28. const IS_AVAILABLE = 1; //可领取
  29. const ALREADY_GOT = 2; //已经领取
  30. const EXHAUST = 3; //已经被抢光
  31. //"个人拥有的优惠券"的状态
  32. const NOT_USED = 1; //未使用
  33. const OVERDUE = 2; //优惠券已经过期
  34. const IS_USED = 3; //已经使用
  35. const NO_LIMIT = -1; //没有限制 (比如对会员等级没有限制, 对领取总数没有限制)
  36. const TEMPLATEID = 'OPENTM200605630'; //成功发放优惠券时, 发送的模板消息的 ID
  37. // const TEMPLATEID = 'tqsXWjFgDGrlUmiOy0ci6VmVtjYxR7s-4BWtJX6jgeQ'; //临时调试用
  38. /**
  39. * coupon.search-coupon.index
  40. * 提供给用户的"优惠券中心"的数据接口
  41. * @return \Illuminate\Http\JsonResponse
  42. */
  43. public function index()
  44. {
  45. $search['goods_id'] = intval(request()->input('goods_id'));
  46. $uid = \YunShop::app()->getMemberId();
  47. $member = MemberShopInfo::getMemberShopInfo($uid);
  48. if (empty($member)) {
  49. return $this->errorJson('没有找到该用户', []);
  50. }
  51. $memberLevel = $member->level_id;
  52. $now = strtotime('now');
  53. $coupons = Coupon::centerCouponsForMember($uid, $memberLevel, null, $now,\YunShop::request()->coupon_type?:'')
  54. ->orderBy('yz_coupon.display_order', 'desc')
  55. ->orderBy('yz_coupon.updated_at', 'desc')
  56. ->get();
  57. if ($coupons->isEmpty()) {
  58. return $this->errorJson('没有找到记录', []);
  59. }
  60. foreach ($coupons as &$item) {
  61. $item->has_many_member_coupon_count = MemberCoupon::uniacid()->where('coupon_id' ,$item->id)->pluck('uid')->unique()->count();
  62. }
  63. //添加"是否可领取" & "是否已抢光" & "是否已领取"的标识
  64. $couponsData = $this->getCouponData($coupons, $search);
  65. $slideShows = CouponSlideShow::uniacid()
  66. ->where('is_show',1)
  67. ->orderBy('sort','asc')
  68. ->orderBy('id','asc')
  69. ->limit(10)
  70. ->get();
  71. $data = [
  72. 'data' => $couponsData,
  73. 'search_array' => $this->getSearchArray(Coupon::$typeComment),//Coupon::$typeComment
  74. 'slide_shows' => $slideShows
  75. ];
  76. //领券中心表单
  77. if (!is_null(\app\common\modules\shop\ShopConfig::current()->get('coupon_form'))) {
  78. $class = array_get(\app\common\modules\shop\ShopConfig::current()->get('coupon_form'), 'class');
  79. $function = array_get(\app\common\modules\shop\ShopConfig::current()->get('coupon_form'), 'function');
  80. $form = $class::$function($uid);
  81. if($form && $form != -1)
  82. {
  83. $data = array_merge($data,['coupon_form' => $form]);
  84. }
  85. }
  86. return $this->successJson('ok', $data);
  87. }
  88. protected function filterCoupon($coupon, $search)
  89. {
  90. if (empty($search['goods_id'])) {
  91. return false;
  92. }
  93. $goods = Goods::find($search['goods_id']);
  94. $goodsCategory = GoodsCategory::select('category_id')->where('goods_id', $search['goods_id'])->get()->pluck('category_id')->toArray();
  95. if ($coupon->time_limit == 1 && (time() < $coupon->time_start || time() > $coupon->time_end)) {
  96. return true;
  97. }
  98. switch ($coupon->use_type) {
  99. case Coupon::COUPON_SHOP_USE: //商城通用
  100. if (!in_array($goods->plugin_id,[31,32,33,36,92,101])) {
  101. $coupon_list[] = $coupon;
  102. }
  103. break;
  104. case Coupon::COUPON_GOODS_USE: //指定商品
  105. if (in_array($goods->id, $coupon['goods_ids'])) {
  106. $coupon_list[] = $coupon;
  107. }
  108. break;
  109. case Coupon::COUPON_GOODS_AND_STORE_USE: //指定商品+指定门店
  110. $use_conditions = unserialize($coupon->use_conditions);
  111. if (($use_conditions['is_all_good'] && $goods->plugin_id == 0) || in_array($goods->id, $use_conditions['good_ids'])) {
  112. $coupon_list[] = $coupon;
  113. }
  114. break;
  115. case Coupon::COUPON_CATEGORY_USE: //指定分类
  116. //商品分类存在该优惠卷中
  117. if ($coupon['category_ids'] && array_intersect($goodsCategory, $coupon['category_ids'])) {
  118. $coupon_list[] = $coupon;
  119. }
  120. break;
  121. default:
  122. }
  123. return true;
  124. }
  125. /**
  126. * 过滤不满足条件的优惠卷 &
  127. * 添加"是否可领取" & "是否已抢光" & "是否已领取"的标识
  128. * @param $coupons
  129. * @param $search
  130. * @return mixed
  131. */
  132. public function getCouponData($coupons, $search)
  133. {
  134. $coupons = $coupons->map(function ($item) use ($search) {
  135. if ($this->filterCoupon($item,$search)) {
  136. return null;
  137. }
  138. if (($item->total != self::NO_LIMIT) && ($item->has_many_member_coupon_count >= $item->total)) {
  139. $item->api_availability = self::EXHAUST;
  140. } elseif ($item->member_got_count > 0) {
  141. $item->api_availability = self::ALREADY_GOT;
  142. } else {
  143. $item->api_availability = self::IS_AVAILABLE;
  144. }
  145. //增加属性 - 对于该优惠券,用户可领取的数量
  146. if ($item->get_max != self::NO_LIMIT) {
  147. $item->api_remaining = $item->get_max - $item->member_got_count;
  148. if ($item->api_availability < 0) { //考虑到优惠券设置会变更,比如原来允许领取6张,之后修改为3张,那么可领取张数可能会变成负数
  149. $item->api_availability = 0;
  150. }
  151. } elseif ($item->get_max == self::NO_LIMIT) {
  152. $item->api_availability = -1;
  153. }
  154. //添加优惠券使用范围描述
  155. switch ($item->use_type) {
  156. case Coupon::COUPON_SHOP_USE:
  157. $item->api_limit = '商城通用';
  158. break;
  159. case Coupon::COUPON_CATEGORY_USE:
  160. $item->api_limit = '适用于下列分类: '.implode(',', $item['categorynames']);
  161. break;
  162. case Coupon::COUPON_GOODS_USE:
  163. $item->api_limit = '适用于下列商品: '.implode(',', $item['goods_names']);
  164. break;
  165. case 8:
  166. $item->api_limit = '适用于下列商品: '.implode(',', $item['goods_names']);
  167. break;
  168. case 9:
  169. $item->api_limit = '适用范围: ';
  170. $use_condition = unserialize($item['use_conditions']);
  171. if (empty($use_condition)) {
  172. $item->api_limit .= '无适用范围';
  173. }
  174. if (app('plugins')->isEnabled('store-cashier')) {
  175. if ($use_condition['is_all_store'] == 1) {
  176. $item->api_limit .= "全部门店";
  177. } else {
  178. $item->api_limit .= '门店:'.implode(',', Store::uniacid()->whereIn('id', $use_condition['store_ids'])->pluck('store_name')->all());
  179. }
  180. }
  181. if ($use_condition['is_all_good'] == 1) {
  182. $item->api_limit .= "平台自营商品";
  183. } else {
  184. $item->api_limit .= '商品:'.implode(',', Goods::uniacid()->whereIn('id', $use_condition['good_ids'])->pluck('title')->all());
  185. }
  186. break;
  187. }
  188. return $item;
  189. })->filter()->values();
  190. return $coupons;
  191. }
  192. private function getSearchArray($arr)
  193. {
  194. if (!app('plugins')->isEnabled('store-cashier')) {
  195. unset($arr[Coupon::TYPE_STORE]);
  196. }
  197. if (!app('plugins')->isEnabled('hotel')) {
  198. unset($arr[Coupon::TYPE_HOTEL]);
  199. }
  200. return $arr;
  201. }
  202. }