Compare commits

..

No commits in common. "1cf9ddb6df458292f67e63917244cdabedca20b1" and "06d17d9766a54da71c02afe601a4a36b69714c24" have entirely different histories.

11 changed files with 43 additions and 172 deletions

View File

@ -8,10 +8,6 @@ return [
'corp_id' => env('WECHAT_WORK_CORP_ID','ww9ed271946b8fc4a4'),
//企业内各个应用的 secrets
'agents' => [
'default' => [
'id' => env('WECHAT_WORK_AGENT_ID'),
'secret' => env('WECHAT_WORK_AGENT_SECRET'),
],
'notify' => [
'id' => '1000015',
'secret' => env('WECHAT_WORK_NOTIFY_SECRET'),

View File

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

View File

@ -4,20 +4,18 @@ namespace Wechat\Work;
use Wechat\Work\Message\MessageInterface;
class Chat
class Chat extends Base
{
protected $id;
protected $work;
public function __construct($id, $work)
public function __construct($id = null)
{
$this->work = $work;
parent::__construct();
$this->id = $id;
}
/**
* 发送消息
* @param $msg MessageInterface
* @return mixed
*/
@ -60,6 +58,14 @@ class Chat
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)
{
$params = [

View File

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

View File

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

View File

@ -1,16 +0,0 @@
<?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

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

View File

@ -1,35 +0,0 @@
<?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,40 +10,9 @@ class Work extends Base
parent::__construct($config);
}
/**
* 消息推送
* @return Message
*/
public function message()
{
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);
}
}