161 lines
5.4 KiB
PHP
161 lines
5.4 KiB
PHP
<?php
|
||
|
||
|
||
namespace app\common\service;
|
||
use think\Exception;
|
||
use think\facade\Cache;
|
||
|
||
|
||
class ExpressService
|
||
{
|
||
|
||
|
||
public $key = 'jhyyydydxxb';
|
||
//暂时翻转key再连接起来md5
|
||
public $md5_key = '5ec1092c4833247e3bae48a38c3364d4';
|
||
|
||
//快递appcode
|
||
public $appcode='18a0cb65e1614f6197e661f64bd82245';
|
||
|
||
public function initialize()
|
||
{
|
||
if(array_key_exists("HTTP_ORIGIN",$_SERVER)) {
|
||
$domain = $_SERVER['HTTP_ORIGIN'];
|
||
if(strstr($domain,'.ydyd.com') || strstr($domain,'.ydyd51.com') || strstr($domain,'.jinhu11.com')|| strstr($domain,'.jh.com')) {
|
||
$origin = $domain;
|
||
}
|
||
|
||
header("Access-Control-Allow-Credentials: true");
|
||
header("Access-Control-Allow-Origin: ".$origin);
|
||
}
|
||
}
|
||
|
||
public function get_express($parames){
|
||
if($this->get_auth($this->key)){
|
||
//验证成功 开始获取内容
|
||
$type = trim(request()->param('type')); //get_type get_detail
|
||
if($type == 'get_type'){
|
||
$express_type_list = Cache::get('EXPRESS_TYPE_LIST');
|
||
if(!$express_type_list){
|
||
$express_type_list = json_decode($this->get_express_type(),TRUE );
|
||
Cache::set("EXPRESS_TYPE_LIST", $express_type_list, 604800);
|
||
}
|
||
if($express_type_list['status'] == '200'){
|
||
$result['errcode'] = 0;
|
||
$result['errmsg'] = '获取快递厂家列表成功';
|
||
$result['data']['list'] = $express_type_list['result'];
|
||
}else{
|
||
$result['errcode'] = -1;
|
||
$result['errmsg'] = '获取快递厂家列表失败';
|
||
$result['data']['list'] = $express_type_list;
|
||
}
|
||
return json($result);
|
||
}elseif($type == 'get_detail'){
|
||
$exp_num =$parames['exp_num'];
|
||
$exp_type = explode('*',$parames['exp_type']);
|
||
if(isset($exp_type[1])){
|
||
$exp_type = $exp_type[1];
|
||
if($exp_num && $exp_type){
|
||
$express_detail = json_decode($this->get_express_detail($exp_num,$exp_type),true);
|
||
$ret = [];
|
||
if($express_detail['code'] == 'OK'){
|
||
foreach ($express_detail['list'] as $value)
|
||
{
|
||
$item['title'] = $value['content'];
|
||
$item['desc'] = $value['time'];
|
||
$ret[] = $item;
|
||
}
|
||
if(!$ret){
|
||
$item['title'] = '暂无轨迹信息';
|
||
$item['desc'] = date("Y-m-d h:i:s", time());
|
||
$ret[] = $item;
|
||
}
|
||
return $ret;
|
||
}else{
|
||
if(!$ret){
|
||
$item['title'] = '暂无轨迹信息';
|
||
$item['desc'] = date("Y-m-d h:i:s", time());
|
||
$ret[] = $item;
|
||
}
|
||
return $ret;
|
||
}
|
||
}
|
||
throw new Exception('请检查快递单号或者快递类型');
|
||
}
|
||
throw new Exception('未发送要查询的类型');
|
||
}else{
|
||
throw new Exception('未发送要查询的类型');
|
||
}
|
||
}else{
|
||
throw new Exception('非法请求');
|
||
}
|
||
|
||
}
|
||
|
||
public function get_auth($_key){
|
||
$_md5_key = md5($this->key . strrev($_key));
|
||
if($_md5_key == $this->md5_key){
|
||
return true;
|
||
}else{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public function get_express_type($exp_type=''){
|
||
$host = "https://cexpress.market.alicloudapi.com";
|
||
$path = "/cExpressLists";
|
||
$method = "GET";
|
||
$headers = array();
|
||
array_push($headers, "Authorization:APPCODE " . $this->appcode);
|
||
$querys = "";
|
||
$bodys = "";
|
||
$url = $host . $path . "?" . $querys;
|
||
|
||
$curl = curl_init();
|
||
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
|
||
curl_setopt($curl, CURLOPT_URL, $url);
|
||
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
||
curl_setopt($curl, CURLOPT_FAILONERROR, false);
|
||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||
curl_setopt($curl, CURLOPT_HEADER, false);
|
||
if (1 == strpos("$".$host, "https://"))
|
||
{
|
||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
||
}
|
||
$result = curl_exec($curl);
|
||
return $result;
|
||
}
|
||
|
||
public function get_express_detail($exp_num,$exp_type){
|
||
$host = "https://cexpress.market.alicloudapi.com";
|
||
$path = "/cexpress";
|
||
$method = "GET";
|
||
$appcode = $this->appcode;
|
||
$headers = array();
|
||
array_push($headers, "Authorization:APPCODE " . $appcode);
|
||
$querys = "no=" . $exp_num . "&type=" . $exp_type;
|
||
$bodys = "";
|
||
$url = $host . $path . "?" . $querys;
|
||
|
||
$curl = curl_init();
|
||
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
|
||
curl_setopt($curl, CURLOPT_URL, $url);
|
||
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
||
curl_setopt($curl, CURLOPT_FAILONERROR, false);
|
||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||
curl_setopt($curl, CURLOPT_HEADER, false);
|
||
//curl_setopt($curl, CURLOPT_HEADER, true); 如不输出json, 请打开这行代码,打印调试头部状态码。
|
||
//状态码: 200 正常;400 URL无效;401 appCode错误; 403 次数用完; 500 API网管错误
|
||
if (1 == strpos("$".$host, "https://"))
|
||
{
|
||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
||
}
|
||
$result = curl_exec($curl);
|
||
return $result;
|
||
}
|
||
|
||
|
||
|
||
} |