Client.php 5.66 KB
<?php

/**
 * Created by PhpStorm.
 * User: Ziy
 * Date: 14/9/29
 * Time: 下午5:15
 */
class YHMIm_Client
{

    /**
     * 原始发送消息
     * @param array $mesData
     */
    static function send(array $mesData)
    {
 
        $amqp = new YHMIm_Relay();
        $amqp->exchange(YHMIm_Config::exchangeName);
        $mesData = YHMIm_Agreement::factory($mesData)->getQueuePackageData();
        $amqp->publish($mesData, YHMIm_Config::routeName);
        $amqp->disconnect();
    }

    /**
     * 图片消息
     * @param $fromUid
     * @param $toUid
     * @param YHMIm_MessagesParam_YHMImage $body
     * @throws Exception
     */
    static function yhmImage($fromUid, $toUid, YHMIm_MessagesParam_YHMImage $body)
    {
        $mesData = array(
            'to' => 'user:' . $toUid,
            'from' => 'user:' . $fromUid,
            'type' => 'yhm-image',
            'body' => self::checkParam($body)
        );
        self::send($mesData);
    }

    /**
     * 文字消息
     * @param $fromUid
     * @param $toUid
     * @param $text
     */
    static function yhmText($fromUid, $toUid, $text)
    { 
        $mesData = array(
            'to' => 'user:' . $toUid,
            'from' => 'user:' . $fromUid,
            'type' => 'yhm-text',
            'body' => array(
                'text' => $text
            )
        );
        self::send($mesData);
    }

    /**
     * 商品消息
     * @param $fromUid
     * @param $toUid
     * @param YHMIm_MessagesParam_YHMProduct $body
     * @throws Exception
     */
    static function yhmProduct($fromUid, $toUid, YHMIm_MessagesParam_YHMProduct $body)
    {
          
        $mesData = array(
            'to' => 'user:' . $toUid,
            'from' => 'user:' . $fromUid,
            'type' => 'yhm-product',
            'body' => self::checkParam($body)
        );
        self::send($mesData);
    }

    /**
     * 价格变更
     * @param $fromUid
     * @param $toUid
     * @param YHMIm_MessagesParam_YHMProductChangePrice $body
     * @throws Exception
     */
    static function yhmProductChangePrice($fromUid, $toUid, YHMIm_MessagesParam_YHMProductChangePrice $body)
    {
        $mesData = array(
            'to' => 'user:' . $toUid,
            'from' => 'user:' . $fromUid,
            'type' => 'yhm-product-change-price',
            'body' => self::checkParam($body)
        );
        self::send($mesData);
    }

    /**
     * 订单信息
     * @param $fromUid
     * @param $toUid
     * @param YHMIm_MessagesParam_YHMOrderInfo $body
     * @throws Exception
     */
    static function yhmOrderInfo($fromUid, $toUid, YHMIm_MessagesParam_YHMOrderInfo $body)
    {
        $mesData = array(
            'to' => 'user:' . $toUid,
            'from' => 'user:' . $fromUid,
            'type' => 'yhm-order-info',
            'body' => self::checkParam($body)
        );
        self::send($mesData);
    }

    /**
     * 关闭订单
     * @param $fromUid
     * @param $toUid
     * @param YHMIm_MessagesParam_YHMOrderClose $body
     * @throws Exception
     */
    static function yhmOrderClose($fromUid, $toUid, YHMIm_MessagesParam_YHMOrderClose $body)
    {
        $mesData = array(
            'to' => 'user:' . $toUid,
            'from' => 'user:' . $fromUid,
            'type' => 'yhm-order-close',
            'body' => self::checkParam($body)
        );
        self::send($mesData);
    }

    /**
     * 订单状态
     * @param $fromUid
     * @param $toUid
     * @param YHMIm_MessagesParam_YHMOrderStatus $body
     * @throws Exception
     */
    static function yhmOrderStatus($fromUid, $toUid, YHMIm_MessagesParam_YHMOrderStatus $body)
    {
        $mesData = array(
            'to' => 'user:' . $toUid,
            'from' => 'user:' . $fromUid,
            'type' => 'yhm-order-status',
            'body' => self::checkParam($body)
        );
        self::send($mesData);
    }
    
     /**
     * 投诉
     * @param $fromUid
     * @param $toUid
     * @param YHMIm_MessagesParam_YHMComplain $body
     * @throws Exception
     */
    static function yhmComplain($fromUid, $toUid, YHMIm_MessagesParam_YHMComplain $body)
    {
        $mesData = array(
            'to' => 'user:' . $toUid,
            'from' => 'user:' . $fromUid,
            'type' => 'yhm-order-complain',
            'body' => self::checkParam($body)
        );
        self::send($mesData);
    }

    /**
     * 降价通知
     * @param $fromUid
     * @param $toUid
     * @param YHMIm_MessagesParam_YHMProductReducePrice $body
     * @throws Exception
     */
    static function yhmProductReducePrice($fromUid, $toUid, YHMIm_MessagesParam_YHMProductReducePrice $body)
    {
        $mesData = array(
            'to' => 'user:' . $toUid,
            'from' => 'user:' . $fromUid,
            'type' => 'yhm-product-reduce-price',
            'body' => self::checkParam($body)
        );
        self::send($mesData);
    }
     /**
     * 商品消息
     * @param $fromUid
     * @param $toUid
     * @param YHMIm_MessagesParam_YHMProduct $body
     * @throws Exception
     */
    static function yhmProductTxt($fromUid, $toUid, YHMIm_MessagesParam_YHMProductTxt $body)
    {
        $mesData = array(
            'to' => 'user:' . $toUid,
            'from' => 'user:' . $fromUid,
            'type' => 'yhm-product-txt',
            'body' => self::checkParam($body)
        );
        self::send($mesData);
    }
    /**
     * 检查数据
     * @param $body
     * @throws Exception
     */
    static private function checkParam($body)
    {
        $data = get_object_vars($body);
        foreach ($data as $key => $val) {
            if (is_null($val)) {
                throw new Exception($key . ' Is Null.');
            }
        }
        return $data;
    }
}