This commit is contained in:
候学杰 2019-02-19 15:58:00 +08:00
parent d49cfce08c
commit 78e70a9b8a
8 changed files with 160 additions and 109 deletions

View File

@ -5,12 +5,28 @@ return [
*/ */
'work' => [ 'work' => [
//企业ID //企业ID
'corp_id' => env('WECHAT_WORK_CORP_ID'), 'corp_id' => env('WECHAT_WORK_CORP_ID','ww9ed271946b8fc4a4'),
//企业内各个应用的 secrets //企业内各个应用的 secrets
'secrets' => [ 'agents' => [
'miniprogram' => env('WECHAT_WORK_SECRET_MINIPROGRAM'), 'notify' => [
'contact' => env('WECHAT_WORK_SECRET_CONTACT'), 'id' => '1000015',
'external_contact' => env('WECHAT_WORK_SECRET_EXTERNAL_CONTACT'), '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'),
], ],
] ]
]; ];

View File

@ -4,6 +4,7 @@ namespace Wechat\ServiceProvider;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Wechat\Work\Miniprogram; use Wechat\Work\Miniprogram;
use Wechat\Work\Work;
class WechatServiceProvider extends ServiceProvider class WechatServiceProvider extends ServiceProvider
{ {
@ -29,16 +30,22 @@ class WechatServiceProvider extends ServiceProvider
{ {
$this->setupConfig(); $this->setupConfig();
$config = config('wx'); $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;
// });
// }
//
// }
} }
} }

View File

@ -11,16 +11,25 @@ class Base
protected $client; protected $client;
public function __construct() protected $config;
protected $agent;
public function __construct($config)
{ {
$this->config = $config;
$this->client = new Client([ $this->client = new Client([
'base_uri' => 'https://qyapi.weixin.qq.com/cgi-bin/', 'base_uri' => 'https://qyapi.weixin.qq.com/cgi-bin/',
'timeout' => '3', '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 = new Uri($uri);
$uri = Uri::withQueryValue($uri, 'access_token', $this->getAccessToken()); $uri = Uri::withQueryValue($uri, 'access_token', $this->getAccessToken());
@ -30,31 +39,50 @@ class Base
$body = null; $body = null;
} }
$request = new Request($method, $uri, [], $body); $request = new Request('POST', $uri, [], $body);
return $request; 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() protected function getAccessToken()
{ {
$corpId = 'ww9ed271946b8fc4a4'; $config = $this->getAgentConfig();
$agentId = '1000015';
$agentSecret = 'zH3N1fwrECl913R1r17MjDiNhMKSlJf2P3tSXmk_YG0'; //通讯录的secret没有id
if (isset($config['id'])) {
$key = 'wx_work_access_token_' . $config['key'];
} else {
$key = 'wx_work_access_token_' . $config['id'];
}
$cache = app('cache'); $cache = app('cache');
$key = 'wechat_work_access_token_' . $agentId;
$token = $cache->get($key); $token = $cache->get($key);
if ($token) { if ($token) {
return $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(); $body = $this->request($request);
$resp = $client->get($url);
$body = json_decode($resp->getBody(), true);
if ($body['errcode'] == 0) { if ($body['errcode'] == 0) {
$cache->put($key, $body['access_token'], $body['expires_in'] / 60); $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;
}
} }

View File

@ -2,90 +2,25 @@
namespace Wechat\Work; namespace Wechat\Work;
abstract class Message extends Base use Wechat\Work\MessageInterface;
class Message
{ {
const TYPE_TEXT = 'text'; /**
const TYPE_IMAGE = 'image'; * @var Work
const TYPE_VOICE = 'voice'; */
const TYPE_VIDEO = 'video'; protected $work;
const TYPE_FILE = 'file';
const TYPE_TEXT_CARD = 'textcard';
const TYPE_NEWS = 'news';
const TYPE_MP_NEWS = 'mpnews';
protected $type; public function __construct($work)
protected $attr;
protected $safe;
protected $params;
abstract public function toArray();
public function ofAgent($agentId)
{ {
$this->params['agentid'] = $agentId; $this->work = $work;
return $this;
} }
public function toUser($user) public function send($msg)
{ {
$this->params['touser'] = $user; $request = $this->work->transformForJsonRequest('message/send',
return $this; $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'],
// ]
// ];
// }
// }
} }

View File

@ -0,0 +1,36 @@
<?php
namespace Wechat\Work\Message;
abstract class MessageInterface
{
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';
protected $type;
protected $attr;
protected $safe;
protected $params;
abstract public function toArray();
public function ofAgent($agentId)
{
$this->params['agentid'] = $agentId;
return $this;
}
public function toUser($user)
{
$this->params['touser'] = $user;
return $this;
}
}

View File

@ -1,8 +1,10 @@
<?php <?php
namespace Wechat\Work; namespace Wechat\Work\Message;
class TextCardMessage extends Message use Wechat\Work\Message\MessageInterface;
class TextCardMessage extends MessageInterface
{ {
@ -11,7 +13,6 @@ class TextCardMessage extends Message
public function __construct() public function __construct()
{ {
parent::__construct();
$this->type = self::TYPE_TEXT_CARD; $this->type = self::TYPE_TEXT_CARD;
} }

18
src/Wechat/Work/Work.php Normal file
View File

@ -0,0 +1,18 @@
<?php
namespace Wechat\Work;
;
class Work extends Base
{
public function __construct($config)
{
parent::__construct($config);
}
public function message()
{
return new Message($this);
}
}