Model_yuntu.php 12 KB

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