Client.php 5.11 KB
<?php
namespace common\lib\YohoApi;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * 有货接口
 * @author    wuxiao
 * 
 * @文档地址     http://git.yoho.cn/yoho-documents/api-interfaces
 */

class Client{

    private static $_domain = array(
        'prod'       => 'http://api.yoho.cn/',
        'dev'      => 'http://192.168.102.205:8080/gateway/',
    );
    
    /**
     * 私钥列表
     * @var array
     */
    private static $privateKey = array(
        'iphone'  => 'a85bb0674e08986c6b115d5e3a4884fa',
        'android' => 'fd4ad5fcfa0de589ef238c0e7331b585',
        'ipad' => 'ad9fcda2e679cf9229e37feae2cdcf80',
		'web' => '0ed29744ed318fd28d2c07985d3ba633',
		'h5' => 'fd4ad5fcfa0de589ef238c0e7331b585',
        'other' => '6tjjbg7ecrcd3ulgqizbqavfrutixhm7',
    );
    
    /**
     * 默认客户端类型
     */
    const client_type = 'web';
    
    /**
     * $_requestUrl
     * 请求url
     * @var string
     */
    protected static $_requestUrl = '';

    /**
     * $_rawResponse
     * 原始的返回信息
     * @var string
     */
    protected static $_rawResponse = '';
    
    /**
     * $_timeOut
     * 设置连接主机的超时时间
     * @var int 数量级:秒
     * */
    protected static $_timeOut = 2;

    /**
     * getRequestUrl
     * 获取请求url
     */
    public static function getRequestUrl()
    {
        return self::$_requestUrl;
    }

    /**
     * getRawResponse
     * 获取原始的返回信息
     */
    public static function getRawResponse()
    {
        return self::$_rawResponse;
    }
    
    /**
     * 实例化自身
     */
	public static function self()
	{
        static $self;
        if (empty($self)){
            $self = new self();
        }
		return $self;
	}
    
    private function __construct(){
        ;
    }

    /**
     * 根据多个skn查询的接口
     * @param type $productSkn
     * @return type
     */
    public function h5ProductBatch($productSkn){
        $productSkn = (array)$productSkn;
        $params = [
            'client_type'=>self::client_type,
            'method'=>'h5.product.batch',
            'productSkn'=>implode(',',$productSkn),
        ];
        return $this->send($params);
    }
    
    /**
     * 根据多个优惠券id查询的接口
     * @param type $couponId
     * @return type
     */
    public function couponList($couponId){
        $couponId = (array)$couponId;
        $url = 'http://192.168.102.210:8088/platform/coupon/queryBroadCouponList';
        $params = [
            'client_type'=>self::client_type,
            'couponIds'=>implode(',',$couponId),
        ];
        $params = $this->makeUrl($params);
        return $this->_sendRequest($url,$params);
    }
    
    protected function send(array $params)
    {
        $params = $this->makeUrl($params);
        $domain = $this->getDomain();
        $ret = self::_sendRequest($domain,$params,'GET');
        if (!is_array($ret) || ($ret['code'] !== 200)){
            return false;
        }
        return $ret['data'];
    }
    
    protected function makeUrl(array $package){
        $package['private_key'] = self::$privateKey[self::client_type];
        ksort($package);
        reset($package);
        
        $packageList = array();
        foreach ($package as $key => $val) {
            $packageList[] = trim($key . '=' . $val);
        }
        $client_secret = strtolower(md5(implode('&', $packageList)));
        
        $package['client_secret'] = $client_secret;
        unset($package['private_key']);        
        return $package;
    }
    
    protected function getDomain(){
        return ArrayHelper::getValue(self::$_domain, YII_ENV,self::$_domain['prod']);
    }
    
    /**
     * _sendRequest
     * @param  string $url        请求url
     * @param  array  $paramArray 请求参数
     * @param  string $method     请求方法
     * @return
     */
    protected static function _sendRequest($url, $paramArray, $method = 'POST')
    {

        $ch = curl_init();

        if ($method == 'POST')
        {
            $paramArray = is_array( $paramArray ) ? http_build_query( $paramArray ) : $paramArray;
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $paramArray);
        }
        else
        {
            $url .= '?' . http_build_query($paramArray);
        }
        
        self::$_requestUrl = $url;
        Yii::$app->response->headers->add('X-YohoApi',$url);

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT,self::$_timeOut);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        if (false !== strpos($url, "https")) {
            // 证书
            // curl_setopt($ch,CURLOPT_CAINFO,"ca.crt");
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,  false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  false);
        }
        $resultStr = curl_exec($ch);

        self::$_rawResponse = $resultStr;
        
        if (!$resultStr)
        {
            return $resultStr;
        }
        $result = json_decode($resultStr, true);
        if (!$result)
        {
            return $resultStr;
        }
        return $result;
    }
}