diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0724ff2 --- /dev/null +++ b/composer.json @@ -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": { + } +} diff --git a/src/Wechat/AccessToken.php b/src/Wechat/AccessToken.php new file mode 100644 index 0000000..39c0fa6 --- /dev/null +++ b/src/Wechat/AccessToken.php @@ -0,0 +1,20 @@ +token = $token; + } + + public function __toString() + { + return $this->token; + } + +} \ No newline at end of file diff --git a/src/Wechat/ServerProvider/WechatServiceProvider.php b/src/Wechat/ServerProvider/WechatServiceProvider.php new file mode 100644 index 0000000..a814366 --- /dev/null +++ b/src/Wechat/ServerProvider/WechatServiceProvider.php @@ -0,0 +1 @@ +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; + } + } + + +} \ No newline at end of file diff --git a/src/Wechat/Work/Chat.php b/src/Wechat/Work/Chat.php new file mode 100644 index 0000000..4596a51 --- /dev/null +++ b/src/Wechat/Work/Chat.php @@ -0,0 +1,81 @@ +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); + } + +} diff --git a/src/Wechat/Work/ExternalContact.php b/src/Wechat/Work/ExternalContact.php new file mode 100644 index 0000000..a2c2104 --- /dev/null +++ b/src/Wechat/Work/ExternalContact.php @@ -0,0 +1,27 @@ +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); + } + +} \ No newline at end of file diff --git a/src/Wechat/Work/Message.php b/src/Wechat/Work/Message.php new file mode 100644 index 0000000..13bb1ff --- /dev/null +++ b/src/Wechat/Work/Message.php @@ -0,0 +1,91 @@ +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'], +// ] +// ]; +// } +// } + +} \ No newline at end of file diff --git a/src/Wechat/Work/TextCardMessage.php b/src/Wechat/Work/TextCardMessage.php new file mode 100644 index 0000000..a432521 --- /dev/null +++ b/src/Wechat/Work/TextCardMessage.php @@ -0,0 +1,62 @@ +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; + } + +} \ No newline at end of file diff --git a/src/Wechat/Work/TextMessage.php b/src/Wechat/Work/TextMessage.php new file mode 100644 index 0000000..fe43b13 --- /dev/null +++ b/src/Wechat/Work/TextMessage.php @@ -0,0 +1,35 @@ +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; + } + +} \ No newline at end of file diff --git a/src/config.php b/src/config.php new file mode 100644 index 0000000..ae0cb6a --- /dev/null +++ b/src/config.php @@ -0,0 +1,16 @@ + [ + //企业ID + 'corp_id' => '', + //企业内各个应用的 secrets + 'secrets' => [ + 'auth' => '', + 'contact' => env('WECHAT_WORK_SECRET_CONTACT'), + 'external_contact' => env('WECHAT_WORK_SECRET_EXTERNAL_CONTACT'), + ], + ] +]; \ No newline at end of file diff --git a/tests/1.php b/tests/1.php new file mode 100644 index 0000000..e69de29