diff --git a/config/wx.php b/config/wx.php index 0ec8b19..0d1ad48 100644 --- a/config/wx.php +++ b/config/wx.php @@ -5,12 +5,28 @@ return [ */ 'work' => [ //企业ID - 'corp_id' => env('WECHAT_WORK_CORP_ID'), + 'corp_id' => env('WECHAT_WORK_CORP_ID','ww9ed271946b8fc4a4'), //企业内各个应用的 secrets - 'secrets' => [ - 'miniprogram' => env('WECHAT_WORK_SECRET_MINIPROGRAM'), - 'contact' => env('WECHAT_WORK_SECRET_CONTACT'), - 'external_contact' => env('WECHAT_WORK_SECRET_EXTERNAL_CONTACT'), + 'agents' => [ + 'notify' => [ + 'id' => '1000015', + 'secret' => 'zH3N1fwrECl913R1r17MjDiNhMKSlJf2P3tSXmk_YG0', + ], +// 'miniprogram' => [ +// 'id' => '', +// 'secret' => '', +// ], +// 'contact' => [ +// 'id' => '', +// 'secret' => '', +// ], +// 'external_contact' => [ +// 'id' => '', +// 'secret' => '', +// ], +// 'miniprogram' => env('WECHAT_WORK_SECRET_MINIPROGRAM'), +// 'contact' => env('WECHAT_WORK_SECRET_CONTACT'), +// 'external_contact' => env('WECHAT_WORK_SECRET_EXTERNAL_CONTACT'), ], ] ]; \ No newline at end of file diff --git a/src/Wechat/ServerProvider/WechatServiceProvider.php b/src/Wechat/ServerProvider/WechatServiceProvider.php index c482469..d41b1bd 100644 --- a/src/Wechat/ServerProvider/WechatServiceProvider.php +++ b/src/Wechat/ServerProvider/WechatServiceProvider.php @@ -4,6 +4,7 @@ namespace Wechat\ServiceProvider; use Illuminate\Support\ServiceProvider; use Wechat\Work\Miniprogram; +use Wechat\Work\Work; class WechatServiceProvider extends ServiceProvider { @@ -29,16 +30,22 @@ class WechatServiceProvider extends ServiceProvider { $this->setupConfig(); $config = config('wx'); - if (isset($config['work'])) { - foreach ($config['work']['secrets'] as $key => $item) { - $this->app->singleton('wx.work.' . $key, function () use ($key) { - $class = ucwords($key); - return new $class ; - }); - } - } + $this->app->singleton('wx.work', function () use ($config) { + return new Work($config['work']); + }); + +// if (isset($config['work'])) { +// +// foreach ($config['work']['secrets'] as $key => $item) { +// $this->app->singleton('wx.work.' . $key, function () use ($key) { +// $class = ucwords($key); +// return new $class; +// }); +// } +// +// } } } \ No newline at end of file diff --git a/src/Wechat/Work/Base.php b/src/Wechat/Work/Base.php index f8dd6a4..d078bd3 100644 --- a/src/Wechat/Work/Base.php +++ b/src/Wechat/Work/Base.php @@ -11,16 +11,25 @@ class Base protected $client; - public function __construct() + protected $config; + protected $agent; + + public function __construct($config) { + $this->config = $config; $this->client = new Client([ 'base_uri' => 'https://qyapi.weixin.qq.com/cgi-bin/', 'timeout' => '3', -// 'proxy' => '3', ]); } - protected function transformForJsonRequest($method, $uri, array $params = null) + public function agent($name) + { + $this->agent = $name; + return $this; + } + + public function transformForJsonRequest($uri, array $params = null) { $uri = new Uri($uri); $uri = Uri::withQueryValue($uri, 'access_token', $this->getAccessToken()); @@ -30,31 +39,50 @@ class Base $body = null; } - $request = new Request($method, $uri, [], $body); + $request = new Request('POST', $uri, [], $body); return $request; } + public function transformForGetAccessToken($uri, $params) + { + $uri = new Uri($uri); + foreach ($params as $param) { + $uri = Uri::withQueryValues($uri, $params); + } + + $request = new Request('GET', $uri, []); + return $request; + } + + public function request($request) + { + $resp = $this->client->send($request); + return json_decode($resp->getBody(), true); + } + protected function getAccessToken() { - $corpId = 'ww9ed271946b8fc4a4'; - $agentId = '1000015'; - $agentSecret = 'zH3N1fwrECl913R1r17MjDiNhMKSlJf2P3tSXmk_YG0'; + $config = $this->getAgentConfig(); + + //通讯录的secret没有id + if (isset($config['id'])) { + $key = 'wx_work_access_token_' . $config['key']; + } else { + $key = 'wx_work_access_token_' . $config['id']; + } $cache = app('cache'); - - $key = 'wechat_work_access_token_' . $agentId; - $token = $cache->get($key); if ($token) { return $token; } - $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpId&corpsecret=$agentSecret"; + $request = $this->transformForGetAccessToken('gettoken', [ + 'corpid' => $config['corp_id'], + 'corpsecret' => $config['secret'] + ]); - $client = new Client(); - $resp = $client->get($url); - - $body = json_decode($resp->getBody(), true); + $body = $this->request($request); if ($body['errcode'] == 0) { $cache->put($key, $body['access_token'], $body['expires_in'] / 60); @@ -64,5 +92,15 @@ class Base } } + protected function getAgentConfig() + { + if (isset($this->config['agents'][$this->agent])) { + $config = array_merge($this->config['agents'][$this->agent], $this->config, ['key' => $this->agent]); + unset($config['agents']); + return $config; + } + return null; + } + } \ No newline at end of file diff --git a/src/Wechat/Work/Message.php b/src/Wechat/Work/Message.php index 13bb1ff..adaf1f8 100644 --- a/src/Wechat/Work/Message.php +++ b/src/Wechat/Work/Message.php @@ -2,90 +2,25 @@ namespace Wechat\Work; -abstract class Message extends Base +use Wechat\Work\MessageInterface; + +class Message { - const TYPE_TEXT = 'text'; - const TYPE_IMAGE = 'image'; - const TYPE_VOICE = 'voice'; - const TYPE_VIDEO = 'video'; - const TYPE_FILE = 'file'; - const TYPE_TEXT_CARD = 'textcard'; - const TYPE_NEWS = 'news'; - const TYPE_MP_NEWS = 'mpnews'; + /** + * @var Work + */ + protected $work; - protected $type; - protected $attr; - protected $safe; - - protected $params; - - abstract public function toArray(); - - public function ofAgent($agentId) + public function __construct($work) { - $this->params['agentid'] = $agentId; - return $this; + $this->work = $work; } - public function toUser($user) + public function send($msg) { - $this->params['touser'] = $user; - return $this; + $request = $this->work->transformForJsonRequest('message/send', + $msg->toArray()); + return $this->work->request($request); } - - public function send() - { - $request = $this->transformForJsonRequest('POST', 'message/send', - $this->toArray()); - $resp = $this->client->send($request); - return json_decode($resp->getBody(), true); - } - -// { -// switch ($this->type) { -// case self::TYPE_TEXT: -// return [ -// 'text' => [ -// 'content' => $this->attr['content'], -// ] -// ]; -// case self::TYPE_IMAGE: -// return [ -// 'image' => [ -// 'media_id' => $this->attr['media_id'], -// ] -// ]; -// case self::TYPE_VOICE: -// return [ -// 'voice' => [ -// 'media_id' => $this->attr['media_id'], -// ] -// ]; -// case self::TYPE_VIDEO: -// return [ -// 'video' => [ -// 'media_id' => $this->attr['media_id'], -// 'title' => $this->attr['title'], -// 'description' => $this->attr['description'], -// ] -// ]; -// case self::TYPE_FILE: -// return [ -// 'file' => [ -// 'media_id' => $this->attr['media_id'], -// ] -// ]; -// case self::TYPE_TEXT_CARD: -// $arr = [ -// 'textcard' => [ -// 'title' => $this->attr['title'], -// 'description' => $this->attr['description'], -// 'url' => $this->attr['url'], -// 'btntxt' => $this->attr['btntxt'], -// ] -// ]; -// } -// } - } \ No newline at end of file diff --git a/src/Wechat/Work/Message/MessageInterface.php b/src/Wechat/Work/Message/MessageInterface.php new file mode 100644 index 0000000..3b0cc8f --- /dev/null +++ b/src/Wechat/Work/Message/MessageInterface.php @@ -0,0 +1,36 @@ +params['agentid'] = $agentId; + return $this; + } + + public function toUser($user) + { + $this->params['touser'] = $user; + return $this; + } + +} \ No newline at end of file diff --git a/src/Wechat/Work/TextCardMessage.php b/src/Wechat/Work/Message/TextCardMessage.php similarity index 90% rename from src/Wechat/Work/TextCardMessage.php rename to src/Wechat/Work/Message/TextCardMessage.php index a432521..3a03a39 100644 --- a/src/Wechat/Work/TextCardMessage.php +++ b/src/Wechat/Work/Message/TextCardMessage.php @@ -1,8 +1,10 @@ type = self::TYPE_TEXT_CARD; } diff --git a/src/Wechat/Work/TextMessage.php b/src/Wechat/Work/Message/TextMessage.php similarity index 100% rename from src/Wechat/Work/TextMessage.php rename to src/Wechat/Work/Message/TextMessage.php diff --git a/src/Wechat/Work/Work.php b/src/Wechat/Work/Work.php new file mode 100644 index 0000000..b0aa858 --- /dev/null +++ b/src/Wechat/Work/Work.php @@ -0,0 +1,18 @@ +