ZopOrderService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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. // 判断产品是否使用用户收货地址区号
  146. $homeLocationType = $produce->home_location_type ?? 'address';
  147. if ($homeLocationType == 'assign') {
  148. $postProvinceCode = $produce->home_location_province ?? 0;
  149. $postCityCode = $produce->home_location_city ?? 0;
  150. }
  151. $no = $order['no'];
  152. if (empty($no)) {
  153. // 自动选号-订单同步接口
  154. $autoOrderParams = [
  155. 'orderId' => $orderId,
  156. 'goodsId' => $goodsId,
  157. 'certName' => $certName,
  158. 'certNo' => $certNum,
  159. 'contactNum' => $contactNum,
  160. 'postProvinceCode' => $postProvinceCode,
  161. 'postCityCode' => $postCityCode,
  162. 'postDistrictCode' => $postDistrictCode,
  163. 'postAddr' => $postAddr,
  164. 'channel' => $channel,
  165. 'createTime' => $createTime,
  166. 'updateTime' => $updateTime,
  167. 'orderTotalFee' => $orderTotalFee,
  168. 'pageUrl' => $pageUrl,
  169. 'resourceId' => $resourceId,
  170. ];
  171. $orderResult = self::autoNumSync($autoOrderParams, $order);
  172. if ($orderResult['code'] != 0) {
  173. // 写入订单错误信息
  174. $order->failure_reason = $orderResult['message'];
  175. $order->save();
  176. return $orderResult;
  177. }
  178. $no = $orderResult['data']['preNumber'] ?? '';
  179. $order->no = $no;
  180. $order->save();
  181. } else {
  182. // 3. 选号后置-意向单同步
  183. $preOrderParams = [
  184. 'orderId' => $orderId,
  185. 'goodsId' => $goodsId,
  186. 'certName' => $certName,
  187. 'certNo' => $certNum,
  188. 'contactNum' => $contactNum,
  189. 'postProvinceCode' => $postProvinceCode,
  190. 'postCityCode' => $postCityCode,
  191. 'postDistrictCode' => $postDistrictCode,
  192. 'postAddr' => $postAddr,
  193. 'channel' => $channel,
  194. 'createTime' => $createTime,
  195. 'updateTime' => $updateTime,
  196. 'orderTotalFee' => $orderTotalFee,
  197. 'pageUrl' => $pageUrl,
  198. 'resourceId' => $resourceId,
  199. ];
  200. $preOrderResult = self::preOrdersync($preOrderParams, $order);
  201. if ($preOrderResult['code'] != 0) {
  202. // 写入订单错误信息
  203. $order->failure_reason = $preOrderResult['message'];
  204. $order->save();
  205. return $preOrderResult;
  206. }
  207. $token = $preOrderResult['data']['token'] ?? '';
  208. // 4. 选号服务(暂不需要)
  209. // 5. 选号后置-正式单同步(非提前预占版)
  210. $phoneNum = $order->no ?? '';
  211. $isOpenCF = 0;
  212. // 判断产品是否使用用户收货地址区号
  213. $homeLocationType = $produce->home_location_type ?? 'address';
  214. if ($homeLocationType == 'assign') {
  215. $provinceNumCode = $produce->home_location_province ?? 0;
  216. $cityNumCode = $produce->home_location_city ?? 0;
  217. }
  218. $orderParams = [
  219. 'goodsId' => $goodsId,
  220. 'provinceCode' => $provinceNumCode,
  221. 'cityCode' => $cityNumCode,
  222. 'phoneNum' => $phoneNum,
  223. 'token' => $token,
  224. 'createTime' => $createTime,
  225. 'isOpenCF' => $isOpenCF,
  226. ];
  227. $orderResult = self::ordersync($orderParams, $order);
  228. if ($orderResult['code'] != 0) {
  229. // 写入订单错误信息
  230. $order->failure_reason = $orderResult['message'];
  231. $order->save();
  232. return $orderResult;
  233. }
  234. }
  235. $order->is_push_zop = 1;
  236. $order->save();
  237. return $orderResult;
  238. }
  239. /**
  240. * 资源上传
  241. *
  242. * @param array $params [channel: 渠道, goodsId: 商品ID, resourceContents: [content: 资源内容, sort: 排序]]]
  243. * @return array
  244. */
  245. public static function resourceUpload($params = [], $order = null)
  246. {
  247. $url = '/link/king/resource/upload/v1';
  248. $data = array_only($params, [
  249. 'channel',
  250. 'goodsId',
  251. 'resourceContents'
  252. ]);
  253. $result = self::send($url, $data, $order);
  254. if (empty($result)) {
  255. return self::error('资源上传请求失败');
  256. }
  257. $resArr = json_decode($result, true);
  258. if ($resArr['rspCode'] != '0000') {
  259. return self::error('资源上传请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']);
  260. }
  261. $body = $resArr['body'];
  262. return self::success($body);
  263. }
  264. /**
  265. * 客户资料校验
  266. *
  267. * @param array $params [province: 省份编码, city.地市编码, certName. 证件姓名, certNum. 证件编码]
  268. * @return array
  269. */
  270. public static function identityCust($params = [], $order = null)
  271. {
  272. $url = '/link/king/identity/cust/v2';
  273. $data = array_only($params, [
  274. 'province',
  275. 'city',
  276. 'certName',
  277. 'certNum'
  278. ]);
  279. $result = self::send($url, $data, $order);
  280. if (empty($result)) {
  281. return self::error('客户资料校验请求失败');
  282. }
  283. $resArr = json_decode($result, true);
  284. if ($resArr['aCode'] != '0000') {
  285. return self::error('客户资料校验请求失败, 错误编码为: ' . $resArr['aCode'] . '错误信息为: ' . $resArr['aDesc']);
  286. }
  287. if ($resArr['bCode'] != '0000') {
  288. return self::error('客户资料校验请求失败, 错误编码为: ' . $resArr['bCode'] . '错误信息为: ' . $resArr['bDesc']);
  289. }
  290. $uuid = $resArr['uuid'] ?? '';
  291. return self::success(['uuid' => $uuid]);
  292. }
  293. /**
  294. * 选号后置-意向单同步
  295. *
  296. * @param array $params [orderId: 订单ID, goodsId: 商品ID, certName: 证件姓名, certNo: 证件编码, contactNum: 联系电话, postProvinceCode: 省份编码, postCityCode: 地市编码, postDistrictCode: 区县编码, postAddr: 详细地址, channel: 渠道, createTime: 创建时间, updateTime: 更新时间, orderTotalFee: 订单总金额, pageUrl: 页面地址, resourceId: 资源ID]
  297. * @return array
  298. */
  299. public static function preOrdersync($params = [], $order = null)
  300. {
  301. $url = '/link/king/card/preOrder/preOrdersync';
  302. $data = array_only($params, [
  303. 'orderId',
  304. 'goodsId',
  305. 'certName',
  306. 'certNo',
  307. 'contactNum',
  308. 'postProvinceCode',
  309. 'postCityCode',
  310. 'postDistrictCode',
  311. 'postAddr',
  312. 'channel',
  313. 'createTime',
  314. 'updateTime',
  315. 'orderTotalFee',
  316. 'pageUrl',
  317. 'resourceId'
  318. ]);
  319. $data['launchPlatform'] = self::getRandLaunchPlatform();
  320. $result = self::send($url, $data, $order);
  321. if (empty($result)) {
  322. return self::error('意向单同步请求失败');
  323. }
  324. $resArr = json_decode($result, true);
  325. if ($resArr['rspCode'] != '0000') {
  326. return self::error('意向单同步请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']);
  327. }
  328. $body = $resArr['body'] ?? '';
  329. if (is_string($body)) {
  330. $body = json_decode($body, true);
  331. }
  332. return self::success($body);
  333. }
  334. /**
  335. * 随机获取投放平台
  336. *
  337. * @return void
  338. */
  339. public static function getRandLaunchPlatform()
  340. {
  341. $data = [
  342. '抖音APP',
  343. '快手APP',
  344. '拼多多APP',
  345. '腾讯视频APP',
  346. '手淘APP',
  347. '百度APP',
  348. '美团APP',
  349. '拼团微信小程序',
  350. '生态合作',
  351. '广点通',
  352. '巨量引擎'
  353. ];
  354. $randKey = array_rand($data);
  355. $platform = $data[$randKey];
  356. return $platform;
  357. }
  358. /**
  359. * 选号服务
  360. *
  361. * @param array $params [goodsId: 商品ID, provinceCode: 省份编码, cityCode: 地市编码, searchCategory: 搜索类别: 1、普通选号 2、靓号选号 3、全部(普通、靓号都包括)]
  362. * @return array
  363. */
  364. public static function linkNumSelect($params = [], $order = null)
  365. {
  366. $url = '/link/num/select/v1';
  367. $data = array_only($params ,[
  368. 'goodsId',
  369. 'provinceCode',
  370. 'cityCode',
  371. 'searchCategory',
  372. ]);
  373. $data['qryType'] = '02';
  374. $result = self::send($url, $data, $order);
  375. if (empty($result)) {
  376. return self::error('选号服务请求失败');
  377. }
  378. $resArr = json_decode($result, true);
  379. if ($resArr['rspCode'] != 'M0') {
  380. return self::error('选号服务请求失败: ' . $resArr['rspDesc']);
  381. }
  382. $body = $resArr['body'] ?? '';
  383. $numArray = $body['numArray'] ?? [];
  384. $splitLen = $body['splitLen'] ?? 12;
  385. if (!empty($numArray)) {
  386. $numArray = array_column(array_chunk($numArray, $splitLen), 0);
  387. }
  388. return self::success($numArray);
  389. }
  390. /**
  391. * 选号后置-正式单同步(非提前预占版)
  392. *
  393. * @param array $params
  394. * @return array
  395. */
  396. public static function ordersync($params = [], $order = null)
  397. {
  398. $url = '/link/king/card/preOrder/ordersync/v2';
  399. $data = array_only($params, [
  400. 'goodsId',
  401. 'provinceCode',
  402. 'cityCode',
  403. 'phoneNum',
  404. 'token',
  405. 'createTime',
  406. 'isOpenCF'
  407. ]);
  408. $result = self::send($url, $data, $order);
  409. if (empty($result)) {
  410. return self::error('正式单同步请求失败');
  411. }
  412. $resArr = json_decode($result, true);
  413. if ($resArr['rspCode'] != '0000') {
  414. return self::error('正式单同步请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']);
  415. }
  416. $body = $resArr['body'] ?? '';
  417. if (is_string($body)) {
  418. $body = json_decode($body, true);
  419. }
  420. return self::success($body);
  421. }
  422. /**
  423. * 查询省市区信息
  424. *
  425. * @param array $params
  426. * @return array
  427. */
  428. public static function postInfo($params = [], $order = null)
  429. {
  430. $url = '/link/king/postInfo/qry';
  431. $data = array_only($params, [
  432. 'provinceCode'
  433. ]);
  434. $result = self::send($url, $data, $order);
  435. if (empty($result)) {
  436. return self::error('查询省市区信息请求失败');
  437. }
  438. $resArr = json_decode($result, true);
  439. if ($resArr['rspCode'] != '0000') {
  440. return self::error('查询省市区信息请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']);
  441. }
  442. $body = $resArr['body'] ?? '';
  443. if (is_string($body)) {
  444. $body = json_decode($body, true);
  445. }
  446. return self::success($body);
  447. }
  448. /**
  449. * 获取订单消息
  450. *
  451. * @return void
  452. */
  453. public static function orderMsg($type = 4)
  454. {
  455. $messageChannelCode = config('site.message_channel_code');
  456. if (empty($messageChannelCode)) {
  457. common_log('获取订单消息请求失败: 消息渠道编码为空');
  458. return self::error('获取订单消息请求失败: 消息渠道编码为空');
  459. }
  460. $url = '/link/king/card/msg/get/v1';
  461. $data = [
  462. 'msgChannel' => config('site.message_channel_code'),
  463. 'type' => $type,
  464. ];
  465. $result = self::send($url, $data);
  466. if (empty($result)) {
  467. common_log('获取订单消息请求失败');
  468. return self::error('获取订单消息请求失败');
  469. }
  470. $resArr = json_decode($result, true);
  471. if ($resArr['rspCode'] != '0000') {
  472. common_log('获取订单消息请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']);
  473. return self::error('获取订单消息请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']);
  474. }
  475. $body = $resArr['body'] ?? '';
  476. if (is_string($body)) {
  477. $body = json_decode($body, true);
  478. }
  479. $delMsgIds = [];
  480. foreach ($body as $val) {
  481. // 根据返回信息, 更新订单状态
  482. $orderNo = $val['order'] ?? '';
  483. // 订单变更类型:1:激活,2:退单(激活前),3:转套餐(要根据产品id判断是否为享有特权的套餐),4:销户(激活后),6:首充数据同步,C1:开户完成,E0:发货,SX:未支付超期,AX:未支付用户取消,SX和AX状态目前仅支持3.27接口同步订单
  484. $state = $val['state'] ?? '';
  485. $id = $val['id'] ?? '';
  486. $order = MobileOrder::where('order_no', $orderNo)->find();
  487. if (empty($order)) {
  488. $delMsgIds[] = $id;
  489. continue;
  490. }
  491. if ($state == 1) {
  492. // 激活
  493. $order->produce_activation = 1;
  494. $order->save();
  495. } else if ($state == 6) {
  496. // 首充
  497. $amount = $val['lgtsId'] ?? 0;
  498. $order->produce_is_recharge = 1;
  499. $order->first_amount = $amount;
  500. $order->save();
  501. } else if ($state == 'C1') {
  502. // 开户
  503. $order->produce_status = 1;
  504. $order->save();
  505. } else if ($state == 'E0') {
  506. // 发货
  507. $order->logistics_numbers = $val['trackingNumber'] ?? '';
  508. $order->save();
  509. }
  510. $delMsgIds[] = $id;
  511. }
  512. $delMsgIds = implode(',', $delMsgIds);
  513. self::delOrderMsg($delMsgIds, $type);
  514. }
  515. /**
  516. * 删除订单消息
  517. *
  518. * @return void
  519. */
  520. public static function delOrderMsg($msgId = '', $type = 4)
  521. {
  522. $url = '/link/king/card/msg/del/v1';
  523. $data = [
  524. 'msgId' => $msgId,
  525. 'type' => $type,
  526. ];
  527. $result = self::send($url, $data);
  528. if (empty($result)) {
  529. common_log('删除订单消息请求失败');
  530. return self::error('删除订单消息请求失败');
  531. }
  532. $resArr = json_decode($result, true);
  533. if ($resArr['rspCode'] != '0000') {
  534. common_log('删除订单消息请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']);
  535. return self::error('删除订单消息请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']);
  536. }
  537. $body = $resArr['body'] ?? '';
  538. if (is_string($body)) {
  539. $body = json_decode($body, true);
  540. }
  541. $delSuccessNum = $body['delSuccessNum'] ?? 0;
  542. common_log('删除成功数量: ' . $delSuccessNum . ', 删除成功ID: ' . $msgId);
  543. return self::success('删除成功数量: ' . $delSuccessNum);
  544. }
  545. /**
  546. * 自动选号-订单同步接口
  547. *
  548. * @param array $params [orderId: 订单ID, goodsId: 商品ID, certName: 证件姓名, certNo: 证件编码, contactNum: 联系电话, postProvinceCode: 省份编码, postCityCode: 地市编码, postDistrictCode: 区县编码, postAddr: 详细地址, channel: 渠道, createTime: 创建时间, updateTime: 更新时间, orderTotalFee: 订单总金额, pageUrl: 页面地址, resourceId: 资源ID]
  549. * @return array
  550. */
  551. public static function autoNumSync($params = [], $order = null)
  552. {
  553. $url = '/link/king/card/preOrder/autoNumSync';
  554. $data = array_only($params, [
  555. 'orderId',
  556. 'goodsId',
  557. 'certName',
  558. 'certNo',
  559. 'contactNum',
  560. 'postProvinceCode',
  561. 'postCityCode',
  562. 'postDistrictCode',
  563. 'postAddr',
  564. 'channel',
  565. 'createTime',
  566. 'updateTime',
  567. 'orderTotalFee',
  568. 'pageUrl',
  569. 'resourceId',
  570. ]);
  571. $data['launchPlatform'] = self::getRandLaunchPlatform();
  572. // 额外加
  573. $data['contractId'] = '91200266';
  574. $result = self::send($url, $data, $order);
  575. if (empty($result)) {
  576. return self::error('自动选号请求失败');
  577. }
  578. $resArr = json_decode($result, true);
  579. if ($resArr['rspCode'] != '0000') {
  580. return self::error('自动选号请求失败, 错误编码为: ' . $resArr['rspCode'] . '错误信息为: ' . $resArr['rspDesc']);
  581. }
  582. $body = $resArr['body'] ?? '';
  583. if (is_string($body)) {
  584. $body = json_decode($body, true);
  585. }
  586. return self::success($body);
  587. }
  588. }