新增事件消息

This commit is contained in:
JaguarJack 2020-06-26 22:08:01 +08:00
parent a232f555c3
commit bcd7215333
19 changed files with 420 additions and 57 deletions

View File

@ -8,3 +8,41 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ] // | Author: JaguarJack [ njphper@gmail.com ]
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace catchAdmin\wechat\controller;
use catchAdmin\wechat\repository\WechatUsersRepository;
use catcher\base\CatchController;
use catcher\base\CatchRequest;
use catcher\CatchResponse;
use catcher\library\WeChat;
use catcher\Utils;
use think\facade\Console;
use think\Request;
class Material extends CatchController
{
public function index(CatchRequest $request)
{
}
public function read($id)
{
}
public function save(CatchRequest $request)
{
}
public function update($id, Request $request)
{
}
public function delete($id)
{
}
}

View File

@ -10,6 +10,7 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace catchAdmin\wechat\controller; namespace catchAdmin\wechat\controller;
use catchAdmin\wechat\library\messages\Factory;
use catcher\base\CatchController; use catcher\base\CatchController;
use catcher\library\WeChat; use catcher\library\WeChat;
use think\Request; use think\Request;
@ -18,22 +19,17 @@ class Message extends CatchController
{ {
public function done(Request $request) public function done(Request $request)
{ {
$app = WeChat::officialAccount();
if ($request->isPost()) { if ($request->isPost()) {
WeChat::officialAccount()->server->push(function ($message) { $app->server->push(function ($message) {
switch ($message['MsgType']) { file_put_contents('root.txt', var_export($message, true), FILE_APPEND);
case 'subscribe': if ($res = Factory::make($message)->reply()) {
return '收到事件消息'; return $res;
break;
case 'unsubscribe':
return '收到文字消息';
break;
case 'image':
default:
} }
return '';
}); });
} }
$app->server->serve()->send();exit;
} }
} }

View File

@ -35,7 +35,6 @@ class Users extends CatchController
*/ */
public function index() public function index()
{ {
dd(WeChat::officialAccount()->server->serve()->send());
return CatchResponse::paginate($this->user->getList()); return CatchResponse::paginate($this->user->getList());
} }
@ -64,11 +63,25 @@ class Users extends CatchController
return CatchResponse::success($this->user->block($id)); return CatchResponse::success($this->user->block($id));
} }
/**
* 贴标签
*
* @time 2020年06月26日
* @param $id
* @param Request $request
* @return \think\response\Json
*/
public function tag($id, Request $request) public function tag($id, Request $request)
{ {
return CatchResponse::success($this->user->tag($id, $request->post())); return CatchResponse::success($this->user->tag($id, $request->post()));
} }
/**
* 用户同步
*
* @time 2020年06月26日
* @return \think\response\Json
*/
public function sync() public function sync()
{ {
Console::call('sync:users'); Console::call('sync:users');

View File

@ -1,43 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\wechat\controller;
use catcher\base\CatchController;
use think\facade\Log;
use think\Request;
use catcher\library\WeChat as WechatServer;
class Wechat extends CatchController
{
public function index(Request $request)
{
$app = WechatServer::officialAccount();
$app->server->push(function ($message) {
switch ($message['MsgType']) {
case 'event':
file_put_contents(base_path() . DIRECTORY_SEPARATOR .'root.txt', json_encode((array)$message));
return '收到事件消息了吗😄';
break;
case 'text':
return '收到文字消息';
break;
case 'image':
break;
default:
}
return '结束了';
});
$app->server->serve()->send();exit;
}
}

View File

@ -0,0 +1,46 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\wechat\library\messages;
class Factory
{
/**
* 对象生产
*
* @time 2020年06月26日
* @param $message
* @return mixed
*/
public static function make($message)
{
return self::parse($message);
}
/**
* 解析
*
* @time 2020年06月26日
* @param $message
* @return mixed
*/
protected static function parse($message)
{
// 事件类型
if ($message['MsgType'] == 'event') {
$event = __NAMESPACE__ . '\\events\\' . ucfirst($message['Event']);
return new $event($message);
}
$messageClass = __NAMESPACE__ . '\\' . ucfirst($message['MsgType']);
return new $messageClass($message);
}
}

View File

@ -8,3 +8,12 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ] // | Author: JaguarJack [ njphper@gmail.com ]
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace catchAdmin\wechat\library\messages;
class Image extends Message
{
public function reply()
{
// TODO: Implement reply() method.
}
}

View File

@ -8,3 +8,12 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ] // | Author: JaguarJack [ njphper@gmail.com ]
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace catchAdmin\wechat\library\messages;
class Link extends Message
{
public function reply()
{
// TODO: Implement reply() method.
}
}

