Ejemplo n.º 1
0
 /**
  * 按类型分发消息给子类执行响应
  * @return Wechat_OutMessage实例
  */
 public function run()
 {
     // 未通过消息真假性验证
     if (!$this->debug && (!$this->isValid() || !$this->validateSignature($token))) {
         throw new PhalApi_Exception_BadRequest(T('wrong token'));
     }
     $inMessage = new Wechat_InMessage();
     $outMessage = null;
     switch ($inMessage->getMsgType()) {
         case Wechat_InMessage::MSG_TYPE_EVENT:
             $this->handleEvent($inMessage, $outMessage);
             break;
         case Wechat_InMessage::MSG_TYPE_TEXT:
             $this->handleText($inMessage, $outMessage);
             break;
         case Wechat_InMessage::MSG_TYPE_IMAGE:
             $this->handleImage($inMessage, $outMessage);
             break;
         case Wechat_InMessage::MSG_TYPE_VOICE:
             $this->handleVoice($inMessage, $outMessage);
             break;
         case Wechat_InMessage::MSG_TYPE_VIDEO:
             $this->handleVideo($inMessage, $outMessage);
             break;
         case Wechat_InMessage::MSG_TYPE_LOCATION:
             $this->handleLocation($inMessage, $outMessage);
             break;
         case Wechat_InMessage::MSG_TYPE_LINK:
             $this->handleLink($inMessage, $outMessage);
             break;
         case Wechat_InMessage::MSG_TYPE_DEVICE_EVENT:
             $this->handleDeviceEvent($inMessage, $outMessage);
             break;
         case Wechat_InMessage::MSG_TYPE_DEVICE_TEXT:
             $this->handleDeviceText($inMessage, $outMessage);
             break;
         default:
             break;
     }
     if ($outMessage !== null) {
         $outMessage->setFromUserName($inMessage->getFromUserName());
         $outMessage->setToUserName($inMessage->getToUserName());
     }
     return $outMessage;
 }
Ejemplo n.º 2
0
 public function response()
 {
     try {
         $rs = $this->run();
         if ($rs === NULL) {
             throw new PhalApi_Exception_BadRequest(T('coming soon!'));
         }
         return $rs;
     } catch (PhalApi_Exception $ex) {
         $inMessage = new Wechat_InMessage();
         $outMessage = new Wechat_OutMessage_Text();
         $outMessage->setFromUserName($inMessage->getFromUserName());
         $outMessage->setToUserName($inMessage->getToUserName());
         $outMessage->setContent($ex->getMessage());
         return $outMessage;
     } catch (Exception $ex) {
         DI()->logger->error('Wechat Lite caught an exception', $ex->getMessage());
         throw new $ex();
     }
 }
    public function testVoiceMsg()
    {
        $GLOBALS['HTTP_RAW_POST_DATA'] = '<xml><ToUserName><![CDATA[gh_43235ff1360f]]></ToUserName>
<FromUserName><![CDATA[oWNXvjipYqRViMpO8GZwXxE43pUY]]></FromUserName>
<CreateTime>1419759468</CreateTime>
<MsgType><![CDATA[voice]]></MsgType>
<MediaId><![CDATA[fwGXgqFETvBRyh1LHivKGd0juSiXUDvFzzzoKyNgKO_oyU6dD_CrB3mFQGt2BcQN]]></MediaId>
<Format><![CDATA[amr]]></Format>
<MsgId>6097820483449399071</MsgId>
<Recognition><![CDATA[]]></Recognition>
</xml>';
        $wechatInMessage = new Wechat_InMessage();
        $this->assertEquals(Wechat_InMessage::MSG_TYPE_VOICE, $wechatInMessage->getMsgType());
    }