php-wechat-sdk/src/Wechat/Work/Chat.php

91 lines
2.0 KiB
PHP
Raw Normal View History

2019-01-29 05:56:25 +00:00
<?php
2019-02-20 07:59:57 +00:00
namespace Wechat\Work;
use Wechat\Work\Message\MessageInterface;
2019-01-29 05:56:25 +00:00
2019-02-20 08:28:32 +00:00
class Chat
2019-01-29 05:56:25 +00:00
{
protected $id;
2019-02-20 08:28:32 +00:00
protected $work;
2019-01-29 05:56:25 +00:00
2019-02-20 08:28:32 +00:00
public function __construct($id, $work)
2019-01-29 05:56:25 +00:00
{
2019-02-20 08:28:32 +00:00
$this->work = $work;
2019-01-29 05:56:25 +00:00
$this->id = $id;
}
2019-02-20 07:59:57 +00:00
/**
2019-02-20 08:28:32 +00:00
* 发送消息
2019-02-20 07:59:57 +00:00
* @param $msg MessageInterface
* @return mixed
*/
public function send($msg)
{
$request = $this->work->transformForJsonRequest('appchat/send',
array_merge($msg->toArray(), [
'chatid' => $this->id
]));
return $this->work->request($request);
}
2019-01-29 05:56:25 +00:00
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 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);
}
}