Commit 5572cfba by HanSon Committed by GitHub

修改分享类,增加文件存储 (#24)

* get请求设置超时

* 增加了ShareFactory,增加了File类型、Content工具类

* 优化console输出

* 修改消息存储方式

* 修复群组接收文件
1 parent 3ff71adf
......@@ -33,9 +33,7 @@ class ContactFactory
{
$url = sprintf(server()->baseUri . '/webwxgetcontact?pass_ticket=%s&skey=%s&r=%s', server()->passTicket, server()->skey, time());
$content = http()->json($url, [
'BaseRequest' => server()->baseRequest
], true);
$content = http()->json($url, [], true);
$this->makeContactList($content['MemberList']);
}
......
......@@ -9,7 +9,6 @@
namespace Hanson\Vbot\Collections;
use Hanson\Vbot\Support\Console;
use Illuminate\Support\Collection;
class Message extends Collection
......
......@@ -9,8 +9,6 @@
namespace Hanson\Vbot\Core;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Cookie\FileCookieJar;
use Hanson\Vbot\Support\Console;
class Http
......@@ -32,11 +30,15 @@ class Http
return static::$instance;
}
public function get($url, array $query = [])
public function get($url, array $query = [], array $options = [])
{
$query = $query ? ['query' => $query] : [];
if($query){
$options['query'] = $query;
}
$options['connect_timeout'] = 60;
return $this->request($url, 'GET', $query);
return $this->request($url, 'GET', $options);
}
public function post($url, $query = [], $array = false)
......@@ -88,10 +90,11 @@ class Http
$response = $this->getClient()->request($method, $url, $options);
return $response->getBody()->getContents();
}catch (\Exception $e){
Console::log('http链接失败:' . $e->getMessage());
Console::log('错误URL:' . $url);
Console::log('http链接失败:' . $e->getMessage(), Console::ERROR);
Console::log('错误URL:' . $url, Console::ERROR);
}
return null;
}
......
......@@ -17,7 +17,7 @@ use Hanson\Vbot\Message\Entity\Recall;
use Hanson\Vbot\Message\Entity\Recommend;
use Hanson\Vbot\Message\Entity\RedPacket;
use Hanson\Vbot\Message\Entity\RequestFriend;
use Hanson\Vbot\Message\Entity\Share;
use Hanson\Vbot\Message\Entity\ShareFactory;
use Hanson\Vbot\Message\Entity\Text;
use Hanson\Vbot\Message\Entity\Touch;
use Hanson\Vbot\Message\Entity\Transfer;
......@@ -27,7 +27,7 @@ use Hanson\Vbot\Message\Entity\Voice;
class MessageFactory
{
public function make($selector, $msg)
public function make($msg)
{
return $this->handleMessageByType($msg);
}
......@@ -69,7 +69,7 @@ class MessageFactory
if($msg['Status'] == 3 && $msg['FileName'] === '微信转账'){
return new Transfer($msg);
}else{
return new Share($msg);
return (new ShareFactory())->make($msg);
}
case 37: // 好友验证
return new RequestFriend($msg);
......@@ -90,5 +90,6 @@ class MessageFactory
//Unknown
break;
}
return null;
}
}
\ No newline at end of file
......@@ -9,7 +9,6 @@
namespace Hanson\Vbot\Core;
use Closure;
use Hanson\Vbot\Collections\Account;
use Hanson\Vbot\Message\Entity\Emoticon;
use Hanson\Vbot\Message\Entity\Image;
use Hanson\Vbot\Message\Entity\Message;
......@@ -130,7 +129,7 @@ class MessageHandler
list($retCode, $selector) = $this->sync->checkSync();
if(in_array($retCode, ['1100', '1101'])){ # 微信客户端上登出或者其他设备登录
Console::log('[INFO] 微信客户端正常退出');
Console::log('微信客户端正常退出');
if($this->exitHandler){
call_user_func_array($this->exitHandler, []);
}
......@@ -138,7 +137,7 @@ class MessageHandler
}elseif ($retCode == 0){
$this->handlerMessage($selector);
}else{
Console::log('[INFO] 微信客户端异常退出');
Console::log('微信客户端异常退出');
if($this->exceptionHandler){
call_user_func_array($this->exitHandler, []);
}
......@@ -147,7 +146,7 @@ class MessageHandler
$this->sync->checkTime($time);
}
Console::log('[INFO] 程序结束');
Console::log('程序结束');
}
/**
......@@ -165,7 +164,7 @@ class MessageHandler
if($message['AddMsgList']){
foreach ($message['AddMsgList'] as $msg) {
$content = $this->messageFactory->make($selector, $msg);
$content = $this->messageFactory->make($msg);
if($content){
$this->addToMessageCollection($content);
if($this->handler){
......@@ -194,7 +193,9 @@ class MessageHandler
{
message()->put($message->msg['MsgId'], $message);
file_put_contents(server()->config['tmp'].'/message.json', json_encode(message()->all()));
$file = fopen(server()->config['tmp'].'/message.json', 'a');
fwrite($file, json_encode($message) . PHP_EOL);
fclose($file);
}
}
\ No newline at end of file
......@@ -80,12 +80,12 @@ class Server
{
$this->prepare();
$this->init();
Console::log('[INFO] 初始化成功');
Console::log('初始化成功');
$this->statusNotify();
Console::log('[INFO] 开始初始化联系人');
Console::log('开始初始化联系人');
$this->initContact();
Console::log('[INFO] 初始化联系人成功');
Console::log('初始化联系人成功');
MessageHandler::getInstance()->listen();
}
......@@ -95,11 +95,11 @@ class Server
$this->getUuid();
$this->generateQrCode();
Console::showQrCode('https://login.weixin.qq.com/l/' . $this->uuid);
Console::log('[INFO] 请扫描二维码登录');
Console::log('请扫描二维码登录');
$this->waitForLogin();
$this->login();
Console::log('[INFO] 登录成功');
Console::log('登录成功');
}
/**
......@@ -120,7 +120,8 @@ class Server
preg_match('/window.QRLogin.code = (\d+); window.QRLogin.uuid = \"(\S+?)\"/', $content, $matches);
if(!$matches){
throw new \Exception('[ERROR] 获取UUID失败');
Console::log('获取UUID失败', Console::ERROR);
exit;
}
$this->uuid = $matches[2];
......@@ -164,13 +165,12 @@ class Server
$code = $matches[1];
switch($code){
case '201':
Console::log('[INFO] 请点击确认登录微信');
Console::log('请点击确认登录微信');
$tip = 0;
break;
case '200':
preg_match('/window.redirect_uri="(\S+?)";/', $content, $matches);
$this->redirectUri = $matches[1] . '&fun=new';
Console::log('登录URL:'.$this->redirectUri);
$domainList = [
'wx2.qq.com' => ['file.wx2.qq.com', 'webpush.wx2.qq.com'],
'wx.qq.com' => ['file.wx.qq.com', 'webpush.wx.qq.com'],
......@@ -188,16 +188,15 @@ class Server
break;
}
}
Console::log('url is:'. $this->baseUri);
return;
case '408':
Console::log('[ERROR] 登录超时,请重试');
Console::log('登录超时,请重试', Console::ERROR);
$tip = 1;
$retryTime -= 1;
sleep(1);
break;
default:
Console::log("[ERROR] 登录失败,错误码:$code 。请重试");
Console::log("登录失败,错误码:$code 。请重试", Console::ERROR);
$tip = 1;
$retryTime -= 1;
sleep(1);
......@@ -205,7 +204,8 @@ class Server
}
}
die('[ERROR] 登录超时,退出应用');
Console::log('登录超时,退出应用', Console::ERROR);
exit;
}
/**
......@@ -225,7 +225,8 @@ class Server
$this->passTicket = $data['pass_ticket'];
if(in_array('', [$this->skey, $this->sid, $this->uin, $this->passTicket])){
throw new \Exception('[ERROR] 登录失败');
Console::log('登录失败', Console::ERROR);
exit;
}
$this->deviceId = 'e' .substr(mt_rand().mt_rand(), 1, 15);
......@@ -256,7 +257,8 @@ class Server
$this->initContactList($result['ContactList']);
if($result['BaseResponse']['Ret'] != 0){
throw new \Exception('[ERROR] 初始化失败,链接:' . $url);
Console::log('初始化失败,链接:' . $url, Console::ERROR);
exit;
}
}
......
......@@ -32,7 +32,7 @@ class Sync
]);
try{
$content = http()->get($url, [], ['connect_timeout' => 60]);
$content = http()->get($url);
preg_match('/window.synccheck=\{retcode:"(\d+)",selector:"(\d+)"\}/', $content, $matches);
......
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2017/1/15
* Time: 12:29
*/
namespace Hanson\Vbot\Message\Entity;
use Hanson\Vbot\Message\MediaInterface;
use Hanson\Vbot\Message\MessageInterface;
use Hanson\Vbot\Message\UploadAble;
use Hanson\Vbot\Support\FileManager;
class File extends Message implements MessageInterface, MediaInterface
{
use UploadAble;
public $title;
static $folder = 'file';
public function __construct($msg)
{
parent::__construct($msg);
$this->make();
}
public function make()
{
$array = (array)simplexml_load_string($this->msg['Content'], 'SimpleXMLElement', LIBXML_NOCDATA);
$info = (array)$array['appmsg'];
$this->title = $info['title'];
$this->download();
}
public function download()
{
$url = server()->fileUri . '/webwxgetmedia';
$content = http()->get($url, [
'sender' => $this->msg['FromUserName'],
'mediaid' => $this->msg['MediaId'],
'filename' => $this->msg['FileName'],
'fromuser' => myself()->username,
'pass_ticket' => server()->passTicket,
'webwx_data_ticket' => static::getTicket()
]);
FileManager::download($this->msg['FileName'], $content, static::$folder);
}
}
\ No newline at end of file
......@@ -14,6 +14,7 @@ use Hanson\Vbot\Core\Server;
use Hanson\Vbot\Collections\Contact;
use Hanson\Vbot\Collections\Official;
use Hanson\Vbot\Collections\Special;
use Hanson\Vbot\Support\Content;
use Hanson\Vbot\Support\FileManager;
use Hanson\Vbot\Support\Console;
......@@ -56,7 +57,7 @@ class Message
$this->setFrom();
$this->setFromType();
$this->msg['Content'] = html_entity_decode($this->formatContent($this->msg['Content']));
$this->msg['Content'] = Content::formatContent($this->msg['Content']);
if($this->fromType === 'Group'){
$this->handleGroupContent($this->msg['Content']);
}
......@@ -104,12 +105,7 @@ class Message
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);
$this->msg['Content'] = Content::replaceBr($content);
}
public function __toString()
......
......@@ -21,13 +21,6 @@ class Share extends Message implements MessageInterface
public $app;
/**
* 转账金额 单位 元
*
* @var string
*/
public $fee;
public function __construct($msg)
{
parent::__construct($msg);
......
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2017/1/15
* Time: 12:29
*/
namespace Hanson\Vbot\Message\Entity;
use Hanson\Vbot\Message\MessageInterface;
use Hanson\Vbot\Support\Content;
class ShareFactory
{
protected $xml;
public $type;
public function make($msg)
{
$xml = Content::formatContent($msg['Content']);
$this->parse($xml);
if($this->type == 6){
return new File($msg);
}else{
return new Share($msg);
}
}
private function parse($xml)
{
if(starts_with($xml, '@')){
$xml = preg_replace('/(@\S+:\\n)/', '', $xml);
}
$array = (array)simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->xml = $info = (array)$array['appmsg'];
$this->type = $info['type'];
}
}
\ No newline at end of file
......@@ -9,6 +9,7 @@
namespace Hanson\Vbot\Support;
use Carbon\Carbon;
use PHPQRCode\QRcode;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Output\ConsoleOutput;
......@@ -17,14 +18,19 @@ use Symfony\Component\Console\Output\OutputInterface;
class Console
{
const INFO = 'INFO';
const WARNING = 'WARNING';
const ERROR = 'ERROR';
/**
* 输出字符串
*
* @param $str
* @param string $level
*/
public static function log($str)
public static function log($str, $level = 'INFO')
{
echo $str . PHP_EOL;
echo '[' . Carbon::now()->toDateTimeString() . ']' . "[{$level}] " . $str . PHP_EOL;
}
/**
......
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/1/24
* Time: 17:44
*/
namespace Hanson\Vbot\Support;
/**
* Content 处理类
*
* Class Content
* @package Hanson\Vbot\Support
*/
class Content
{
/**
* 格式化Content
*
* @param $content
* @return string
*/
public static function formatContent($content)
{
return self::htmlDecode(self::replaceBr($content));
}
public static function htmlDecode($content)
{
return html_entity_decode($content);
}
public static function replaceBr($content)
{
return str_replace('<br/>', "\n", $content);
}
}
\ 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!