View File

@ -0,0 +1,19 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\wechat\library\messages;
class Location extends Message
{
public function reply()
{
// TODO: Implement reply() method.
}
}

View File

@ -0,0 +1,74 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\wechat\library\messages;
use think\helper\Str;
abstract class Message
{
protected $message;
public function __construct(array $message)
{
$this->message = $message;
}
/**
* 接收方账号
*
* @time 2020年06月26日
* @return mixed
*/
protected function toUserName()
{
return $this->message['ToUserName'];
}
/**
* 发送方账号
*
* @time 2020年06月26日
* @return mixed
*/
protected function fromUserName()
{
return $this->message['FromUserName'];
}
abstract public function reply();
/**
* 访问消息内容
*
* @time 2020年06月26日
* @param $name
* @return mixed
*/
public function __get($name)
{
// TODO: Implement __get() method.
return $this->message[Str::camel($name)];
}
/**
* 访问消息内容
*
* @time 2020年06月26日
* @param $name
* @param $arguments
* @return mixed
*/
public function __call($name, $arguments)
{
// TODO: Implement __call() method.
return $this->message[lcfirst($name)];
}
}

View File

@ -0,0 +1,20 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\wechat\library\messages;
class ShortVideo extends Message
{
public function reply()
{
// TODO: Implement reply() method.
}
}

View File

@ -8,3 +8,12 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ] // | Author: JaguarJack [ njphper@gmail.com ]
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace catchAdmin\wechat\library\messages;
class Text extends Message
{
public function reply()
{
// TODO: Implement reply() method.
}
}

View File

@ -8,3 +8,12 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ] // | Author: JaguarJack [ njphper@gmail.com ]
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace catchAdmin\wechat\library\messages;
class Video extends Message
{
public function reply()
{
// TODO: Implement reply() method.
}
}

View File

@ -0,0 +1,19 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\wechat\library\messages;
class Voice extends Message
{
public function reply()
{
// TODO: Implement reply() method.
}
}

View File

@ -0,0 +1,21 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\wechat\library\messages\events;
use catchAdmin\wechat\library\messages\Message;
class Click extends Message
{
public function reply()
{
// TODO: Implement reply() method.
}
}

View File

@ -0,0 +1,21 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\wechat\library\messages\events;
use catchAdmin\wechat\library\messages\Message;
class Location extends Message
{
public function reply()
{
// TODO: Implement reply() method.
}
}

View File

@ -0,0 +1,21 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\wechat\library\messages\events;
use catchAdmin\wechat\library\messages\Message;
class Scan extends Message
{
public function reply()
{
// TODO: Implement reply() method.
}
}

View File

@ -0,0 +1,52 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\wechat\library\messages\events;
use catchAdmin\wechat\library\messages\Message;
use catchAdmin\wechat\model\WechatUsers;
use catcher\library\WeChat;
/**
* 订阅事件
*
* Class Subscribe
* @package catchAdmin\wechat\library\messages\events
*/
class Subscribe extends Message
{
public function reply()
{
// TODO: Implement reply() method.
$wechatUser = WechatUsers::onlyTrashed()->where('openid', $this->fromUserName())->find();
if ($wechatUser) {
return $wechatUser->restore();
}
$user = WeChat::officialAccount()->user->get($this->fromUserName());
$user['avatar'] = $user['headimgurl'];
$user['unionid'] = $user['unionid'] ?? '';
$user['created_at'] = time();
$user['updated_at'] = time();
if (!empty($user['tagid_list'])) {
$user['tagid_list'] = trim(implode(',', $user['tagid_list']), ',');
}
unset($user['headimgurl'], $user['qr_scene'], $user['qr_scene_str']);
if (app(WechatUsers::class)->storeBy($user)) {
return '谢谢你的关注';
}
return false;
}
}

View File

@ -8,3 +8,22 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ] // | Author: JaguarJack [ njphper@gmail.com ]
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace catchAdmin\wechat\library\messages\events;
use catchAdmin\wechat\library\messages\Message;
use catchAdmin\wechat\model\WechatUsers;
/**
* 取消订阅事件
*
* Class Unsubscribe
* @package catchAdmin\wechat\library\messages\events
*/
class Unsubscribe extends Message
{
public function reply()
{
// TODO: Implement reply() method.
WechatUsers::where('openid', $this->fromUserName())->find()->delete();
}
}

View File

@ -8,3 +8,14 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ] // | Author: JaguarJack [ njphper@gmail.com ]
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace catchAdmin\wechat\library\messages\events;
use catchAdmin\wechat\library\messages\Message;
class View extends Message
{
public function reply()
{
// TODO: Implement reply() method.
}
}