Skip to content

Commit

Permalink
内部调用支持
Browse files Browse the repository at this point in the history
  • Loading branch information
asundust committed Feb 10, 2021
1 parent a5f478d commit 7980842
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=测试内容`
地址为`http://{www.abc.com}/push/secretSecret?title=测试标题&content=测试内容`

## 内部调用支持

- 引用此Trait类`\Asundust\WechatWorkPush\Http\Traits\SendMessageTrait`
- 使用默认配置发送`defaultSend()`,使用自定配置发送`send()`,具体入参看方法。
3 changes: 3 additions & 0 deletions src/Http/Controllers/WechatWorkPushHandleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
37 changes: 32 additions & 5 deletions src/Http/Traits/SendMessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}

0 comments on commit 7980842

Please sign in to comment.