hyperf-chat/app/Controller/Api/V1/Contact/ContactApplyController.php

150 lines
4.2 KiB
PHP
Raw Normal View History

2021-07-07 19:43:09 +08:00
<?php
2021-09-05 15:47:01 +08:00
declare(strict_types=1);
2021-07-07 19:43:09 +08:00
2022-01-16 10:29:16 +08:00
namespace App\Controller\Api\V1\Contact;
2021-07-07 19:43:09 +08:00
use App\Cache\FriendApply;
2021-07-08 22:28:51 +08:00
use App\Cache\Repository\LockRedis;
2022-01-16 10:29:16 +08:00
use App\Controller\Api\V1\CController;
2022-01-22 16:04:54 +08:00
use App\Middleware\JWTAuthMiddleware;
use App\Repository\UserRepository;
use App\Service\Contact\ContactApplyService;
use Hyperf\Di\Annotation\Inject;
2021-07-07 19:43:09 +08:00
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;
/**
* Class ContactsApplyController
2022-01-16 10:29:16 +08:00
* @Controller(prefix="/api/v1/contact/apply")
2021-07-07 19:43:09 +08:00
* @Middleware(JWTAuthMiddleware::class)
*
* @package App\Controller\Api\V1
*/
2022-01-16 10:29:16 +08:00
class ContactApplyController extends CController
2021-07-07 19:43:09 +08:00
{
/**
* @Inject
* @var ContactApplyService
*/
private $service;
/**
2021-09-11 12:41:28 +08:00
* 添加联系人申请接口
*
2021-07-07 19:43:09 +08:00
* @RequestMapping(path="create", methods="post")
2022-01-22 16:04:54 +08:00
*
2021-07-07 19:43:09 +08:00
* @return ResponseInterface
*/
2022-01-22 16:04:54 +08:00
public function create(): ResponseInterface
2021-07-07 19:43:09 +08:00
{
2021-07-08 22:28:51 +08:00
$params = $this->request->inputs(['friend_id', 'remark']);
2021-07-07 19:43:09 +08:00
$this->validate($params, [
'friend_id' => 'required|integer',
2021-07-08 22:28:51 +08:00
'remark' => 'present|max:50'
2021-07-07 19:43:09 +08:00
]);
$params['friend_id'] = (int)$params['friend_id'];
2022-01-22 16:04:54 +08:00
$user = di()->get(UserRepository::class)->findById($params['friend_id']);
2021-08-01 00:45:35 +08:00
2022-01-22 16:04:54 +08:00
if (!$user) {
return $this->response->fail('用户不存在!');
}
2021-07-07 19:43:09 +08:00
$user_id = $this->uid();
2021-07-08 22:28:51 +08:00
$key = "{$user_id}_{$params['friend_id']}";
2022-01-22 16:04:54 +08:00
2021-07-08 22:28:51 +08:00
if (LockRedis::getInstance()->lock($key, 10)) {
$isTrue = $this->service->create($user_id, $params['friend_id'], $params['remark']);
if ($isTrue) {
return $this->response->success([], '发送好友申请成功...');
} else {
LockRedis::getInstance()->delete($key);
}
2021-07-07 19:43:09 +08:00
}
2021-07-08 22:28:51 +08:00
return $this->response->fail('添加好友申请失败!');
2021-07-07 19:43:09 +08:00
}
/**
2021-09-05 15:47:01 +08:00
* 好友同意接口
*
2021-07-07 19:43:09 +08:00
* @RequestMapping(path="accept", methods="post")
*/
2021-09-05 15:47:01 +08:00
public function accept(): ResponseInterface
2021-07-07 19:43:09 +08:00
{
2021-07-08 22:28:51 +08:00
$params = $this->request->inputs(['apply_id', 'remark']);
2021-07-07 19:43:09 +08:00
$this->validate($params, [
'apply_id' => 'required|integer',
2021-07-08 22:28:51 +08:00
'remark' => 'present|max:20'
2021-07-07 19:43:09 +08:00
]);
$user_id = $this->uid();
2021-09-05 15:47:01 +08:00
$isTrue = $this->service->accept($user_id, (int)$params['apply_id'], $params['remark']);
2021-07-07 19:43:09 +08:00
if (!$isTrue) {
return $this->response->fail('处理失败!');
}
return $this->response->success([], '处理成功...');
}
/**
2021-09-05 15:47:01 +08:00
* 好友拒绝接口
*
2021-07-07 19:43:09 +08:00
* @RequestMapping(path="decline", methods="post")
*/
2021-09-05 15:47:01 +08:00
public function decline(): ResponseInterface
2021-07-07 19:43:09 +08:00
{
2021-07-08 22:28:51 +08:00
$params = $this->request->inputs(['apply_id', 'remark']);
2021-07-07 19:43:09 +08:00
$this->validate($params, [
'apply_id' => 'required|integer',
2021-07-08 22:28:51 +08:00
'remark' => 'present|max:20'
2021-07-07 19:43:09 +08:00
]);
2021-09-05 15:47:01 +08:00
$isTrue = $this->service->decline($this->uid(), (int)$params['apply_id'], $params['remark']);
2021-07-08 22:28:51 +08:00
if (!$isTrue) {
return $this->response->fail('处理失败!');
}
2021-07-07 19:43:09 +08:00
2021-07-08 22:28:51 +08:00
return $this->response->success([], '处理成功...');
2021-07-07 19:43:09 +08:00
}
/**
* 获取联系人申请未读数
*
2021-09-05 15:47:01 +08:00
* @RequestMapping(path="records", methods="get")
2021-07-07 19:43:09 +08:00
*/
2021-09-05 15:47:01 +08:00
public function records(): ResponseInterface
2021-07-07 19:43:09 +08:00
{
$params = $this->request->inputs(['page', 'page_size']);
$this->validate($params, [
'page' => 'present|integer',
'page_size' => 'present|integer'
]);
2021-09-05 15:47:01 +08:00
$page = (int)$this->request->input('page', 1);
$page_size = (int)$this->request->input('page_size', 10);
2021-07-07 19:43:09 +08:00
$user_id = $this->uid();
FriendApply::getInstance()->rem(strval($user_id));
2021-07-08 22:28:51 +08:00
return $this->response->success(
$this->service->getApplyRecords($user_id, $page, $page_size)
);
2021-07-07 19:43:09 +08:00
}
2022-01-16 10:29:16 +08:00
/**
* 获取联系人申请未读数
*
* @RequestMapping(path="unread-num", methods="get")
*/
public function getContactApplyUnreadNum(): ResponseInterface
{
return $this->response->success([
'unread_num' => (int)FriendApply::getInstance()->get(strval($this->uid()))
]);
}
2021-07-07 19:43:09 +08:00
}