Message.php 3.15 KB
<?php
/**
 * Created by PhpStorm.
 * User: Hanson
 * Date: 2016/12/15
 * Time: 0:12
 */

namespace Hanson\Vbot\Message\Entity;


use Carbon\Carbon;
use Hanson\Vbot\Core\Server;
use Hanson\Vbot\Collections\Contact;
use Hanson\Vbot\Collections\Official;
use Hanson\Vbot\Collections\SpecialAccount;
use Hanson\Vbot\Support\FileManager;
use Hanson\Vbot\Support\Console;
use Hanson\Vbot\Support\ObjectAble;

class Message
{

    /**
     * @var array 消息来源
     */
    public $from;

    /**
     * @var array 当from为群组时,sender为用户发送者
     */
    public $sender;

    /**
     * @var array 消息接收者
     */
    public $to;

    /**
     * @var string 经过处理的内容
     */
    public $content;

    /**
     * @var Carbon 时间
     */
    public $time;

    /**
     * @var string 消息发送者类型
     */
    public $fromType;

    public $msg;

    static $mediaCount = -1;

    public function __construct($msg)
    {
        $this->msg = $msg;

        $this->setFrom();
        $this->setTo();
        $this->setFromType();

        $this->msg['Content'] = html_entity_decode($this->formatContent($this->msg['Content']));
        if($this->fromType === 'Group'){
            $this->handleGroupContent($this->msg['Content']);
        }

        $this->time = $msg['CreateTime'];
    }

    /**
     * 设置消息发送者
     */
    private function setFrom()
    {
        $this->from = account()->getAccount($this->msg['FromUserName']);
    }

    private function setTo()
    {
        $this->to = account()->getAccount($this->msg['ToUserName']);
    }

    private function setFromType()
    {
        if ($this->msg['MsgType'] == 51) {
            $this->fromType = 'System';
        } elseif ($this->msg['MsgType'] == 37) {
            $this->fromType = 'FriendRequest';
        } elseif ($this->msg['FromUserName'] === myself()->username) {
            $this->fromType = 'Self';
        } elseif ($this->msg['ToUserName'] === 'filehelper') {
            $this->fromType = 'FileHelper';
        } elseif (substr($this->msg['FromUserName'], 0, 2) === '@@') { # group
            $this->fromType = 'Group';
        } elseif (contact()->getContactByUsername($this->msg['FromUserName'])) {
            $this->fromType = 'Contact';
        } elseif (Official::getInstance()->get($this->msg['FromUserName'])) {
            $this->fromType = 'Official';
        } elseif (SpecialAccount::getInstance()->get($this->msg['FromUserName'], false)) {
            $this->fromType = 'Special';
        } else {
            $this->fromType = 'Unknown';
        }
    }

    /**
     * 处理群发消息的内容
     *
     * @param $content string 内容
     */
    private function handleGroupContent($content)
    {
        if(!$content || !str_contains($content, ":\n")){
            return;
        }
        list($uid, $content) = explode(":\n", $content, 2);

        $this->sender = account()->getAccount($uid);
        $this->msg['Content'] = $this->formatContent($content);
    }

    protected function formatContent($content)
    {
        return str_replace('<br/>', "\n", $content);
    }

    public function __toString()
    {
        return $this->content;
    }

}