This commit is contained in:
候学杰 2019-02-19 16:14:18 +08:00
parent 78e70a9b8a
commit ca76cbc976
3 changed files with 47 additions and 15 deletions

View File

@ -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');

View File

@ -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);
}
}

View File

@ -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;
}
}