'checkUser', // 下单来源(立即购买/购物车购买) 'source' => 'require', // 下单动作(结算/下单) 'action' => 'require', // 订单类型 'order_type' => 'require|checkGoods|checkOrderType', // 配送方式 'delivery_type' => 'require|checkDeliveryType' ]; protected $message = [ 'source.require' => '下单来源缺失', 'action.require' => '下单动作缺失', 'order_type.require' => '订单类型缺失', 'delivery_type.require' => '配送参数缺失', ]; /** * @notes 检测用户是否登录 * @param $value * @param $rule * @param $data * @return bool|string * @author Tab * @date 2021/10/14 11:59 */ public static function checkUser($value, $rule, $data) { $user = User::findOrEmpty($value); if ($user->isEmpty()) { return '请先登录'; } return true; } /** * @notes 检查配送方式 * @param $value * @param $rule * @param $data * @author Tab * @date 2021/10/9 10:59 */ public function checkDeliveryType($value, $rule, $data) { if (!in_array($data['delivery_type'],DeliveryEnum::DELIVERY_TYPE)) { return '无效的配送方式'; } return true; } /** * @notes 验证商品参数 * @param $value * @param $rule * @param $data * @return bool|string * @author 段誉 * @date 2021/7/26 17:22 */ public function checkGoods($value, $rule, $data) { // 购物车下单 或 砍价订单 无需商品信息 if ($data['source'] == 'cart' || $data['order_type'] == OrderEnum::BARGAIN_ORDER) { return true; } if (!isset($data['goods'])) { return '商品信息参数缺失'; } if (!is_array($data['goods'])) { return '商品信息格式不正确'; } foreach ($data['goods'] as $item) { if (!isset($item['item_id']) || !isset($item['goods_num'])) { return '商品信息参数不完整'; } } return true; } /** * @notes 校验订单类型 * @param $value * @param $rule * @param $data * @return bool|string * @author Tab * @date 2021/10/8 18:59 */ public function checkOrderType($value, $rule, $data) { if (!in_array($value, OrderEnum::ORDER_TYPE)) { return '无效的订单类型'; } // 营销活动验证 switch($value) { // 拼团验证 case OrderEnum::TEAM_ORDER: return $this->checkTeam($data); // 秒杀验证 case OrderEnum::SECKILL_ORDER: return $this->checkSeckill($data); // 砍价验证 case OrderEnum::BARGAIN_ORDER: return $this->checkBargain($data); case OrderEnum::PRESELL_ORDER: return $this->checkPresell($data); //抽奖订单验证 case OrderEnum::DRAW_ORDER: return $this->checkDraw($data); } return true; } /** * @notes 拼团验证 * @param $data * @author Tab * @date 2021/10/8 19:06 */ public function checkTeam($data) { if (!isset($data['team_id'])) { return '拼团活动ID缺失'; } // 拼团商品信息 $teamGoodsItem = (new TeamGoodsItem())->alias('TGI') ->field('TGI.*,TG.goods_snap,TA.name,TA.people_num, TA.min_buy,TA.max_buy,TA.is_coupon,TA.is_distribution,TA.effective_time') ->join('team_activity TA', 'TA.id = TGI.team_id') ->join('team_goods TG', 'TG.id = TGI.team_gid') ->where([ ['TGI.team_id', '=', (int)$data['team_id']], ['TGI.item_id', '=', (int)$data['goods'][0]['item_id']], ['TA.status', '=', TeamEnum::TEAM_STATUS_CONDUCT], ['TA.start_time', '<=', time()], ['TA.end_time', '>=', time()], ])->findOrEmpty()->toArray(); if (empty($teamGoodsItem)) { return '拼团活动已结束'; } if ($teamGoodsItem['max_buy'] > 0 && $data['goods'][0]['goods_num'] > $teamGoodsItem['max_buy']) { return '下单数量不能大于'.$teamGoodsItem['max_buy'].'件'; } // 参团 if (isset($data['found_id']) && !empty($data['found_id'])) { $teamFound = (new TeamFound())->where(['id'=>$data['found_id']])->findOrEmpty()->toArray(); if (empty($teamFound)) { return '选择的团不存在'; } if ($teamFound['status'] != 0) { return '当前拼团已结束,请重新选择拼团'; } if ($teamFound['invalid_time'] <= time()) { return '当前拼团已结束,请重新选择拼团'; } if ($teamFound['user_id'] == $data['user_id']) { return '您是该团发起人,不能参团哦'; } if ($teamFound['people'] == $teamFound['join']) { return '当前拼团已满员,请重新选择拼团'; } // 获取已参团记录 $people = (new TeamJoin())->where(['found_id'=>$data['found_id'], 'user_id'=>$data['user_id']])->findOrEmpty()->toArray(); if (!empty($people)) { return '您已是该团成员了,不能重复参团哦!'; } } return true; } /** * @notes 秒杀验证 * @author Tab * @date 2021/10/8 19:07 */ public function checkSeckill($data) { if (!isset($data['seckill_id'])) { return '秒杀活动ID缺失'; } // 秒杀商品信息 $seckillGoodsItem = (new SeckillGoodsItem())->alias('SGI') ->field('SGI.*,SG.goods_snap,SA.name,SA.min_buy,SA.max_buy,SA.is_coupon,SA.is_distribution') ->join('seckill_activity SA', 'SA.id = SGI.seckill_id') ->join('seckill_goods SG', 'SG.id = SGI.seckill_gid') ->where([ ['SGI.seckill_id', '=', (int)$data['seckill_id']], ['SGI.item_id', '=', (int)$data['goods'][0]['item_id']], ['SA.status', '=', SeckillEnum::SECKILL_STATUS_CONDUCT], ['SA.start_time', '<=', time()], ['SA.end_time', '>=', time()], ])->findOrEmpty()->toArray(); if (empty($seckillGoodsItem)) { return '秒杀活动已结束'; } if ($seckillGoodsItem['max_buy'] > 0 && $data['goods'][0]['goods_num'] > $seckillGoodsItem['max_buy']) { return '下单数量不能大于' . $seckillGoodsItem['max_buy'] . '件'; } return true; } /** * @notes 砍价验证 * @author Tab * @date 2021/10/8 19:07 */ public function checkBargain($data) { if (!isset($data['initiate_id'])) { return '发起砍价ID缺失'; } if (!isset($data['buy_condition'])) { return '购买条件缺失'; } $bargainInitiate = BargainInitiate::findOrEmpty($data['initiate_id'])->toArray(); if (empty($bargainInitiate)) { return '砍价信息不存在'; } if ($bargainInitiate['status'] != BargainEnum::STATUS_SUCCESS && $bargainInitiate['bargain_snapshot']['buy_condition'] != BargainEnum::BUY_CONDITION_RAND) { return '未砍价成功 或 不支持任意金额购买'; } if (!empty($bargainInitiate['order_id'])) { return '已下过单,不能重复购买'; } if ($bargainInitiate['bargain_snapshot']['order_limit'] > 0 && $bargainInitiate['goods_snapshot']['goods_num'] > $bargainInitiate['bargain_snapshot']['order_limit']) { return '超出每单购买限制'; } return true; } function checkPresell($data) { if (!isset($data['presell_id'])) { return '预售活动ID缺失'; } $presellGoodsItem = PresellGoodsItem::alias('pgi') ->join('presell p', 'pgi.presell_id=p.id') ->field([ 'p.id', 'p.buy_limit', 'p.buy_limit_num' ]) ->where('pgi.item_id', $data['goods'][0]['item_id']) ->where('pgi.presell_id', $data['presell_id']) ->where('p.status', PresellEnum::STATUS_START) ->where('p.start_time', '<=', time()) ->where('p.end_time', '>=', time()) ->findOrEmpty()->toArray(); if (empty($presellGoodsItem['id'])) { return '预售活动已结束'; } if ($presellGoodsItem['buy_limit'] && $data['goods'][0]['goods_num'] > $presellGoodsItem['buy_limit_num']) { return '下单数量不能大于' . $presellGoodsItem['buy_limit_num'] . '件'; } return true; } /** * @notes 校验抽奖订单 * @param $data * @return string|true * @author ljj * @date 2024/7/30 下午2:16 */ function checkDraw($data) { if (!isset($data['draw_record_id'])) { return '抽奖记录ID缺失'; } $LuckyDrawPrize = LuckyDrawPrize::alias('ldp') ->join('lucky_draw_record ldr', 'ldr.prize_id = ldp.id') ->where(['ldr.id'=>$data['draw_record_id']]) ->field('ldr.is_send,ldr.prize_type,ldp.type_value') ->findOrEmpty(); if ($LuckyDrawPrize->isEmpty()) { return '不存在抽奖记录'; } if ($LuckyDrawPrize->is_send == 1) { return '抽奖已兑现'; } if ($LuckyDrawPrize->prize_type != LuckyDrawEnum::GOODS) { return '该抽奖记录非商品类型'; } $goods_id = GoodsItem::where(['id'=>$data['goods'][0]['item_id']])->value('goods_id'); if ($LuckyDrawPrize->type_value != $goods_id) { return '商品错误'; } if (1 != $data['goods'][0]['goods_num']) { return '商品数量错误'; } return true; } }