Commit 4fb2b794 by hanccc

设置from

1 parent 3ec1bce2
...@@ -37,6 +37,11 @@ class Account extends Collection ...@@ -37,6 +37,11 @@ class Account extends Collection
return static::$instance; return static::$instance;
} }
/**
* 增加群聊天
*
* @param $groupMember
*/
public function addGroupMember($groupMember) public function addGroupMember($groupMember)
{ {
$account = static::$instance->all(); $account = static::$instance->all();
...@@ -46,6 +51,11 @@ class Account extends Collection ...@@ -46,6 +51,11 @@ class Account extends Collection
static::$instance->make($account); static::$instance->make($account);
} }
/**
* 增加联系人聊天
*
* @param $normalMember
*/
public function addNormalMember($normalMember) public function addNormalMember($normalMember)
{ {
$account = static::$instance->all(); $account = static::$instance->all();
...@@ -55,6 +65,14 @@ class Account extends Collection ...@@ -55,6 +65,14 @@ class Account extends Collection
static::$instance->make($account); static::$instance->make($account);
} }
/**
* 获取联系人名称
*
* @param string $id
* @param string $type 群或者联系人
* @param bool $prefer 返回最佳名称或名称数组
* @return array|null
*/
public function getContactName($id, $type, $prefer = false) public function getContactName($id, $type, $prefer = false)
{ {
$target = static::$instance->get($type); $target = static::$instance->get($type);
...@@ -75,7 +93,21 @@ class Account extends Collection ...@@ -75,7 +93,21 @@ class Account extends Collection
return null; return null;
} }
return $prefer ? array_values($name)[0] : $name; return $prefer ? current($name) : $name;
}
/**
* 获取联系人
*
* @param $id
* @param string $type 类型
* @return array
*/
public function getContact($id, $type)
{
$target = static::$instance->get($type);
return $target[$id] ?? null;
} }
} }
\ No newline at end of file \ No newline at end of file
...@@ -91,7 +91,7 @@ class MessageHandler ...@@ -91,7 +91,7 @@ class MessageHandler
$message = $this->sync(); $message = $this->sync();
foreach ($message['AddMsgList'] as $msg) { foreach ($message['AddMsgList'] as $msg) {
Log::echo((new Message)->make($selector, $msg)->content->msg);
} }
Log::echo(json_encode($message)); Log::echo(json_encode($message));
......
...@@ -11,8 +11,8 @@ namespace Hanson\Robot\Core; ...@@ -11,8 +11,8 @@ namespace Hanson\Robot\Core;
use Endroid\QrCode\QrCode; use Endroid\QrCode\QrCode;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Hanson\Robot\Models\ContactFactory; use Hanson\Robot\Collections\ContactFactory;
use Hanson\Robot\Models\GroupAccount; use Hanson\Robot\Collections\GroupAccount;
use Hanson\Robot\Support\Log; use Hanson\Robot\Support\Log;
use QueryPath\Exception; use QueryPath\Exception;
use Symfony\Component\DomCrawler\Crawler; use Symfony\Component\DomCrawler\Crawler;
...@@ -220,6 +220,7 @@ class Server ...@@ -220,6 +220,7 @@ class Server
static::$myAccount = $result['User']; static::$myAccount = $result['User'];
if($result['BaseResponse']['Ret'] != 0){ if($result['BaseResponse']['Ret'] != 0){
file_put_contents($this->config['tmp'] . 'debug.json', $content);
throw new Exception('[ERROR] init fail!'); throw new Exception('[ERROR] init fail!');
} }
} }
......
...@@ -12,4 +12,15 @@ namespace Hanson\Robot\Message; ...@@ -12,4 +12,15 @@ namespace Hanson\Robot\Message;
class Location class Location
{ {
public static function isLocation($content)
{
return str_contains('webwxgetpubliclinkimg', $content);
}
public static function getLocationText($content)
{
$result = explode('<br/>', $content);
return current(array_slice($result, -2, 1));
}
} }
\ No newline at end of file \ No newline at end of file
...@@ -10,21 +10,26 @@ namespace Hanson\Robot\Message; ...@@ -10,21 +10,26 @@ namespace Hanson\Robot\Message;
use Hanson\Robot\Core\Server; use Hanson\Robot\Core\Server;
use Hanson\Robot\Models\Account; use Hanson\Robot\Collections\Account;
use Hanson\Robot\Models\ContactAccount; use Hanson\Robot\Collections\ContactAccount;
use Hanson\Robot\Models\OfficialAccount; use Hanson\Robot\Collections\OfficialAccount;
use Hanson\Robot\Models\SpecialAccount; use Hanson\Robot\Collections\SpecialAccount;
use Hanson\Robot\Models\Content;
use Hanson\Robot\Models\Sender;
class Message class Message
{ {
/** /**
* @ * @var Sender
*/ */
public $sender; public $from;
public $receiver; public $to;
/**
* @var Content
*/
public $content; public $content;
public $time; public $time;
...@@ -55,70 +60,121 @@ class Message ...@@ -55,70 +60,121 @@ class Message
$this->rawMsg = $msg; $this->rawMsg = $msg;
if ($msg['MsgType'] == 51) { // $this->sender = new Sender();
$this->sender->name = 'system'; // $this->content = new Content();
$this->sender->type = 0;
} elseif ($msg['MsgType'] == 37) {
$this->sender->type = 37;
} elseif (Server::isMyself($msg['FromUserName'])) {
$this->sender->name = 'self';
$this->sender->type = 1;
} elseif ($msg['ToUserName'] === 'filehelper') {
$this->sender->name = 'file_helper';
$this->sender->type = 2;
} elseif (substr($msg['FromUserName'], 0, 2) === '@@') {
$this->sender->name = Account::getInstance()->getContactName($msg['FromUserName'], Account::NORMAL_MEMBER, true);
$this->sender->type = 3;
} elseif (ContactAccount::getInstance()->isContact($msg['FromUserName'])) {
$this->sender->name = Account::getInstance()->getContactName($msg['FromUserName'], Account::NORMAL_MEMBER, true);
$this->sender->type = 4;
} elseif (OfficialAccount::getInstance()->isPublic($msg['FromUserName'])) {
$this->sender->name = Account::getInstance()->getContactName($msg['FromUserName'], Account::NORMAL_MEMBER, true);
$this->sender->type = 5;
} elseif (SpecialAccount::getInstance()->get($msg['FromUserName'], false)) {
$this->sender->name = Account::getInstance()->getContactName($msg['FromUserName'], Account::NORMAL_MEMBER, true);
$this->sender->type = 6;
} else {
$this->sender->name = 'unknown';
$this->sender->type = 99;
}
$this->sender->name = html_entity_decode($this->sender->name); $this->setSender();
$this->setContent();
return $this;
}
/**
* 设置消息发送者
*/
private function setSender()
{
$account = Account::getInstance();
$from = $this->rawMsg['FromUserName'];
$fromType = substr($this->rawMsg['FromUserName'], 0, 2) === '@@' ? Account::GROUP_MEMBER : Account::NORMAL_MEMBER;
$this->from = $account->getContact($from, $fromType);
// if ($this->rawMsg['MsgType'] == 51) {
// $this->sender->name = 'system';
// $this->sender->type = 'System';
// } elseif ($this->rawMsg['MsgType'] == 37) {
// $this->sender->type = 'FriendRequest';
// } elseif (Server::isMyself($this->rawMsg['FromUserName'])) {
// $this->sender->name = 'self';
// $this->sender->type = 'Self';
// } elseif ($this->rawMsg['ToUserName'] === 'filehelper') {
// $this->sender->name = 'file_helper';
// $this->sender->type = 'FileHelper';
// } elseif (substr($this->rawMsg['FromUserName'], 0, 2) === '@@') { # group
// $this->sender->name = $account->getContactName($this->rawMsg['FromUserName'], Account::GROUP_MEMBER, true);
// $this->sender->type = 'Group';
// $this->sender->group = $account->getContact($this->rawMsg['FromUserName'], Account::GROUP_MEMBER);
// } elseif (ContactAccount::getInstance()->isContact($this->rawMsg['FromUserName'])) {
// $this->sender->name = $account->getContactName($this->rawMsg['FromUserName'], Account::NORMAL_MEMBER, true);
// $this->sender->type = 'Contact';
// } elseif (OfficialAccount::getInstance()->isPublic($this->rawMsg['FromUserName'])) {
// $this->sender->name = $account->getContactName($this->rawMsg['FromUserName'], Account::NORMAL_MEMBER, true);
// $this->sender->type = 'Public';
// } elseif (SpecialAccount::getInstance()->get($this->rawMsg['FromUserName'], false)) {
// $this->sender->name = $account->getContactName($this->rawMsg['FromUserName'], Account::NORMAL_MEMBER, true);
// $this->sender->type = 'Special';
// } else {
// $this->sender->name = 'unknown';
// $this->sender->type = 'Unknown';
// }
//
// if($this->sender->type !== 'Group'){
// $this->sender->from = $account->getContact($this->rawMsg['FromUserName'], Account::NORMAL_MEMBER);
// }
//
// $this->sender->name = html_entity_decode($this->sender->name);
}
private function setContent()
{
$this->handleContent(); $this->handleContent();
} }
private function handleContent() private function handleContent()
{ {
// $msgType = $msg['MsgType']; // $msgType = $msg['MsgType'];
$content = html_entity_decode($this->rawMsg['Content']); $this->rawMsg['Content'] = html_entity_decode($this->rawMsg['Content']);
// $msgId = $msg['MsgId']; // $msgId = $msg['MsgId'];
$this->handleContentByType($content); $this->handleContentByType();
$this->handleMessageByType(); $this->handleMessageByType();
} }
private function handleContentByType($content) /**
* 根据消息来源处理消息
*/
private function handleContentByType()
{ {
if($this->sender->type === 0){ if($this->sender->type === 'System'){
$this->type = 'Empty'; $this->content->type = 'Empty';
}elseif ($this->sender->type === 2){ }elseif ($this->sender->type === 'FileHelper'){ # File Helper
$this->type = 'Text'; $this->content->type = 'Text';
$this->content = $this->formatContent($content); $this->content->msg = $this->formatContent($this->rawMsg['Content']);
}elseif ($this->sender->type === 3){ }elseif ($this->sender->type === 'Group'){ # group
$this->handleGroupContent($content); $this->handleGroupContent($this->rawMsg['Content']);
} }
} }
/**
* 处理消息类型
*/
private function handleMessageByType() private function handleMessageByType()
{ {
if($this->rawMsg['MsgType'] == 1){ if($this->rawMsg['MsgType'] == 1){
if(Location::isLocation($this->rawMsg['Content'])){
$this->setLocationMessage();
}else{
}
} }
} }
/** /**
* 设置当前message 为 location
*/
private function setLocationMessage()
{
$this->type = 'Location';
$this->url = $this->rawMsg['Url'];
$this->content->msg = Location::getLocationText($this->rawMsg['Content']);
}
/**
* handle group content * handle group content
* *
* @param $content * @param $content
...@@ -128,7 +184,7 @@ class Message ...@@ -128,7 +184,7 @@ class Message
list($uid, $content) = explode('<br/>', $content, 2); list($uid, $content) = explode('<br/>', $content, 2);
$this->sender->user = Account::getInstance()->get('normalMember')[substr($uid, 0, -1)]; $this->sender->user = Account::getInstance()->get('normalMember')[substr($uid, 0, -1)];
$this->content = $this->formatContent($content); $this->content->msg = $this->formatContent($content);
} }
private function formatContent($content) private function formatContent($content)
......
...@@ -12,4 +12,8 @@ namespace Hanson\Robot\Message; ...@@ -12,4 +12,8 @@ namespace Hanson\Robot\Message;
class Text class Text
{ {
public static function make($msg)
{
}
} }
\ No newline at end of file \ No newline at end of file
...@@ -12,4 +12,9 @@ namespace Hanson\Robot\Models; ...@@ -12,4 +12,9 @@ namespace Hanson\Robot\Models;
class Content class Content
{ {
public $type;
public $msg;
public $raw;
} }
\ No newline at end of file \ No newline at end of file
...@@ -12,4 +12,13 @@ namespace Hanson\Robot\Models; ...@@ -12,4 +12,13 @@ namespace Hanson\Robot\Models;
class Origin class Origin
{ {
public $type;
public $id;
const GROUP = 'Group';
const CONTACT = 'Contact';
} }
\ No newline at end of file \ No newline at end of file
...@@ -12,4 +12,19 @@ namespace Hanson\Robot\Models; ...@@ -12,4 +12,19 @@ namespace Hanson\Robot\Models;
class Sender class Sender
{ {
/**
* @var String 消息来源名称
*/
public $name;
public $type;
public $from;
public $to;
/**
* @var array 显示的聊天窗口
*/
public $contact;
} }
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!