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

310 lines
9.5 KiB
PHP
Raw Normal View History

2021-01-28 20:07:14 +08:00
<?php
/**
* 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
*/
namespace App\Controller\Api\V1;
use App\Model\UsersFriendsApply;
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;
use Hyperf\Amqp\Producer;
use App\Amqp\Producer\ChatMessageProducer;
use App\Service\ContactsService;
use App\Service\SocketClientService;
use App\Service\UserService;
use App\Cache\ApplyNumCache;
use App\Cache\FriendRemarkCache;
use App\Model\UsersChatList;
use App\Constants\SocketConstants;
/**
* Class ContactsController
* @Controller(path="/api/v1/contacts")
* @Middleware(JWTAuthMiddleware::class)
*
* @package App\Controller\Api\V1
*/
class ContactsController extends CController
{
/**
* @Inject
* @var ContactsService
*/
private $contactsService;
/**
* @inject
* @var SocketClientService
*/
private $socketClientService;
/**
* @Inject
* @var Producer
*/
private $producer;
/**
* 获取用户联系人列表
* @RequestMapping(path="list", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
public function getContacts()
{
$rows = $this->contactsService->getContacts($this->uid());
if ($rows) {
$runArr = $this->socketClientService->getServerRunIdAll();
foreach ($rows as $k => $row) {
// 查询用户当前是否在线
$rows[$k]['online'] = $this->socketClientService->isOnlineAll($row['id'], $runArr);
}
}
return $this->response->success($rows);
}
/**
* 添加联系人
* @RequestMapping(path="add", methods="post")
*
* @param UserService $userService
* @return ResponseInterface
*/
public function addContact(UserService $userService)
{
$params = $this->request->inputs(['friend_id', 'remarks']);
$this->validate($params, [
'friend_id' => 'required|integer',
2021-04-20 16:30:57 +08:00
'remarks' => 'present|max:50'
2021-01-28 20:07:14 +08:00
]);
$user = $userService->findById($params['friend_id']);
if (!$user) {
return $this->response->fail('用户不存在...');
}
$user_id = $this->uid();
if (!$this->contactsService->addContact($user_id, intval($params['friend_id']), $params['remarks'])) {
return $this->response->fail('添加好友申请失败...');
}
// 好友申请未读消息数自增
ApplyNumCache::setInc(intval($params['friend_id']));
2021-04-22 16:54:01 +08:00
// 判断对方是否在线。如果在线发送消息通知
2021-01-28 20:07:14 +08:00
if ($this->socketClientService->isOnlineAll(intval($params['friend_id']))) {
$this->producer->produce(
new ChatMessageProducer(SocketConstants::EVENT_FRIEND_APPLY, [
2021-04-20 16:30:57 +08:00
'sender' => $user_id,
2021-01-28 20:07:14 +08:00
'receive' => intval($params['friend_id']),
2021-04-20 16:30:57 +08:00
'type' => 1,
'status' => 1,
'remark' => ''
2021-01-28 20:07:14 +08:00
])
);
}
return $this->response->success([], '发送好友申请成功...');
}
/**
* 删除联系人
* @RequestMapping(path="delete", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
public function deleteContact()
{
$params = $this->request->inputs(['friend_id']);
$this->validate($params, [
'friend_id' => 'required|integer'
]);
$user_id = $this->uid();
if (!$this->contactsService->deleteContact($user_id, intval($params['friend_id']))) {
return $this->response->fail('好友关系解除失败...');
}
2021-04-22 16:54:01 +08:00
// 删除好友会话列表
2021-01-28 20:07:14 +08:00
UsersChatList::delItem($user_id, $params['friend_id'], 2);
UsersChatList::delItem($params['friend_id'], $user_id, 2);
2021-04-22 16:54:01 +08:00
// ... TODO 推送消息(待完善)
2021-01-28 20:07:14 +08:00
return $this->response->success([], '好友关系解除成功...');
}
/**
* 同意添加联系人
* @RequestMapping(path="accept-invitation", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
public function acceptInvitation()
{
$params = $this->request->inputs(['apply_id', 'remarks']);
$this->validate($params, [
'apply_id' => 'required|integer',
2021-04-20 16:30:57 +08:00
'remarks' => 'present|max:20'
2021-01-28 20:07:14 +08:00
]);
$user_id = $this->uid();
2021-04-20 16:30:57 +08:00
$isTrue = $this->contactsService->acceptInvitation($user_id, intval($params['apply_id']), $params['remarks']);
2021-01-28 20:07:14 +08:00
if (!$isTrue) {
return $this->response->fail('处理失败...');
}
$friend_id = $info = UsersFriendsApply::where('id', $params['apply_id'])
->where('friend_id', $user_id)
->value('user_id');
2021-04-22 16:54:01 +08:00
// 判断对方是否在线。如果在线发送消息通知
2021-01-28 20:07:14 +08:00
if ($this->socketClientService->isOnlineAll($friend_id)) {
// 待完善
$this->producer->produce(
new ChatMessageProducer(SocketConstants::EVENT_FRIEND_APPLY, [
2021-04-20 16:30:57 +08:00
'sender' => $user_id,
2021-01-28 20:07:14 +08:00
'receive' => $friend_id,
2021-04-20 16:30:57 +08:00
'type' => 1,
'status' => 1,
'remark' => ''
2021-01-28 20:07:14 +08:00
])
);
}
return $this->response->success([], '处理成功...');
}
/**
* 拒绝添加联系人(预留)
* @RequestMapping(path="decline-invitation", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
public function declineInvitation()
{
$params = $this->request->inputs(['apply_id', 'remarks']);
$this->validate($params, [
'apply_id' => 'required|integer',
2021-04-20 16:30:57 +08:00
'remarks' => 'present|max:20'
2021-01-28 20:07:14 +08:00
]);
$isTrue = $this->contactsService->declineInvitation($this->uid(), intval($params['apply_id']), $params['remarks']);
return $isTrue
? $this->response->success()
: $this->response->fail();
}
/**
* 删除联系人申请记录
* @RequestMapping(path="delete-apply", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
public function deleteContactApply()
{
$params = $this->request->inputs(['apply_id']);
$this->validate($params, [
'apply_id' => 'required|integer'
]);
$isTrue = $this->contactsService->delContactApplyRecord($this->uid(), intval($params['apply_id']));
return $isTrue
? $this->response->success()
: $this->response->fail();
}
/**
* 获取联系人申请未读数
* @RequestMapping(path="apply-unread-num", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
public function getContactApplyUnreadNum()
{
$num = ApplyNumCache::get($this->uid());
return $this->response->success([
'unread_num' => $num ? $num : 0
]);
}
/**
* 获取联系人申请未读数
* @RequestMapping(path="apply-records", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
public function getContactApplyRecords()
{
$params = $this->request->inputs(['page', 'page_size']);
$this->validate($params, [
2021-04-20 16:30:57 +08:00
'page' => 'present|integer',
2021-01-28 20:07:14 +08:00
'page_size' => 'present|integer'
]);
2021-04-20 16:30:57 +08:00
$page = $this->request->input('page', 1);
2021-01-28 20:07:14 +08:00
$page_size = $this->request->input('page_size', 10);
2021-04-20 16:30:57 +08:00
$user_id = $this->uid();
2021-01-28 20:07:14 +08:00
$data = $this->contactsService->getContactApplyRecords($user_id, $page, $page_size);
ApplyNumCache::del($user_id);
return $this->response->success($data);
}
/**
* 搜索联系人
* @RequestMapping(path="search", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
public function searchContacts()
{
$params = $this->request->inputs(['mobile']);
$this->validate($params, [
'mobile' => "present|regex:/^1[3456789][0-9]{9}$/"
]);
$result = $this->contactsService->findContact($params['mobile']);
return $this->response->success($result);
}
/**
* 编辑联系人备注
* @RequestMapping(path="edit-remark", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
public function editContactRemark()
{
$params = $this->request->inputs(['friend_id', 'remarks']);
$this->validate($params, [
'friend_id' => 'required|integer|min:1',
2021-04-20 16:30:57 +08:00
'remarks' => "required|max:20"
2021-01-28 20:07:14 +08:00
]);
$user_id = $this->uid();
2021-04-20 16:30:57 +08:00
$isTrue = $this->contactsService->editContactRemark($user_id, intval($params['friend_id']), $params['remarks']);
2021-01-28 20:07:14 +08:00
if (!$isTrue) {
return $this->response->fail('备注修改失败...');
}
FriendRemarkCache::set($user_id, intval($params['friend_id']), $params['remarks']);
return $this->response->success([], '备注修改成功...');
}
}