This commit is contained in:
候学杰 2019-01-29 13:56:25 +08:00
parent cce729e027
commit 6028eeb034
12 changed files with 411 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea

15
composer.json Normal file
View File

@ -0,0 +1,15 @@
{
"name": "paidian/weichat",
"type": "library",
"description": "weichat library.",
"require": {
"guzzlehttp/guzzle": "^6.3",
},
"autoload": {
"psr-4": {
"Wechat\\": "src/Wechat"
}
},
"require-dev": {
}
}

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

@ -0,0 +1 @@
<?php

62
src/Wechat/Work/Base.php Normal file
View File

@ -0,0 +1,62 @@
<?php
namespace Wechat\Work;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Uri;
class Base
{
protected $client;
public function __construct()
{
$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)
{
$uri = new Uri($uri);
$uri = Uri::withQueryValue($uri, 'access_token', $this->getAccessToken());
$request = new Request($method, $uri, [], \GuzzleHttp\json_encode($params));
return $request;
}
protected function getAccessToken()
{
$corpId = 'ww9ed271946b8fc4a4';
$agentId = '1000015';
$agentSecret = 'zH3N1fwrECl913R1r17MjDiNhMKSlJf2P3tSXmk_YG0';
$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";
$client = new Client();
$resp = $client->get($url);
$body = json_decode($resp->getBody(), true);
if ($body['errcode'] == 0) {
$cache->put($key, $body['access_token'], $body['expires_in'] / 60);
return $body['access_token'];
} else {
return null;
}
}
}

81
src/Wechat/Work/Chat.php Normal file
View File

@ -0,0 +1,81 @@
<?php
namespace App\Wechat\Work;
class Chat extends Base
{
protected $id;
public function __construct($id = null)
{
parent::__construct();
$this->id = $id;
}
public function create(array $users, $owner = null, $name = null)
{
$params = [
'userlist' => $users,
];
if ($name) {
$params['name'] = $name;
}
if ($owner) {
$params['owner'] = $owner;
}
if ($this->id != null) {
$params['chatid'] = $this->id;
}
$request = $this->transformForJsonRequest('POST', 'appchat/create', $params);
$resp = $this->client->send($request);
return json_decode($resp->getBody(), true);
}
public function get($chatId)
{
$request = $this->transformForJsonRequest('GET', 'appchat/get?chatid=' . $chatId);
$resp = $this->client->send($request);
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 = [
'chatid' => $chatId,
];
if ($name) {
$params['name'] = $name;
}
if ($owner) {
$params['owner'] = $owner;
}
if ($add) {
$params['add_user_list'] = $add;
}
if ($del) {
$params['del_user_list'] = $del;
}
$request = $this->transformForJsonRequest('POST', 'appchat/update', $params);
$resp = $this->client->send($request);
return json_decode($resp->getBody(), true);
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Wechat;
use App\Wechat\Work\Base;
class ExternalContact extends Base
{
public function __construct()
{
parent::__construct();
}
public function list($uid)
{
$request = $this->transformForJsonRequest('GET', 'crm/get_external_contact_list?userid='.$uid);
$resp = $this->client->send($request);
return json_decode($resp->getBody(), true);
}
public function detail($uid){
$request = $this->transformForJsonRequest('GET', 'crm/get_external_contact?external_userid='.$uid);
$resp = $this->client->send($request);
return json_decode($resp->getBody(), true);
}
}

View File

@ -0,0 +1,91 @@
<?php
namespace Wechat\Work;
abstract class Message extends Base
{
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;
}
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,62 @@
<?php
namespace Wechat\Work;
class TextCardMessage extends Message
{
protected $type;
protected $attr;
public function __construct()
{
parent::__construct();
$this->type = self::TYPE_TEXT_CARD;
}
public function title($title)
{
$this->attr['title'] = $title;
return $this;
}
public function desc($desc)
{
$this->attr['description'] = $desc;
return $this;
}
public function button($text)
{
$this->attr['btntxt'] = $text;
return $this;
}
public function url($url)
{
$this->attr['url'] = $url;
return $this;
}
public function toArray()
{
$arr = [
'msgtype' => $this->type,
$this->type => [
'title' => $this->attr['title'],
'description' => $this->attr['description'],
'url' => $this->attr['url'],
'btntxt' => $this->attr['btntxt'],
],
'safe' => $this->safe,
];
if (!empty($this->params)) {
$arr = array_merge($this->params, $arr);
}
return $arr;
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Wechat\Work;
class TextMessage extends Message
{
protected $type;
protected $attr;
public function __construct($content)
{
parent::__construct();
$this->type = self::TYPE_TEXT;
$this->attr['content'] = $content;
}
public function toArray()
{
$arr = [
'msgtype' => $this->type,
$this->type => [
'content' => $this->attr['content'],
],
'safe' => $this->safe,
];
if (!empty($this->params)) {
$arr = array_merge($this->params, $arr);
}
return $arr;
}
}

16
src/config.php Normal file
View File

@ -0,0 +1,16 @@
<?php
return [
/**
* 企业微信的配置
*/
'work' => [
//企业ID
'corp_id' => '',
//企业内各个应用的 secrets
'secrets' => [
'auth' => '',
'contact' => env('WECHAT_WORK_SECRET_CONTACT'),
'external_contact' => env('WECHAT_WORK_SECRET_EXTERNAL_CONTACT'),
],
]
];

0
tests/1.php Normal file
View File