with('produce')->find(); if (empty($order)) { return self::error('订单不存在'); } $produce = $order->produce ?? null; if (empty($produce)) { return self::error('商品不存在'); } if ($order->is_cancel == 1) { return self::error('订单已取消'); } // 根据商品类型, 调用不同的推送方法 $operator = $produce->operator ?? ''; $result = []; switch ($operator) { case 'cucc': $result = self::cuccPush($order, $produce); break; case 'cmcc': break; case 'ctcc': break; default: $result = self::error('运营商类型错误'); } return $result; } /** * 联通推送 * * @param MobileOrder $order * @param Produce $produce * @return void */ public static function cuccPush(MobileOrder $order, Produce $produce) { // 1. 资源上传 $goodsId = $produce->api_goods_id ?? 0; $channel = $produce->contact_code ?? ''; $images = $produce->images ?? ''; $resourceId = $produce->resource_id ?? 0; // if (empty($resourceId)) { if (empty($goodsId)) { return self::error('上游编码为空'); } if (empty($channel)) { return self::error('触点编码为空'); } if (empty($images)) { return self::error('图片为空'); } // 如果图片是地址的话, 就转换成base64 if (strpos($images, 'http') === 0) { $imagesBase = base64_encode(file_get_contents($images)); // 获取图片 MIME 类型 $sizeInfo = getimagesize($images); $mimeType = $sizeInfo['mime']; // 构建数据 URI $images = 'data:' . $mimeType . ';base64,' . $imagesBase; } $resourceParams = [ 'goodsId' => $goodsId, 'channel' => $channel, 'resourceContents' => [ [ 'content' => $images, 'sort' => 0 ] ], ]; $resourceResult = self::resourceUpload($resourceParams); if ($resourceResult['code'] != 0) { // 写入订单错误信息 $order->failure_reason = $resourceResult['message']; $order->save(); return $resourceResult; } // 获取资源ID $resourceId = $resourceResult['data']['resourceId'] ?? ''; $produce->resource_id = $resourceId; $produce->save(); // } // 2. 客户资料校验 $cityData = $order->city ?? ''; $cityData = explode(',', $cityData); if (count($cityData) < 2) { return self::error('城市信息不完整'); } $province = $cityData[0] ?? 0; $city = $cityData[1] ?? 0; $district = $cityData[2] ?? 0; $provinceNumCode = Area::where('id', $province)->value('num_code'); $cityNumCode = Area::where('id', $city)->value('num_code'); $certName = $order->name ?? ''; $certNum = $order->id_no ?? ''; if (empty($provinceNumCode) || empty($cityNumCode) || empty($certName) || empty($certNum)) { return self::error('客户信息不完整'); } $identityParams = [ 'province' => $provinceNumCode, 'city' => $cityNumCode, 'certName' => $certName, 'certNum' => $certNum, ]; $identityResult = self::identityCust($identityParams); if ($identityResult['code'] != 0) { // 写入订单错误信息 $order->failure_reason = $identityResult['message']; $order->save(); return $identityResult; } // 获取uuid $uuid = $identityResult['data']['uuid'] ?? ''; // 3.0 判断有没有手机号, 如果没有手机号, 则走自动选好接口 $orderId = $order->order_no ?? ''; $contactNum = $order->phone ?? ''; $postProvinceCode = Area::where('id', $province)->value('post_code'); $postCityCode = Area::where('id', $city)->value('post_code'); $postDistrictCode = Area::where('id', $district)->value('post_code'); $postAddr = $order->address ?? ''; $createTime = $order->create_time ?? ''; $updateTime = $order->update_time ?? ''; $amount = $order->amount ?? 0; $orderTotalFee = bcmul($amount, 1000, 0); $pageUrl = $produce->link ?? ''; $no = $order['no']; if (empty($no)) { // 自动选号-订单同步接口 $autoOrderParams = [ 'orderId' => $orderId, 'goodsId' => $goodsId, 'certName' => $certName, 'certNo' => $certNum, 'contactNum' => $contactNum, 'postProvinceCode' => $postProvinceCode, 'postCityCode' => $postCityCode, 'postDistrictCode' => $postDistrictCode, 'postAddr' => $postAddr, 'channel' => $channel, 'createTime' => $createTime, 'updateTime' => $updateTime, 'orderTotalFee' => $orderTotalFee, 'pageUrl' => $pageUrl, 'resourceId' => $resourceId, ]; $autoOrderResult = self::autoNumSync($autoOrderParams); if ($autoOrderResult['code'] != 0) { // 写入订单错误信息 $order->failure_reason = $autoOrderResult['message']; $order->save(); return $autoOrderResult; } $no = $autoOrderResult['preNumber']; $order->no = $no; $order->save(); } else { // 3. 选号后置-意向单同步 $preOrderParams = [ 'orderId' => $orderId, 'goodsId' => $goodsId, 'certName' => $certName, 'certNo' => $certNum, 'contactNum' => $contactNum, 'postProvinceCode' => $postProvinceCode, 'postCityCode' => $postCityCode, 'postDistrictCode' => $postDistrictCode, 'postAddr' => $postAddr, 'channel' => $channel, 'createTime' => $createTime, 'updateTime' => $updateTime, 'orderTotalFee' => $orderTotalFee, 'pageUrl' => $pageUrl, 'resourceId' => $resourceId, ]; $preOrderResult = self::preOrdersync($preOrderParams); if ($preOrderResult['code'] != 0) { // 写入订单错误信息 $order->failure_reason = $preOrderResult['message']; $order->save(); return $preOrderResult; } $token = $preOrderResult['data']['token'] ?? ''; // 4. 选号服务(暂不需要) // 5. 选号后置-正式单同步(非提前预占版) $phoneNum = $order->no ?? ''; $isOpenCF = 0; // 判断产品是否使用用户收货地址区号 $homeLocationType = $produce->home_location_type ?? 'address'; if ($homeLocationType == 'assign') { $provinceNumCode = $produce->home_location_province ?? 0; $cityNumCode = $produce->home_location_city ?? 0; } $orderParams = [ 'goodsId' => $goodsId, 'provinceCode' => $provinceNumCode, 'cityCode' => $cityNumCode, 'phoneNum' => $phoneNum, 'token' => $token, 'createTime' => $createTime, 'isOpenCF' => $isOpenCF, ]; $orderResult = self::ordersync($orderParams); if ($orderResult['code'] != 0) { // 写入订单错误信息 $order->failure_reason = $orderResult['message']; $order->save(); return $orderResult; } } $order->is_push_zop = 1; $order->save(); return $orderResult; } /** * 资源上传 * * @param array $params [channel: 渠道, goodsId: 商品ID, resourceContents: [content: 资源内容, sort: 排序]]] * @return array */ public static function resourceUpload($params = []) { $url = '/link/king/resource/upload/v1'; $data = array_only($params, [ 'channel', 'goodsId', 'resourceContents' ]); $result = self::send($url, $data); if (empty($result)) { return self::error('资源上传请求失败'); } $resArr = json_decode($result, true); if ($resArr['rspCode'] != '0000') { return self::error('资源上传请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']); } $body = $resArr['body']; return self::success($body); } /** * 客户资料校验 * * @param array $params [province: 省份编码, city.地市编码, certName. 证件姓名, certNum. 证件编码] * @return array */ public static function identityCust($params = []) { $url = '/link/king/identity/cust/v2'; $data = array_only($params, [ 'province', 'city', 'certName', 'certNum' ]); $result = self::send($url, $data); if (empty($result)) { return self::error('客户资料校验请求失败'); } $resArr = json_decode($result, true); if ($resArr['aCode'] != '0000') { return self::error('客户资料校验请求失败, 错误编码为: ' . $resArr['aCode'] . '错误信息为: ' . $resArr['aDesc']); } if ($resArr['bCode'] != '0000') { return self::error('客户资料校验请求失败, 错误编码为: ' . $resArr['bCode'] . '错误信息为: ' . $resArr['bDesc']); } $uuid = $resArr['uuid'] ?? ''; return self::success(['uuid' => $uuid]); } /** * 选号后置-意向单同步 * * @param array $params [orderId: 订单ID, goodsId: 商品ID, certName: 证件姓名, certNo: 证件编码, contactNum: 联系电话, postProvinceCode: 省份编码, postCityCode: 地市编码, postDistrictCode: 区县编码, postAddr: 详细地址, channel: 渠道, createTime: 创建时间, updateTime: 更新时间, orderTotalFee: 订单总金额, pageUrl: 页面地址, resourceId: 资源ID] * @return array */ public static function preOrdersync($params = []) { $url = '/link/king/card/preOrder/preOrdersync'; $data = array_only($params, [ 'orderId', 'goodsId', 'certName', 'certNo', 'contactNum', 'postProvinceCode', 'postCityCode', 'postDistrictCode', 'postAddr', 'channel', 'createTime', 'updateTime', 'orderTotalFee', 'pageUrl', 'resourceId' ]); $data['launchPlatform'] = self::getRandLaunchPlatform(); $result = self::send($url, $data); if (empty($result)) { return self::error('意向单同步请求失败'); } $resArr = json_decode($result, true); if ($resArr['rspCode'] != '0000') { return self::error('意向单同步请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']); } $body = $resArr['body'] ?? ''; if (is_string($body)) { $body = json_decode($body, true); } return self::success($body); } /** * 随机获取投放平台 * * @return void */ public static function getRandLaunchPlatform() { $data = [ '抖音APP', '快手APP', '拼多多APP', '腾讯视频APP', '手淘APP', '百度APP', '美团APP', '拼团微信小程序', '生态合作', '广点通', '巨量引擎' ]; $randKey = array_rand($data); $platform = $data[$randKey]; return $platform; } /** * 选号服务 * * @param array $params [goodsId: 商品ID, provinceCode: 省份编码, cityCode: 地市编码, searchCategory: 搜索类别: 1、普通选号 2、靓号选号 3、全部(普通、靓号都包括)] * @return array */ public static function linkNumSelect($params = []) { $url = '/link/num/select/v1'; $data = array_only($params ,[ 'goodsId', 'provinceCode', 'cityCode', 'searchCategory', ]); $data['qryType'] = '02'; $result = self::send($url, $data); if (empty($result)) { return self::error('选号服务请求失败'); } $resArr = json_decode($result, true); if ($resArr['rspCode'] != 'M0') { return self::error('选号服务请求失败: ' . $resArr['rspDesc']); } $body = $resArr['body'] ?? ''; $numArray = $body['numArray'] ?? []; $splitLen = $body['splitLen'] ?? 12; if (!empty($numArray)) { $numArray = array_column(array_chunk($numArray, $splitLen), 0); } return self::success($numArray); } /** * 选号后置-正式单同步(非提前预占版) * * @param array $params * @return array */ public static function ordersync($params = []) { $url = '/link/king/card/preOrder/ordersync/v2'; $data = array_only($params, [ 'goodsId', 'provinceCode', 'cityCode', 'phoneNum', 'token', 'createTime', 'isOpenCF' ]); $result = self::send($url, $data); if (empty($result)) { return self::error('正式单同步请求失败'); } $resArr = json_decode($result, true); if ($resArr['rspCode'] != '0000') { return self::error('正式单同步请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']); } $body = $resArr['body'] ?? ''; if (is_string($body)) { $body = json_decode($body, true); } return self::success($body); } /** * 查询省市区信息 * * @param array $params * @return array */ public static function postInfo($params = []) { $url = '/link/king/postInfo/qry'; $data = array_only($params, [ 'provinceCode' ]); $result = self::send($url, $data); if (empty($result)) { return self::error('查询省市区信息请求失败'); } $resArr = json_decode($result, true); if ($resArr['rspCode'] != '0000') { return self::error('查询省市区信息请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']); } $body = $resArr['body'] ?? ''; if (is_string($body)) { $body = json_decode($body, true); } return self::success($body); } /** * 获取订单消息 * * @return void */ public static function orderMsg($type = 4) { $messageChannelCode = config('site.message_channel_code'); if (empty($messageChannelCode)) { common_log('获取订单消息请求失败: 消息渠道编码为空'); return self::error('获取订单消息请求失败: 消息渠道编码为空'); } $url = '/link/king/card/msg/get/v1'; $data = [ 'msgChannel' => config('site.message_channel_code'), 'type' => $type, ]; $result = self::send($url, $data); if (empty($result)) { common_log('获取订单消息请求失败'); return self::error('获取订单消息请求失败'); } $resArr = json_decode($result, true); if ($resArr['rspCode'] != '0000') { common_log('获取订单消息请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']); return self::error('获取订单消息请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']); } $body = $resArr['body'] ?? ''; if (is_string($body)) { $body = json_decode($body, true); } $delMsgIds = []; foreach ($body as $val) { // 根据返回信息, 更新订单状态 $orderNo = $val['order'] ?? ''; // 订单变更类型:1:激活,2:退单(激活前),3:转套餐(要根据产品id判断是否为享有特权的套餐),4:销户(激活后),6:首充数据同步,C1:开户完成,E0:发货,SX:未支付超期,AX:未支付用户取消,SX和AX状态目前仅支持3.27接口同步订单 $state = $val['state'] ?? ''; $id = $val['id'] ?? ''; $order = MobileOrder::where('order_no', $orderNo)->find(); if (empty($order)) { $delMsgIds[] = $id; continue; } if ($state == 1) { // 激活 $order->produce_activation = 1; $order->save(); } else if ($state == 6) { // 首充 $amount = $val['lgtsId'] ?? 0; $order->produce_is_recharge = 1; $order->first_amount = $amount; $order->save(); } else if ($state == 'C1') { // 开户 $order->produce_status = 1; $order->save(); } else if ($state == 'E0') { // 发货 $order->logistics_numbers = $val['trackingNumber'] ?? ''; $order->save(); } $delMsgIds[] = $id; } $delMsgIds = implode(',', $delMsgIds); self::delOrderMsg($delMsgIds, $type); } /** * 删除订单消息 * * @return void */ public static function delOrderMsg($msgId = '', $type = 4) { $url = '/link/king/card/msg/del/v1'; $data = [ 'msgId' => $msgId, 'type' => $type, ]; $result = self::send($url, $data); if (empty($result)) { common_log('删除订单消息请求失败'); return self::error('删除订单消息请求失败'); } $resArr = json_decode($result, true); if ($resArr['rspCode'] != '0000') { common_log('删除订单消息请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']); return self::error('删除订单消息请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']); } $body = $resArr['body'] ?? ''; if (is_string($body)) { $body = json_decode($body, true); } $delSuccessNum = $body['delSuccessNum'] ?? 0; common_log('删除成功数量: ' . $delSuccessNum . ', 删除成功ID: ' . $msgId); return self::success('删除成功数量: ' . $delSuccessNum); } /** * 自动选号-订单同步接口 * * @param array $params [orderId: 订单ID, goodsId: 商品ID, certName: 证件姓名, certNo: 证件编码, contactNum: 联系电话, postProvinceCode: 省份编码, postCityCode: 地市编码, postDistrictCode: 区县编码, postAddr: 详细地址, channel: 渠道, createTime: 创建时间, updateTime: 更新时间, orderTotalFee: 订单总金额, pageUrl: 页面地址, resourceId: 资源ID] * @return array */ public static function autoNumSync($params = []) { $url = '/link/king/card/preOrder/autoNumSync'; $data = array_only($params, [ 'orderId', 'goodsId', 'certName', 'certNo', 'contactNum', 'postProvinceCode', 'postCityCode', 'postDistrictCode', 'postAddr', 'channel', 'createTime', 'updateTime', 'orderTotalFee', 'pageUrl', 'resourceId' ]); $data['launchPlatform'] = self::getRandLaunchPlatform(); $result = self::send($url, $data); if (empty($result)) { return self::error('自动选号请求失败'); } $resArr = json_decode($result, true); if ($resArr['rspCode'] != '0000') { return self::error('自动选号请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']); } $body = $resArr['body'] ?? ''; if (is_string($body)) { $body = json_decode($body, true); } return self::success($body); } }