之前同事改的,从存表到存cookie,现在又要改回存到数据库中,我和他的心情大概都是一样dammit... 这么多行的方法需要被封装再封装。嗯 ```php /** * 生成订单 */ function cart3() { $code = new config('code_config'); $shop_cart = json_decode($_COOKIE[$code->project_store_shop_cart], true); $goodsIds = array(); foreach($shop_cart as $v){ $goodsIds[$v['goods_id']] = $v['count']; } $shop_id = $shop_cart[0]['shop_id']; $company_id = $shop_cart[0]['company_id']; $accept_name = IFilter::act(IReq::get('accept_name')); $telphone = IFilter::act(IReq::get('telphone')); $province = IFilter::act(IReq::get('province'),'int'); $city = IFilter::act(IReq::get('city'),'int'); $area = IFilter::act(IReq::get('area'),'int'); $address = IFilter::act(IReq::get('address')); $mobile = IFilter::act(IReq::get('mobile')); $zip = IFilter::act(IReq::get('zip')); $accept_time = IFilter::act(IReq::get('accept_time')); $takeself = IFilter::act(IReq::get('takeself')); $is_self = IFilter::act(IReq::get('is_self')); $id_card = IFilter::act(IReq::get('id_card')); if($is_self == 1){ $accept_name = ''; $telphone = ''; $province = ''; $city = 0; $area = 0; $address = ''; $mobile = ''; $zip = ''; $id_card = ''; } $area_freight_price = $this->code->area_freight_price; $australia_storege = $this->code->australia_storege;//澳洲仓库id $delivery_id = IFilter::act(IReq::get('delivery_id'),'int') ? IFilter::act(IReq::get('delivery_id'),'int') : 1; $payment = 8;//让支付方式默认为支付宝 $dataArray = array(); //获取商品数据信息 $countSumObj = new CountSum(0); $goodsResult = $countSumObj->cart_count(0,'goods','','','', true); $Obj = new IQuery('goods_storage'); $Obj->fields = '*'; $Obj->order = 'num desc'; $read = IFilter::act(IReq::get('readAgree'),'int'); if (!$read) { IError::show(403,'是否同意'); } //判断商品商品是否存在 if(is_string($goodsResult) || empty($goodsResult['goodsList'])) { IError::show(403,'商品数据错误'); exit; } $paymentObj = new IModel('payment'); $paymentRow = $paymentObj->getObj('id = '.$payment,'type,name'); $paymentName= $paymentRow['name']; $paymentType= $paymentRow['type']; $order_class = new Order_Class(); $goodsInfo = $order_class->orders_for_goods_group(0, $goodsResult['goodsList'], $this->code->default_storege);//判断库存获取商品仓库信息 if(is_string($goodsInfo)){ IError::show(403, $goodsInfo); } //地区运费 $area_freight = empty($area_freight_price[$province]) ? 0 : $area_freight_price[$province]; $orderInfo = array(); foreach($goodsInfo as $k => $v){ $orderInfo[$k]['goodsList'] = array(); $orderInfo[$k]['freight'] = 0; $orderInfo[$k]['tax'] = 0; $orderInfo[$k]['amount'] = 0; if($k == $australia_storege){ foreach($v['goodsList'] as $key => $val){ if($val['goods_type'] == 'milk_powder'){ for($i=0;$i<$val['count'];$i++){ $goods = $val; $goods['count'] = 1; $orderInfo[$k .''. $key . $i]['goodsList'][] = $goods; $orderInfo[$k .''. $key . $i]['freight'] = $val['freight'] + $area_freight; $orderInfo[$k .''. $key . $i]['tax'] = $val['taxPrice']; $orderInfo[$k .''. $key . $i]['sum'] = $val['sell_price']; $orderInfo[$k .''. $key . $i]['exp'] = $val['exp']; $orderInfo[$k .''. $key . $i]['point'] = $val['point']; $orderInfo[$k .''. $key . $i]['final_sum'] = $val['sell_price'] - $val['reduce']; $orderInfo[$k .''. $key . $i]['reduce'] = $val['reduce']; } } else { $orderInfo[$k]['final_sum'] += ($val['sell_price'] - $val['reduce']) * $val['count']; array_push($orderInfo[$k]['goodsList'], $val); } } $orderInfo[$k]['freight'] = $v['freight'] + $area_freight; $orderInfo[$k]['tax'] = $v['taxPrice']; $orderInfo[$k]['sum'] = $v['sum']; $orderInfo[$k]['exp'] = $v['exp']; $orderInfo[$k]['point'] = $v['point']; $orderInfo[$k]['reduce'] = $v['reduce']; } else { foreach($v['goodsList'] as $val){ $orderInfo[$k]['final_sum'] += ($val['sell_price'] - $val['reduce']) * $val['count']; array_push($orderInfo[$k]['goodsList'], $val); } $orderInfo[$k]['tax'] = $v['taxPrice']; $orderInfo[$k]['freight'] = $v['freight']; $orderInfo[$k]['sum'] = $v['sum']; $orderInfo[$k]['exp'] = $v['exp']; $orderInfo[$k]['point'] = $v['point']; $orderInfo[$k]['reduce'] = $v['reduce']; } if(empty($orderInfo[$k]['goodsList'])){ unset($orderInfo[$k]); } } //根据商品所属商家不同批量生成订单 $orderIdArray = array(); $orderNumArray = array(); $order_id_info = array();//订单ID数组 foreach($orderInfo as $v){ //最终订单金额计算 $orderData = $countSumObj->countOrderFee($v,$province,$delivery_id,$payment,0,0,0,0, $shop_id); if(is_string($orderData)) { IError::show(403,$orderData); exit; } //记录订单号和订单金额 $tempArr = array(); $final_sum = 0; $parent_id = ''; $userCode = new UserCode(); $ip = $userCode->getIP(); $fromIp = $userCode->getAddId($ip); foreach($orderData as $seller_id => $goodsResult) { //生成的订单数据 $dataArray = array( 'order_no' => Order_Class::createOrderNum(), 'user_id' => 0, 'accept_name' => $accept_name, 'pay_type' => $payment, 'distribution' => $delivery_id, 'postcode' => $zip, 'telphone' => $telphone, 'province' => $province, 'city' => $city, 'area' => $area, 'address' => $address, 'mobile' => $mobile, 'create_time' => ITime::getDateTime(), 'accept_time' => $accept_time, 'exp' => $v['exp'],//经验值 'point' => $v['point'],//积分 'type' => 0, 'shop_id' => $shop_id, 'company_id' => $company_id, //商品价格 'payable_amount' => $v['sum'], 'real_amount' => $v['final_sum'], //运费价格 'payable_freight' => 0, 'real_freight' => $v['freight'],//运费 //手续费 'pay_fee' => $goodsResult['paymentPrice'], //税金 'invoice' => 1, 'invoice_title' => 0, 'taxes' => $v['tax'],//税 //优惠价格 'promotions' => $goodsResult['proReduce'] + $goodsResult['reduce'], //订单应付总额 'order_amount' => $v['final_sum'] + $v['tax'] + $v['freight'], //订单保价 'insured' => $goodsResult['insuredPrice'], //自提点ID 'takeself' => $takeself, 'is_self' => $is_self, //促销活动ID 'active_id' => 0, //商家ID 'storage_id' => $seller_id, //备注信息 'note' => '', 'one_goods_id' => $v['goodsList'][0]['goods_id'], 'parent_id' => $parent_id, 'pay_account_id' => $seller_id, 'from_country' => $fromIp['from_country'], 'from_province' => $fromIp['from_province'], 'from_city' => $fromIp['from_city'], 'id_card' => $id_card, ); $dataArray['order_no_zhengzhou'] = '';//清空郑州仓库 $dataArray['pay_mode'] = '1'; if($seller_id==1){ $dataArray['order_no_zhengzhou'] = Order_Class::creatZhengOrderNum(); } //促销规则 if(isset($goodsResult['promotion']) && $goodsResult['promotion']) { foreach($goodsResult['promotion'] as $key => $val) { $dataArray['note'] .= " 【".$val['info']."】 "; } } $dataArray['order_amount'] = $dataArray['order_amount'] <= 0 ? 0 : $dataArray['order_amount']; //生成订单插入order表中 $orderObj = new IModel('order'); $orderObj->setData($dataArray); $order_id = $orderObj->add(); if(!empty($_COOKIE[$code->project_store_cart])){ $project_store_cart = json_decode($_COOKIE[$code->project_store_cart], true); foreach($shop_cart as $val){ if(isset($project_store_cart['goods'][$val['goods_id']])){ unset($project_store_cart['goods'][$val['goods_id']]); } } setcookie($code->project_store_cart, json_encode($project_store_cart), time()+86400, '/', null); } $tempArr[$order_id] = $goodsResult['final_sum']; if($order_id == false) { IError::show(403,'订单生成错误'); } $order_id_info[] = $order_id; /*将订单中的商品插入到order_goods表*/ $order_class->insertOrderGoods($order_id, $v, $shop_id); $goods_cookie = array(); foreach($v['goodsList'] as $value){ $goods_cookie[$value['goods_id']] = $value['count']; } setcookie($code->project_store_shop_order, json_encode(array(time() => $goods_cookie)), time()+60, '/', null);//清空订单商品 //订单金额小于等于0直接免单 if($dataArray['order_amount'] <= 0) { Order_Class::updateOrderStatus($dataArray['order_no']); } else { $orderIdArray[] = $order_id; $orderNumArray[] = $dataArray['order_no']; $final_sum += $dataArray['order_amount']; } } } setcookie($code->project_store_shop_cart, '', time(), '/', null);//清空订单商品 if(empty($_COOKIE[$code->project_store_order])){ setcookie($code->project_store_order, json_encode($order_id_info), time()+86400, '/', null); } else { $data = json_decode($_COOKIE[$code->project_store_order],true); setcookie($code->project_store_order, json_encode(array_merge($data, $order_id_info)), time()+86400, '/', null); } //获取备货时间 $siteConfigObj = new Config("site_config"); $site_config = $siteConfigObj->getInfo(); $this->stockup_time = isset($site_config['stockup_time'])?$site_config['stockup_time']:2; //数据渲染 $this->order_id = join(",",$orderIdArray); $this->final_sum = $final_sum; $this->order_num = $orderNumArray[0]; $this->payment = $paymentName; $this->paymentType = $paymentType; // $this->delivery = $deliveryRow['name']; // $this->tax_title = $tax_title; // $this->deliveryType= $deliveryRow['type']; $this->order_count = count($orderNumArray); $area_province = self::getAreaName($province); $area_city = self::getAreaName($city); $area_area = self::getAreaName($area); $this->area_province = $area_province; $this->area_city = $area_city; $this->area = $area_area; $this->project_host_url = project_HOST_URL; $this->is_self = $is_self; //订单金额为0时,订单自动完成 if($this->final_sum <= 0) { $this->redirect('/site/success/message/'.urlencode("订单确认成功,等待发货").'/?callback=ucenter/order'); } else { $this->setRenderData($dataArray); $this->redirect('cart3'); } } ```