ZopOrderService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\Area;
  4. use app\common\model\MobileOrder;
  5. use app\common\model\Produce;
  6. /**
  7. * 联通商城zop订单统一接入文档
  8. */
  9. class ZopOrderService extends ZopBaseService
  10. {
  11. /**
  12. * 推送订单到联通商城
  13. *
  14. * @param integer $orderId
  15. * @return array
  16. */
  17. public static function push($orderId = 0)
  18. {
  19. // 获取订单信息
  20. $order = MobileOrder::where('id', $orderId)->with('produce')->find();
  21. if (empty($order)) {
  22. return self::error('订单不存在');
  23. }
  24. $produce = $order->produce ?? null;
  25. if (empty($produce)) {
  26. return self::error('商品不存在');
  27. }
  28. if ($order->is_cancel == 1) {
  29. return self::error('订单已取消');
  30. }
  31. // 根据商品类型, 调用不同的推送方法
  32. $operator = $produce->operator ?? '';
  33. $result = [];
  34. switch ($operator) {
  35. case 'cucc':
  36. $result = self::cuccPush($order, $produce);
  37. break;
  38. case 'cmcc':
  39. break;
  40. case 'ctcc':
  41. break;
  42. default:
  43. $result = self::error('运营商类型错误');
  44. }
  45. return $result;
  46. }
  47. /**
  48. * 联通推送
  49. *
  50. * @param MobileOrder $order
  51. * @param Produce $produce
  52. * @return void
  53. */
  54. public static function cuccPush(MobileOrder $order, Produce $produce)
  55. {
  56. // 1. 资源上传
  57. $goodsId = $produce->api_goods_id ?? 0;
  58. $channel = $produce->contact_code ?? '';
  59. $images = $produce->images ?? '';
  60. $resourceId = $produce->resource_id ?? 0;
  61. // if (empty($resourceId)) {
  62. if (empty($goodsId)) {
  63. return self::error('上游编码为空');
  64. }
  65. if (empty($channel)) {
  66. return self::error('触点编码为空');
  67. }
  68. if (empty($images)) {
  69. return self::error('图片为空');
  70. }
  71. // 如果图片是地址的话, 就转换成base64
  72. if (strpos($images, 'http') === 0) {
  73. $imagesBase = base64_encode(file_get_contents($images));
  74. // 获取图片 MIME 类型
  75. $sizeInfo = getimagesize($images);
  76. $mimeType = $sizeInfo['mime'];
  77. // 构建数据 URI
  78. $images = 'data:' . $mimeType . ';base64,' . $imagesBase;
  79. }
  80. $resourceParams = [
  81. 'goodsId' => $goodsId,
  82. 'channel' => $channel,
  83. 'resourceContents' => [
  84. [
  85. 'content' => $images,
  86. 'sort' => 0
  87. ]
  88. ],
  89. ];
  90. $resourceResult = self::resourceUpload($resourceParams, $order);
  91. if ($resourceResult['code'] != 0) {
  92. // 写入订单错误信息
  93. $order->failure_reason = $resourceResult['message'];
  94. $order->save();
  95. return $resourceResult;
  96. }
  97. // 获取资源ID
  98. $resourceId = $resourceResult['data']['resourceId'] ?? '';
  99. $produce->resource_id = $resourceId;
  100. $produce->save();
  101. // }
  102. // 2. 客户资料校验
  103. $cityData = $order->city ?? '';
  104. $cityData = explode(',', $cityData);
  105. if (count($cityData) < 2) {
  106. return self::error('城市信息不完整');
  107. }
  108. $province = $cityData[0] ?? 0;
  109. $city = $cityData[1] ?? 0;
  110. $district = $cityData[2] ?? 0;
  111. $provinceNumCode = Area::where('id', $province)->value('num_code');
  112. $cityNumCode = Area::where('id', $city)->value('num_code');
  113. $certName = $order->name ?? '';
  114. $certNum = $order->id_no ?? '';
  115. if (empty($provinceNumCode) || empty($cityNumCode) || empty($certName) || empty($certNum)) {
  116. return self::error('客户信息不完整');
  117. }
  118. $identityParams = [
  119. 'province' => $provinceNumCode,
  120. 'city' => $cityNumCode,
  121. 'certName' => $certName,
  122. 'certNum' => $certNum,
  123. ];
  124. $identityResult = self::identityCust($identityParams, $order);
  125. if ($identityResult['code'] != 0) {
  126. // 写入订单错误信息
  127. $order->failure_reason = $identityResult['message'];
  128. $order->save();
  129. return $identityResult;
  130. }
  131. // 获取uuid
  132. $uuid = $identityResult['data']['uuid'] ?? '';
  133. // 3.0 判断有没有手机号, 如果没有手机号, 则走自动选好接口
  134. $orderId = $order->order_no ?? '';
  135. $contactNum = $order->phone ?? '';
  136. $postProvinceCode = Area::where('id', $province)->value('post_code');
  137. $postCityCode = Area::where('id', $city)->value('post_code');
  138. $postDistrictCode = Area::where('id', $district)->value('post_code');
  139. $postAddr = $order->address ?? '';
  140. $createTime = $order->create_time ?? '';
  141. $updateTime = $order->update_time ?? '';
  142. $amount = $order->amount ?? 0;
  143. $orderTotalFee = bcmul($amount, 1000, 0);
  144. $pageUrl = $produce->link ?? '';
  145. $no = $order['no'];
  146. if (empty($no)) {
  147. // 自动选号-订单同步接口
  148. $autoOrderParams = [
  149. 'orderId' => $orderId,
  150. 'goodsId' => $goodsId,
  151. 'certName' => $certName,
  152. 'certNo' => $certNum,
  153. 'contactNum' => $contactNum,
  154. 'postProvinceCode' => $postProvinceCode,
  155. 'postCityCode' => $postCityCode,
  156. 'postDistrictCode' => $postDistrictCode,
  157. 'postAddr' => $postAddr,
  158. 'channel' => $channel,
  159. 'createTime' => $createTime,
  160. 'updateTime' => $updateTime,
  161. 'orderTotalFee' => $orderTotalFee,
  162. 'pageUrl' => $pageUrl,
  163. 'resourceId' => $resourceId,
  164. ];
  165. $autoOrderResult = self::autoNumSync($autoOrderParams, $order);
  166. if ($autoOrderResult['code'] != 0) {
  167. // 写入订单错误信息
  168. $order->failure_reason = $autoOrderResult['message'];
  169. $order->save();
  170. return $autoOrderResult;
  171. }
  172. $no = $autoOrderResult['preNumber'];
  173. $order->no = $no;
  174. $order->save();
  175. } else {
  176. // 3. 选号后置-意向单同步
  177. $preOrderParams = [
  178. 'orderId' => $orderId,
  179. 'goodsId' => $goodsId,
  180. 'certName' => $certName,
  181. 'certNo' => $certNum,
  182. 'contactNum' => $contactNum,
  183. 'postProvinceCode' => $postProvinceCode,
  184. 'postCityCode' => $postCityCode,
  185. 'postDistrictCode' => $postDistrictCode,
  186. 'postAddr' => $postAddr,
  187. 'channel' => $channel,
  188. 'createTime' => $createTime,
  189. 'updateTime' => $updateTime,
  190. 'orderTotalFee' => $orderTotalFee,
  191. 'pageUrl' => $pageUrl,
  192. 'resourceId' => $resourceId,
  193. ];
  194. $preOrderResult = self::preOrdersync($preOrderParams, $order);
  195. if ($preOrderResult['code'] != 0) {
  196. // 写入订单错误信息
  197. $order->failure_reason = $preOrderResult['message'];
  198. $order->save();
  199. return $preOrderResult;
  200. }
  201. $token = $preOrderResult['data']['token'] ?? '';
  202. // 4. 选号服务(暂不需要)
  203. // 5. 选号后置-正式单同步(非提前预占版)
  204. $phoneNum = $order->no ?? '';
  205. $isOpenCF = 0;
  206. // 判断产品是否使用用户收货地址区号
  207. $homeLocationType = $produce->home_location_type ?? 'address';
  208. if ($homeLocationType == 'assign') {
  209. $provinceNumCode = $produce->home_location_province ?? 0;
  210. $cityNumCode = $produce->home_location_city ?? 0;
  211. }
  212. $orderParams = [
  213. 'goodsId' => $goodsId,
  214. 'provinceCode' => $provinceNumCode,
  215. 'cityCode' => $cityNumCode,
  216. 'phoneNum' => $phoneNum,
  217. 'token' => $token,
  218. 'createTime' => $createTime,
  219. 'isOpenCF' => $isOpenCF,
  220. ];
  221. $orderResult = self::ordersync($orderParams, $order);
  222. if ($orderResult['code'] != 0) {
  223. // 写入订单错误信息
  224. $order->failure_reason = $orderResult['message'];
  225. $order->save();
  226. return $orderResult;
  227. }
  228. }
  229. $order->is_push_zop = 1;
  230. $order->save();
  231. return $orderResult;
  232. }
  233. /**
  234. * 资源上传
  235. *
  236. * @param array $params [channel: 渠道, goodsId: 商品ID, resourceContents: [content: 资源内容, sort: 排序]]]
  237. * @return array
  238. */
  239. public static function resourceUpload($params = [], $order = null)
  240. {
  241. $url = '/link/king/resource/upload/v1';
  242. $data = array_only($params, [
  243. 'channel',
  244. 'goodsId',
  245. 'resourceContents'
  246. ]);
  247. $result = self::send($url, $data, $order);
  248. if (empty($result)) {
  249. return self::error('资源上传请求失败');
  250. }
  251. $resArr = json_decode($result, true);
  252. if ($resArr['rspCode'] != '0000') {
  253. return self::error('资源上传请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']);
  254. }
  255. $body = $resArr['body'];
  256. return self::success($body);
  257. }
  258. /**
  259. * 客户资料校验
  260. *
  261. * @param array $params [province: 省份编码, city.地市编码, certName. 证件姓名, certNum. 证件编码]
  262. * @return array
  263. */
  264. public static function identityCust($params = [], $order = null)
  265. {
  266. $url = '/link/king/identity/cust/v2';
  267. $data = array_only($params, [
  268. 'province',
  269. 'city',
  270. 'certName',
  271. 'certNum'
  272. ]);
  273. $result = self::send($url, $data, $order);
  274. if (empty($result)) {
  275. return self::error('客户资料校验请求失败');
  276. }
  277. $resArr = json_decode($result, true);
  278. if ($resArr['aCode'] != '0000') {
  279. return self::error('客户资料校验请求失败, 错误编码为: ' . $resArr['aCode'] . '错误信息为: ' . $resArr['aDesc']);
  280. }
  281. if ($resArr['bCode'] != '0000') {
  282. return self::error('客户资料校验请求失败, 错误编码为: ' . $resArr['bCode'] . '错误信息为: ' . $resArr['bDesc']);
  283. }
  284. $uuid = $resArr['uuid'] ?? '';
  285. return self::success(['uuid' => $uuid]);
  286. }
  287. /**
  288. * 选号后置-意向单同步
  289. *
  290. * @param array $params [orderId: 订单ID, goodsId: 商品ID, certName: 证件姓名, certNo: 证件编码, contactNum: 联系电话, postProvinceCode: 省份编码, postCityCode: 地市编码, postDistrictCode: 区县编码, postAddr: 详细地址, channel: 渠道, createTime: 创建时间, updateTime: 更新时间, orderTotalFee: 订单总金额, pageUrl: 页面地址, resourceId: 资源ID]
  291. * @return array
  292. */
  293. public static function preOrdersync($params = [], $order = null)
  294. {
  295. $url = '/link/king/card/preOrder/preOrdersync';
  296. $data = array_only($params, [
  297. 'orderId',
  298. 'goodsId',
  299. 'certName',
  300. 'certNo',
  301. 'contactNum',
  302. 'postProvinceCode',
  303. 'postCityCode',
  304. 'postDistrictCode',
  305. 'postAddr',
  306. 'channel',
  307. 'createTime',
  308. 'updateTime',
  309. 'orderTotalFee',
  310. 'pageUrl',
  311. 'resourceId'
  312. ]);
  313. $data['launchPlatform'] = self::getRandLaunchPlatform();
  314. $result = self::send($url, $data, $order);
  315. if (empty($result)) {
  316. return self::error('意向单同步请求失败');
  317. }
  318. $resArr = json_decode($result, true);
  319. if ($resArr['rspCode'] != '0000') {
  320. return self::error('意向单同步请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']);
  321. }
  322. $body = $resArr['body'] ?? '';
  323. if (is_string($body)) {
  324. $body = json_decode($body, true);
  325. }
  326. return self::success($body);
  327. }
  328. /**
  329. * 随机获取投放平台
  330. *
  331. * @return void
  332. */
  333. public static function getRandLaunchPlatform()
  334. {
  335. $data = [
  336. '抖音APP',
  337. '快手APP',
  338. '拼多多APP',
  339. '腾讯视频APP',
  340. '手淘APP',
  341. '百度APP',
  342. '美团APP',
  343. '拼团微信小程序',
  344. '生态合作',
  345. '广点通',
  346. '巨量引擎'
  347. ];
  348. $randKey = array_rand($data);
  349. $platform = $data[$randKey];
  350. return $platform;
  351. }
  352. /**
  353. * 选号服务
  354. *
  355. * @param array $params [goodsId: 商品ID, provinceCode: 省份编码, cityCode: 地市编码, searchCategory: 搜索类别: 1、普通选号 2、靓号选号 3、全部(普通、靓号都包括)]
  356. * @return array
  357. */
  358. public static function linkNumSelect($params = [], $order = null)
  359. {
  360. $url = '/link/num/select/v1';
  361. $data = array_only($params ,[
  362. 'goodsId',
  363. 'provinceCode',
  364. 'cityCode',
  365. 'searchCategory',
  366. ]);
  367. $data['qryType'] = '02';
  368. $result = self::send($url, $data, $order);
  369. if (empty($result)) {
  370. return self::error('选号服务请求失败');
  371. }
  372. $resArr = json_decode($result, true);
  373. if ($resArr['rspCode'] != 'M0') {
  374. return self::error('选号服务请求失败: ' . $resArr['rspDesc']);
  375. }
  376. $body = $resArr['body'] ?? '';
  377. $numArray = $body['numArray'] ?? [];
  378. $splitLen = $body['splitLen'] ?? 12;
  379. if (!empty($numArray)) {
  380. $numArray = array_column(array_chunk($numArray, $splitLen), 0);
  381. }
  382. return self::success($numArray);
  383. }
  384. /**
  385. * 选号后置-正式单同步(非提前预占版)
  386. *
  387. * @param array $params
  388. * @return array
  389. */
  390. public static function ordersync($params = [], $order = null)
  391. {
  392. $url = '/link/king/card/preOrder/ordersync/v2';
  393. $data = array_only($params, [
  394. 'goodsId',
  395. 'provinceCode',
  396. 'cityCode',
  397. 'phoneNum',
  398. 'token',
  399. 'createTime',
  400. 'isOpenCF'
  401. ]);
  402. $result = self::send($url, $data, $order);
  403. if (empty($result)) {
  404. return self::error('正式单同步请求失败');
  405. }
  406. $resArr = json_decode($result, true);
  407. if ($resArr['rspCode'] != '0000') {
  408. return self::error('正式单同步请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']);
  409. }
  410. $body = $resArr['body'] ?? '';
  411. if (is_string($body)) {
  412. $body = json_decode($body, true);
  413. }
  414. return self::success($body);
  415. }
  416. /**
  417. * 查询省市区信息
  418. *
  419. * @param array $params
  420. * @return array
  421. */
  422. public static function postInfo($params = [], $order = null)
  423. {
  424. $url = '/link/king/postInfo/qry';
  425. $data = array_only($params, [
  426. 'provinceCode'
  427. ]);
  428. $result = self::send($url, $data, $order);
  429. if (empty($result)) {
  430. return self::error('查询省市区信息请求失败');
  431. }
  432. $resArr = json_decode($result, true);
  433. if ($resArr['rspCode'] != '0000') {
  434. return self::error('查询省市区信息请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']);
  435. }
  436. $body = $resArr['body'] ?? '';
  437. if (is_string($body)) {
  438. $body = json_decode($body, true);
  439. }
  440. return self::success($body);
  441. }
  442. /**
  443. * 获取订单消息
  444. *
  445. * @return void
  446. */
  447. public static function orderMsg($type = 4)
  448. {
  449. $messageChannelCode = config('site.message_channel_code');
  450. if (empty($messageChannelCode)) {
  451. common_log('获取订单消息请求失败: 消息渠道编码为空');
  452. return self::error('获取订单消息请求失败: 消息渠道编码为空');
  453. }
  454. $url = '/link/king/card/msg/get/v1';
  455. $data = [
  456. 'msgChannel' => config('site.message_channel_code'),
  457. 'type' => $type,
  458. ];
  459. $result = self::send($url, $data);
  460. if (empty($result)) {
  461. common_log('获取订单消息请求失败');
  462. return self::error('获取订单消息请求失败');
  463. }
  464. $resArr = json_decode($result, true);
  465. if ($resArr['rspCode'] != '0000') {
  466. common_log('获取订单消息请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']);
  467. return self::error('获取订单消息请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']);
  468. }
  469. $body = $resArr['body'] ?? '';
  470. if (is_string($body)) {
  471. $body = json_decode($body, true);
  472. }
  473. $delMsgIds = [];
  474. foreach ($body as $val) {
  475. // 根据返回信息, 更新订单状态
  476. $orderNo = $val['order'] ?? '';
  477. // 订单变更类型:1:激活,2:退单(激活前),3:转套餐(要根据产品id判断是否为享有特权的套餐),4:销户(激活后),6:首充数据同步,C1:开户完成,E0:发货,SX:未支付超期,AX:未支付用户取消,SX和AX状态目前仅支持3.27接口同步订单
  478. $state = $val['state'] ?? '';
  479. $id = $val['id'] ?? '';
  480. $order = MobileOrder::where('order_no', $orderNo)->find();
  481. if (empty($order)) {
  482. $delMsgIds[] = $id;
  483. continue;
  484. }
  485. if ($state == 1) {
  486. // 激活
  487. $order->produce_activation = 1;
  488. $order->save();
  489. } else if ($state == 6) {
  490. // 首充
  491. $amount = $val['lgtsId'] ?? 0;
  492. $order->produce_is_recharge = 1;
  493. $order->first_amount = $amount;
  494. $order->save();
  495. } else if ($state == 'C1') {
  496. // 开户
  497. $order->produce_status = 1;
  498. $order->save();
  499. } else if ($state == 'E0') {
  500. // 发货
  501. $order->logistics_numbers = $val['trackingNumber'] ?? '';
  502. $order->save();
  503. }
  504. $delMsgIds[] = $id;
  505. }
  506. $delMsgIds = implode(',', $delMsgIds);
  507. self::delOrderMsg($delMsgIds, $type);
  508. }
  509. /**
  510. * 删除订单消息
  511. *
  512. * @return void
  513. */
  514. public static function delOrderMsg($msgId = '', $type = 4)
  515. {
  516. $url = '/link/king/card/msg/del/v1';
  517. $data = [
  518. 'msgId' => $msgId,
  519. 'type' => $type,
  520. ];
  521. $result = self::send($url, $data);
  522. if (empty($result)) {
  523. common_log('删除订单消息请求失败');
  524. return self::error('删除订单消息请求失败');
  525. }
  526. $resArr = json_decode($result, true);
  527. if ($resArr['rspCode'] != '0000') {
  528. common_log('删除订单消息请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']);
  529. return self::error('删除订单消息请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']);
  530. }
  531. $body = $resArr['body'] ?? '';
  532. if (is_string($body)) {
  533. $body = json_decode($body, true);
  534. }
  535. $delSuccessNum = $body['delSuccessNum'] ?? 0;
  536. common_log('删除成功数量: ' . $delSuccessNum . ', 删除成功ID: ' . $msgId);
  537. return self::success('删除成功数量: ' . $delSuccessNum);
  538. }
  539. /**
  540. * 自动选号-订单同步接口
  541. *
  542. * @param array $params [orderId: 订单ID, goodsId: 商品ID, certName: 证件姓名, certNo: 证件编码, contactNum: 联系电话, postProvinceCode: 省份编码, postCityCode: 地市编码, postDistrictCode: 区县编码, postAddr: 详细地址, channel: 渠道, createTime: 创建时间, updateTime: 更新时间, orderTotalFee: 订单总金额, pageUrl: 页面地址, resourceId: 资源ID]
  543. * @return array
  544. */
  545. public static function autoNumSync($params = [], $order = null)
  546. {
  547. $url = '/link/king/card/preOrder/autoNumSync';
  548. $data = array_only($params, [
  549. 'orderId',
  550. 'goodsId',
  551. 'certName',
  552. 'certNo',
  553. 'contactNum',
  554. 'postProvinceCode',
  555. 'postCityCode',
  556. 'postDistrictCode',
  557. 'postAddr',
  558. 'channel',
  559. 'createTime',
  560. 'updateTime',
  561. 'orderTotalFee',
  562. 'pageUrl',
  563. 'resourceId'
  564. ]);
  565. $data['launchPlatform'] = self::getRandLaunchPlatform();
  566. $result = self::send($url, $data, $order);
  567. if (empty($result)) {
  568. return self::error('自动选号请求失败');
  569. }
  570. $resArr = json_decode($result, true);
  571. if ($resArr['rspCode'] != '0000') {
  572. return self::error('自动选号请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']);
  573. }
  574. $body = $resArr['body'] ?? '';
  575. if (is_string($body)) {
  576. $body = json_decode($body, true);
  577. }
  578. return self::success($body);
  579. }
  580. }