From 7980842c91901c91fc32a544c267482f10c5e556 Mon Sep 17 00:00:00 2001 From: asundust Date: Wed, 10 Feb 2021 09:35:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=85=E9=83=A8=E8=B0=83=E7=94=A8=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 +++- .../WechatWorkPushHandleController.php | 3 ++ src/Http/Traits/SendMessageTrait.php | 37 ++++++++++++++++--- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 97130be..6cc1ea1 100644 --- a/README.md +++ b/README.md @@ -94,4 +94,9 @@ php artisan admin:import wechat-work-push - 默认路由支持`get`和`post`,记得在`VerifyCsrfToken`里的`except`添加`push/*`,以便支持`post`接口请求。 - 接口地址为`http://{www.abc.com}/push/{推送密钥}`,标题为`title`不可控,内容为`content`可不传。 示例:`get` - 地址为`http://{www.abc.com}/push/secretSecret?title=测试标题&content=测试内容` \ No newline at end of file + 地址为`http://{www.abc.com}/push/secretSecret?title=测试标题&content=测试内容` + +## 内部调用支持 + +- 引用此Trait类`\Asundust\WechatWorkPush\Http\Traits\SendMessageTrait`。 +- 使用默认配置发送`defaultSend()`,使用自定配置发送`send()`,具体入参看方法。 \ No newline at end of file diff --git a/src/Http/Controllers/WechatWorkPushHandleController.php b/src/Http/Controllers/WechatWorkPushHandleController.php index 912d507..846a838 100644 --- a/src/Http/Controllers/WechatWorkPushHandleController.php +++ b/src/Http/Controllers/WechatWorkPushHandleController.php @@ -40,6 +40,9 @@ public function push($secret, Request $request): array ]; } else { $config = WechatWorkPushConfig::firstOrNew([]); + if (!$config->is_complete) { + return ['code' => 1, 'message' => '系统配置错误']; + } $config = [ 'corp_id' => $config->corp_id, 'agent_id' => $config->agent_id, diff --git a/src/Http/Traits/SendMessageTrait.php b/src/Http/Traits/SendMessageTrait.php index 57d8b9b..30ac17f 100644 --- a/src/Http/Traits/SendMessageTrait.php +++ b/src/Http/Traits/SendMessageTrait.php @@ -2,20 +2,23 @@ namespace Asundust\WechatWorkPush\Http\Traits; +use Asundust\WechatWorkPush\Models\WechatWorkPushConfig; use EasyWeChat\Factory; trait SendMessageTrait { /** - * @param array $config - * @param string $name - * @param string $title - * @param string|null $content + * 使用自定配置发送消息 + * + * @param array $config 配置 ['corp_id' => 'xxx', 'agent_id' => 'xxx', 'secret' => 'xxx']; + * @param string $name 用户 + * @param string $title 标题 + * @param string|null $content 内容 * @return mixed * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException */ - public function send(array $config, string $name, string $title, ?string $content = null) + public function send(array $config, string $name, string $title, ?string $content = null): array { $message = $title; if ($content) { @@ -28,4 +31,28 @@ public function send(array $config, string $name, string $title, ?string $conten } return ['code' => 1, 'message' => $result['errmsg'], 'original' => app()->isLocal() ? $result : []]; } + + /** + * 使用默认配置发送消息 + * + * @param string $name 用户 + * @param string $title 标题 + * @param string|null $content 内容 + * @return array + * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException + * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException + */ + public function defaultSend(string $name, string $title, ?string $content = null): array + { + $config = WechatWorkPushConfig::firstOrNew([]); + if (!$config->is_complete) { + return ['code' => 1, 'message' => '系统配置错误']; + } + $config = [ + 'corp_id' => $config->corp_id, + 'agent_id' => $config->agent_id, + 'secret' => $config->secret, + ]; + return $this->send($config, $name, $title, $content); + } } \ No newline at end of file