site_id = $site_id; } /*** * 发起免密支付 * @param $site_id * @param $id * @return array * @throws \Exception */ public function noPasswordPay($site_id, $id) { $result = model('supply_order')->getInfo(['order_id' => $id]); if ($result['three_pay_status'] == 0 && $result['status'] == 1) { $cloudApi = new CloudApi($site_id); $res = $cloudApi->protocolPayPreparePay($result['third_order']); if ($res['success']) { $data = ['three_pay_status' => 1]; model('supply_order')->update($data, ['order_id' => $id]); return $this->success('', '支付成功'); } else { $code = [ 'USER_BALANCE_NOT_ENOUGH' => '用户余额不足' ]; return $this->error(-1, $code[$res['code']] ?? '发生未知错误'); } } else { return $this->error(-1, '该订单已支付,或未下单!'); } } /*** * 获取在线支付链接 * @param $site_id * @param $id * @return array|void */ public function onlinePay($site_id, $id) { $result = model('supply_order')->getInfo(['order_id' => $id]); if ($result['three_pay_status'] == 0 && $result['status'] == 1) { $cloudApi = new CloudApi($site_id); $order_ids = [$result['third_order']]; $res = $cloudApi->GroupPayUrl(json_encode($order_ids)); if ($res['success']) { $res['order_money'] = $result['result_total_pay']; return $this->success($res); } else { return $this->error(-1, $res['message']); } } else { return $this->error(-1, '该订单已支付,或未下单!'); } } /*** * 取消订单 * @param $site_id * @param $id * @return array * @throws \Exception */ public function cancelOrder($site_id, $id, $cancelReason) { $result = model('supply_order')->getInfo(['order_id' => $id]); if ($result['three_pay_status'] == 0 && $result['status'] == 1) { $cloudApi = new CloudApi($site_id); $order_ids = [$result['third_order']]; $res = $cloudApi->cancelOrder($order_ids, $cancelReason); if ($res['success']) { $data = ['reason_failure' => $cancelReason, 'remark' => $cancelReason, 'order_status' => -1]; model('supply_order')->update($data, ['order_id' => $id]); return $this->success('', '取消成功'); } else { return $this->error(-1, $res['errorMessage']); } } else { return $this->error(-1, '该订单已支付,或未下单!'); } } /*** * 创建订单浏览 * @param $order_no * @return void */ public function createOrderPpreview($orderId = 0) { try { // 获取订单商品信息 if ($orderId <= 0) throw new Exception("订单或者门店不存在!"); $goodsData = $this->getOrderArrangeInfo($orderId); if (empty($goodsData)) throw new Exception("当前信息不存在!"); $cloudApi = new CloudApi($this->site_id); $buyOrderInfo = $cloudApi->createOrderPreview($goodsData); if (isset($buyOrderInfo['orderPreviewResuslt'])) { $totalPay = 0; $sumCarriage = 0; $cargoList = []; //待整改与实时下单有冲突 foreach ($buyOrderInfo['orderPreviewResuslt'] as $k => $v) { $totalPay += $v['sumPayment']; $sumCarriage += $v['sumCarriage']; $cargoList = array_merge($cargoList, $v['cargoList']); } $orderRecordUpdateData['cost_price'] = $totalPay / 100; $orderRecordUpdateData['result_express_fee'] = $sumCarriage / 100;// 快递费 $orderRecordUpdateData['result_total_pay'] = $totalPay / 100;// 支付金额,包含快递费 $orderRecordUpdateData['status'] = 1;// 下单成功 $orderRecordUpdateData['result_id'] = 0; $orderRecordUpdateData['result_user_id'] = 0; $orderRecordUpdateData['result_status'] = 1;// 订单状态 1:待支付 2:支付完成 $orderRecordUpdateData['order_sub_list'] = $cargoList;// 子订单列表 $result = $this->success(); } else { throw new Exception($buyOrderInfo['error_message'] ?? '未知错误'); } } catch (\Exception $e) { $message = $e->getMessage(); $orderRecordUpdateData['result_id'] = $buyOrderInfo['request_id'] ?? ''; $orderRecordUpdateData['status'] = 0; $orderRecordUpdateData['reason_failure'] = $message; $result = [ 'code' => -2, 'msg' => $message, ]; } // 修改下单记录信息 $this->updateOrderInfo($orderRecordUpdateData, $orderId); return $result; } /*** * 获取订单信息 * @param $order_id * @param $field * @return mixed */ public function getOrderInfo($order_id, $field = '*') { $result = model('supply_order')->getInfo(['order_id' => $order_id], $field); return $result; } /** * 获取供应链订单列表 * @param $search * @return array */ public function getOrderList($search) { // 查询条件 $where = [ ['site_id', '=', $search['site_id']] ]; if (!empty($search['order_no'])) $where[] = ['order_no', '=', $search['order_no']]; if (!empty($search['result_id'])) $where[] = ['result_id', '=', $search['result_id']]; if (strlen($search['status'] ?? '') > 0) $where[] = ['status', '=', $search['status']]; // 其他查询配置 $result = model('supply_order')->pageList($where, '*', 'order_id DESC', $search['page'] ?? 1); // 基本信息处理 return $this->success($result ?? []); } /*** * 获取订单子订单列表 * @param $site_id * @param $order_id * @return array */ public function getSubOrder($site_id, $order_id, $search = []) { $where = [ ['sup.order_id', '=', $order_id], ['sup.site_id', '=', $site_id], ]; $join = [ [ 'order_goods og', 'sup.order_id = og.order_id', 'inner' ] ]; $field = [ 'og.order_goods_id', 'og.sku_name', 'og.sku_image', 'og.sku_no', 'og.cost_price', 'sup.id', 'sup.order_id', 'sup.result_id', 'sup.result_express_company', 'sup.result_express_number', 'sup.result_express_status', 'sup.result_express_time', 'sup.result_product_specs_id', 'sup.result_quantity', 'sup.result_seller_remarks', ]; // 获取订单商品信息 $result = model('supply_sub_order')->pageList($where, $field, 'id DESC', $search['page'] ?? 1, $search['page_size'] ?? 1, 'sup', $join); return $this->success($result ?? []); } /*** * 获取供应链信息 * @param $orderId * @param $field * @return mixed */ public function getOrderSupplyGoods($orderId, $field = ['og.order_goods_id', 'og.sku_id', 'og.num', 'og.goods_id', 'o.buyer_message']) { $where = [ ['og.order_id', '=', $orderId], ['fg.goods_id', '>', 0] ]; $join = [ [ 'supply_goods_warehousing fg', 'og.goods_id = fg.goods_id', 'right' ], [ 'order o', 'o.order_id=og.order_id', 'inner' ] ]; $goods_order_sku = model('order_goods')->getList($where, $field, '', 'og', $join); // 获取订单商品信息 return $goods_order_sku; } /** * 下单记录 - 创建初始化下单记录 * @param $order * @throws Exception */ public function orderPay($order) { $orderId = (int)$order['order_id'] ?? 0; $this->site_id = (int)$order['site_id'] ?? 0; if ($orderId <= 0 || $this->site_id <= 0) return $this->error('订单或者店铺不存在!'); // 获取订单商品信息 仅获取供应链的商品 $orderGoodsList = $this->getOrderSupplyGoods($orderId); if (count($orderGoodsList)) { // 记录当前订单信息 $res = $this->updateOrderState(['site_id' => $this->site_id, 'order_id' => $orderId], [ 'order_no' => $order['order_no'], 'buyer_shop_id' => $order['site_id'], 'order_name' => $order['order_name'], 'order_from' => $order['order_from'], 'pay_status' => 1, 'buyer_uid' => $order['member_id'], 'name' => $order['name'], 'mobile' => $order['mobile'] ?: $order['telephone'], 'province_id' => $order['province_id'], 'city_id' => $order['city_id'], 'district_id' => $order['district_id'], 'address' => $order['address'], 'buyer_message' => $order['buyer_message'], 'full_address' => $order['full_address'], 'goods_num' => $order['goods_num'], 'pay_money' => $order['pay_money'], 'pay_time' => $order['pay_time'] ], $this->site_id ); $ConfigModel = new ConfigModel(); $config = $ConfigModel->getConfig($this->site_id)['data']['value']; $isAutoBuyOrder = $config['isAutoBuyOrder'] ?? 0; if ($isAutoBuyOrder == 1) { $cron = new Cron(); $cron->addCron(1, 1, '1688自动下单', 'CronAutoAliOrderBuy', time(), $orderId); } } } /** * Common: 下单记录 - 创建初始化下单记录 * @param $order * @throws Exception */ public function orderCreate($order) { $orderId = (int)$order['order_id'] ?? 0; $this->site_id = (int)$order['site_id'] ?? 0; if ($orderId <= 0 || $this->site_id <= 0) return $this->error('订单或者店铺不存在!'); if (isset($order['create_data']['shop_goods_list']['goods_list'])) { $goods_ids = array_column($order['create_data']['shop_goods_list']['goods_list'], 'goods_id'); if (model('supply_goods_warehousing')->getCount([['goods_id', 'in', $goods_ids]], 'id') > 0) { $data = [ 'site_id' => $order['site_id'], 'order_id' => $order['order_id'], 'order_no' => $order['order_no'] ?? '', 'order_from' => $order['create_data']['order_from'] ?? '',//订单来源 'order_from_name' => $order['create_data']['order_from_name'] ?? '',//来源名称 'channel_type' => '1688', 'status' => -1,// 订单状态:-1=待请求下单,0=下单失败,1=下单成功 'pay_status' => 0,// 是否扣款 'create_time' => time(), 'goods_money' => $order['create_data']['goods_money'], 'delivery_money' => $order['create_data']['delivery_money'], 'invoice_money' => $order['create_data']['invoice_money'], 'order_money' => $order['create_data']['order_money'], 'adjust_money' => $order['create_data']['adjust_money'], 'balance_money' => $order['create_data']['balance_money'], 'pay_money' => $order['create_data']['pay_money'], ]; $this->createRecordAdd($data); } } } /*** * 订单状态更新 * @param $condition * @param $data * @param $site_id * @return int */ public function updateOrderState($condition, $data, $site_id = 0) { $res = model('supply_order')->update($data, $condition); return $res; } /** * 下单记录 - 记录下单日志 * @param $data */ private function createRecordAdd($data) { $isHas = (int)model('supply_order')->getValue([ ['order_id', '=', $data['order_id']] ], 'order_id'); if ($isHas <= 0) { model('supply_order')->add($data); } } /** * 下单-自动请求供应链下单 * @param $siteId */ public function authBuyOrder($order_id = 0) { // 获取最早的、未进行下单请求的订单id // 存在id 进行自动下单操作 if ($order_id > 0) { return $this->orderCreateRequest($order_id); } else { return $this->error('1688订单号不存在'); } } /** * 下单 - 请求进行下单操作 * @param $order */ public function orderCreateRequest($orderId = 0) { try { // 获取订单商品信息 if ($orderId <= 0) return $this->error(-1, '订单或者门店不存在!'); $goodsData = $this->getOrderArrangeInfo($orderId); if (empty($goodsData)) return $this->error(-1, '当前信息不存在'); $cloudApi = new CloudApi($this->site_id); $buyOrderInfo = $cloudApi->fastCreateOrder($goodsData); if (isset($buyOrderInfo['result']['totalSuccessAmount'])) { $result = $buyOrderInfo['result']; $cargoList = json_decode($goodsData['cargoParamList'], true); $cargoList = array_column($cargoList, null, 'specId'); foreach ($this->skuList as $k => $v) { $offer = json_decode($v['goods_supplier_format'], true); $cargoList[$offer['spec_id']]['cost_price'] = $v['cost_price']; $cargoList[$offer['spec_id']]['sku_id'] = $v['sku_id']; $cargoList[$offer['spec_id']]['sku_no'] = $v['sku_no']; $cargoList[$offer['spec_id']]['goods_id'] = $v['goods_id']; $cargoList[$offer['spec_id']]['third_order'] = $result['orderId']; } $totalPay = $result['totalSuccessAmount'] ?? 0; $sumCarriage = $result['postFee']; $orderRecordUpdateData['third_order'] = $result['orderId']; $orderRecordUpdateData['cost_price'] = $totalPay / 100; $orderRecordUpdateData['result_express_fee'] = $sumCarriage / 100;// 快递费 $orderRecordUpdateData['result_total_pay'] = $totalPay / 100;// 支付金额,包含快递费 $orderRecordUpdateData['status'] = 1;// 下单成功 $orderRecordUpdateData['result_id'] = 0; $orderRecordUpdateData['result_user_id'] = 0; $orderRecordUpdateData['result_status'] = 1;// 订单状态 1:待支付 2:支付完成 $orderRecordUpdateData['order_sub_list'] = array_values($cargoList);// 子订单列表 $this->updateOrderInfo($orderRecordUpdateData, $orderId); if (isset($result['failedOfferList'])) { foreach ($result['failedOfferList'] as $k => $v) { model('supply_sub_order')->update(['result_express_status' => -1, 'result_exception_reason' => $v['errorMessage']], ['result_product_specs_id' => $v['specId']]); } } $result = $this->success('下单成功'); } else { return $this->error('发生错误', $buyOrderInfo['message'] ?? '未知错误'); } } catch (\Exception $e) { $message = $e->getMessage(); $orderRecordUpdateData['result_id'] = $buyOrderInfo['request_id'] ?? ''; $orderRecordUpdateData['status'] = 0; $orderRecordUpdateData['reason_failure'] = $message; $result = [ 'code' => -2, 'msg' => $message, ]; $this->updateOrderInfo($orderRecordUpdateData, $orderId); } // 修改下单记录信息 return $result; } /** * Common: 订单处理 - 供应链订单信息修改 * @param $orderRecordUpdateData * @param $orderId * @throws \Exception */ public function updateOrderInfo($orderRecordUpdateData, $orderId) { // 判断:是否存在子订单信息 $orderSubList = $orderRecordUpdateData['order_sub_list'] ?? []; unset($orderRecordUpdateData['order_sub_list']); if (count($orderSubList) > 0) $this->updateOrCreateSubOrder($orderSubList, $orderId);//创建子订单 // 修改订单信息 $orderRecordUpdateData['update_time'] = time(); model('supply_order')->update($orderRecordUpdateData, [ ['order_id', '=', $orderId] ]); } /** * 订单处理 - 处理子订单信息 * @param $orderSubList * @param $orderId * @throws \Exception */ public function updateOrCreateSubOrder($orderSubList, $orderId) { // 循环处理子订单信息 $subList = array_map(function ($item) use ($orderId) { return [ 'site_id' => $this->site_id, 'order_id' => $orderId, 'result_exception_reason' => '',// 请求结果:订单异常原因 1:恢复正常 2:需要换货 3:需要补发 4:地址错误 5:疫情停发 'result_express_company' => '',// 请求结果:快递公司 'result_express_number' => '',// 请求结果:快递单号 'result_express_status' => 0,// 请求结果:订单物流状态 1 待发货 2 已发货 3 已签收 4 拒收 5 已退款 6 申请退款 7 同意申请 8 拒绝申请 'result_express_time' => '',// 发货时间 'result_id' => $item['offerId'] ?? '', 'goods_id' => $item['goods_id'] ?? '', 'sku_id' => $item['sku_id'] ?? '', 'result_sub_id' => $item['third_order'] ?? '',// 请求结果:子订单id 'result_price' => $item['cost_price'] ?? 0,// 请求结果:下单时的商品结算价格 'result_product_specs_id' => $item['specId'] ?? '',// 请求结果:商品规格编号 'result_quantity' => $item['quantity'] ?? 1,// 请求结果:购买单品数量 'result_seller_remarks' => $item['sku_no'] ?? '',// 请求结果:卖家备注 ]; }, $orderSubList); // 获取已经存在的子订单信息 $deleteData = (array)model('supply_sub_order')->getColumn([ ['order_id', '=', $orderId], ], 'order_id'); if ($deleteData) { // 进行对应的操作 先删除、在修改、最后添加 model('supply_sub_order')->delete([ ['order_id', '=', $orderId], ['site_id', '=', $this->site_id] ]); } $goodsSkuModel = (new NewBaseModel(['table_name' => 'supply_sub_order'])); $goodsSkuModel->saveAll($subList); return $subList; } /*** * 整理下单信息 * @param $orderId 下单ID * @param $flow general(创建大市场订单),fenxiao(创建分销订单),saleproxy流程将校验分销关系,paired(火拼下单),boutiquefenxiao(精选货源分销价下单,采购量1个使用包邮), boutiquepifa(精选货源批发价下单,采购量大于2使用). flow如果为空的情况,会比价择优预览,并返回最优下单方式flow * @param $openOfferId 是否加密 * @return array */ public function getOrderArrangeInfo($orderId, $flow = 'boutiquefenxiao', $openOfferId = 0) { $orderGoodsList = $this->getOrderSupplyGoods($orderId); // 获取规格信息 $skuIds = array_column($orderGoodsList, 'sku_id'); $skuList = model('goods_sku')->getList([ ['sku_id', 'in', $skuIds] ], ['sku_id', 'goods_id', 'sku_no', 'cost_price', 'goods_supplier_format']); // 规格对应的数量信息 $nums = array_column($orderGoodsList, 'num', 'sku_id'); $order = model('order')->getInfo([['order_id', '=', $orderId]], ['site_id', 'order_no', 'province_id', 'city_id', 'district_id', 'address', 'name', 'telephone', 'mobile', 'full_address', 'buyer_message']); $this->orderInfo = $order; $this->skuList = $skuList; $quantity = []; if (array_sum(array_values($nums)) > 2) { $flow = 'boutiquepifa'; } if (empty($order)) return []; $this->site_id = $order['site_id']; // 获取收货地址信息 $fullAddress = $order['full_address'] ?? '';// 收货地区信息 格式:省-市-区 $fullAddress = explode('-', $fullAddress); $orderInfo = [ 'flow' => $flow, 'outOrderId' => $order['order_no'], 'message' => $order['buyer_message'], 'addressParam' => [ 'fullName' => mb_strlen($order['name'], 'UTF-8') > 1 ? $order['name'] : $order['name'] . $order['name'],//收货人姓名 'mobile' => $order['mobile'], //手机 'phone' => $order['telephone'] ?: $order['mobile'], //电话 'provinceText' => $fullAddress[0] ?? '',//省份文本 'cityText' => $fullAddress[1] ?? '',//市文本 'areaText' => $fullAddress[2] ?? '', //区文本 'townText' => '', //镇文本 'address' => str_replace($order['full_address'] . '-', '', $order['address']), //街道地址 'postCode' => 000000,//邮编 ], 'cargoParamList' => array_map(function ($skuItem) use ($nums, $quantity) { $offer = json_decode($skuItem['goods_supplier_format'], true); $quantity[$offer['spec_id']] = (int)$nums[$skuItem['sku_id']]; $data = [ 'offerId' => $offer['offerId'], 'specId' => $offer['spec_id'], 'quantity' => (int)$nums[$skuItem['sku_id']], ]; if (isset($offer['openOfferId'])) { $data['openOfferId'] = $offer['openOfferId']; } return $data; }, $skuList), ]; $this->quantity_temp = $quantity; $orderInfo['addressParam'] = json_encode($orderInfo['addressParam']); $orderInfo['cargoParamList'] = json_encode($orderInfo['cargoParamList']); return $orderInfo; } /** * 退款 - 根据子订单发起退款申请 * @param $data */ public function refundOrder($data) { try { } catch (\Exception $e) { Log::debug('1688供应链 - 退款成功 - 供应链退款失败原因:' . $e->getMessage()); } return []; } /*** * 发货 * @param $params * @return void */ public function SendDelivery($params, $site_id, $type = 'CONSIGN') { if ($params && isset($params['orderLogsItems']) && $type == 'CONSIGN') { $orderLogsItems = $params['orderLogsItems']; $sub_order_ids = array_column($orderLogsItems, 'orderEntryId'); $order_ids = array_column($orderLogsItems, 'orderId'); $where = [ ['sub.site_id', '=', $site_id], ['sub.result_sub_id', 'in', $sub_order_ids] ]; $join = [ ['order_goods og', 'sub.order_id=og.order_id', 'inner'] ]; $info = model('supply_sub_order')->getList($where, 'og.order_goods_id,og.order_id,og.site_id', '', 'sub', $join); if ($info) { $cloudApi = new CloudApi($site_id); $logislist = $cloudApi->getLogisticCompanyList(); $logis = $logislist[$params['cpCode']] ?? []; if ($logis) { $companyName = $logis['companyName']; $companyNo = $logis['companyNo']; } else { $companyName = '其他物流'; $companyNo = 'OTHER'; } $express_company_id = $this->getExpressCompanyId($site_id, $companyName, $companyNo); $delivery_no = $params['mailNo'] ?? ''; $user_info = model('user')->getInfo([['site_id', '=', $site_id], ['app_module', '=', 'shop']]); $log_data = [ 'uid' => $user_info['uid'], 'nick_name' => $user_info['username'], 'action' => '商家对订单进行了发货', 'action_way' => 2, ]; $order_model = new OrderModel(); foreach ($info as $k => $v) { $data = array( 'type' => 'manual', //发货方式(手动发货、电子面单) 'order_goods_ids' => $v['order_goods_id'],//商品id 'express_company_id' => $express_company_id,//物流公司 'delivery_no' => $delivery_no,//快递单号 'order_id' => $v['order_id'],//订单id 'delivery_type' => 1,//是否需要物流 'site_id' => $site_id, 'template_id' => 0,//电子面单模板id 'user_info' => $user_info ); model('supply_sub_order')->update(['result_express_number' => $delivery_no, 'result_express_time' => time(), 'result_express_company' => $companyName], [ 'site_id' => $site_id, 'result_sub_id' => $v['result_sub_id'] ]); $order_model->orderGoodsDelivery($data, 1, $log_data); } } } return $this->success(); } /*** * 自动添加物流信息 * @param $site_id * @param $company_name * @param $express_no * @return mixed */ public function getExpressCompanyId($site_id, $company_name, $express_no) { $where = [ ['site_id', '=', $site_id], ['express_no', '=', $express_no] ]; $express_company_id = model('express_company')->getValue($where, 'company_id'); if (empty($express_company_id)) { $data = [ 'site_id' => $this->site_id, 'company_name' => $company_name,//物流公司名称 'sort' => 0,//排序 'logo' => '',//logo 'url' => '',//网址 'express_no' => $express_no,//编码 'express_no_kd100' => strtolower($express_no),//编码(快递100) 'express_no_cainiao' => strtoupper($express_no),//编码(菜鸟) 'express_no_alipay' => strtoupper($express_no),//编码(支付宝) 'content_json' => '',//打印内容 'background_image' => '',//打印背景图 'font_size' => 14,//打印字体大小 单位px 'width' => 0,//显示尺寸宽度 px 'height' => 0,//显示尺寸高度 px 'scale' => 1,//真实尺寸(mm)与显示尺寸(px)的比例 'create_time' => time(), 'is_electronicsheet' => 0,//是否支持电子面单 'print_style' => 0,//电子面单打印风格 ]; $template_model = new ExpressCompanyTemplate(); $res = $template_model->addExpressCompanyTemplate($data); if ($res['code'] >= 0) { //添加店铺模版 $express_company_model = new ExpressCompany(); $company = $express_company_model->addExpressCompany(['site_id' => $site_id, 'company_id' => $res['data']]); $express_company_id = $company['data']; Cache::tag("cache_tableexpress_company")->clear(); } } return $express_company_id; } }