Esempio n. 1
0
 /**
  * 获取自动回复消息
  * @method autoReply
  * @since 0.0.1
  * @param {string} [$messageClass] 调用公众号消息类回复(优先级最高), 否则默认规则回复
  * @return {array}
  * @example $this->autoReply($messageClass);
  */
 public function autoReply($messageClass = null)
 {
     if ($messageClass) {
         return ($message = static::findOne($messageClass::reply($this->id))) && $message->pid == $this->id ? $message->replyFormat : null;
     }
     $reply = [];
     switch ($this->msg_type) {
         case 'text':
             $reply = WechatMessageRule::keywords($this->appid, $this->content);
             break;
         case 'event':
             switch ($this->event) {
                 case 'unsubscribe':
                     break;
                 case 'subscribe':
                     $reply = WechatMessageRule::beAdded($this->appid);
                     break;
             }
             break;
     }
     if ($reply) {
         $message = new static();
         foreach ($reply as $k => $v) {
             $message[$k] = $v;
         }
         $message->appid = $this->appid;
         $message->type = 2;
         $message->pid = $this->id;
         $message->to_user_name = $this->from_user_name;
         $message->from_user_name = $this->to_user_name;
         if ($message->save()) {
             return $message->replyFormat;
         }
     }
     return null;
 }
 /**
  * 获取消息回复规则
  * @method getRule
  * @since 0.0.1
  * @return {object}
  * @example $this->getRule();
  */
 public function getRule()
 {
     return $this->hasOne(WechatMessageRule::classname(), ['id' => 'rule_id']);
 }