ZopOrderService.php 17 KB

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