From ca76cbc976211f27efe24d7771291d6796c43b7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=80=99=E5=AD=A6=E6=9D=B0?= Date: Tue, 19 Feb 2019 16:14:18 +0800 Subject: [PATCH] message --- src/Wechat/Work/Base.php | 16 ++++--- src/Wechat/Work/Message.php | 2 +- src/Wechat/Work/Message/MessageInterface.php | 44 ++++++++++++++++---- 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/Wechat/Work/Base.php b/src/Wechat/Work/Base.php index d078bd3..537bd68 100644 --- a/src/Wechat/Work/Base.php +++ b/src/Wechat/Work/Base.php @@ -23,12 +23,17 @@ class Base ]); } - public function agent($name) + public function agent($key) { - $this->agent = $name; + $this->agent = $key; return $this; } + public function getAgentId() + { + return $this->getAgentConfig()['id']; + } + public function transformForJsonRequest($uri, array $params = null) { $uri = new Uri($uri); @@ -63,12 +68,11 @@ class Base protected function getAccessToken() { $config = $this->getAgentConfig(); - //通讯录的secret没有id - if (isset($config['id'])) { - $key = 'wx_work_access_token_' . $config['key']; + if (!isset($config['id'])) { + $key = 'wx:work:access_token:' . $config['key']; } else { - $key = 'wx_work_access_token_' . $config['id']; + $key = 'wx:work:access_token:' . $config['id']; } $cache = app('cache'); diff --git a/src/Wechat/Work/Message.php b/src/Wechat/Work/Message.php index adaf1f8..4afe4c8 100644 --- a/src/Wechat/Work/Message.php +++ b/src/Wechat/Work/Message.php @@ -20,7 +20,7 @@ class Message public function send($msg) { $request = $this->work->transformForJsonRequest('message/send', - $msg->toArray()); + $msg->toArray() + ['agentid' => $this->work->getAgentId()]); return $this->work->request($request); } } \ No newline at end of file diff --git a/src/Wechat/Work/Message/MessageInterface.php b/src/Wechat/Work/Message/MessageInterface.php index 3b0cc8f..9869fbf 100644 --- a/src/Wechat/Work/Message/MessageInterface.php +++ b/src/Wechat/Work/Message/MessageInterface.php @@ -19,18 +19,46 @@ abstract class MessageInterface protected $params; - abstract public function toArray(); - - public function ofAgent($agentId) - { - $this->params['agentid'] = $agentId; - return $this; - } - public function toUser($user) { + if (is_array($user)) { + $user = implode('|', $user); + } $this->params['touser'] = $user; return $this; } + public function toTag($tag) + { + if (is_array($tag)) { + $tag = implode('|', $tag); + } + $this->params['totag'] = $tag; + return $this; + } + + public function toArray() + { + $params = array_merge([ + 'msgtype' => $this->type, + 'safe' => $this->safe, + ], $this->params); + + switch ($this->type) { + case self::TYPE_TEXT_CARD: + $params = array_merge($params, [ + $this->type => [ + 'title' => $this->attr['title'], + 'description' => $this->attr['description'], + 'url' => $this->attr['url'], + 'btntxt' => $this->attr['btntxt'], + ], + ]); + break; + default: + throw new \Exception('message type ' . $this->type . ' is not allow'); + } + return $arr; + } + } \ No newline at end of file