Model_yuntuuk.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <?php
  2. if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  3. use GuzzleHttp\Client;
  4. /**
  5. * 此类主要是为了服务于云途快递的API接口
  6. */
  7. class Model_yuntuuk extends Lin_Model
  8. {
  9. //public $base_url = "https://openapi-sbx.yunexpress.cn"; //测试环境
  10. public $base_url = "https://openapi.yunexpress.cn";//正式环境
  11. public $token_url = "/openapi/oauth2/token";
  12. //正式环境
  13. public $yt_config = [
  14. "appId"=> "1fc6c0ca252c",
  15. "appSecret"=> "0b6bdc0ecab04b59981c3a18a84919be",
  16. "sourceKey"=> "wzmu5q6x"
  17. ];
  18. //测试环境
  19. // public $yt_config = [
  20. // "appId"=> "d34f30df34ae",
  21. // "appSecret"=> "9ace1fa73c4b4567a6ae5ac7e254b48f",
  22. // "sourceKey"=> "wzmu5q6x"
  23. // ];
  24. function __construct(){
  25. parent::__construct();
  26. $this->load->_model("Model_logic_ding",'logic_ding');
  27. }
  28. public function get_data($data){
  29. $token = $this->getAccessToken();
  30. if(empty($token)){
  31. return [
  32. 'x'=>'0',
  33. 'Description'=>"请求时token为空,请联系技术!!!",
  34. ];
  35. }
  36. $url = "/v1/order/package/create";
  37. //拼接参数
  38. $receive_address = [];
  39. $receive_address[] = $data['address'];
  40. if(!empty($data['address2'])){
  41. $receive_address[] = $data['address2'];
  42. }
  43. if(!empty($data['printnumber'])){
  44. $data['printnumber'] = $data['printnumber']*1 + 1;
  45. }
  46. $params = [
  47. "product_code"=> "JFPHTKR",
  48. //"product_code"=> "S1002",
  49. "customer_order_number"=> empty($data['printnumber'])?$data['number']: $data['number']."_".$data['printnumber'],
  50. "packages"=> [
  51. [
  52. "weight"=> $data['zzl']
  53. ],
  54. ],
  55. "receiver"=> [
  56. "first_name"=> $data['name'],
  57. "country_code"=> $data['lb'],
  58. "province"=> $data['province'],
  59. "city"=> $data['city'],
  60. "address_lines"=> $receive_address,
  61. "postal_code"=> $data['zipcode'],
  62. "phone_number"=> $data['phone'],
  63. "email"=> isset($data['email'])?$data['email']:'',
  64. "company"=>$data['client'],
  65. ],
  66. "declaration_info"=> [
  67. [
  68. "quantity"=>$data['ts'],
  69. "unit_price"=>$data['dtsbjz'],
  70. "unit_weight"=>sprintf("%.3f",$data['zzl']/$data['ts']),
  71. "name_en"=> $data['sbpm'],
  72. "name_local"=>$data['zwpm'],
  73. ],
  74. ],
  75. "customs_number"=>""
  76. // "sender"=> [
  77. // "first_name"=> "first_name",
  78. // "last_name"=> "last_name",
  79. // "company"=> "company",
  80. // "country_code"=> "11",
  81. // "province"=> "province",
  82. // "city"=> "city",
  83. // "address_lines"=> [
  84. // "address_lines1"
  85. // ],
  86. // "postal_code"=> "postal_code",
  87. // "phone_number"=> "phone_number",
  88. // "email"=> "email",
  89. // ],
  90. ];
  91. // if($data['printcode'] == 'YunExpress_BaoXian'){
  92. // $params["extra_services"] =[
  93. // [
  94. // "extra_code"=>"VAS_IP",
  95. // "extra_value"=>"BJFDR",
  96. // ]
  97. // ];
  98. // }
  99. // if($data['printcode'] == 'YunExpress_JFPHTKR'){
  100. // $params["product_code"] = 'JFPHTKR';
  101. // $params['customs_number'] = "";
  102. // }
  103. $start_time = time();
  104. $res = $this->sendNewPost($url,$params,$token);
  105. // echo "<pre>";
  106. // var_dump($params);
  107. // var_dump($res);
  108. $this->logic_ding->sendToDing($data['number']."云途快递报文信息【".json_encode($params)."】返回参数[".json_encode($res,JSON_UNESCAPED_UNICODE)."]请求域名".$url);
  109. if(isset($res['code'])&&($res['code'] != 1)){
  110. return [
  111. 'x'=>'0',
  112. 'Description'=>$res['msg'],
  113. ];
  114. }else{
  115. usleep(30);
  116. $r = $this->get_label($res['result']['waybill_number']);
  117. $end_time = time();
  118. if( $end_time - $start_time > 45){
  119. $this->logic_ding->addJobs('outtime_order',['number'=>$data['number']]);
  120. }
  121. //var_dump($res);
  122. if($r['x'] != 1){
  123. return [
  124. 'x'=>'2',
  125. 'waybill'=>$res['result']['waybill_number'],
  126. "url"=>"",
  127. 'Description'=>$r['Description'],
  128. ];
  129. }else{
  130. return [
  131. 'x'=>'1',
  132. 'waybill'=>$res['result']['waybill_number'],
  133. "url"=>$r['url']
  134. ];
  135. }
  136. }
  137. }
  138. /**
  139. * 获取电子面单
  140. */
  141. public function get_label($order_number){
  142. $token = $this->getAccessToken();
  143. if(empty($token)){
  144. return [
  145. 'x'=>'0',
  146. 'Description'=>"获取token失败",
  147. ];
  148. //throw new Exception("获取token失败");
  149. }
  150. $query = [
  151. 'order_number'=> $order_number,
  152. ];
  153. $url ="/v1/order/label/get";
  154. $res = $this->sendNewGet($url,$query,$token);
  155. $this->logic_ding->sendToDing($order_number."云途快递报文信息获取电子面单返回参数[".json_encode($res)."]");
  156. if(isset($res['code'])&&($res['code'] != 1)){
  157. return [
  158. 'x'=>'0',
  159. 'Description'=>$res['msg'],
  160. ];
  161. }else{
  162. return [
  163. 'x'=>'1',
  164. 'url'=>$res['result']['url'],
  165. ];
  166. }
  167. }
  168. /**
  169. * 基于guzzlehttp的get请求
  170. */
  171. protected function sendNewGet($url,$query,$token = ""){
  172. $timestamp = time() . "000";
  173. $bodyString=null;
  174. // 设置请求的URL
  175. $all_url = $this->base_url.$url."?".http_build_query($query);
  176. $tmp_sign = $this->generateSignatureContent($timestamp, "GET", $url, $bodyString);
  177. $sign= $this->generateSha256Signature($tmp_sign, $this->yt_config['appSecret']);
  178. // 初始化cURL会话
  179. $ch = curl_init($all_url);
  180. // 设置cURL选项
  181. $options = array(
  182. CURLOPT_SSL_VERIFYPEER=>false,
  183. CURLOPT_SSL_VERIFYHOST=>false,
  184. CURLOPT_RETURNTRANSFER => true,
  185. CURLOPT_HTTPHEADER => array(
  186. 'Content-Type: application/json',
  187. 'token: ' . $token ,
  188. 'date: ' . $timestamp,
  189. 'sign: ' . $sign
  190. ),
  191. );
  192. // 应用这些选项到cURL会话
  193. curl_setopt_array($ch, $options);
  194. // 执行cURL会话并获取响应
  195. $response = curl_exec($ch);
  196. // 检查是否有错误发生
  197. if ($response === false) {
  198. $error = curl_error($ch);
  199. curl_close($ch);
  200. return [
  201. "code"=> -1,
  202. "msg"=> $error
  203. ];
  204. }
  205. // 关闭cURL会话
  206. curl_close($ch);
  207. // 输出响应内容
  208. return json_decode($response,true);
  209. }
  210. /**
  211. * 基于guzzlehttp的post请求
  212. */
  213. protected function sendNewPost($url,$params,$token = ""){
  214. $timestamp = time() . "000";
  215. $bodyString=json_encode($params);
  216. // 设置请求的URL
  217. $all_url = $this->base_url.$url;
  218. $tmp_sign = $this->generateSignatureContent($timestamp, "POST", $url, $bodyString);
  219. $sign= $this->generateSha256Signature($tmp_sign, $this->yt_config['appSecret']);
  220. // 初始化cURL会话
  221. $ch = curl_init($all_url);
  222. // 设置cURL选项
  223. $options = array(
  224. CURLOPT_SSL_VERIFYPEER=>false,
  225. CURLOPT_SSL_VERIFYHOST=>false,
  226. CURLOPT_RETURNTRANSFER => true,
  227. CURLOPT_POST => true,
  228. CURLOPT_POSTFIELDS => $bodyString,
  229. CURLOPT_HTTPHEADER => array(
  230. 'Content-Type: application/json',
  231. 'token: ' . $token ,
  232. 'date: ' . $timestamp,
  233. 'sign: ' . $sign
  234. ),
  235. );
  236. // 应用这些选项到cURL会话
  237. curl_setopt_array($ch, $options);
  238. // 执行cURL会话并获取响应
  239. $response = curl_exec($ch);
  240. // 检查是否有错误发生
  241. // 检查是否有错误发生
  242. if ($response === false) {
  243. $error = curl_error($ch);
  244. curl_close($ch);
  245. return [
  246. "code"=> -1,
  247. "msg"=> $error
  248. ];
  249. }
  250. // 关闭cURL会话
  251. curl_close($ch);
  252. // 输出响应内容
  253. return json_decode($response,true);
  254. }
  255. /**
  256. * 基于guzzlehttp的post请求
  257. */
  258. protected function sendPost($url,$params,$token = ""){
  259. $all_url = $this->base_url.$url;
  260. $jsonData = json_encode($params);
  261. // 初始化cURL会话
  262. $ch = curl_init($all_url);
  263. // 设置cURL选项
  264. $options = [
  265. CURLOPT_SSL_VERIFYPEER=>false,
  266. CURLOPT_SSL_VERIFYHOST=>false,
  267. CURLOPT_RETURNTRANSFER => true,
  268. CURLOPT_POST => true,
  269. CURLOPT_POSTFIELDS => $jsonData,
  270. CURLOPT_HTTPHEADER => [
  271. 'Content-Type: application/json'
  272. ],
  273. ];
  274. curl_setopt_array($ch, $options);
  275. // 执行cURL会话并获取响应
  276. $response = curl_exec($ch);
  277. // 检查是否有错误发生
  278. if ($response === false) {
  279. $error = curl_error($ch);
  280. curl_close($ch);
  281. return [
  282. "code"=> -1,
  283. "msg"=> $error
  284. ];
  285. }
  286. // 关闭cURL会话
  287. curl_close($ch);
  288. $data = json_decode($response, true);
  289. return $data;
  290. }
  291. /**
  292. * 签名算法
  293. */
  294. /*
  295. * 加签内容构造
  296. */
  297. private function generateSignatureContent($timestamp, $method, $uri, $body = null) {
  298. $params = [
  299. 'date' => $timestamp,
  300. 'method' => $method,
  301. 'uri' => $uri,
  302. ];
  303. if (!empty($body)) {
  304. $params['body'] = $body;
  305. }
  306. ksort($params);
  307. $signatureContent = http_build_query($params, '', '&');
  308. return urldecode($signatureContent);
  309. }
  310. /*
  311. * 加签
  312. */
  313. private function generateSha256Signature($data, $key){
  314. //return utf8_encode(base64_encode(hash_hmac('sha256', $data,$key,true)));
  315. return mb_convert_encoding(base64_encode(hash_hmac('sha256', $data,$key,true)), 'ISO-8859-1', 'UTF-8');
  316. }
  317. private function getAccessToken(){
  318. $info= $this->setting->find("skey = 'yuntu_express_token'");
  319. if(empty($info)){
  320. $token = $this->makeToken();
  321. $this->setting->insert([
  322. "skey"=> "yuntu_express_token",
  323. "svalue"=> $token,
  324. "expiretime"=> (time() + 7100),
  325. "sessionkey"=> "yuntu_express_token"
  326. ]);
  327. return $token;
  328. }
  329. if($info['expiretime'] < time()){
  330. $token = $this->makeToken();
  331. $this->setting->save([
  332. "svalue"=> $token,
  333. "expiretime"=> (time() + 7100)
  334. ],"yuntu_express_token");
  335. return $token;
  336. }
  337. return $info['svalue'];
  338. }
  339. private function makeToken(){
  340. $res = $this->sendPost($this->token_url,[
  341. "grantType"=>"client_credentials",
  342. "appId"=> $this->yt_config['appId'],
  343. "appSecret"=>$this->yt_config['appSecret'],
  344. "sourceKey"=> $this->yt_config['sourceKey']
  345. ]);
  346. if(isset($res['accessToken'])){
  347. $token = $res['accessToken'];
  348. return $token;
  349. }else{
  350. $this->logic_ding->sendToDing("云途快递获取token失败,返回参数[".json_encode($res)."]");
  351. return "";
  352. }
  353. }
  354. /**
  355. * 获取运单号
  356. */
  357. public function getTransferNumber($waybill_numbers){
  358. // /v1/order/last-mile/get
  359. $token = $this->getAccessToken();
  360. if(empty($token)){
  361. throw new Exception("获取token失败");
  362. }
  363. $query = [
  364. 'waybill_numbers'=> $waybill_numbers,
  365. ];
  366. $url ="/v1/order/last-mile/get";
  367. $res = $this->sendNewPost($url,$query,$token);
  368. return $res;
  369. }
  370. }