Logistics.php 5.25 KB
<?php

/**
 * 物流相关
 * @author tongdesheng
 *
 */
class YHMApi_App_V1_Logistics extends YHMApi_App_V1_Base {

    /**
     * 获取货流公司列表
     */
    public static function getCompanyList(array $params, $fileds = '*') {
        $data = YHMOrders_Models_Logistics_Client::getList();
        $companyList = array();
        foreach ($data as $k => $v) {
            $list = array(
                'id' => $v['id'],
                'company_name' => $v['company_name'],
                'initials' => $v['initials'],
                'pinyin' => $v['pinyin'],
                'logistics_logo' => YHMUpload_Images::template($v['logo'], 'yhfair')
            );
            $companyList[] = $list;
        }
        return self::result(200, '快递公司列表', $companyList);
    }

    /**
     * 查看物流信息
     * @param array $params
     * @param string $fields
     */
    public static function getLogisticsDetail(array $params, $fields = "*") {
      
        
        $uid = $params['uid'];
        $order_code = $params['order_code']; //订单号
        if (empty($order_code)) {
            return self::result(400, '订单号不能为空');
        }
        
        
        //根据订单号查出物流信息
        $order_delivery = YHMOrders_Models_Delivery_Client::getOneByOrderCode($order_code);
        if (empty($order_delivery)) {
            return self::result(400, '没有查到物流信息');
        }
        //物流公司信息
        $company = YHMOrders_Models_Logistics_Client::getOneById($order_delivery['deliver_company_id']);
        $company_code = $company['company_code']; //公司代码
        
        $list= YHMOrders_Models_Logistics_Client::getOrderLogistics($order_code);  
       if ($list)
       {
        //   print_r($list['detail_list']);
         $retData= json_decode($list['detail_list'], true);  
          return self::result(200, '获取成功', $retData);
       }
//        $req_config = array(
//            'adapter' => 'Zend_Http_Client_Adapter_Curl',
//            'timeout' => 5000
//        );
        $retData = array();
        //先查库, 如果库里有信息就不调用接口
        try {
             $mc = YHMUtils_Mc::getMc();
             $retData = $mc->get('logistics_' . $order_code);
      // if (!isset($retData['message']))
            {
//            $client = new Zend_Http_Client(YHMConfig_Express::$_url, $req_config);
//            $data = array(
//                'appid' => YHMConfig_Express::$_appid,
//                'com' => $company_code,
//                'nu' => $order_delivery['delivery_code']
//            );
//            $client->setParameterGet($data);
//            $requestResults = $client->request('GET');
//            $result = $requestResults->getBody();
//            $express = json_decode($result, true);
//            $retData = array(
//                'logistics_company' => $company['company_name'],
//                'logistics_code' => $order_delivery['delivery_code'],
//                'logistics_logo' => YHMUpload_Images::template($company['logo'], 'yhfair'),
//                'detail_list' => array()
//            );
//            if (!empty($express['data']['info']['context'])) {
//                foreach ($express['data']['info']['context'] as $oneData) {
//                    $retData['detail_list'][] = array(
//                        'status_time' => empty($oneData['time']) ? '' : $oneData['time'],
//                        'status_info' => $oneData['desc']
//                    );
//                }
//            }
                $retData = array(
                'logistics_company' => $company['company_name'],
                'logistics_code' => $order_delivery['delivery_code'],
                'logistics_logo' => YHMUpload_Images::template($company['logo'], 'yhfair'),
                'detail_list' => array()
               );
                $url="http://www.kuaidi100.com/query?type=$company_code&postid=".$order_delivery['delivery_code']."&id=1&valicode=&temp=0.2546041894394805";
                $list=file_get_contents($url);
       // print_r(json_decode($list,true));  
                $express=json_decode($list,true);
               if (!empty($express['data'])) {
                  foreach ($express['data'] as $oneData) {
                    $retData['detail_list'][] = array(
                        'status_time' => empty($oneData['time']) ? '' : strtotime($oneData['time']),
                        'status_info' => $oneData['context']
                    );
                }
             //  print_r($list);
                $retData['message']=$express['message'];
              
              //  print_r($express);
                if ($express['state']==3)
                {
                    $mc->set('logistics_' . $order_code,$retData);
                 //   echo $order_code;
                    YHMOrders_Models_Logistics_Client::setOrderLogistics($order_code, json_encode($retData));
                }
                 $mc->set('logistics_' . $order_code,$retData,84600);
               }
             
           }
          
        } catch (Exception $ex) {
            $retData['message'] = $ex->getMessage();
            $retData['status_time'] = 0;
            $retData['status_info'] = 0;
        }
        return self::result(200, '获取成功', $retData);
    }

}