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

142 lines
4.2 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;
2021-07-20 23:12:18 +08:00
use App\Constants\TalkModeConstant;
2021-07-08 23:44:43 +08:00
use App\Service\TalkListService;
2021-07-07 19:43:09 +08:00
use App\Service\UserService;
2021-01-28 20:07:14 +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;
use App\Service\ContactsService;
use App\Service\SocketClientService;
2021-05-22 21:45:18 +08:00
use App\Cache\FriendApply;
use App\Cache\FriendRemark;
use App\Cache\ServerRunID;
2021-01-28 20:07:14 +08:00
/**
* Class ContactsController
2021-07-05 21:52:44 +08:00
* @Controller(prefix="/api/v1/contacts")
2021-01-28 20:07:14 +08:00
* @Middleware(JWTAuthMiddleware::class)
*
* @package App\Controller\Api\V1
*/
class ContactsController extends CController
{
/**
* @Inject
* @var ContactsService
*/
2021-07-07 19:43:09 +08:00
private $service;
2021-01-28 20:07:14 +08:00
/**
* 获取用户联系人列表
* @RequestMapping(path="list", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
2021-07-07 19:43:09 +08:00
public function getContacts(UserService $service)
2021-01-28 20:07:14 +08:00
{
2021-07-07 19:43:09 +08:00
$rows = $service->getUserFriends($this->uid());
2021-01-28 20:07:14 +08:00
if ($rows) {
2021-05-21 22:56:42 +08:00
$runArr = ServerRunID::getInstance()->getServerRunIdAll();
2021-07-06 23:32:14 +08:00
foreach ($rows as $k => $row) {
2021-07-23 21:34:29 +08:00
$rows[$k]['is_online'] = di()->get(SocketClientService::class)->isOnlineAll($row['id'], $runArr);
2021-01-28 20:07:14 +08:00
}
}
return $this->response->success($rows);
}
/**
* 删除联系人
* @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();
2021-07-07 19:43:09 +08:00
if (!$this->service->delete($user_id, intval($params['friend_id']))) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('好友关系解除失败!');
2021-01-28 20:07:14 +08:00
}
2021-07-23 21:34:29 +08:00
di()->get(TalkListService::class)->deleteByType($user_id, $params['friend_id'], TalkModeConstant::PRIVATE_CHAT);
2021-01-28 20:07:14 +08:00
2021-07-07 19:43:09 +08:00
// TODO 推送消息(待完善)
2021-01-28 20:07:14 +08:00
return $this->response->success([], '好友关系解除成功...');
}
/**
* 获取联系人申请未读数
* @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()
{
return $this->response->success([
2021-05-23 17:43:49 +08:00
'unread_num' => (int)FriendApply::getInstance()->get(strval($this->uid()))
2021-01-28 20:07:14 +08:00
]);
}
/**
* 搜索联系人
* @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}$/"
]);
2021-07-07 19:43:09 +08:00
$result = $this->service->findContact($params['mobile']);
2021-01-28 20:07:14 +08:00
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-08-10 21:35:41 +08:00
$isTrue = $this->service->editRemark($user_id, intval($params['friend_id']), $params['remarks']);
2021-01-28 20:07:14 +08:00
if (!$isTrue) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('备注修改失败!');
2021-01-28 20:07:14 +08:00
}
2021-05-20 22:23:48 +08:00
FriendRemark::getInstance()->save($user_id, (int)$params['friend_id'], $params['remarks']);
2021-01-28 20:07:14 +08:00
return $this->response->success([], '备注修改成功...');
}
}