Model_yuntu.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  3. use GuzzleHttp\Client;
  4. /**
  5. * 此类主要是为了服务于云途快递的API接口
  6. */
  7. class Model_yuntu 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"=> "96ab2e464e8e",
  15. "appSecret"=> "9ecc186728b64103ac1a5967c90c70e3",
  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"=> "YTKD",
  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. "ioss_code"=>$data['ioss']
  77. ]
  78. // "sender"=> [
  79. // "first_name"=> "first_name",
  80. // "last_name"=> "last_name",
  81. // "company"=> "company",
  82. // "country_code"=> "11",
  83. // "province"=> "province",
  84. // "city"=> "city",
  85. // "address_lines"=> [
  86. // "address_lines1"
  87. // ],
  88. // "postal_code"=> "postal_code",
  89. // "phone_number"=> "phone_number",
  90. // "email"=> "email",
  91. // ],
  92. ];
  93. if($data['printcode'] == 'YunExpress_BaoXian'){
  94. $params["extra_services"] =[
  95. [
  96. "extra_code"=>"VAS_IP",
  97. "extra_value"=>"BJFDR",
  98. ]
  99. ];
  100. }
  101. $start_time = time();
  102. $res = $this->sendNewPost($url,$params,$token);
  103. // echo "<pre>";
  104. // var_dump($params);
  105. // var_dump($res);
  106. $this->logic_ding->sendToDing($data['number']."云途快递报文信息【".json_encode($params)."】返回参数[".json_encode($res)."]");
  107. if(isset($res['code'])&&($res['code'] != 1)){
  108. return [
  109. 'x'=>'0',
  110. 'Description'=>$res['msg'],
  111. ];
  112. }else{
  113. usleep(30);
  114. $r = $this->get_label($res['result']['waybill_number']);
  115. $end_time = time();
  116. if( $end_time - $start_time > 45){
  117. $this->logic_ding->addJobs('outtime_order',['number'=>$data['number']]);
  118. }
  119. //var_dump($res);
  120. if($r['x'] != 1){
  121. return [
  122. 'x'=>'2',
  123. 'waybill'=>$res['result']['waybill_number'],
  124. "url"=>"",
  125. 'Description'=>$r['Description'],
  126. ];
  127. }else{
  128. return [
  129. 'x'=>'1',
  130. 'waybill'=>$res['result']['waybill_number'],
  131. "url"=>$r['url']
  132. ];
  133. }
  134. }
  135. }
  136. /**
  137. * 获取电子面单
  138. */
  139. public function get_label($order_number){
  140. $token = $this->getAccessToken();
  141. if(empty($token)){
  142. throw new Exception("获取token失败");
  143. }
  144. $query = [
  145. 'order_number'=> $order_number,
  146. ];
  147. $url ="/v1/order/label/get";
  148. $res = $this->sendNewGet($url,$query,$token);
  149. $this->logic_ding->sendToDing($order_number."云途快递报文信息获取电子面单返回参数[".json_encode($res)."]");
  150. if(isset($res['code'])&&($res['code'] != 1)){
  151. return [
  152. 'x'=>'0',
  153. 'Description'=>$res['msg'],
  154. ];
  155. }else{
  156. return [
  157. 'x'=>'1',
  158. 'url'=>$res['result']['url'],
  159. ];
  160. }
  161. }
  162. /**
  163. * 基于guzzlehttp的get请求
  164. */
  165. protected function sendNewGet($url,$query,$token = ""){
  166. $timestamp = time() . "000";
  167. $bodyString=null;
  168. // 设置请求的URL
  169. $all_url = $this->base_url.$url."?".http_build_query($query);
  170. $tmp_sign = $this->generateSignatureContent($timestamp, "GET", $url, $bodyString);
  171. $sign= $this->generateSha256Signature($tmp_sign, $this->yt_config['appSecret']);
  172. // 初始化cURL会话
  173. $ch = curl_init($all_url);
  174. // 设置cURL选项
  175. $options = array(
  176. CURLOPT_SSL_VERIFYPEER=>false,
  177. CURLOPT_SSL_VERIFYHOST=>false,
  178. CURLOPT_RETURNTRANSFER => true,
  179. CURLOPT_HTTPHEADER => array(
  180. 'Content-Type: application/json',
  181. 'token: ' . $token ,
  182. 'date: ' . $timestamp,
  183. 'sign: ' . $sign
  184. ),
  185. );
  186. // 应用这些选项到cURL会话
  187. curl_setopt_array($ch, $options);
  188. // 执行cURL会话并获取响应
  189. $response = curl_exec($ch);
  190. // 检查是否有错误发生
  191. if ($response === false) {
  192. $error = curl_error($ch);
  193. curl_close($ch);
  194. return [
  195. "code"=> -1,
  196. "msg"=> $error
  197. ];
  198. }
  199. // 关闭cURL会话
  200. curl_close($ch);
  201. // 输出响应内容
  202. return json_decode($response,true);
  203. }
  204. /**
  205. * 基于guzzlehttp的post请求
  206. */
  207. protected function sendNewPost($url,$params,$token = ""){
  208. $timestamp = time() . "000";
  209. $bodyString=json_encode($params);
  210. // 设置请求的URL
  211. $all_url = $this->base_url.$url;
  212. $tmp_sign = $this->generateSignatureContent($timestamp, "POST", $url, $bodyString);
  213. $sign= $this->generateSha256Signature($tmp_sign, $this->yt_config['appSecret']);
  214. // 初始化cURL会话
  215. $ch = curl_init($all_url);
  216. // 设置cURL选项
  217. $options = array(
  218. CURLOPT_SSL_VERIFYPEER=>false,
  219. CURLOPT_SSL_VERIFYHOST=>false,
  220. CURLOPT_RETURNTRANSFER => true,
  221. CURLOPT_POST => true,
  222. CURLOPT_POSTFIELDS => $bodyString,
  223. CURLOPT_HTTPHEADER => array(
  224. 'Content-Type: application/json',
  225. 'token: ' . $token ,
  226. 'date: ' . $timestamp,
  227. 'sign: ' . $sign
  228. ),
  229. );
  230. // 应用这些选项到cURL会话
  231. curl_setopt_array($ch, $options);
  232. // 执行cURL会话并获取响应
  233. $response = curl_exec($ch);
  234. // 检查是否有错误发生
  235. // 检查是否有错误发生
  236. if ($response === false) {
  237. $error = curl_error($ch);
  238. curl_close($ch);
  239. return [
  240. "code"=> -1,
  241. "msg"=> $error
  242. ];
  243. }
  244. // 关闭cURL会话
  245. curl_close($ch);
  246. // 输出响应内容
  247. return json_decode($response,true);
  248. }
  249. /**
  250. * 基于guzzlehttp的post请求
  251. */
  252. protected function sendPost($url,$params,$token = ""){
  253. $all_url = $this->base_url.$url;
  254. $jsonData = json_encode($params);
  255. // 初始化cURL会话
  256. $ch = curl_init($all_url);
  257. // 设置cURL选项
  258. $options = [
  259. CURLOPT_SSL_VERIFYPEER=>false,
  260. CURLOPT_SSL_VERIFYHOST=>false,
  261. CURLOPT_RETURNTRANSFER => true,
  262. CURLOPT_POST => true,
  263. CURLOPT_POSTFIELDS => $jsonData,
  264. CURLOPT_HTTPHEADER => [
  265. 'Content-Type: application/json'
  266. ],
  267. ];
  268. curl_setopt_array($ch, $options);
  269. // 执行cURL会话并获取响应
  270. $response = curl_exec($ch);
  271. // 检查是否有错误发生
  272. if ($response === false) {
  273. $error = curl_error($ch);
  274. curl_close($ch);
  275. return [
  276. "code"=> -1,
  277. "msg"=> $error
  278. ];
  279. }
  280. // 关闭cURL会话
  281. curl_close($ch);
  282. $data = json_decode($response, true);
  283. return $data;
  284. }
  285. /**
  286. * 签名算法
  287. */
  288. /*
  289. * 加签内容构造
  290. */
  291. private function generateSignatureContent($timestamp, $method, $uri, $body = null) {
  292. $params = [
  293. 'date' => $timestamp,
  294. 'method' => $method,
  295. 'uri' => $uri,
  296. ];
  297. if (!empty($body)) {
  298. $params['body'] = $body;
  299. }
  300. ksort($params);
  301. $signatureContent = http_build_query($params, '', '&');
  302. return urldecode($signatureContent);
  303. }
  304. /*
  305. * 加签
  306. */
  307. private function generateSha256Signature($data, $key){
  308. //return utf8_encode(base64_encode(hash_hmac('sha256', $data,$key,true)));
  309. return mb_convert_encoding(base64_encode(hash_hmac('sha256', $data,$key,true)), 'ISO-8859-1', 'UTF-8');
  310. }
  311. private function getAccessToken(){
  312. $info= $this->setting->find("skey = 'yuntu_express_token'");
  313. if(empty($info)){
  314. $token = $this->makeToken();
  315. $this->setting->insert([
  316. "skey"=> "yuntu_express_token",
  317. "svalue"=> $token,
  318. "expiretime"=> (time() + 7100),
  319. "sessionkey"=> "yuntu_express_token"
  320. ]);
  321. return $token;
  322. }
  323. if($info['expiretime'] < time()){
  324. $token = $this->makeToken();
  325. $this->setting->save([
  326. "svalue"=> $token,
  327. "expiretime"=> (time() + 7100)
  328. ],"yuntu_express_token");
  329. return $token;
  330. }
  331. return $info['svalue'];
  332. }
  333. private function makeToken(){
  334. $res = $this->sendPost($this->token_url,[
  335. "grantType"=>"client_credentials",
  336. "appId"=> $this->yt_config['appId'],
  337. "appSecret"=>$this->yt_config['appSecret'],
  338. "sourceKey"=> $this->yt_config['sourceKey']
  339. ]);
  340. if(isset($res['accessToken'])){
  341. $token = $res['accessToken'];
  342. return $token;
  343. }else{
  344. return "";
  345. }
  346. }
  347. /**
  348. * 获取运单号
  349. */
  350. public function getTransferNumber($waybill_numbers){
  351. // /v1/order/last-mile/get
  352. $token = $this->getAccessToken();
  353. if(empty($token)){
  354. throw new Exception("获取token失败");
  355. }
  356. $query = [
  357. 'waybill_numbers'=> $waybill_numbers,
  358. ];
  359. $url ="/v1/order/last-mile/get";
  360. $res = $this->sendNewPost($url,$query,$token);
  361. return $res;
  362. }
  363. }