request->param('ids', ''); $coupons = CouponModel::with(['user_coupons']) ->normal() // 正常的可以展示的优惠券 ->canGet() // 在领取时间之内的 ->order('id', 'desc'); if ($ids) { $coupons = $coupons->whereIn('id', $ids); } $coupons = $coupons->paginate($this->request->param('list_rows', 10))->each(function ($coupon) { $coupon->get_status = $coupon->get_status; $coupon->get_status_text = $coupon->get_status_text; }); $this->success('获取成功', $coupons); } /** * 获取商品可领取的优惠券 * @param Request $request * @param int $goods_id * @return void */ public function goodsGetCoupon() { $user = user_info(); $goods_id = $this->request->param('goods_id'); $goods_type = $this->request->param('goods_type'); //获取绑定的课程 $goods = \app\common\model\goods\Handle::getGoodsQuery() ->where([ 'type'=>$goods_type, 'id'=>$goods_id ])->find(); if (!$goods) { $this->error(__('No Results were found')); } $coupons = CouponModel::canGet() ->publicShow() ->goods([ 'goods_type'=>$goods_type, 'goods_id'=>$goods_id ]) // 符合指定商品,并且检测商品所属分类 ->order('id', 'desc'); if ($user) { // 关联用户优惠券 $coupons = $coupons->with(['userCoupons']); } $coupons = $coupons->select(); $coupons = collection($coupons)->each(function ($coupon) { $coupon->get_status = $coupon->get_status; $coupon->get_status_text = $coupon->get_status_text; }); $this->success('获取成功', $coupons); } /** * 领取优惠券 * @return void */ public function receive() { $id = $this->request->param('id'); $userCoupon = $this->getCoupon($id); $this->success('领取成功', $userCoupon); } /** * 获取用户可用和不可用优惠券 * * @return void */ public function useCoupon() { $goodsList = $this->request->post('goodsList/a'); $orderType = $this->request->post('goodsType','goods'); $couponId = $this->request->post('coupon_id',''); if(!$goodsList){ return $this->error("商品参数无效"); } $params = [ 'goods_list'=>$goodsList, 'order_type'=>$orderType, 'coupon_id'=>$couponId, ]; $order = new \app\common\library\order\OrderCreate($params); if(!$goodsList){ return $this->error("商品参数无效"); } $result = $order->getCoupons(); $this->success('获取成功', $result); } /** * 我的优惠券 * @return void */ public function my() { $type = $this->request->param('type', 'can_use'); // 优惠券类型:geted=已领取,can_use=可用,cannot_use=暂不可用,used=已使用,expired=已过期,invalid=已失效(包含已使用和已过期) $limit = $this->request->param('limit',10); $page = $this->request->param('page',1); $userCoupons = CouponUserModel::with('coupon')->where('user_id', $this->auth->id); if (in_array($type, ['geted', 'can_use', 'cannot_use', 'used', 'expired', 'invalid'])) { $userCoupons = $userCoupons->{\think\helper\Str::camel($type)}(); } $userCoupons = $userCoupons->order('id', 'desc') ->limit($limit)->page($page) ->select(); $this->success('获取成功', $userCoupons); } /** * 优惠券详情 * @return void */ public function detail() { $id = $this->request->param('id'); $coupon = CouponModel::where('id', $id)->find(); if (!$coupon) { $this->error(__('No Results were found')); } $coupon->get_status = $coupon->get_status; $coupon->get_status_text = $coupon->get_status_text; $coupon->goods = $coupon->can_use_goods_value; $canUse = CouponUserModel::where('user_id', $this->auth->id) ->where('user_id', $this->auth->id) ->where('coupon_id', $id) ->canUse() ->find(); $coupon->can_use = $canUse ? true : false; $this->success('优惠券详情', $coupon); } }