Compare commits

...

10 Commits

Author SHA1 Message Date
董巍
1cf9ddb6df Merge branch 'feature/notify_file' into 'master'
支持文件

See merge request composer/php-wechat-sdk!2
2019-07-15 10:41:14 +08:00
侯登峰
56ea314e71 支持文件 2019-07-12 18:21:06 +08:00
董巍
98f05653ad Merge branch 'feature/media_upload' into 'master'
增效图片消息

See merge request composer/php-wechat-sdk!1
2019-07-10 17:42:30 +08:00
lisida
aec96fb36d 增效图片消息 2019-07-10 17:41:42 +08:00
候学杰
db20f3453c login 2019-06-11 16:43:08 +08:00
候学杰
f069d47bce message 2019-02-21 16:30:17 +08:00
候学杰
faa9c142ca message 2019-02-21 12:10:36 +08:00
候学杰
48a7d08bac message 2019-02-21 12:05:41 +08:00
候学杰
9caf26e7cd message 2019-02-20 19:18:30 +08:00
候学杰
2f75f83084 message 2019-02-20 16:28:32 +08:00
11 changed files with 172 additions and 43 deletions

View File

@ -5,9 +5,13 @@ return [
*/ */
'work' => [ 'work' => [
//企业ID //企业ID
'corp_id' => env('WECHAT_WORK_CORP_ID','ww9ed271946b8fc4a4'), 'corp_id' => env('WECHAT_WORK_CORP_ID', 'ww9ed271946b8fc4a4'),
//企业内各个应用的 secrets //企业内各个应用的 secrets
'agents' => [ 'agents' => [
'default' => [
'id' => env('WECHAT_WORK_AGENT_ID'),
'secret' => env('WECHAT_WORK_AGENT_SECRET'),
],
'notify' => [ 'notify' => [
'id' => '1000015', 'id' => '1000015',
'secret' => env('WECHAT_WORK_NOTIFY_SECRET'), 'secret' => env('WECHAT_WORK_NOTIFY_SECRET'),

View File

@ -1,20 +0,0 @@
<?php
namespace Wechat;
class AccessToken
{
protected $token;
public function __construct($app,string $token, $expiresIn)
{
$this->token = $token;
}
public function __toString()
{
return $this->token;
}
}

View File

@ -21,6 +21,7 @@ class Base
'base_uri' => 'https://qyapi.weixin.qq.com/cgi-bin/', 'base_uri' => 'https://qyapi.weixin.qq.com/cgi-bin/',
'timeout' => '3', 'timeout' => '3',
]); ]);
$this->agent('default');
} }
public function agent($key) public function agent($key)
@ -29,11 +30,21 @@ class Base
return $this; return $this;
} }
/**
* 获取当前使用的agent
* @return mixed
*/
public function getAgentId() public function getAgentId()
{ {
return $this->getAgentConfig()['id']; return $this->getAgentConfig()['id'];
} }
/**
* POST 请求 传递的body为json格式
* @param $uri
* @param array|null $params
* @return Request
*/
public function transformForJsonRequest($uri, array $params = null) public function transformForJsonRequest($uri, array $params = null)
{ {
$uri = new Uri($uri); $uri = new Uri($uri);
@ -48,7 +59,13 @@ class Base
return $request; return $request;
} }
public function transformForGetAccessToken($uri, $params) /**
* GET 请求,不自动附加 access token用于获取access token 接口
* @param $uri
* @param $params
* @return Request
*/
public function transformForAccessTokenRequest($uri, $params)
{ {
$uri = new Uri($uri); $uri = new Uri($uri);
foreach ($params as $param) { foreach ($params as $param) {
@ -59,6 +76,24 @@ class Base
return $request; return $request;
} }
/**
* GET 请求附带上 access token
* @param $uri
* @param $params
* @return Request
*/
public function transformForGetRequest($uri, $params)
{
$uri = new Uri($uri);
$params['access_token'] = $this->getAccessToken();
foreach ($params as $param) {
$uri = Uri::withQueryValues($uri, $params);
}
$request = new Request('GET', $uri, []);
return $request;
}
public function request($request) public function request($request)
{ {
$resp = $this->client->send($request); $resp = $this->client->send($request);
@ -81,7 +116,7 @@ class Base
return $token; return $token;
} }
$request = $this->transformForGetAccessToken('gettoken', [ $request = $this->transformForAccessTokenRequest('gettoken', [
'corpid' => $config['corp_id'], 'corpid' => $config['corp_id'],
'corpsecret' => $config['secret'] 'corpsecret' => $config['secret']
]); ]);

View File

@ -4,18 +4,20 @@ namespace Wechat\Work;
use Wechat\Work\Message\MessageInterface; use Wechat\Work\Message\MessageInterface;
class Chat extends Base class Chat
{ {
protected $id; protected $id;
protected $work;
public function __construct($id = null) public function __construct($id, $work)
{ {
parent::__construct(); $this->work = $work;
$this->id = $id; $this->id = $id;
} }
/** /**
* 发送消息
* @param $msg MessageInterface * @param $msg MessageInterface
* @return mixed * @return mixed
*/ */
@ -58,14 +60,6 @@ class Chat extends Base
return json_decode($resp->getBody(), true); return json_decode($resp->getBody(), true);
} }
public function send(Message $message)
{
$request = $this->transformForJsonRequest('POST', 'appchat/send',
array_merge(['chatid' => $this->id], $message->toArray()));
$resp = $this->client->send($request);
return json_decode($resp->getBody(), true);
}
public function update($chatId, array $add = null, array $del = null, $name = null, $owner = null) public function update($chatId, array $add = null, array $del = null, $name = null, $owner = null)
{ {
$params = [ $params = [

View File

@ -4,7 +4,7 @@ namespace App\Wechat;
use App\Wechat\Work\Base; use App\Wechat\Work\Base;
class ExternalContact extends Base class ExternalContact
{ {
public function __construct() public function __construct()
{ {

View File

@ -16,7 +16,7 @@ abstract class MessageInterface
protected $type; protected $type;
protected $params; protected $params = [];
protected $attr; protected $attr;
public function toUser($user) public function toUser($user)
@ -67,6 +67,14 @@ abstract class MessageInterface
$this->type => $this->attr, $this->type => $this->attr,
]); ]);
break; break;
case self::TYPE_FILE:
case self::TYPE_IMAGE:
$params = array_merge($params, [
$this->type => [
'media_id' => $this->attr['media_id'],
],
]);
break;
default: default:
throw new \Exception('message type ' . $this->type . ' is not allow'); throw new \Exception('message type ' . $this->type . ' is not allow');
} }

View File

@ -0,0 +1,16 @@
<?php
namespace Wechat\Work\Message;
use Wechat\Work\Message\MessageInterface;
class TextFileMessage extends MessageInterface
{
public function __construct($content)
{
$this->type = self::TYPE_FILE;
$this->attr['media_id'] = $content;
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Wechat\Work\Message;
use Wechat\Work\Message\MessageInterface;
class TextImageMessage extends MessageInterface
{
public function __construct($content)
{
$this->type = self::TYPE_IMAGE;
$this->attr['media_id'] = $content;
}
}

View File

@ -3,16 +3,26 @@
namespace Wechat\Work; namespace Wechat\Work;
class Miniprogram extends Base class Miniprogram
{ {
protected $app = 'miniprogram';
public function code2Session($type = 'authorization_code') /**
* @var Work
*/
protected $work;
public function __construct($work)
{ {
$url = "https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session?js_code=$code&grant_type=$type"); $this->work = $work;
$request = $this->client->transformForJsonRequest($url); }
$resp = $this->client->send($request);
return json_decode($resp->getBody(), true); public function code2Session($code, $type = 'authorization_code')
{
$request = $this->work->transformForGetRequest('miniprogram/jscode2session', [
'js_code' => $code,
'grant_type' => $type,
]);
return $this->work->request($request);
} }
} }

35
src/Wechat/Work/User.php Normal file
View File

@ -0,0 +1,35 @@
<?php
namespace Wechat\Work;
class User
{
/**
* @var Work
*/
protected $work;
public function __construct($work)
{
$this->work = $work;
}
public function getUserByCode($code)
{
$request = $this->work->transformForGetRequest('user/getuserinfo', [
'code' => $code,
]);
return $this->work->request($request);
}
public function get($id)
{
$request = $this->work->transformForGetRequest('user/get', [
'userid' => $id,
]);
return $this->work->request($request);
}
}

View File

@ -10,9 +10,40 @@ class Work extends Base
parent::__construct($config); parent::__construct($config);
} }
/**
* 消息推送
* @return Message
*/
public function message() public function message()
{ {
return new Message($this); return new Message($this);
} }
/**
* 群聊接口
* @param string $id 群id
*/
public function chat($id)
{
return new Chat($id, $this);
}
/**
* 小程序接口
* @return Miniprogram
*/
public function miniProgram()
{
return new Miniprogram($this);
}
/**
* 用户接口
* @return User
*/
public function user()
{
return new User($this);
}
} }