Skip to content

Commit

Permalink
修改分享类,增加文件存储 (Hanson#24)
Browse files Browse the repository at this point in the history
* get请求设置超时

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

* 优化console输出

* 修改消息存储方式

* 修复群组接收文件
  • Loading branch information
Hanson authored Jan 26, 2017
1 parent 3ff71ad commit 5572cfb
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 51 deletions.
4 changes: 1 addition & 3 deletions src/Collections/ContactFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public function getContacts()
{
$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']);
}
Expand Down
1 change: 0 additions & 1 deletion src/Collections/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace Hanson\Vbot\Collections;


use Hanson\Vbot\Support\Console;
use Illuminate\Support\Collection;

class Message extends Collection
Expand Down
17 changes: 10 additions & 7 deletions src/Core/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,11 +30,15 @@ public static function getInstance()
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)
Expand Down Expand Up @@ -88,10 +90,11 @@ public function request($url, $method = 'GET', $options = [])
$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;
}


Expand Down
7 changes: 4 additions & 3 deletions src/Core/MessageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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;
Expand All @@ -27,7 +27,7 @@
class MessageFactory
{

public function make($selector, $msg)
public function make($msg)
{
return $this->handleMessageByType($msg);
}
Expand Down Expand Up @@ -69,7 +69,7 @@ private function handleMessageByType($msg)
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);
Expand All @@ -90,5 +90,6 @@ private function handleMessageByType($msg)
//Unknown
break;
}
return null;
}
}
13 changes: 7 additions & 6 deletions src/Core/MessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -130,15 +129,15 @@ public function listen()
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, []);
}
break;
}elseif ($retCode == 0){
$this->handlerMessage($selector);
}else{
Console::log('[INFO] 微信客户端异常退出');
Console::log('微信客户端异常退出');
if($this->exceptionHandler){
call_user_func_array($this->exitHandler, []);
}
Expand All @@ -147,7 +146,7 @@ public function listen()

$this->sync->checkTime($time);
}
Console::log('[INFO] 程序结束');
Console::log('程序结束');
}

/**
Expand All @@ -165,7 +164,7 @@ private function handlerMessage($selector)

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){
Expand Down Expand Up @@ -194,7 +193,9 @@ private function addToMessageCollection($message)
{
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);
}

}
30 changes: 16 additions & 14 deletions src/Core/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ public function run()
{
$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();
}
Expand All @@ -95,11 +95,11 @@ public function prepare()
$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('登录成功');
}

/**
Expand All @@ -120,7 +120,8 @@ protected function getUuid()
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];
Expand Down Expand Up @@ -164,13 +165,12 @@ protected function waitForLogin()
$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'],
Expand All @@ -188,24 +188,24 @@ protected function waitForLogin()
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);
break;
}
}

die('[ERROR] 登录超时,退出应用');
Console::log('登录超时,退出应用', Console::ERROR);
exit;
}

/**
Expand All @@ -225,7 +225,8 @@ public function login()
$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);
Expand Down Expand Up @@ -256,7 +257,8 @@ protected function init($first = true)
$this->initContactList($result['ContactList']);

if($result['BaseResponse']['Ret'] != 0){
throw new \Exception('[ERROR] 初始化失败,链接:' . $url);
Console::log('初始化失败,链接:' . $url, Console::ERROR);
exit;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Core/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function checkSync()
]);

try{
$content = http()->get($url, [], ['connect_timeout' => 60]);
$content = http()->get($url);

preg_match('/window.synccheck=\{retcode:"(\d+)",selector:"(\d+)"\}/', $content, $matches);

Expand Down
56 changes: 56 additions & 0 deletions src/Message/Entity/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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);
}
}
10 changes: 3 additions & 7 deletions src/Message/Entity/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;

Expand Down Expand Up @@ -56,7 +57,7 @@ public function __construct($msg)
$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']);
}
Expand Down Expand Up @@ -104,12 +105,7 @@ private function handleGroupContent($content)
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()
Expand Down
7 changes: 0 additions & 7 deletions src/Message/Entity/Share.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ class Share extends Message implements MessageInterface

public $app;

/**
* 转账金额 单位 元
*
* @var string
*/
public $fee;

public function __construct($msg)
{
parent::__construct($msg);
Expand Down
Loading

0 comments on commit 5572cfb

Please sign in to comment.