verify_sign(); // 验证参数 $request->scene('import')->validate(); // 获取参数 $order['weizan_orderid'] = request('id',0); $order['pay_total'] = request('pay_amount',0); $order['price_total'] = request('order_amount',0); $order['insert_time'] = request('create_time',''); $order['update_time'] = request('modify_time',''); $order['status'] = request('status',''); $order['status'] = $Model->getWeibanStatus($order['status']); $order['coupon_total'] = $order['pay_total'] - $order['price_total']; $order['insert_time'] = strtotime($order['insert_time']); $order['update_time'] = strtotime($order['update_time']); $order['insert_time'] = $order['insert_time'] < 0 ? time() : $order['insert_time']; $order['update_time'] = $order['update_time'] < 0 ? time() : $order['update_time']; // 用户信息 $phone = request('user_phone',0); $userName = request('user_name',0); // $userId = request('user_id',0); // 收货地址 $addr = request('address',''); $addr = explode(' ',$addr); $contact['contact_name'] = request('contact_name',''); $contact['contact_phone'] = request('contact_phone',''); $contact['contact_province'] = empty($addr[0]) ? '' : $addr[0]; $contact['contact_city'] = empty($addr[1]) ? '' : $addr[1]; $contact['contact_area'] = empty($addr[2]) ? '' : $addr[2]; $contact['contact_addr'] = empty($addr[3]) ? '' : $addr[3]; // 子订单数据 $orderItems = request('order_items','[]'); // 数据解析 $orderItems = json_decode($orderItems,true); // 如果客户存在 if( !$orderItems ) return json_send(['code'=>'error','msg'=>'缺少子订单信息']); // 循环处理 foreach ($orderItems as $key => $value) { // 字符串转换 $value['sku_value'] = (string) $value['sku_value']; // 时间转换 $modify_time = strtotime($value['modify_time']); // 重组数据 $value = [ 'status'=>$order['status'], 'product_name'=> str_ireplace(['()','()','(','('],'',str_ireplace($value['sku_value'],'',$value['product_name'])), 'sku_attr_names'=>$value['sku_value'], 'buy_num'=>$value['number'], 'pay_total'=>$value['sub_total'], 'price_total'=>$value['sub_total'], 'insert_time'=>$order['insert_time'], 'update_time'=>($modify_time<0?time():$modify_time), ]; // 时间 $orderItems[$key] = $value; } // 获取手机号,查询是否用客户 $custom = $Custom->getOneByPhone($phone); // 如果存在手机号 $uid = $custom ? $custom['uid'] : $Custom->add(['username'=>$userName,'phone'=>$phone]); // 如果客户存在 if( !$uid ) return json_send(['code'=>'error','msg'=>'无法创建用户']); // 通过订单号查询是否存在系统订单 $orderId = (int) $Model->query()->where([['weizan_orderid','=',$order['weizan_orderid']]])->value('id'); // 客户ID $order['custom_uid'] = $uid; // 组合数据,写入订单表,子表 DB::beginTransaction(); // 写入数据 try { // 存在订单获取订单ID,不存在则新增 $orderId = $orderId ? $Model->edit($orderId,$order) : $Model->add($order); // 如果客户存在 if( !$orderId ) { // 回退数据 DB::rollBack(); // 失败提示 return json_send(['code'=>'error','msg'=>'订单写入失败']); } // 循环子订单 foreach ( $orderItems as $item ) { // 数据结果 $item['custom_uid'] = $uid; $item['order_id'] = $orderId; $item['status'] = $order['status']; $item['id'] = $OrdersProduct->query()->where([['order_id','=',$orderId],'product_name'=>$item['product_name'],'sku_attr_names'=>$item['sku_attr_names']])->value('id'); // 新增还是修改 $result = $item['id'] ? $OrdersProduct->edit($item['id'],$item) : $OrdersProduct->add($item); // 失败提示 if( !$result ) { // 回退数据 DB::rollBack(); // 失败提示 return json_send(['code'=>'error','msg'=>'子订单写入失败']); } } // 存在详细地址,才创建地址库 if( $contact['contact_name'] && $contact['contact_phone'] && $contact['contact_province'] && $contact['contact_city'] && $contact['contact_area'] && $contact['contact_addr'] ) { // 收件地址是否存在 $oldAddr = $CustomAddr->query()->where([['custom_uid','=',$uid]])->first(); // 如果不存在地址 if( !$oldAddr ) $CustomAddr->add(array_merge($contact,['custom_uid'=>$uid])); } // 订单地址 $addrId = $OrdersAddr->query()->where([['order_id','=',$orderId]])->value('id'); // 如果不存在地址 if( !$addrId ) $OrdersAddr->add(array_merge($contact,['order_id'=>$orderId])); // 提交数据 DB::commit(); // 返回结果 return json_send(['code'=>'success','msg'=>'同步成功','data'=>['id'=>$orderId]]); // 返回结果 } catch (\Throwable $th) { // 回退数据 DB::rollBack(); // 失败提示 return json_send(['code'=>'error','msg'=>'系统异常,请稍后再试','data'=>['error'=>$th->getMessage().'=>'.$th->getLine()]]); } } }