hyperf-chat/app/Controller/Api/V1/TalkController.php

714 lines
24 KiB
PHP
Raw Normal View History

2020-11-04 11:57:16 +08:00
<?php
2020-12-26 21:33:40 +08:00
/**
* This is my open source code, please do not use it for commercial applications.
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code
*
* @author Yuandong<837215079@qq.com>
* @link https://github.com/gzydong/hyperf-chat
*/
2021-04-20 16:30:57 +08:00
2020-11-04 11:57:16 +08:00
namespace App\Controller\Api\V1;
2021-05-20 22:23:48 +08:00
use App\Cache\LastMessage;
use App\Cache\UnreadTalk;
2020-11-29 14:44:11 +08:00
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\JWTAuthMiddleware;
use Psr\Http\Message\ResponseInterface;
2020-11-21 19:53:01 +08:00
use App\Model\EmoticonDetail;
use App\Model\FileSplitUpload;
2020-11-11 22:09:23 +08:00
use App\Model\User;
use App\Model\UsersChatList;
use App\Model\UsersFriend;
use App\Model\Group\Group;
2020-11-09 22:59:25 +08:00
use App\Service\TalkService;
2020-11-21 19:53:01 +08:00
use App\Amqp\Producer\ChatMessageProducer;
2020-12-03 16:22:55 +08:00
use App\Constants\SocketConstants;
2021-06-30 19:27:49 +08:00
use League\Flysystem\Filesystem;
2020-11-04 11:57:16 +08:00
2020-11-09 22:59:25 +08:00
/**
* Class TalkController
* @Controller(path="/api/v1/talk")
* @Middleware(JWTAuthMiddleware::class)
*
* @package App\Controller\Api\V1
*/
2020-11-04 11:57:16 +08:00
class TalkController extends CController
{
2020-11-09 22:59:25 +08:00
/**
* @Inject
* @var TalkService
*/
public $talkService;
/**
* 获取用户对话列表
* @RequestMapping(path="list", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function list()
{
$user_id = $this->uid();
// 读取用户的未读消息列表
2021-05-20 22:23:48 +08:00
if ($list = UnreadTalk::getInstance()->reads($user_id)) {
$this->talkService->updateUnreadTalkList($user_id, $list);
2020-11-09 22:59:25 +08:00
}
2021-05-22 21:45:18 +08:00
return $this->response->success($this->talkService->talks($user_id));
2020-11-09 22:59:25 +08:00
}
/**
2020-11-11 22:09:23 +08:00
* 新增对话列表
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="create", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
2020-11-12 22:41:18 +08:00
public function create()
2020-11-09 22:59:25 +08:00
{
2020-11-11 22:09:23 +08:00
$params = $this->request->inputs(['type', 'receive_id']);
$this->validate($params, [
2021-04-20 16:30:57 +08:00
'type' => 'required|in:1,2',
2020-11-11 22:09:23 +08:00
'receive_id' => 'present|integer|min:0'
]);
2020-11-09 22:59:25 +08:00
2020-11-11 22:09:23 +08:00
$user_id = $this->uid();
if ($params['type'] == 1) {
if (!UsersFriend::isFriend($user_id, $params['receive_id'])) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('暂不属于好友关系,无法进行聊天!');
2020-11-09 22:59:25 +08:00
}
} else {
if (!Group::isMember($params['receive_id'], $user_id)) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('暂不属于群成员,无法进行群聊!');
2020-11-09 22:59:25 +08:00
}
}
2020-11-11 22:09:23 +08:00
$result = UsersChatList::addItem($user_id, $params['receive_id'], $params['type']);
2020-11-09 22:59:25 +08:00
if (!$result) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('创建失败!');
2020-11-09 22:59:25 +08:00
}
$data = [
2021-04-20 16:30:57 +08:00
'id' => $result['id'],
'type' => $result['type'],
'group_id' => $result['group_id'],
'friend_id' => $result['friend_id'],
'is_top' => 0,
'msg_text' => '',
2020-11-09 22:59:25 +08:00
'not_disturb' => 0,
2021-04-20 16:30:57 +08:00
'online' => 1,
'name' => '',
2020-11-09 22:59:25 +08:00
'remark_name' => '',
2021-04-20 16:30:57 +08:00
'avatar' => '',
'unread_num' => 0,
'updated_at' => date('Y-m-d H:i:s')
2020-11-09 22:59:25 +08:00
];
if ($result['type'] == 1) {
2021-04-20 19:59:39 +08:00
$userInfo = User::where('id', $user_id)->first(['nickname', 'avatar']);
2021-04-20 16:30:57 +08:00
$data['name'] = $userInfo->nickname;
$data['avatar'] = $userInfo->avatar;
2021-05-20 22:23:48 +08:00
$data['unread_num'] = UnreadTalk::getInstance()->read($result['friend_id'], $user_id);
2020-11-09 22:59:25 +08:00
} else if ($result['type'] == 2) {
2021-04-20 19:59:39 +08:00
$groupInfo = Group::where('id', $result['group_id'])->first(['group_name', 'avatar']);
2021-04-20 16:30:57 +08:00
$data['name'] = $groupInfo->group_name;
2020-11-09 22:59:25 +08:00
$data['avatar'] = $groupInfo->avatar;
}
2021-05-20 22:23:48 +08:00
$records = LastMessage::getInstance()->read(
(int)$result['type'], $user_id,
$result['type'] == 1 ? (int)$result['friend_id'] : (int)$result['group_id']
);
2020-11-09 22:59:25 +08:00
if ($records) {
2021-04-20 16:30:57 +08:00
$data['msg_text'] = $records['text'];
2020-11-09 22:59:25 +08:00
$data['updated_at'] = $records['created_at'];
}
2020-11-11 22:09:23 +08:00
return $this->response->success(['talkItem' => $data]);
2020-11-09 22:59:25 +08:00
}
/**
2020-11-11 22:09:23 +08:00
* 删除对话列表
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="delete", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function delete()
{
2020-11-11 22:09:23 +08:00
$params = $this->request->inputs(['list_id']);
$this->validate($params, [
'list_id' => 'required|integer|min:0'
]);
return UsersChatList::delItem($this->uid(), $params['list_id'])
? $this->response->success([], '对话列表删除成功...')
2021-05-13 18:01:34 +08:00
: $this->response->fail('对话列表删除失败!');
2020-11-09 22:59:25 +08:00
}
/**
2020-11-11 22:09:23 +08:00
* 对话列表置顶
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="topping", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function topping()
{
2020-11-11 22:09:23 +08:00
$params = $this->request->inputs(['list_id', 'type']);
$this->validate($params, [
'list_id' => 'required|integer|min:0',
2021-04-20 16:30:57 +08:00
'type' => 'required|in:1,2',
2020-11-11 22:09:23 +08:00
]);
return UsersChatList::topItem($this->uid(), $params['list_id'], $params['type'] == 1)
2020-11-20 19:17:11 +08:00
? $this->response->success([], '对话列表置顶(或取消置顶)成功...')
2021-05-13 18:01:34 +08:00
: $this->response->fail('对话列表置顶(或取消置顶)失败!');
2020-11-09 22:59:25 +08:00
}
/**
2020-11-11 22:09:23 +08:00
* 设置消息免打扰状态
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="set-not-disturb", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function setNotDisturb()
{
2020-11-12 22:41:18 +08:00
$params = $this->request->inputs(['receive_id', 'type', 'not_disturb']);
2020-11-11 22:09:23 +08:00
$this->validate($params, [
2021-04-20 16:30:57 +08:00
'receive_id' => 'required|integer|min:0',
'type' => 'required|in:1,2',
2020-11-11 22:09:23 +08:00
'not_disturb' => 'required|in:0,1',
]);
$isTrue = UsersChatList::notDisturbItem($this->uid(), $params['receive_id'], $params['type'], $params['not_disturb']);
return $isTrue
2020-11-21 22:47:21 +08:00
? $this->response->success([], '免打扰设置成功...')
2021-05-13 18:01:34 +08:00
: $this->response->fail('免打扰设置失败!');
2020-11-09 22:59:25 +08:00
}
/**
2020-11-11 22:09:23 +08:00
* 更新对话列表未读数
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="update-unread-num", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
2020-11-12 22:41:18 +08:00
public function updateUnreadNum()
2020-11-09 22:59:25 +08:00
{
2020-11-11 22:09:23 +08:00
$params = $this->request->inputs(['receive', 'type']);
$this->validate($params, [
'receive' => 'required|integer|min:0',
2021-04-20 16:30:57 +08:00
'type' => 'required|integer|min:0'
2020-11-11 22:09:23 +08:00
]);
2020-11-09 22:59:25 +08:00
2020-11-11 22:09:23 +08:00
// 设置好友消息未读数
if ($params['type'] == 1) {
2021-05-20 22:23:48 +08:00
UnreadTalk::getInstance()->reset((int)$params['receive'], $this->uid());
2020-11-11 22:09:23 +08:00
}
2021-05-13 18:01:34 +08:00
return $this->response->success();
2020-11-09 22:59:25 +08:00
}
/**
2020-11-12 22:41:18 +08:00
* 撤回聊天对话消息
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="revoke-records", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function revokeChatRecords()
{
2020-11-12 22:41:18 +08:00
$params = $this->request->inputs(['record_id']);
$this->validate($params, [
'record_id' => 'required|integer|min:0'
]);
2020-11-11 22:09:23 +08:00
2020-11-12 22:41:18 +08:00
[$isTrue, $message,] = $this->talkService->revokeRecord($this->uid(), $params['record_id']);
2020-11-29 14:44:11 +08:00
if ($isTrue) {
2021-05-13 18:01:34 +08:00
push_amqp(new ChatMessageProducer(SocketConstants::EVENT_REVOKE_TALK, [
'record_id' => $params['record_id']
]));
2020-11-22 23:10:00 +08:00
}
2020-11-12 22:41:18 +08:00
return $isTrue
? $this->response->success([], $message)
: $this->response->fail($message);
2020-11-09 22:59:25 +08:00
}
/**
2020-11-11 22:09:23 +08:00
* 删除聊天记录
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="remove-records", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function removeChatRecords()
{
2020-11-12 22:41:18 +08:00
$params = $this->request->inputs(['source', 'record_id', 'receive_id']);
$this->validate($params, [
2021-04-22 16:54:01 +08:00
'source' => 'required|in:1,2',// 消息来源[1:好友消息;2:群聊消息;]
2021-04-20 16:30:57 +08:00
'record_id' => 'required|ids',
2020-11-12 22:41:18 +08:00
'receive_id' => 'required|integer|min:0'
]);
2020-11-11 22:09:23 +08:00
2020-11-12 22:41:18 +08:00
$record_ids = explode(',', $params['record_id']);
2020-11-11 22:09:23 +08:00
2020-11-12 22:41:18 +08:00
$isTrue = $this->talkService->removeRecords(
$this->uid(),
$params['source'],
$params['receive_id'],
$record_ids
);
2020-11-09 22:59:25 +08:00
2020-11-12 22:41:18 +08:00
return $isTrue
? $this->response->success([], '删除成功...')
2021-05-13 18:01:34 +08:00
: $this->response->fail('删除失败!');
2020-11-09 22:59:25 +08:00
}
/**
2020-11-12 22:41:18 +08:00
* 转发聊天记录(待优化)
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="forward-records", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function forwardChatRecords()
{
2020-11-12 22:41:18 +08:00
$params = $this->request->inputs(['source', 'records_ids', 'receive_id', 'forward_mode', 'receive_user_ids', 'receive_group_ids']);
$this->validate($params, [
2021-04-22 16:54:01 +08:00
// 消息来源[1:好友消息;2:群聊消息;]
2021-04-20 16:30:57 +08:00
'source' => 'required|in:1,2',
2021-04-22 16:54:01 +08:00
// 聊天记录ID多个逗号拼接
2021-04-20 16:30:57 +08:00
'records_ids' => 'required',
2021-04-22 16:54:01 +08:00
// 接收者ID好友ID或者群聊ID
2021-04-20 16:30:57 +08:00
'receive_id' => 'required|integer|min:0',
2021-04-22 16:54:01 +08:00
// 转发方方式[1:逐条转发;2:合并转发;]
2020-11-12 22:41:18 +08:00
'forward_mode' => 'required|in:1,2',
2021-04-22 16:54:01 +08:00
// 转发的好友的ID
2020-11-29 17:39:24 +08:00
//'receive_user_ids' => 'array',
2021-04-22 16:54:01 +08:00
// 转发的群聊ID
2020-11-29 17:39:24 +08:00
//'receive_group_ids' => 'array',
2020-11-12 22:41:18 +08:00
]);
2020-11-09 22:59:25 +08:00
2020-11-21 22:47:21 +08:00
$receive_user_ids = $receive_group_ids = [];
if (isset($params['receive_user_ids']) && !empty($params['receive_user_ids'])) {
$receive_user_ids = array_map(function ($friend_id) {
2020-11-12 22:41:18 +08:00
return ['source' => 1, 'id' => $friend_id];
2020-11-21 22:47:21 +08:00
}, $params['receive_user_ids']);
}
if (isset($params['receive_group_ids']) && !empty($params['receive_group_ids'])) {
$receive_group_ids = array_map(function ($group_id) {
2020-11-12 22:41:18 +08:00
return ['source' => 2, 'id' => $group_id];
2020-11-21 22:47:21 +08:00
}, $params['receive_group_ids']);
}
2020-12-01 13:54:40 +08:00
$items = array_merge($receive_user_ids, $receive_group_ids);
2020-11-12 22:41:18 +08:00
2020-12-01 13:54:40 +08:00
$user_id = $this->uid();
2021-04-22 16:54:01 +08:00
if ($params['forward_mode'] == 1) {// 单条转发
2020-11-12 22:41:18 +08:00
$ids = $this->talkService->forwardRecords($user_id, $params['receive_id'], $params['records_ids']);
2021-04-22 16:54:01 +08:00
} else {// 合并转发
2020-11-12 22:41:18 +08:00
$ids = $this->talkService->mergeForwardRecords($user_id, $params['receive_id'], $params['source'], $params['records_ids'], $items);
}
if (!$ids) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('转发失败!');
2020-11-12 22:41:18 +08:00
}
2020-11-21 22:47:21 +08:00
if ($receive_user_ids) {
foreach ($receive_user_ids as $v) {
2021-05-20 22:23:48 +08:00
UnreadTalk::getInstance()->increment($user_id, (int)$v['id']);
2020-11-12 22:41:18 +08:00
}
}
2021-05-13 18:01:34 +08:00
// 消息推送队列
2020-11-21 22:47:21 +08:00
foreach ($ids as $value) {
2021-05-13 18:01:34 +08:00
push_amqp(new ChatMessageProducer(SocketConstants::EVENT_TALK, [
'sender' => $user_id, // 发送者ID
'receive' => intval($value['receive_id']), // 接收者ID
'source' => intval($value['source']), // 接收者类型 1:好友;2:群组
'record_id' => $value['record_id']
]));
2020-11-21 22:47:21 +08:00
}
2020-11-12 22:41:18 +08:00
return $this->response->success([], '转发成功...');
2020-11-09 22:59:25 +08:00
}
/**
2020-11-12 22:41:18 +08:00
* 获取对话面板中的聊天记录
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="records", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function getChatRecords()
{
2020-11-12 22:41:18 +08:00
$params = $this->request->inputs(['record_id', 'source', 'receive_id']);
$this->validate($params, [
2021-04-22 16:54:01 +08:00
'source' => 'required|in:1,2',// 消息来源[1:好友消息;2:群聊消息;]
2021-04-20 16:30:57 +08:00
'record_id' => 'required|integer|min:0',
2020-11-12 22:41:18 +08:00
'receive_id' => 'required|integer|min:1',
]);
$user_id = $this->uid();
2021-04-20 16:30:57 +08:00
$limit = 30;
2020-11-12 22:41:18 +08:00
// 判断是否属于群成员
if ($params['source'] == 2 && Group::isMember($params['receive_id'], $user_id) == false) {
2020-11-12 22:41:18 +08:00
return $this->response->success([
2021-04-20 16:30:57 +08:00
'rows' => [],
2020-11-12 22:41:18 +08:00
'record_id' => 0,
2021-04-20 16:30:57 +08:00
'limit' => $limit
2021-05-13 18:01:34 +08:00
], '非群聊成员不能查看群聊信息!');
2020-11-12 22:41:18 +08:00
}
2020-11-09 22:59:25 +08:00
2020-11-12 22:41:18 +08:00
$result = $this->talkService->getChatRecords(
$user_id,
$params['receive_id'],
$params['source'],
$params['record_id'],
$limit
);
return $this->response->success([
2021-04-20 16:30:57 +08:00
'rows' => $result,
2020-11-29 14:44:11 +08:00
'record_id' => $result ? end($result)['id'] : 0,
2021-04-20 16:30:57 +08:00
'limit' => $limit
2020-11-12 22:41:18 +08:00
]);
2020-11-09 22:59:25 +08:00
}
/**
2020-11-12 22:41:18 +08:00
* 获取转发记录详情
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="get-forward-records", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function getForwardRecords()
{
2020-11-12 22:41:18 +08:00
$params = $this->request->inputs(['records_id']);
$this->validate($params, [
'records_id' => 'required|integer|min:0'
]);
2020-11-09 22:59:25 +08:00
2021-04-22 16:54:01 +08:00
$rows = $this->talkService->getForwardRecords($this->uid(), $params['records_id']);
2020-11-12 22:41:18 +08:00
return $this->response->success(['rows' => $rows]);
2020-11-09 22:59:25 +08:00
}
/**
2020-11-12 22:41:18 +08:00
* 查询聊天记录
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="find-chat-records", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function findChatRecords()
{
2020-11-12 22:41:18 +08:00
$params = $this->request->inputs(['record_id', 'source', 'receive_id', 'msg_type']);
$this->validate($params, [
2021-04-22 16:54:01 +08:00
'source' => 'required|in:1,2',// 消息来源[1:好友消息;2:群聊消息;]
2021-04-20 16:30:57 +08:00
'record_id' => 'required|integer|min:0',
2020-11-12 22:41:18 +08:00
'receive_id' => 'required|integer|min:1',
2021-04-20 16:30:57 +08:00
'msg_type' => 'required|in:0,1,2,3,4,5,6',
2020-11-12 22:41:18 +08:00
]);
$user_id = $this->uid();
2021-04-20 16:30:57 +08:00
$limit = 30;
2020-11-12 22:41:18 +08:00
// 判断是否属于群成员
if ($params['source'] == 2 && Group::isMember($params['receive_id'], $user_id) == false) {
2020-11-12 22:41:18 +08:00
return $this->response->success([
2021-04-20 16:30:57 +08:00
'rows' => [],
2020-11-12 22:41:18 +08:00
'record_id' => 0,
2021-04-20 16:30:57 +08:00
'limit' => $limit
2021-05-13 18:01:34 +08:00
], '非群聊成员不能查看群聊信息!');
2020-11-12 22:41:18 +08:00
}
if (in_array($params['msg_type'], [1, 2, 4, 5])) {
$msg_type = [$params['msg_type']];
} else {
$msg_type = [1, 2, 4, 5];
}
2020-11-09 22:59:25 +08:00
2020-11-12 22:41:18 +08:00
$result = $this->talkService->getChatRecords(
$user_id,
$params['receive_id'],
$params['source'],
$params['record_id'],
$limit,
$msg_type
);
return $this->response->success([
2021-04-20 16:30:57 +08:00
'rows' => $result,
2020-11-29 14:44:11 +08:00
'record_id' => $result ? end($result)['id'] : 0,
2021-04-20 16:30:57 +08:00
'limit' => $limit
2020-11-12 22:41:18 +08:00
]);
2020-11-09 22:59:25 +08:00
}
/**
2020-11-12 22:41:18 +08:00
* 搜索聊天记录(待开发)
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="search-chat-records", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function searchChatRecords()
{
}
/**
2020-11-12 22:41:18 +08:00
* 获取聊天记录上下文数据(待开发)
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="get-records-context", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function getRecordsContext()
{
}
/**
2020-11-12 22:41:18 +08:00
* 上传聊天对话图片(待优化)
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="send-image", methods="post")
2020-11-21 19:53:01 +08:00
*
2021-06-30 19:27:49 +08:00
* @param Filesystem $filesystem
2020-11-21 19:53:01 +08:00
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
2021-06-30 19:27:49 +08:00
public function sendImage(Filesystem $filesystem)
2020-11-09 22:59:25 +08:00
{
2020-11-21 19:53:01 +08:00
$params = $this->request->inputs(['source', 'receive_id']);
$this->validate($params, [
2021-04-22 16:54:01 +08:00
'source' => 'required|in:1,2',// 消息来源[1:好友消息;2:群聊消息;]
2020-11-21 19:53:01 +08:00
'receive_id' => 'required|integer|min:1'
]);
$file = $this->request->file('img');
2021-05-28 18:38:10 +08:00
if (!$file || !$file->isValid()) {
2020-11-21 19:53:01 +08:00
return $this->response->fail();
}
$ext = $file->getExtension();
if (!in_array($ext, ['jpg', 'png', 'jpeg', 'gif', 'webp'])) {
return $this->response->fail('图片格式错误目前仅支持jpg、png、jpeg、gif和webp');
}
2021-06-30 19:27:49 +08:00
try {
$path = 'media/images/talks/' . date('Ymd') . '/' . create_image_name($ext, getimagesize($file->getRealPath()));
$filesystem->write($path, file_get_contents($file->getRealPath()));
} catch (\Exception $e) {
2020-11-21 19:53:01 +08:00
return $this->response->fail();
}
$user_id = $this->uid();
// 创建图片消息记录
$record_id = $this->talkService->createImgMessage([
2021-04-20 16:30:57 +08:00
'source' => $params['source'],
'msg_type' => 2,
'user_id' => $user_id,
2020-11-21 19:53:01 +08:00
'receive_id' => $params['receive_id'],
], [
2021-04-20 16:30:57 +08:00
'user_id' => $user_id,
'file_type' => 1,
'file_suffix' => $ext,
'file_size' => $file->getSize(),
'save_dir' => $path,
2020-11-21 19:53:01 +08:00
'original_name' => $file->getClientFilename(),
]);
if (!$record_id) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('图片上传失败!');
2020-11-21 19:53:01 +08:00
}
2021-05-13 18:01:34 +08:00
// 消息推送队列
push_amqp(new ChatMessageProducer(SocketConstants::EVENT_TALK, [
'sender' => $user_id, // 发送者ID
'receive' => intval($params['receive_id']), // 接收者ID
'source' => intval($params['source']), // 接收者类型[1:好友;2:群组;]
'record_id' => $record_id
]));
2020-11-09 22:59:25 +08:00
2021-05-20 22:23:48 +08:00
LastMessage::getInstance()->save((int)$params['source'], $user_id, (int)$params['receive_id'], [
2021-04-20 16:30:57 +08:00
'text' => '[图片消息]',
2021-01-21 20:14:06 +08:00
'created_at' => date('Y-m-d H:i:s')
2021-05-20 22:23:48 +08:00
]);
2021-01-21 20:14:06 +08:00
2020-11-21 19:53:01 +08:00
return $this->response->success();
2020-11-09 22:59:25 +08:00
}
/**
2020-11-12 22:41:18 +08:00
* 发送代码块消息
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="send-code-block", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function sendCodeBlock()
{
2020-11-21 19:53:01 +08:00
$params = $this->request->inputs(['source', 'receive_id', 'lang', 'code']);
$this->validate($params, [
2021-04-22 16:54:01 +08:00
'source' => 'required|in:1,2',// 消息来源[1:好友消息;2:群聊消息;]
2020-11-21 19:53:01 +08:00
'receive_id' => 'required|integer|min:1',
2021-04-20 16:30:57 +08:00
'lang' => 'required',
'code' => 'required'
2020-11-21 19:53:01 +08:00
]);
2021-04-20 16:30:57 +08:00
$user_id = $this->uid();
2020-11-21 19:53:01 +08:00
$record_id = $this->talkService->createCodeMessage([
2021-04-20 16:30:57 +08:00
'source' => $params['source'],
'msg_type' => 5,
'user_id' => $user_id,
2020-11-21 19:53:01 +08:00
'receive_id' => $params['receive_id'],
], [
2021-04-20 16:30:57 +08:00
'user_id' => $user_id,
2020-11-21 19:53:01 +08:00
'code_lang' => $params['lang'],
2021-04-20 16:30:57 +08:00
'code' => $params['code']
2020-11-21 19:53:01 +08:00
]);
if (!$record_id) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('消息发送失败!');
2020-11-21 19:53:01 +08:00
}
2020-11-09 22:59:25 +08:00
2021-05-13 18:01:34 +08:00
// 消息推送队列
push_amqp(new ChatMessageProducer(SocketConstants::EVENT_TALK, [
'sender' => $user_id, // 发送者ID
'receive' => intval($params['receive_id']), // 接收者ID
'source' => intval($params['source']), // 接收者类型[1:好友;2:群组;]
'record_id' => $record_id
]));
2020-11-21 19:53:01 +08:00
2021-05-20 22:23:48 +08:00
LastMessage::getInstance()->save((int)$params['source'], $user_id, (int)$params['receive_id'], [
2021-04-20 16:30:57 +08:00
'text' => '[代码消息]',
2021-01-21 20:14:06 +08:00
'created_at' => date('Y-m-d H:i:s')
2021-05-20 22:23:48 +08:00
]);
2021-01-21 20:14:06 +08:00
2020-11-21 19:53:01 +08:00
return $this->response->success();
2020-11-09 22:59:25 +08:00
}
/**
2020-11-12 22:41:18 +08:00
* 发送文件消息
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="send-file", methods="post")
2020-12-03 11:57:46 +08:00
*
2021-06-30 19:27:49 +08:00
* @param Filesystem $filesystem
2020-12-03 11:57:46 +08:00
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
2021-06-30 19:27:49 +08:00
public function sendFile(Filesystem $filesystem)
2020-11-09 22:59:25 +08:00
{
2020-11-21 19:53:01 +08:00
$params = $this->request->inputs(['hash_name', 'receive_id', 'source']);
$this->validate($params, [
2021-04-22 16:54:01 +08:00
'source' => 'required|in:1,2',// 消息来源[1:好友消息;2:群聊消息;]
2020-11-21 19:53:01 +08:00
'receive_id' => 'required|integer|min:1',
2021-04-20 16:30:57 +08:00
'hash_name' => 'required',
2020-11-21 19:53:01 +08:00
]);
$user_id = $this->uid();
$file = FileSplitUpload::where('user_id', $user_id)->where('hash_name', $params['hash_name'])->where('file_type', 1)->first();
if (!$file || empty($file->save_dir)) {
return $this->response->fail('文件不存在...');
}
2021-06-30 19:27:49 +08:00
$save_dir = "files/talks/" . date('Ymd') . '/' . create_random_filename($file->file_ext);
2020-11-21 19:53:01 +08:00
2021-06-30 19:27:49 +08:00
try {
$filesystem->copy($file->save_dir, $save_dir);
} catch (\Exception $e) {
return $this->response->fail('文件不存在...');
}
2020-11-21 19:53:01 +08:00
$record_id = $this->talkService->createFileMessage([
2021-04-20 16:30:57 +08:00
'source' => $params['source'],
'msg_type' => 2,
'user_id' => $user_id,
2020-11-21 19:53:01 +08:00
'receive_id' => $params['receive_id']
], [
2021-04-20 16:30:57 +08:00
'user_id' => $user_id,
'file_source' => 1,
'file_type' => 4,
2020-11-21 19:53:01 +08:00
'original_name' => $file->original_name,
2021-04-20 16:30:57 +08:00
'file_suffix' => $file->file_ext,
'file_size' => $file->file_size,
'save_dir' => $save_dir,
2020-11-21 19:53:01 +08:00
]);
if (!$record_id) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('表情发送失败!');
2020-11-21 19:53:01 +08:00
}
2021-05-13 18:01:34 +08:00
// 消息推送队列
push_amqp(new ChatMessageProducer(SocketConstants::EVENT_TALK, [
'sender' => $user_id, // 发送者ID
'receive' => intval($params['receive_id']), // 接收者ID
'source' => intval($params['source']), // 接收者类型[1:好友;2:群组;]
'record_id' => $record_id
]));
2020-11-09 22:59:25 +08:00
2021-05-20 22:23:48 +08:00
LastMessage::getInstance()->save((int)$params['source'], $user_id, (int)$params['receive_id'], [
2021-04-20 16:30:57 +08:00
'text' => '[文件消息]',
2021-01-21 20:14:06 +08:00
'created_at' => date('Y-m-d H:i:s')
2021-05-20 22:23:48 +08:00
]);
2021-01-21 20:14:06 +08:00
2020-11-21 19:53:01 +08:00
return $this->response->success();
2020-11-09 22:59:25 +08:00
}
/**
2020-11-12 22:41:18 +08:00
* 发送表情包消息
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="send-emoticon", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function sendEmoticon()
{
2020-11-21 19:53:01 +08:00
$params = $this->request->inputs(['source', 'receive_id', 'emoticon_id']);
$this->validate($params, [
2021-04-22 16:54:01 +08:00
'source' => 'required|in:1,2',// 消息来源[1:好友消息;2:群聊消息;]
2021-04-20 16:30:57 +08:00
'receive_id' => 'required|integer|min:1',
2020-11-21 19:53:01 +08:00
'emoticon_id' => 'required|integer|min:1',
]);
2021-04-20 16:30:57 +08:00
$user_id = $this->uid();
2020-11-21 19:53:01 +08:00
$emoticon = EmoticonDetail::where('id', $params['emoticon_id'])->where('user_id', $user_id)->first([
2020-12-03 11:57:46 +08:00
'url', 'file_suffix', 'file_size'
2020-11-21 19:53:01 +08:00
]);
if (!$emoticon) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('表情不存在!');
2020-11-21 19:53:01 +08:00
}
$record_id = $this->talkService->createEmoticonMessage([
2021-04-20 16:30:57 +08:00
'source' => $params['source'],
'msg_type' => 2,
'user_id' => $user_id,
2020-11-21 19:53:01 +08:00
'receive_id' => $params['receive_id'],
], [
2021-04-20 16:30:57 +08:00
'user_id' => $user_id,
'file_type' => 1,
'file_suffix' => $emoticon->file_suffix,
'file_size' => $emoticon->file_size,
'save_dir' => $emoticon->url,
2020-11-21 19:53:01 +08:00
'original_name' => '表情',
]);
if (!$record_id) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('表情发送失败!');
2020-11-21 19:53:01 +08:00
}
2021-05-13 18:01:34 +08:00
// 消息推送队列
push_amqp(new ChatMessageProducer(SocketConstants::EVENT_TALK, [
'sender' => $user_id, // 发送者ID
'receive' => intval($params['receive_id']), // 接收者ID
'source' => intval($params['source']), // 接收者类型[1:好友;2:群组;]
'record_id' => $record_id
]));
2020-11-04 11:57:16 +08:00
2021-05-20 22:23:48 +08:00
LastMessage::getInstance()->save((int)$params['source'], $user_id, (int)$params['receive_id'], [
2021-04-20 16:30:57 +08:00
'text' => '[表情包消息]',
2021-01-21 20:14:06 +08:00
'created_at' => date('Y-m-d H:i:s')
2021-05-20 22:23:48 +08:00
]);
2021-01-21 20:14:06 +08:00
2020-11-21 19:53:01 +08:00
return $this->response->success();
2020-11-09 22:59:25 +08:00
}
2020-11-04 11:57:16 +08:00
}