123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763 |
- <?php
- namespace app\common\service;
- use app\common\model\Area;
- use app\common\model\MobileOrder;
- use app\common\model\Produce;
- /**
- * 联通商城zop订单统一接入文档
- */
- class ZopOrderService extends ZopBaseService
- {
- const PUSH_STATE = [
- '1' => '激活',
- '2' => '退单(激活前)',
- '3' => '转套餐',
- '4' => '销户(激活后)',
- '6' => '首充数据同步',
- 'C1' => '开户完成',
- 'E0' => '发货',
- ];
- /**
- * 推送订单到联通商城
- *
- * @param integer $orderId
- * @return array
- */
- public static function push($orderId = 0)
- {
- // 获取订单信息
- $order = MobileOrder::where('id', $orderId)->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;
- // 发展人编码
- $developingNo = $produce->developing_no ?? '';
- // 合约ID
- $contractId = $produce->contract_id ?? '';
- // 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, $order);
- 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, $order);
- 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 ?? '';
- // 判断产品是否使用用户收货地址区号
- $homeLocationType = $produce->home_location_type ?? 'address';
- $numProvinceCode = $produce->home_location_province ?? 0;
- $numCityCode = $produce->home_location_city ?? 0;
- $no = $order['no'];
- if ($homeLocationType == 'address') {
- // 自动选号-订单同步接口
- $autoOrderParams = [
- 'orderId' => $orderId,
- 'goodsId' => $goodsId,
- 'certName' => $certName,
- 'certNo' => $certNum,
- 'contactNum' => $contactNum,
- 'numProvinceCode' => $numProvinceCode,
- 'numCityCode' => $numCityCode,
- 'postProvinceCode' => $postProvinceCode,
- 'postCityCode' => $postCityCode,
- 'postDistrictCode' => $postDistrictCode,
- 'postAddr' => $postAddr,
- 'channel' => $channel,
- 'createTime' => $createTime,
- 'updateTime' => $updateTime,
- 'orderTotalFee' => $orderTotalFee,
- 'pageUrl' => $pageUrl,
- 'resourceId' => $resourceId,
- 'referrerCode' => $developingNo,
- ];
- $orderResult = self::autoNumSync($autoOrderParams, $order);
- if ($orderResult['code'] != 0) {
- // 写入订单错误信息
- $order->failure_reason = $orderResult['message'];
- $order->save();
-
- return $orderResult;
- }
- $no = $orderResult['data']['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,
- 'referrerCode' => $developingNo,
- ];
- if (!empty($contractId)) {
- $preOrderParams['contractId'] = $contractId;
- }
-
- $preOrderResult = self::preOrdersync($preOrderParams, $order);
- if ($preOrderResult['code'] != 0) {
- // 写入订单错误信息
- $order->failure_reason = $preOrderResult['message'];
- $order->save();
-
- return $preOrderResult;
- }
-
- $token = $preOrderResult['data']['token'] ?? '';
- // 4. 选号服务
- // 判断如果用户没有手机号, 则调用选号服务, 如果有手机号, 则跳过选号服务
- if (empty($no)) {
- $numSelectType = $produce->num_select_type ?? 1;
- $numSelectParams = [
- 'goodsId' => $goodsId,
- 'provinceCode' => $numProvinceCode,
- 'cityCode' => $numCityCode,
- 'searchCategory' => $numSelectType,
- ];
- $numSelectResult = self::linkNumSelect($numSelectParams, $order);
- if ($numSelectResult['code']!= 0) {
- // 写入订单错误信息
- $order->failure_reason = $numSelectResult['message'];
- $order->save();
-
- return $numSelectResult;
- }
- $numArray = $numSelectResult['data'] ?? [];
- if (count($numArray) > 0) {
- // 随机获取一个号码
- $no = $numArray[array_rand($numArray)]?? '';
- }
- if (empty($no)) {
- // 写入订单错误信息
- $order->failure_reason = '选号服务: 获取号码为空';
- $order->save();
- return self::error('选号服务: 获取号码为空');
- }
- $order->no = $no;
- $order->save();
- }
-
- // 5. 选号后置-正式单同步(非提前预占版)
- $phoneNum = $order->no ?? '';
- $isOpenCF = 0;
-
- $orderParams = [
- 'goodsId' => $goodsId,
- 'provinceCode' => $numProvinceCode,
- 'cityCode' => $numCityCode,
- 'phoneNum' => $phoneNum,
- 'token' => $token,
- 'createTime' => $createTime,
- 'isOpenCF' => $isOpenCF,
- ];
-
- $orderResult = self::ordersync($orderParams, $order);
- if ($orderResult['code'] != 0) {
- // 写入订单错误信息
- $order->failure_reason = $orderResult['message'];
- $order->save();
-
- return $orderResult;
- }
- }
- $order->failure_reason = '操作成功';
- $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 = [], $order = null)
- {
- $url = '/link/king/resource/upload/v1';
- $data = array_only($params, [
- 'channel',
- 'goodsId',
- 'resourceContents'
- ]);
- $result = self::send($url, $data, $order);
- 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 = [], $order = null)
- {
- $url = '/link/king/identity/cust/v2';
- $data = array_only($params, [
- 'province',
- 'city',
- 'certName',
- 'certNum'
- ]);
- $result = self::send($url, $data, $order);
- 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 = [], $order = null)
- {
- $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',
- 'referrerCode',
- ]);
- $data['launchPlatform'] = self::getRandLaunchPlatform();
- $result = self::send($url, $data, $order);
- 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 = [], $order = null)
- {
- $url = '/link/num/select/v1';
- $data = array_only($params ,[
- 'goodsId',
- 'provinceCode',
- 'cityCode',
- 'searchCategory',
- ]);
- $data['qryType'] = '02';
- $result = self::send($url, $data, $order);
- 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 = [], $order = null)
- {
- $url = '/link/king/card/preOrder/ordersync/v2';
- $data = array_only($params, [
- 'goodsId',
- 'provinceCode',
- 'cityCode',
- 'phoneNum',
- 'token',
- 'createTime',
- 'isOpenCF'
- ]);
- $result = self::send($url, $data, $order);
- 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 = [], $order = null)
- {
- $url = '/link/king/postInfo/qry';
- $data = array_only($params, [
- 'provinceCode'
- ]);
- $result = self::send($url, $data, $order);
- 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($messageChannelCode = '', $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' => $messageChannelCode,
- '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 = [];
- $delMsgVal = [];
- 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;
- }
- $order->state = $state;
- $order->state_name = self::PUSH_STATE[$state] ?? '';
- $order->save();
- 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->first_time = date('Y-m-d H:i:s');
- $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;
- $delMsgVal[] = $val;
- }
- $delMsgIds = implode(',', $delMsgIds);
- if (!empty($delMsgIds)) {
- common_log('删除订单消息ID: ' . $delMsgIds);
- common_log('删除消息内容: ' . json_encode($delMsgVal, JSON_UNESCAPED_UNICODE));
- 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 = [], $order = null)
- {
- $url = '/link/king/card/preOrder/autoNumSync';
- $data = array_only($params, [
- 'orderId',
- 'goodsId',
- 'certName',
- 'certNo',
- 'contactNum',
- 'numProvinceCode',
- 'numCityCode',
- 'postProvinceCode',
- 'postCityCode',
- 'postDistrictCode',
- 'postAddr',
- 'channel',
- 'createTime',
- 'updateTime',
- 'orderTotalFee',
- 'pageUrl',
- 'resourceId',
- 'referrerCode'
- ]);
- $data['launchPlatform'] = self::getRandLaunchPlatform();
- // 额外加
- $data['contractId'] = '91200266';
- $result = self::send($url, $data, $order);
- 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);
- }
- }
|