JiangsuliantongkuandaiApi.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <?php
  2. /**
  3. * Created By : z
  4. * Date : 2022/03/25
  5. * Time : 14:31
  6. * Wechat : 3659953
  7. */
  8. namespace App\Http\CompanyApi;
  9. use Modules\Order\Models\Orders as Order;
  10. use Modules\Package\Models\Package;
  11. use App\Models\Apinotify;
  12. use Illuminate\Support\Facades\Log;
  13. use App\Http\CompanyApi\AddressHelper\GetAddressInfo;
  14. use App\Http\CompanyApi\AddressHelper\Add2Code;
  15. class JiangsuliantongkuandaiApi
  16. {
  17. public function __construct()
  18. {
  19. $this->zop_key = [
  20. 'appCode' => '',
  21. 'HMACMD5' => '',
  22. 'AES' => ''
  23. ];
  24. $this->referrerCode = [
  25. '南京' => '3415708788',
  26. '扬州' => '3415711261',
  27. '连云' => '3415708759',
  28. '盐城' => '3415708910',
  29. '淮安' => '3415711466',
  30. '镇江' => '3415711154',
  31. '宿迁' => '3415711567',
  32. '无锡' => '3415708835',
  33. '泰州' => '3415711151',
  34. '南通' => '3415708883',
  35. '苏州' => '3415710870',
  36. '徐州' => '3415710721',
  37. '常州' => '3415710817',
  38. ];
  39. $this->channel = [
  40. '江苏' => '09-2279-bn66-9999',
  41. ];
  42. $this->channelCode = "09-2279-bn66-9999";
  43. }
  44. public function valid($order, $packageBind = NULL)
  45. {
  46. return array('code' => 1, 'msg' => '校验通过');
  47. }
  48. /**
  49. * @inheritDoc 同步
  50. */
  51. public function send($order, $packageBind = NULL)
  52. {
  53. // TODO: Implement send() method.
  54. if (empty($order->iden_card_name) || empty($order->iden_card_id)) return array('code' => 0, 'msg' => '身份证姓名或号码为空');
  55. // $salesId = $this->getSalesId($order, $packageBind);
  56. // if (empty($salesId)) {
  57. // Log::error('订单校验 : 未绑定套餐 .' . $order->order_no);
  58. // return array('code' => 0, 'msg' => '未绑定套餐,销售品编码为空sxlt2');
  59. // }
  60. $salesId = $order->out_package_id;
  61. //地址及编码解析
  62. //地址参数:province,city,area,details_address,format_address
  63. //编码参数:p_code,c_code,a_code
  64. $addinfo = new GetAddressInfo();
  65. $add2code = new Add2Code();
  66. $add_data = $addinfo->getAddInfo($order->express_address);
  67. $province = $add_data['province'];
  68. $city = $add_data['city'];
  69. $area = $add_data['area'];
  70. $format_address = $add_data['format_address'];
  71. if($province != '江苏省') return ['code' => 0, 'msg' => '省外不发'];
  72. $code_data = $add2code->get_codeInfo($add_data, 'Shanxikuandai_code');
  73. $p_code = $code_data['p_code'];
  74. $c_code = $code_data['c_code'];
  75. $a_code = $code_data['a_code'];
  76. if(empty($p_code) or empty($c_code) or empty($a_code)) return array('code' => 0, 'msg' => "地区编码解析失败,$province--$city--$area".'--'.$p_code.'--'.$c_code.'--'.$a_code);
  77. //产品拼接过 这里要进行处理
  78. $salesIdArr = explode('&',$salesId);
  79. if(empty($salesIdArr[0]) || empty($salesIdArr[1]) || empty($salesIdArr[2])) return ['code' => 0, 'msg' => '产品配置错误'];
  80. $prodGuishu = $salesIdArr[0];//发省内还是发全国
  81. $prodId = $salesIdArr[1];//产品编码
  82. $prodName = $salesIdArr[2];//产品名称
  83. $price = $salesIdArr[3] - 100;//价格
  84. // $channel = $salesIdArr[3];//触点编码
  85. // $developerId = $salesIdArr[4];//发展人编码
  86. // L计划&000000000000&480元包年300m宽带(省集中处理)&08-2278-3840-bkvc&1714833893
  87. // $channel = $this->channel[substr($city,0,6)];
  88. $channel = $this->channelCode;
  89. $developerId = $this->referrerCode[substr($city,0,6)];
  90. // NEWACCESSWAY
  91. $url = 'http://e.com/zop/link/boradband/actPreOrder/submit/v1';
  92. $body = [
  93. "channel" => $channel, //触点编码
  94. "sceneTag" => "NEWACCESSWAY",//活动场景
  95. "tId" => trim($order->order_no),//合作方订单号
  96. // "activeId" => "xxxx",//活动ID【重要】需确保活动id的有效性,如果由于活动id传值不正确导致发券问题需责任自负。
  97. // "activeName" =>"xxxx",//活动名称
  98. "goodsId" => $prodId,//商品ID
  99. "goodsName" => $prodName,//商品名称(奖品名称) 命名规范:装宽带意向单-XXXX
  100. "processDiff" => "JSHKZJ",
  101. "developerId" => $developerId,
  102. "referrerCode" => $developerId,
  103. "remarks" => "套餐费".$price."元,调测费0元,未支付JSHKZJ".trim($order->order_no),//备注
  104. // "appointmentTime" => "2022-04-27 10:04:24",//预约安装时间 YYYY-MM-DD HH24:mi:ss
  105. "custInstallData" => [//用户信息
  106. "psptTypeCode" => "02",//证件类型 02 身份证
  107. "psptNo" => trim($order->iden_card_id),//证件号码
  108. "custName" => trim($order->iden_card_name),//客户名称
  109. "contactPhone" => trim($order->mobile),//联系人电话/客户号码
  110. "provinceCode" => $p_code,//省份编码
  111. "cityCode" => $c_code,//地市编码
  112. "districtCode" => $a_code,//区县编码
  113. "custInstallAddr" => $format_address//详细地址
  114. ]
  115. ];
  116. $reqParam = $this->jiami($body);
  117. echo json_encode($body,JSON_UNESCAPED_UNICODE);
  118. echo "\n";
  119. // return;
  120. Log::info($order->order_no."江苏联通宽带提交:".json_encode($body,JSON_UNESCAPED_UNICODE));
  121. $res = $this->req($url, $reqParam);
  122. // echo 222;
  123. echo json_encode($res,JSON_UNESCAPED_UNICODE);
  124. echo "\n";
  125. // return;
  126. Log::info($order->order_no."江苏联通宽带返回:".json_encode($res,JSON_UNESCAPED_UNICODE));
  127. // if(empty($res)) return array('code' => 2, 'msg' => '服务器未响应,请提交');
  128. // //成功
  129. if($res['rspCode'] == '0000'){
  130. if(empty($res['body'])) return array('code' => 0, 'msg' => '失败,body不存在');
  131. // $body = json_decode($res['body'],true);
  132. return array('code' => 1, 'msg' => '成功','api_order_no' => $res['body']['orderNo'],'intercept_msg' => $res['body']['orderId']);
  133. //失败
  134. }else{
  135. return array('code' => 0, 'msg' => $res['rspDesc']);
  136. }
  137. }
  138. //查询宽带是否竣工
  139. public function orderCheck($order, $packageBind = NULL)
  140. {
  141. $apinotify = Apinotify::where('api_order_no', $order->api_order_no)->select(['up_status','active_message','api_order_no','order_id','order_no'])->first();
  142. // echo json_encode($apinotify,JSON_UNESCAPED_UNICODE);
  143. // echo "\n";
  144. if(!empty($apinotify)){
  145. if($apinotify->up_status == 2){
  146. $this->delMsg($apinotify->order_no); //删除消息
  147. return [
  148. 'code' => 1,
  149. 'active_message' => $apinotify->active_message,
  150. 'active_status' => 1,
  151. 'phonenum' => $apinotify->order_id
  152. ];
  153. }
  154. }
  155. return;
  156. }
  157. //获取消息
  158. public function getMsg()
  159. {
  160. // $apinotify = Apinotify::where('api_id', 123)->select(['up_status','active_message','api_order_no'])->get();
  161. // echo json_encode($apinotify,JSON_UNESCAPED_UNICODE);
  162. // echo "\n";
  163. // echo count($apinotify);
  164. // return;
  165. // $salesId = $this->getSalesId($order, $packageBind);
  166. // if (empty($salesId)) {
  167. // Log::error('订单校验 : 未绑定套餐 .' . $order->order_no);
  168. // return array('code' => 0, 'msg' => '未绑定套餐,销售品编码为空sxlt2');
  169. // }
  170. // //产品拼接过 这里要进行处理
  171. // $salesIdArr = explode('&',$salesId);
  172. // if(empty($salesIdArr[0]) || empty($salesIdArr[1]) || empty($salesIdArr[2])) return ['code' => 0, 'msg' => '产品配置错误'];
  173. // $prodGuishu = $salesIdArr[0];//发省内还是发全国
  174. // $prodId = $salesIdArr[1];//产品编码
  175. // $prodName = $salesIdArr[2];//产品名称
  176. // $channel = $salesIdArr[3];//触点编码
  177. $channel = $this->channelCode;
  178. $url = 'http://e.com/zop/link/king/broad/getMsg';
  179. $body = [
  180. // "mesChannel" => "sdhk",
  181. // "gridCode" => "01",
  182. "touchApplyId" => $channel,
  183. "type" => "4"
  184. ];
  185. $reqParam = $this->jiami($body);
  186. // echo json_encode($body,JSON_UNESCAPED_UNICODE);
  187. // echo "\n";
  188. // return;
  189. // $datw = "20240306 17:18:21";
  190. // echo date("Y-m-d H:i:s",strtotime($datw));
  191. Log::info("江苏联通宽带竣工消息提交:".json_encode($body,JSON_UNESCAPED_UNICODE));
  192. $res = $this->req($url, $reqParam);
  193. if(empty($res)) return ["code" => 2, "msg" => "服务器未响应"];
  194. // echo json_encode($res,JSON_UNESCAPED_UNICODE);
  195. Log::info("江苏联通宽带竣工消息返回:".json_encode($res,JSON_UNESCAPED_UNICODE));
  196. if($res['rspCode'] == '0000'){
  197. if(!empty($res['body'])){
  198. foreach ($res['body'] as $value){
  199. $resNotify = $this->notify($value);
  200. // if($resNotify['code'] == 1 && !empty($resNotify['msgId'])){
  201. // $this->delMsg($resNotify['msgId']);
  202. // }
  203. }
  204. }
  205. return;
  206. }else{
  207. return array('code' => 0, 'msg' => $res['rspDesc']);
  208. }
  209. }
  210. //获取消息
  211. public function delMsg($msgId)
  212. {
  213. $url = 'http://e.com/zop/link/king/broad/delMsg';
  214. $body = [
  215. // "mesChannel" => "sdhk",
  216. // "gridCode" => "01",
  217. "msgId" => $msgId,
  218. "type" => "4"
  219. ];
  220. $reqParam = $this->jiami($body);
  221. $res = $this->req($url, $reqParam);
  222. // echo json_encode($res,JSON_UNESCAPED_UNICODE);
  223. if($res['rspCode'] == '0000'){
  224. return array('code' => 1, 'msg' => '删除成功');
  225. }else{
  226. return array('code' => 0, 'msg' => $res['rspDesc']);
  227. }
  228. }
  229. public function notify($data)
  230. {
  231. // 节点 父节点 类型 长度 约束 注释
  232. // Id body string V20 1 消息ID
  233. // orderId body string V20 1 商城宽带订单号
  234. // createTime body string V20 1 宽带竣工时间:yyyyMMdd HH24:mi:ss
  235. // touchApplyId body string V20 ? 下单触点编码(也就是下单链接里带的channel参数,如08-1234-5678-9999)
  236. // contactNumber body string V20 ? 联系电话
  237. // broadBandNumber body string V20 ? 宽带号码
  238. // $data = [
  239. // "orderId" =>"556767610882",
  240. // "createTime" =>"20240306 12:23:33",
  241. // "touchApplyId" =>"07-4355-bdl8-9999",
  242. // "contactNumber" =>"13468929987",
  243. // "broadBandNumber" =>"02904681142",
  244. // "channel" =>null,
  245. // "value1" =>null,
  246. // "id" =>"7824030776196202"
  247. // ];
  248. $status = 3;
  249. $open_message = null;
  250. $active_status = null;
  251. $activityTime = null;
  252. $time = date("Y-m-d H:i:s",time());
  253. $order_id = empty($data['broadBandNumber']) ? '' : $data['broadBandNumber']; //宽带单号
  254. $api_order_no = empty($data['orderId']) ? '' : $data['orderId']; //运营商单号
  255. $order_no = empty($data['id']) ? '' : $data['id']; //消息id
  256. $touchApplyId = empty($data['touchApplyId']) ? '' : $data['touchApplyId']; //下单触点编码
  257. //竣工时间
  258. if(!empty($data['createTime'])){
  259. $activityTime = date("Y-m-d H:i:s",strtotime($data['createTime']));
  260. $status = 2;
  261. $open_message = '已竣工'; //开卡回文
  262. $active_status = 1;
  263. }
  264. $apinotify = Apinotify::where('api_order_no', $api_order_no)->first();
  265. // return $apinotify;
  266. if(empty($apinotify)){
  267. $data = [
  268. 'name' => "江苏联通宽带",
  269. 'api_order_no' => $api_order_no,
  270. 'order_no' => $order_no,
  271. 'order_id' => $order_id,
  272. // 'phone' => $mobile,
  273. // 'express_no' => $logisticsNo,
  274. 'express_name' => $touchApplyId,
  275. // 'open_status' => "",
  276. // 'status_time' => $activityTime,
  277. 'created_at' => $time,
  278. 'updated_at' => $time,
  279. 'api_id' => "158",
  280. // 'first_charge_status' => "",
  281. // 'first_charge_time' => "",
  282. 'up_status' => $status,
  283. 'active_message' => $activityTime,
  284. 'active_status' => $active_status, //已激活
  285. 'open_message' => $open_message,
  286. ];
  287. $res = Apinotify::insert($data);
  288. if ($res == 1) {
  289. return array('code' => 1, 'success' => 'true', 'msgId' => $order_no);
  290. } else {
  291. return array('code' => 0, 'success' => 'false');
  292. }
  293. }else{
  294. $data = [
  295. 'api_order_no' => $api_order_no,
  296. 'order_no' => $order_no,
  297. 'order_id' => $order_id,
  298. 'updated_at' => $time,
  299. // 'phone' => $mobile,
  300. 'up_status' => $status,
  301. 'active_message' => $activityTime,
  302. 'active_status' => $active_status, //已激活
  303. 'open_message' => $open_message,
  304. ];
  305. $apinotify->update($data);
  306. return array('code' => 1, 'success' => 'true', 'msgId' => $order_no);
  307. }
  308. return array('code' => 0, 'success' => 'false');
  309. }
  310. /**
  311. * @inheritDoc 参数加密
  312. */
  313. public function jiami($body)
  314. {
  315. // TODO: Implement send() method.
  316. // uuid
  317. $uuid = $this->uuid();
  318. // 生成时间戳
  319. $timestamp = $this->getTimestamp();
  320. $head = [
  321. 'sign' => $this->genSign($uuid, $timestamp),
  322. 'timestamp' => $timestamp,
  323. 'uuid' => $uuid
  324. ];
  325. $reqObj = [
  326. 'head' => $head,
  327. 'body' => $body
  328. ];
  329. $reqObj = $this->encrypt(json_encode($reqObj),$this->zop_key['AES']);
  330. $reqParam = [
  331. 'appCode' => $this->zop_key['appCode'],
  332. 'reqObj' => $reqObj
  333. ];
  334. return $reqParam;
  335. }
  336. //图片转base64位
  337. function base64EncodeImage($ImageFile) {
  338. $base64_image = '';
  339. $image_data = @file_get_contents($ImageFile);
  340. if(!$image_data){
  341. return false;
  342. }
  343. $base64_image = chunk_split(base64_encode($image_data));
  344. return $base64_image;
  345. }
  346. function req($url, $post_data)
  347. {
  348. $opt_data = json_encode($post_data);
  349. $header = array();
  350. $header[] = 'Accept:application/json';
  351. $header[] = 'Content-Type:application/json;charset=utf-8';
  352. $curl = curl_init(); //初始化
  353. curl_setopt($curl, CURLOPT_URL, $url); //设置url
  354. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  355. curl_setopt($curl, CURLOPT_POSTFIELDS, $opt_data);
  356. curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
  357. curl_setopt($curl, CURLOPT_POST,1);
  358. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 不验证SSL
  359. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 不验证SSL
  360. $result = curl_exec($curl);
  361. $headerSize = curl_getinfo($curl);
  362. // return $headerSize;
  363. if($result === false){
  364. echo curl_errno($curl);
  365. exit();
  366. }
  367. curl_close($curl);
  368. // return $result;
  369. return json_decode($result,true);
  370. }
  371. /**
  372. * uuid 唯一序列码
  373. * @param string $prefix
  374. * @return string
  375. */
  376. function uuid($prefix = '')
  377. {
  378. $chars = md5(uniqid(mt_rand(), true));
  379. $uuid = substr($chars, 0, 8) . '-';
  380. $uuid .= substr($chars, 8, 4) . '-';
  381. $uuid .= substr($chars, 12, 4) . '-';
  382. $uuid .= substr($chars, 16, 4) . '-';
  383. $uuid .= substr($chars, 20, 12);
  384. return $prefix . $uuid;
  385. }
  386. //获取当前时间毫秒
  387. function msectime()
  388. {
  389. list($msec, $sec) = explode(' ', microtime());
  390. $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  391. return $msectime;
  392. }
  393. // 时间戳
  394. function getTimestamp()
  395. {
  396. date_default_timezone_set('PRC');
  397. $time = $this->msectime();
  398. return date('Y-m-d H:i:s.') . substr($time, -3);
  399. }
  400. // 生成签名
  401. function genSign($uuid, $timestamp)
  402. {
  403. $dataStr = 'appCode' .$this->zop_key['appCode'] . 'timestamp' . $timestamp . 'uuid' . $uuid . $this->zop_key['HMACMD5'];
  404. return $this->sign($this->zop_key['HMACMD5'], $dataStr);
  405. }
  406. /**
  407. * 签名
  408. * @param key 密钥 联通侧提供的HmacMD5
  409. * @param dataStr appCode+appCode值+timestamp+timestamp值*+uuid+uuid值+HmacMD5密钥值
  410. */
  411. function sign($key, $dataStr)
  412. {
  413. return base64_encode(hash_hmac("md5", $dataStr, base64_decode($key), true));
  414. }
  415. /**
  416. *AES加密
  417. */
  418. function encrypt($data, $key)
  419. {
  420. $data = openssl_encrypt($data, 'aes-128-ecb', base64_decode($key), OPENSSL_RAW_DATA);
  421. return base64_encode($data);
  422. }
  423. //计算年龄
  424. function getAge($id){
  425. # 1.从身份证中获取出生日期
  426. $id = $id;//身份证
  427. $birth_Date = strtotime(substr($id, 6, 8));//截取日期并转为时间戳
  428. # 2.格式化[出生日期]
  429. $Year = date('Y', $birth_Date);//yyyy
  430. $Month = date('m', $birth_Date);//mm
  431. $Day = date('d', $birth_Date);//dd
  432. # 3.格式化[当前日期]
  433. $current_Y = date('Y');//yyyy
  434. $current_M = date('m');//mm
  435. $current_D = date('d');//dd
  436. # 4.计算年龄()
  437. $age = $current_Y - $Year;//今年减去生日年
  438. // if($Month > $current_M || $Month == $current_M && $Day > $current_D){//深层判断(日)
  439. // $age--;//如果出生月大于当前月或出生月等于当前月但出生日大于当前日则减一岁
  440. // }
  441. # 返回
  442. return $age;
  443. }
  444. //订单编码
  445. public function getSalesId($order, $packageBind): string
  446. {
  447. $salesId = "0";
  448. foreach ($packageBind as $bind) {
  449. if ($bind['id'] == $order->package_id) {
  450. $salesId = $bind['title'];
  451. break;
  452. }
  453. }
  454. //判断是否是外部套餐
  455. // if (empty($salesId)) {
  456. // $package = Package::find($order->package_id);
  457. // if (!empty($package)) {
  458. // if (!empty($package->package_id)) {
  459. // foreach ($packageBind as $bind) {
  460. // if ($bind['id'] == $package->package_id) {
  461. // $salesId = $bind['title'];
  462. // break;
  463. // }
  464. // }
  465. // }
  466. // }
  467. // }
  468. return $salesId;
  469. }
  470. }