diff --git a/catch/wechat/controller/Material.php b/catch/wechat/controller/Material.php index ef90e0a..5fd9e58 100644 --- a/catch/wechat/controller/Material.php +++ b/catch/wechat/controller/Material.php @@ -8,3 +8,41 @@ // +---------------------------------------------------------------------- // | 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) + { + + } +} \ No newline at end of file diff --git a/catch/wechat/controller/Message.php b/catch/wechat/controller/Message.php index 783c2f9..86f16e7 100644 --- a/catch/wechat/controller/Message.php +++ b/catch/wechat/controller/Message.php @@ -10,6 +10,7 @@ // +---------------------------------------------------------------------- namespace catchAdmin\wechat\controller; +use catchAdmin\wechat\library\messages\Factory; use catcher\base\CatchController; use catcher\library\WeChat; use think\Request; @@ -18,22 +19,17 @@ class Message extends CatchController { public function done(Request $request) { + $app = WeChat::officialAccount(); + if ($request->isPost()) { - WeChat::officialAccount()->server->push(function ($message) { - switch ($message['MsgType']) { - case 'subscribe': - return '收到事件消息'; - break; - case 'unsubscribe': - return '收到文字消息'; - break; - case 'image': - default: - + $app->server->push(function ($message) { + file_put_contents('root.txt', var_export($message, true), FILE_APPEND); + if ($res = Factory::make($message)->reply()) { + return $res; } - - return ''; }); } + + $app->server->serve()->send();exit; } } \ No newline at end of file diff --git a/catch/wechat/controller/Users.php b/catch/wechat/controller/Users.php index e0d6c40..0b0d6c5 100644 --- a/catch/wechat/controller/Users.php +++ b/catch/wechat/controller/Users.php @@ -35,7 +35,6 @@ class Users extends CatchController */ public function index() { - dd(WeChat::officialAccount()->server->serve()->send()); return CatchResponse::paginate($this->user->getList()); } @@ -64,11 +63,25 @@ class Users extends CatchController 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) { return CatchResponse::success($this->user->tag($id, $request->post())); } + /** + * 用户同步 + * + * @time 2020年06月26日 + * @return \think\response\Json + */ public function sync() { Console::call('sync:users'); diff --git a/catch/wechat/controller/Wechat.php b/catch/wechat/controller/Wechat.php deleted file mode 100644 index 98e70c9..0000000 --- a/catch/wechat/controller/Wechat.php +++ /dev/null @@ -1,43 +0,0 @@ -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; - } -} \ No newline at end of file diff --git a/catch/wechat/library/messages/Factory.php b/catch/wechat/library/messages/Factory.php new file mode 100644 index 0000000..edfb5fe --- /dev/null +++ b/catch/wechat/library/messages/Factory.php @@ -0,0 +1,46 @@ +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)]; + } +} \ No newline at end of file diff --git a/catch/wechat/library/messages/ShortVideo.php b/catch/wechat/library/messages/ShortVideo.php new file mode 100644 index 0000000..2a13c98 --- /dev/null +++ b/catch/wechat/library/messages/ShortVideo.php @@ -0,0 +1,20 @@ +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; + } + +} \ No newline at end of file diff --git a/catch/wechat/library/events/Unsubscribe.php b/catch/wechat/library/messages/events/Unsubscribe.php similarity index 57% rename from catch/wechat/library/events/Unsubscribe.php rename to catch/wechat/library/messages/events/Unsubscribe.php index ef90e0a..afb4065 100644 --- a/catch/wechat/library/events/Unsubscribe.php +++ b/catch/wechat/library/messages/events/Unsubscribe.php @@ -8,3 +8,22 @@ // +---------------------------------------------------------------------- // | 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(); + } +} \ No newline at end of file diff --git a/catch/wechat/library/events/View.php b/catch/wechat/library/messages/events/View.php similarity index 73% rename from catch/wechat/library/events/View.php rename to catch/wechat/library/messages/events/View.php index ef90e0a..99ffd9e 100644 --- a/catch/wechat/library/events/View.php +++ b/catch/wechat/library/messages/events/View.php @@ -8,3 +8,14 @@ // +---------------------------------------------------------------------- // | 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. + } +} \ No newline at end of file