WaybillController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. <?php
  2. namespace app\backend\modules\order\controllers;
  3. use app\common\components\BaseController;
  4. use app\common\helpers\PaginationHelper;
  5. use app\common\helpers\Url;
  6. use Ixudra\Curl\Facades\Curl;
  7. use app\common\models\Order;
  8. use app\common\models\Goods;
  9. use app\frontend\modules\order\services\OrderService;
  10. use Illuminate\Support\Facades\Input;
  11. use Yunshop\Exhelper\common\models\ElectronicTemplate;
  12. use Yunshop\Exhelper\common\models\ExhelperPanel;
  13. use Yunshop\Exhelper\common\models\ExhelperSys;
  14. use Yunshop\Exhelper\common\models\SendUser;
  15. use app\common\models\member\Address;
  16. // use Ixudra\Curl\Facades\Curl;
  17. class WaybillController extends BaseController
  18. {
  19. protected $apikey;
  20. protected $merchant_id;
  21. protected $name;
  22. protected $print_url = 'http://www.kdniao.com/External/PrintOrder.aspx';
  23. // 测试地址 'http://testapi.kdniao.com:8081/api/Eorderservice';
  24. protected $test_panel_url = 'http://testapi.kdniao.com:8081/api/Eorderservice';
  25. // 正式地址 'http://api.kdniao.com/api/Eorderservice';
  26. protected $panel_url = 'http://api.kdniao.com/api/eorderservice';
  27. protected $zip_code;
  28. public $company = [
  29. 'SF' => '顺丰速运',
  30. 'EMS' => 'EMS',
  31. 'ZJS' => '宅急送',
  32. 'YTO' => '圆通速递',
  33. 'HTKY' => '百世快递',
  34. 'ZTO' => '中通快递',
  35. 'YD' => '韵达速递',
  36. 'STO' => '申通快递',
  37. 'DBL' => '德邦快递',
  38. 'UC' => '优速快递',
  39. 'JD' => '京东快递',
  40. 'XFEX' => '信丰物流',
  41. 'ANE' => '安能物流',
  42. 'GTO' => '国通快递',
  43. 'HHTT' => '天天快递',
  44. 'KYSY' => '跨越速运',
  45. 'YZPY' => '邮政快递包裹',
  46. 'ZTKY' => '中铁快运',
  47. 'YZBK' => '邮政国内标快',
  48. 'YCWL' => '远成快运',
  49. 'UAPEX' => '全一快递',
  50. 'SURE' => '速尔快递',
  51. 'PJ' => '品骏快递',
  52. 'DBLKY' => '德邦快运',
  53. 'ANEKY' => '安能快运',
  54. 'JDKY' => '京东快运',
  55. 'LB' => '龙邦快递',
  56. 'CND' => '承诺达',
  57. 'HTKYKY' => '百世快运',
  58. 'ZTOKY' => '中通快运',
  59. 'SX' => '顺心捷达',
  60. 'JTSD' => '极兔速递'
  61. ];
  62. public $province = [
  63. '北京' => 11,
  64. '天津' => 12,
  65. '河北省' => 13,
  66. '山西省' => 14,
  67. '内蒙古自治区' => 15,
  68. '辽宁省' => 21,
  69. '吉林省' => 22,
  70. '黑龙江省' => 23,
  71. '上海' => 31,
  72. '江苏省' => 32,
  73. '浙江省' => 33,
  74. '安徽省' => 34,
  75. '福建省' => 35,
  76. '江西省' => 36,
  77. '山东省' => 37,
  78. '河南省' => 41,
  79. '湖北省' => 42,
  80. '湖南省' => 43,
  81. '广东省' => 44,
  82. '广西壮族自治区' => 45,
  83. '海南省' => 46,
  84. '重庆' => 50,
  85. '四川省' => 51,
  86. '贵州省' => 52,
  87. '云南省' => 53,
  88. '西藏自治区' => 54,
  89. '陕西省' => 61,
  90. '甘肃省' => 62,
  91. '青海省' => 63,
  92. '宁夏回族自治区' => 64,
  93. '新疆维吾尔自治区' => 65,
  94. '台湾' => 71,
  95. '香港特别行政区' => 81,
  96. '澳门特别行政区' => 82,
  97. ];
  98. public function __construct()
  99. {
  100. if (!app('plugins')->isEnabled('exhelper')){
  101. return json_encode(array('result'=>'error','resp'=>'插件没有开启'));
  102. }
  103. $set = ExhelperSys::uniacid()->first();
  104. if ($set) {
  105. $this->name = trim($set['name']);
  106. $this->apikey = trim($set['apikey']);
  107. $this->merchant_id = trim($set['merchant_id']);
  108. if (empty($set['apikey']) || empty($set['merchant_id'])){
  109. return $this->errorJson('err','没有设置');
  110. }
  111. }
  112. // else {
  113. // return $this->message('请配置面单', Url::absoluteWeb('plugin.exhelper.admin.panel.index'));
  114. // }
  115. }
  116. public function waybill()
  117. {
  118. $ordersn = request()->id;
  119. // $this->zip_code = request()->zip_code;
  120. if (!app('plugins')->isEnabled('exhelper')){
  121. return json_encode(array('result'=>'error','resp'=>'插件没有开启'));
  122. }
  123. $order_model = Order::where('id', $ordersn)->first();
  124. if (!$ordersn || !$order_model) {
  125. return json_encode(array('result'=>'error','resp'=>'请传参数订单编号ordersn'));
  126. }
  127. if (count($ordersn) > 1) {
  128. return json_encode(array('result'=>'error', 'resp'=>'订单超过规定数量,请选择1条订单'));
  129. }
  130. $template_model = ElectronicTemplate::uniacid()->where('order_sn',$order_model['order_sn'])->first();
  131. if ($template_model){
  132. $expressCompanies = \app\common\repositories\ExpressCompany::create()->all();
  133. $company = '';
  134. array_filter( $expressCompanies, function($var) use($template_model,&$company){
  135. if(in_array($template_model['shipper_code'],$var)){
  136. $company = $var;
  137. return $var;
  138. }
  139. });
  140. return json_encode(array('result'=>'success', 'resp'=>['logistic_code'=>$template_model['logistic_code'],
  141. 'shipper_code'=>$template_model['shipper_code'],'company'=> $company]));
  142. }
  143. if (!is_array($ordersn) || count($ordersn) == 1) {
  144. // $order_sn = explode(',', $ordersn);
  145. $order = Order::where('id', $ordersn)
  146. ->uniacid()
  147. ->with('hasManyOrderGoods')
  148. ->with('hasManyOrderGoods.goods')
  149. ->with('hasOneDispatchType')
  150. ->with('belongsToMember')
  151. ->with('address')
  152. ->first()
  153. // ->find($id)
  154. ->toArray();
  155. // dd($order['has_many_order_goods']);
  156. return $this->beginHandle($order);
  157. }
  158. }
  159. /**
  160. * 快递鸟建议用户使用时,详细查看以下注意事项:
  161. * 1、顺丰速运、中铁快运、宅急送、全一快递可以不申请电子面单客户号,直接下单(如果用户已和网点申请到电子面单客户号,严格按照表格对应关系填写);
  162. * 3、顺丰速运,运费现付、到付发货,无需客户号,运费如需月结,仅需要联系网点申请月结号(10位数的纯数字格式);
  163. * 4、通过快递鸟用户管理后台申请的优速电子面单客户号,只需要客户编号即可正常使用电子面单接口,通过当地快递网点线下申请的需要使用客户编号和密钥才可下单;
  164. * 5、通过快递鸟用户管理后台申请的中通电子面单客户号,只需要商家ID即可正常使用电子面单接口,通过当地快递网点线下申请的需要使用商家ID和商家接口密码才可下单;
  165. * 6、宅急送可直接请求电子面单接口下单使用,不用申请电子面单客户号,如果用户是自己联系当地网点申请电子面单客户号,那么LogisticCode(快递单号)字段为必填(网点会提供);
  166. * 7、速尔快递,必须传值SendStaff收件快递员,否则直接影响网点收件;
  167. * 8、安能快递,仅支持通过快递鸟后台申请的客户号发货,客户号格式如1111111_ANE666,网点名称格式如6666666.
  168. * 京东快运下单,需联系京东站点维护事业部编码(EBU开头)、仓库编码以及寄件人简称。
  169. * 宅急送、速尔快递、远成快运、品骏快递、快运公司无测试环境
  170. *
  171. */
  172. public function beginHandle($order)
  173. {
  174. // 正式地址: http://api.kdniao.com/api/eorderservice(不加密)https://api.kdniao.com/api/eorderservice(加密)
  175. $add = explode(' ', $order['address']['address']);
  176. \Log::info('-=-=add=-=-', $add);
  177. $send = SendUser::uniacid()->where('isdefault', 1)->first();
  178. \Log::info('-=-=send=-=-', $send);
  179. if (empty($send)) {
  180. //查找寄件人信息表
  181. die(json_encode(array('result' => 'error', 'resp' => '寄件人信息为空,请输入后在选择。')));
  182. // return $this->message('寄件人信息为空,请输入后在选择', Url::absoluteWeb('plugin.exhelper.admin.panel.index'));
  183. }
  184. $send = $send->toArray();
  185. $panel = ExhelperPanel::uniacid()->where('isdefault', 1)->first();
  186. // dd($panel->exhelper_style);
  187. \Log::info('panel--uniacid', [$panel, \Yunshop::app()->uniacid]);
  188. if (empty($panel)) {
  189. die(json_encode(array('result' => 'error', 'resp' => '无默认模板信息')));
  190. // return $this->message('无默认模板信息', Url::absoluteWeb('plugin.exhelper.admin.panel.index'));
  191. }
  192. \Log::info('begin---panel');
  193. $sender = array(
  194. 'Name' => $send['sender_name'],
  195. 'Mobile' => $send['sender_tel'],
  196. 'ProvinceName' => $send['sender_province'],
  197. 'CityName' => $send['sender_city'],
  198. 'ExpAreaName' => $send['sender_area'],
  199. 'Address' => $send['sender_street'].$send['sender_address']
  200. );
  201. $receiver = array(
  202. 'Name' => $order['address']['realname'],
  203. 'Mobile' => $order['address']['mobile'],
  204. 'ProvinceName' => $add[0],
  205. 'CityName' => $add[1],
  206. 'ExpAreaName' => $add[2],
  207. 'Address' => $add[3].$add[4]
  208. );
  209. if ($receiver['Address'] == ''){
  210. $receiver['Address'] = $add[3].$add[4].$add[5];
  211. }
  212. if (in_array($panel['exhelper_style'], ['EMS','YZPY','YZBK'])) {
  213. //为邮政时需要获取邮政编码
  214. if ($this->getCode($send['sender_province'],$send['sender_city'], $send['sender_area']) != 1) {
  215. $sender['PostCode'] = $sender['sender_code'] ? : $this->getCode($send['sender_province'],$send['sender_city'], $send['sender_area']);
  216. }
  217. // if (empty($this->zip_code)){
  218. // die(json_encode(array('result' => 'error', 'resp' => '邮政编码不能为空')));
  219. // }else{
  220. // $receiver['PostCode'] = $this->zip_code;
  221. // }
  222. if ($this->getCode($add[0],$add[1], $add[2]) != 1) {
  223. $receiver['PostCode'] = $this->getCode($add[0],$add[1], $add[2]);
  224. }
  225. }
  226. // dd($sender, $receiver);
  227. $commodity = [];
  228. $weight = '';
  229. $goos_total = '';
  230. foreach ($order['has_many_order_goods'] as $k => $v) {
  231. $commodityOne = [
  232. //商品名称
  233. 'GoodsName' => $v['title'],
  234. //商品件数
  235. 'Goodsquantity' => $v['total'],
  236. //商品价格
  237. 'GoodsPrice' => $v['goods_price'],
  238. //商品编码
  239. 'GoodsCode' => $v['goods_sn'],
  240. //商品描述
  241. // 'GoodsDesc' => $v['goods_option_title'],
  242. //商品重量
  243. 'GoodsWeight' => $v['goods']['weight'] ? $v['goods']['weight'] * $v['total'] : '',
  244. //商品体积
  245. 'GoodsVol' => ''
  246. ];
  247. $weight += $v['goods']['weight'] ? $v['goods']['weight'] * $v['total'] : 0;
  248. $goos_total .= $v['title'].' '.$v['goods_option']['title'].' x '.$v['total'].'</br>';
  249. $commodity[] = $commodityOne;
  250. }
  251. // dd($commodity,$weight);
  252. $PayType = $panel['panel_name'] == '德邦快递' ? 3 : 1;
  253. $data = [
  254. 'Sender' => $sender,
  255. 'Receiver' => $receiver,
  256. //商品信息
  257. 'Commodity' => $commodity,
  258. // 包裹总重量 (kg) double(10,3)
  259. 'Weight' => $weight ?: '',
  260. // 运输方式 1陆运 2空运 默认为1
  261. 'TransType' => 1,
  262. //快递公司编码
  263. 'ShipperCode' => $panel['exhelper_style'],
  264. //第三方订单号 物流为京东且 ExpType =1时必填
  265. 'ThrOrderCode' => $order['order_sn'],
  266. //订单编号
  267. 'OrderCode' => $order['order_sn'],//$order['order_sn'].rand(1111, 9999),
  268. //运费支付方式 1现付 2到付 3月结 4第三方付
  269. // 'PayType' => $order['pay_type_id'],
  270. 'PayType' => $PayType,
  271. // 快递公司业务类型
  272. 'ExpType' => 1,
  273. //是否要求签回单 0不要求 1要求
  274. 'IsReturnSignBill' => 0,
  275. //快递运费
  276. 'Cost' => '',
  277. //其他费用
  278. 'OtherCost' => '',
  279. //是否通知快递员上门揽件
  280. 'IsNotice' => $panel->isself,
  281. //包裹数
  282. 'Quantity' => count($order['has_many_order_goods']),
  283. //包裹为快运时必填,包裹总体积
  284. 'Volume' => '',
  285. // 'Remark' => $order['remark'],
  286. 'Remark' => $order['note'],
  287. //返回电子面单模板
  288. 'IsReturnPrintTemplate' => 1,
  289. //送货方式 0自提 1送货上门 2送货上楼
  290. 'DeliveryMethod' => 1,
  291. 'CustomArea' => $goos_total//商品数量
  292. ];
  293. /*
  294. * JD
  295. * 快递鸟用户,通常ExpType传值6.
  296. * 订单来源:京东商城 1
  297. * 订单来源:天猫 2
  298. * 订单来源:苏宁 3
  299. * 订单来源:亚马逊中国 4
  300. * 订单来源:ChinaSkin 5
  301. * 订单来源:其他销售平台 6
  302. */
  303. if ($panel['exhelper_style'] == 'JD'){
  304. $data['ExpType'] = 6;
  305. }
  306. // if ($panel['exhelper_style'] == 'SF'){
  307. // $data['TemplateSize'] = 15001;
  308. // }
  309. //取模板样式传到快递鸟
  310. $data['TemplateSize'] = strstr($panel['panel_style'],'mm') ? '' : $panel['panel_style'];
  311. // dd($data);
  312. // dd($panel->isself);
  313. if ($panel->isself == 1) {
  314. //上门揽件时间
  315. $data['StartDate'] = $panel->begin_time;
  316. $data['EndDate'] = $panel->end_time;
  317. }
  318. $set = $this->panelSet();
  319. if($panel['exhelper_style'] == 'JTSD'){
  320. //极兔速递参数不同
  321. $data['DeliveryMethod'] = 3; //6-代收点自提, 5-快递柜自提,4-站点自提,3-派送上门
  322. $data['Weight'] = $data['Weight']/1000; //使用千克
  323. }
  324. if (in_array($panel['exhelper_style'], ['DBL','PJ','DBLKY'])) {
  325. $data['CustomerName'] = trim($panel['panel_sign']);
  326. } elseif (!in_array($panel['exhelper_style'], ['SF','ZTKY', 'UAPEX', 'HOAU']) || ($panel['exhelper_style'] == 'EMS' && $sender['ProvinceName'] != '广东省')) {
  327. $data['CustomerName'] = trim($panel['panel_no']);
  328. } else {
  329. $data['CustomerName'] = '';
  330. }
  331. $data['CustomerPwd'] = $set[$panel['exhelper_style']]['CustomerPwd'] ? trim($panel['panel_pass']) : '';
  332. $data['MonthCode'] = ($set[$panel['exhelper_style']]['MonthCode'] && $panel['exhelper_style'] == 'YTO') ? trim($panel['panel_pass']) : '';
  333. $data['SendSite'] = $set[$panel['exhelper_style']]['SendSite'] ? trim($panel['panel_code']) : '';
  334. //快递公司参数 SendStaff-速尔快递需要提供收件快递员信息,LogisticCode-宅急送需要快递单号信息-有待完善
  335. $data['SendStaff'] = (isset($set[$panel['exhelper_style']]['SendStaff']) && $panel['exhelper_style'] == 'SURE') ? '' : '';
  336. $data['LogisticCode'] = (isset($set[$panel['exhelper_style']]['LogisticCode']) && $panel['exhelper_style'] == 'ZJS') ? '' : '' ;
  337. //快运公司参数 京东快运需要仓库编码和寄件人简称
  338. $data['WareHouseID'] = (isset($set[$panel['exhelper_style']]['WareHouseID']) && $panel['exhelper_style'] == 'JDKY') ? trim($panel['panel_code']) : '' ;
  339. $data['Name'] = (isset($set[$panel['exhelper_style']]['Name']) && $panel['exhelper_style'] == 'JDKY')? trim($send['sender_name']) : '';
  340. // dd($data);
  341. // header('Content_Type', 'Content-Type:application/json');
  342. //return $this->getCompanyPanel($data, $this->test_panel_url, $order['id']); //测试地址
  343. return $this->getCompanyPanel($data, $this->panel_url, $order['id'],$order['order_sn']); //正式地址
  344. }
  345. //获取快递公司电子面单
  346. public function getCompanyPanel($data, $url, $order_id,$order_sn)
  347. {
  348. \Log::info('get---panel');
  349. //调用电子面单
  350. $jsonParam = json_encode($data, JSON_UNESCAPED_UNICODE);
  351. $jsonResult = $this->submitEOrder($jsonParam, $url);
  352. \Log::info('data2', $data);
  353. //解析电子面单返回结果
  354. $result = json_decode($jsonResult, true);
  355. \Log::info('result', $result);
  356. if($result['ResultCode'] == '100' && $result['Success'] == 'true') {
  357. $panelsetting = ExhelperPanel::uniacid()->where('isdefault',1)->first()->toArray();
  358. $opera = new OrderService;
  359. $param = array(
  360. 'order_id' => $order_id,
  361. 'express_code' => $result['Order']['ShipperCode'],
  362. 'express_company_name' => $this->company[$result['Order']['ShipperCode']],
  363. 'express_sn' => $result['Order']['LogisticCode']
  364. );
  365. // dd($result);
  366. // $opera->orderSend($param);
  367. if (!$this->saveTemplate($result,$order_sn,$data)){
  368. return json_encode(array('result'=>'error','resp'=>'电子面单储存失败'));
  369. }
  370. $expressCompanies = \app\common\repositories\ExpressCompany::create()->all();
  371. $company = '';
  372. $shipperCode = $result['Order']['ShipperCode'] ?: $data['ShipperCode'];
  373. array_filter( $expressCompanies, function($var) use($result,&$company,$shipperCode){
  374. if(in_array($shipperCode,$var)){
  375. $company = $var;
  376. return $var;
  377. }
  378. });
  379. return json_encode(array('result'=>'success', 'resp'=>['logistic_code'=>$result['Order']['LogisticCode'], 'shipper_code'=>$shipperCode ,
  380. 'company'=> $company]));
  381. } else {
  382. return json_encode(array('result'=>'error', 'resp'=>$result['Reason']));
  383. }
  384. }
  385. /*
  386. * 电子面单打印优化,储存电子面单模板
  387. */
  388. public function saveTemplate($result,$order_sn,$data)
  389. {
  390. if (!$order_sn){
  391. return false;
  392. }
  393. if (!$result){
  394. return false;
  395. }
  396. $data = [
  397. 'uniacid' => \Yunshop::app()->uniacid,
  398. 'order_sn' => $order_sn,
  399. 'print_template' => $result['PrintTemplate'],
  400. 'mark_destination' => $result['Order']['MarkDestination'] ?: '',
  401. 'logistic_code' => $result['Order']['LogisticCode'] ?: '',
  402. 'shipper_code' => $result['Order']['ShipperCode'] ?: $data['ShipperCode'],
  403. 'order_code' => $result['Order']['OrderCode'] ?: '',
  404. 'kdn_order_code' => $result['Order']['KDNOrderCode'] ?: '',
  405. 'package_code' => $result['Order']['PackageCode'] ?: '',
  406. 'sorting_code' => $result['Order']['SortingCode'] ?: '',
  407. 'sub_count' => $result['SubCount'] ?: '',
  408. 'ebusiness_id' => $result['EBusinessID'] ?: '',
  409. 'uniquer_request_number' => $result['UniquerRequestNumber'] ?: '',
  410. 'result_code' => $result['ResultCode'],
  411. 'reason' => $result['Reason'],
  412. 'success' => $result['Success']
  413. ];
  414. $template_model = new ElectronicTemplate();
  415. $template_model->setRawAttributes($data);
  416. $validator = $template_model->validator($template_model->getAttributes());
  417. if ($validator->fails()) {
  418. $this->error($validator->messages());
  419. } else {
  420. if ($template_model->save()) {
  421. //显示信息并跳转
  422. return true;
  423. } else {
  424. return false;
  425. }
  426. }
  427. }
  428. private function submitEOrder($requestData, $url)
  429. {
  430. $datas = array(
  431. 'EBusinessID' => $this->merchant_id,
  432. 'RequestType' => '1007',
  433. 'RequestData' => urlencode($requestData),
  434. 'DataType' => '2',
  435. );
  436. $datas['DataSign'] = $this->encrypt($requestData, $this->apikey);
  437. $result = Curl::to($url)->withData($datas)->post();
  438. return $result;
  439. }
  440. /**
  441. * 电商Sign签名生成
  442. * @param data 内容
  443. * @param appkey Appkey
  444. * @return DataSign签名
  445. */
  446. private function encrypt($data, $appkey) {
  447. return urlencode(base64_encode(md5($data.$appkey)));
  448. }
  449. /**
  450. * 获取地址区域邮政编码
  451. * @param string $provice_name 省份名称
  452. * @param string $area_name 区/县名称
  453. * @return int $code 邮政编码
  454. */
  455. public function getCode($provice_name,$city_name, $area_name)
  456. {
  457. $address = new Address();
  458. $provice_id = $address->where('areaname','like','%'.$city_name.'%')->first();
  459. $area_id = $address->where('areaname', 'like','%'.$area_name.'%')->where('parentid',$provice_id->id)->select('id')->first();
  460. if(!$area_id) {
  461. \Log::info('通过数据表获取城市名'.$city_name.'失败');
  462. return 1;
  463. }
  464. $area_id = $area_id->toArray();
  465. $url = 'http://www.ems.com.cn/ems/tool/rpq/queryPostCode?city='.$area_id['id'].'&province='.$this->province[$provice_name];
  466. $code = json_decode(Curl::to($url)->get(), true);
  467. if (is_array($code['model']['postCode'])) {
  468. return $code['model']['postCode'][0];
  469. }
  470. \Log::info('====--请求获取邮政编码失败, 地址信息为'.$provice_name.$city_name);
  471. return 1;
  472. }
  473. //快递公司参数配置
  474. public function panelSet()
  475. {
  476. return [
  477. 'EMS' => [
  478. 'CustomerName' => '大客户号',
  479. 'CustomerPwd' => 'APP_SECRET',
  480. 'MonthCode' => '',
  481. 'SendSite' => '',
  482. 'SendStaff' => '',
  483. 'LogisticCode' => ''
  484. ],
  485. 'SF' => [
  486. 'CustomerName' => '',
  487. 'CustomerPwd' => '',
  488. 'MonthCode' => '月结号(选填)',
  489. 'SendSite' => '',
  490. 'SendStaff' => '',
  491. 'LogisticCode' => ''
  492. ],
  493. 'ZTKY' => [
  494. 'CustomerName' => '',
  495. 'CustomerPwd' => '',
  496. 'MonthCode' => '',
  497. 'SendSite' => '',
  498. 'SendStaff' => '',
  499. 'LogisticCode' => ''
  500. ],
  501. 'YZBK' => [
  502. 'CustomerName' => '',
  503. 'CustomerPwd' => '',
  504. 'MonthCode' => '',
  505. 'SendSite' => '',
  506. 'SendStaff' => '',
  507. 'LogisticCode' => ''
  508. ],
  509. 'YZPY' => [
  510. 'CustomerName' => '',
  511. 'CustomerPwd' => '',
  512. 'MonthCode' => '',
  513. 'SendSite' => '',
  514. 'SendStaff' => '',
  515. 'LogisticCode' => ''
  516. ],
  517. 'ZJS' => [
  518. 'CustomerName' => '标识',
  519. 'CustomerPwd' => '秘钥',
  520. 'MonthCode' => '',
  521. 'SendSite' => '',
  522. 'SendStaff' => '',
  523. 'LogisticCode' => '快递单号',
  524. ],
  525. 'UAPEX' => [
  526. 'CustomerName' => '',
  527. 'CustomerPwd' => '',
  528. 'MonthCode' => '',
  529. 'SendSite' => '',
  530. 'SendStaff' => '',
  531. 'LogisticCode' => ''
  532. ],
  533. 'ZTO' => [
  534. 'CustomerName' => '商家ID',
  535. 'CustomerPwd' => '商家接口密码(选填)',
  536. 'MonthCode' => '',
  537. 'SendSite' => '',
  538. 'SendStaff' => '',
  539. 'LogisticCode' => ''
  540. ],
  541. 'STO' => [
  542. 'CustomerName' => '客户简称',
  543. 'CustomerPwd' => '客户密码',
  544. 'MonthCode' => '',
  545. 'SendSite' => '所属网点',
  546. 'SendStaff' => '',
  547. 'LogisticCode' => ''
  548. ],
  549. 'DBL' => [
  550. 'CustomerName' => '月结编码',
  551. 'CustomerPwd' => '',
  552. 'MonthCode' => '',
  553. 'SendSite' => '',
  554. 'SendStaff' => '',
  555. 'LogisticCode' => ''
  556. ],
  557. 'JD' => [
  558. 'CustomerName' => '商家编码',
  559. 'CustomerPwd' => '',
  560. 'MonthCode' => '',
  561. 'SendSite' => '',
  562. 'SendStaff' => '',
  563. 'LogisticCode' => ''
  564. ],
  565. 'XFEX' => [
  566. 'CustomerName' => '客户平台ID',
  567. 'CustomerPwd' => '客户平台验证码',
  568. 'MonthCode' => '',
  569. 'SendSite' => '客户商号ID或仓库ID',
  570. 'SendStaff' => '',
  571. 'LogisticCode' => ''
  572. ],
  573. 'HHTT' => [
  574. 'CustomerName' => '客户帐号',
  575. 'CustomerPwd' => '客户密码',
  576. 'MonthCode' => '',
  577. 'SendSite' => '网点名称',
  578. 'SendStaff' => '',
  579. 'LogisticCode' => ''
  580. ],
  581. 'GTO' => [
  582. 'CustomerName' => '客户简称',
  583. 'CustomerPwd' => '客户密码',
  584. 'MonthCode' => '',
  585. 'SendSite' => '网点名称',
  586. 'SendStaff' => '',
  587. 'LogisticCode' => ''
  588. ],
  589. 'SURE' => [
  590. 'CustomerName' => '客户号',
  591. 'CustomerPwd' => '',
  592. 'MonthCode' => '',
  593. 'SendSite' => '网点编号(仓库号)',
  594. 'SendStaff' => '收件快递员(网点提供)',
  595. 'LogisticCode' => ''
  596. ],
  597. 'KYSY' => [
  598. 'CustomerName' => '客户号',
  599. 'CustomerPwd' => '',
  600. 'MonthCode' => '',
  601. 'SendSite' => '',
  602. 'SendStaff' => '',
  603. 'LogisticCode' => ''
  604. ],
  605. 'YD' => [
  606. 'CustomerName' => '客户ID',
  607. 'CustomerPwd' => '接口联调密码',
  608. 'MonthCode' => '',
  609. 'SendSite' => '',
  610. 'SendStaff' => '',
  611. 'LogisticCode' => ''
  612. ],
  613. 'HTKY' => [
  614. 'CustomerName' => '操作编码',
  615. 'CustomerPwd' => 'ERP秘钥',
  616. 'MonthCode' => '',
  617. 'SendSite' => '',
  618. 'SendStaff' => '',
  619. 'LogisticCode' => ''
  620. ],
  621. 'YTO' => [
  622. 'CustomerName' => '商家代码',
  623. 'CustomerPwd' => '',
  624. 'MonthCode' => '密钥串',
  625. 'SendSite' => '',
  626. 'SendStaff' => '',
  627. 'LogisticCode' => ''
  628. ],
  629. 'YCWL' => [
  630. 'CustomerName' => '商户代码',
  631. 'CustomerPwd' => '',
  632. 'MonthCode' => '',
  633. 'SendSite' => '网点名称',
  634. 'SendStaff' => '',
  635. 'LogisticCode' => ''
  636. ],
  637. 'UC' => [
  638. 'CustomerName' => '客户编号',
  639. 'CustomerPwd' => '密钥(选填)',
  640. 'MonthCode' => '',
  641. 'SendSite' => '',
  642. 'SendStaff' => '',
  643. 'LogisticCode' => ''
  644. ],
  645. 'ANE' => [
  646. 'CustomerName' => '客户号',
  647. 'CustomerPwd' => '',
  648. 'MonthCode' => '',
  649. 'SendSite' => '网点名称(仅数字部分)',
  650. 'SendStaff' => '',
  651. 'LogisticCode' => ''
  652. ],
  653. 'PJ' => [
  654. 'CustomerName' => '月结号',
  655. 'CustomerPwd' => '密钥',
  656. 'MonthCode' => '',
  657. 'SendSite' => '',
  658. 'SendStaff' => '',
  659. 'LogisticCode' => ''
  660. ],
  661. 'CND' => [
  662. 'CustomerName' => '客户编码',
  663. 'CustomerPwd' => '客户密码',
  664. 'MonthCode' => '',
  665. 'SendSite' => '网点名称',
  666. 'SendStaff' => '',
  667. 'LogisticCode' => ''
  668. ],
  669. 'HOAU' => [
  670. 'CustomerName' => '',
  671. 'CustomerPwd' => '',
  672. 'MonthCode' => '',
  673. 'SendSite' => '',
  674. 'WareHouseID' => '',
  675. 'Name' => ''
  676. ],
  677. 'DBLKY' => [
  678. 'CustomerName' => '月结编码',
  679. 'CustomerPwd' => '',
  680. 'MonthCode' => '',
  681. 'SendSite' => '',
  682. 'WareHouseID' => '',
  683. 'Name' => ''
  684. ],
  685. 'ANEKY' => [
  686. 'CustomerName' => '客户编码',
  687. 'CustomerPwd' => '客户秘钥',
  688. 'MonthCode' => '',
  689. 'SendSite' => '',
  690. 'WareHouseID' => '',
  691. 'Name' => ''
  692. ],
  693. 'JDKY' => [
  694. 'CustomerName' => '商家编码',
  695. 'CustomerPwd' => '事业部编码(EBU开头)',
  696. 'MonthCode' => '',
  697. 'SendSite' => '',
  698. 'WareHouseID' => '仓库编码',
  699. 'Name' => '寄件人简称'
  700. ],
  701. 'LB' => [
  702. 'CustomerName' => '电子面单账号',
  703. 'CustomerPwd' => '密码',
  704. 'MonthCode' => '',
  705. 'SendSite' => '',
  706. 'WareHouseID' => '',
  707. 'Name' => ''
  708. ],
  709. 'HTKYKY' => [
  710. 'CustomerName' => '账户信息',
  711. 'CustomerPwd' => '账户密码',
  712. 'MonthCode' => '',
  713. 'SendSite' => '',
  714. 'WareHouseID' => '',
  715. 'Name' => ''
  716. ],
  717. 'ZTOKY' => [
  718. 'CustomerName' => '商家CP',
  719. 'CustomerPwd' => '商家秘钥',
  720. 'MonthCode' => '',
  721. 'SendSite' => '寄件网点ID',
  722. 'WareHouseID' => '',
  723. 'Name' => ''
  724. ],
  725. 'JTSD' => [
  726. 'CustomerName' => '电子面单账号',
  727. 'CustomerPwd' => '密码',
  728. 'MonthCode' => '',
  729. 'SendSite' => '',
  730. 'WareHouseID' => '',
  731. 'Name' => ''
  732. ]
  733. ];
  734. }
  735. //批量打印
  736. /**
  737. * 组装POST表单用于调用快递鸟批量打印接口页面
  738. */
  739. public function build_form() {
  740. //OrderCode:需要打印的订单号,和调用快递鸟电子面单的订单号一致,PortName:本地打印机名称,请参考使用手册设置打印机名称。支持多打印机同时打印。
  741. // $request_data = '[{"OrderCode":"234351215333113311353","PortName":"打印机名称一"},{"OrderCode":"234351215333113311354","PortName":"打印机名称二"}]';
  742. $ordersns = request()->ordersn;
  743. // dd($ordersns);
  744. foreach ($ordersns as $k => $v) {
  745. $request_data[$k]['OrderCode'] = $v;
  746. $request_data[$k]['PortName'] = $this->name;
  747. }
  748. \Log::info('--panel_print---ordersn,--request-data', ['ordersn'=>$ordersns, 'request_data'=>$request_data]);
  749. $request_data_encode = urlencode($request_data);
  750. $data_sign = $this->encrypt($this->get_ip().$request_data_encode, $this->apikey);
  751. //是否预览,0-不预览 1-预览
  752. $is_priview = '0';
  753. //组装表单
  754. $form = '<form id="form1" method="POST" action="'.$this->print_url.'"><input type="text" name="RequestData" value="'.$request_data.'"/><input type="text" name="EBusinessID" value="'.$this->merchant_id.'"/><input type="text" name="DataSign" value="'.$data_sign.'"/><input type="text" name="IsPriview" value="'.$is_priview.'"/></form><script>form1.submit();</script>';
  755. print_r($form);
  756. }
  757. /**
  758. * 判断是否为内网IP
  759. * @param ip IP
  760. * @return 是否内网IP
  761. */
  762. private function is_private_ip($ip) {
  763. return !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
  764. }
  765. /**
  766. * 获取客户端IP(非用户服务器IP)
  767. * @return 客户端IP
  768. */
  769. private function get_ip() {
  770. //获取客户端IP
  771. if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
  772. $ip = getenv('HTTP_CLIENT_IP');
  773. } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
  774. $ip = getenv('HTTP_X_FORWARDED_FOR');
  775. } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
  776. $ip = getenv('REMOTE_ADDR');
  777. } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
  778. $ip = $_SERVER['REMOTE_ADDR'];
  779. }
  780. if(!$ip || $this->is_private_ip($ip)) {
  781. $ch = curl_init();
  782. curl_setopt($ch, CURLOPT_URL, 'http://www.kdniao.com/External/GetIp.aspx');
  783. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  784. $output = curl_exec($ch);
  785. return $output;
  786. }
  787. else{
  788. return $res;
  789. }
  790. }
  791. }