<?php
namespace addons\kuaidi\model;
use think\addons\BaseModel as Base;
use think\Db;
/**
 * ============================================================================
 * 快递查询业务处理
 */
class Kuaidi extends Base{
	
	/**
	 * 绑定勾子
	 */
	public function install(){
		Db::startTrans();
		try{
			$hooks = array("adminDocumentOrderView","homeDocumentOrderView","afterQueryUserOrders","mobileDocumentOrderList","wechatDocumentOrderList");
			$this->bindHoods("Kuaidi", $hooks);
			
			Db::commit();
			return true;
		}catch (\Exception $e) {
			Db::rollback();
			return false;
		}
	}
	
	/**
	 * 解绑勾子
	 */
	public function uninstall(){
		Db::startTrans();
		try{
			$hooks = array("adminDocumentOrderView","homeDocumentOrderView","afterQueryUserOrders","mobileDocumentOrderList","wechatDocumentOrderList");
			$this->unbindHoods("Kuaidi", $hooks);
			
			Db::commit();
			return true;
		}catch (\Exception $e) {
			Db::rollback();
			return false;
		}
	}
	
	public function getExpress($orderId){
		$conf = $this->getConf("Kuaidi");
		$express = Db::name('orders')->where(["orderId"=>$orderId])->field(['expressId','expressNo'])->find();
		return $express;
	}
	
	public function getOrderExpress($orderId){
		$conf = $this->getConf("Kuaidi");
		$express = Db::name('orders')->where(["orderId"=>$orderId])->field(['expressId','expressNo'])->find();
		
		if($express["expressId"]>0){
			$expressId = $express["expressId"];
			$row = Db::name('express')->where(["expressId"=>$expressId])->find();
			$typeCom =  strtolower($row["expressCode"]); //快递公司
			$typeNu = $express["expressNo"]; //快递单号
			
			$appKey= $conf["kuaidiKey"];
			
			$expressLogs = null;
			$post_data['param']		= json_encode(['com'=>$typeCom,'num'=>$typeNu]);
			$key 					='jMIRnHZa5264';
			$post_data['customer']	='466DC9B2C8C2CA140FFB2E6FAFE705DF';
			$post_data['sign']= strtoupper(md5($post_data['param'].$key.$post_data['customer']));
		
			
			$expressLogs = $this->curl_request('http://poll.kuaidi100.com/poll/query.do',$post_data);

			//$url ='http://api.kuaidi100.com/api?id='.$appKey.'&com='.$typeCom.'&nu='.$typeNu.'&show=0&muti=1&order=asc';
			//$companys = array('ems','shentong','yuantong','shunfeng','yunda','tiantian','zhongtong','zengyisudi');
			// if(in_array($typeCom,$companys)){
			// 	$url = 'http://www.kuaidi100.com/query?type=' . $typeCom . '&postid=' . $typeNu;
			// }else{
			//	$url ='http://api.kuaidi100.com/api?id='.$appKey.'&com='.$typeCom.'&nu='.$typeNu.'&show=0&muti=1&order=asc';
			// }
			//$expressLogs = $this -> curl($url);
			return $expressLogs;
		}
		
	}
	//参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies
 	function curl_request($url,$post='',$cookie='', $returnCookie=0){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        curl_setopt($curl, CURLOPT_REFERER, "http://XXX");
        if($post) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
        }
        if($cookie) {
            curl_setopt($curl, CURLOPT_COOKIE, $cookie);
        }
        curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        if (curl_errno($curl)) {
            return curl_error($curl);
        }
        curl_close($curl);
        if($returnCookie){
            list($header, $body) = explode("\r\n\r\n", $data, 2);
            preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
            $info['cookie']  = substr($matches[1][0], 1);
            $info['content'] = $body;
            return $info;
        }else{
            return $data;
        }
	}
	public function getOrderInfo($orderId = 0){//添加可传订单号方法 mark hsf 20171130
		$data = array();
		if(!$orderId){
			$orderId = input("orderId");
		}
		$data["express"] = Db::name('orders o')->join('__EXPRESS__ e', 'o.expressId=e.expressId')->where(["orderId"=>$orderId])->field(['e.expressId','e.expressImg','o.expressNo','e.expressName'])->find();
		$data["express"]['expressImg'] = WSTImg($data["express"]['expressImg'],3);
		$data["goodsImg"] = Db::name('orders o')->join('__ORDER_GOODS__ og','o.orderId=og.orderId')->where(["o.orderId"=>$orderId])->value('og.goodsImg');//这个只返回一个数据
		return $data;
	}
	
	public function curl($url) {
		$curl = curl_init();
		curl_setopt ($curl, CURLOPT_URL, $url);
		curl_setopt ($curl, CURLOPT_HEADER,0);
		curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($curl, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
		curl_setopt ($curl, CURLOPT_TIMEOUT,5);
		$content = curl_exec($curl);
		curl_close ($curl);
		return $content;
	}
	

	public  function getOrderDeliver($orderId){
		$rs = Db::name('orders o')->where(["orderId"=>$orderId])->field("deliverType,orderStatus,expressNo")->find();
		return $rs;
	}
	
	
	
}