初始化

main
gzydong 2020-12-01 13:54:40 +08:00
parent 219b4c26de
commit 7ea3d1be4e
11 changed files with 203 additions and 111 deletions

View File

@ -20,7 +20,6 @@ use App\Model\Chat\ChatRecordsInvite;
use App\Model\Chat\ChatRecordsForward;
use App\Service\SocketClientService;
use App\Service\SocketRoomService;
use App\Helper\PushMessageHelper;
/**
* 消息推送消费者队列
@ -89,7 +88,7 @@ class ChatMessageConsumer extends ConsumerMessage
{
$this->socketClientService = $socketClientService;
$this->socketRoomService = $socketRoomService;
$this->setQueue('queue:im:message:' . SERVER_RUN_ID);
$this->setQueue('queue:im_message:' . SERVER_RUN_ID);
}
/**
@ -127,7 +126,7 @@ class ChatMessageConsumer extends ConsumerMessage
* @param AMQPMessage $message
* @return string
*/
public function onConsumeTalk(array $data, AMQPMessage $message)
public function onConsumeTalk(array $data, AMQPMessage $message): string
{
$redis = container()->get(Redis::class);
@ -149,9 +148,8 @@ class ChatMessageConsumer extends ConsumerMessage
}
}
// 去重
$fds = array_unique($fds);
if (empty($fds)) {
// 客户端ID去重
if (!$fds = array_unique($fds)) {
return Result::ACK;
}
@ -174,9 +172,7 @@ class ChatMessageConsumer extends ConsumerMessage
'users.avatar as avatar',
]);
if (!$result) {
return Result::ACK;
}
if (!$result) return Result::ACK;
$file = [];
$code_block = [];
@ -186,9 +182,8 @@ class ChatMessageConsumer extends ConsumerMessage
case 2://文件消息
$file = ChatRecordsFile::where('record_id', $result->id)->first(['id', 'record_id', 'user_id', 'file_source', 'file_type', 'save_type', 'original_name', 'file_suffix', 'file_size', 'save_dir']);
$file = $file ? $file->toArray() : [];
if ($file) {
$file['file_url'] = get_media_url($file['save_dir']);
}
$file && $file['file_url'] = get_media_url($file['save_dir']);
break;
case 3://入群消息/退群消息
$notifyInfo = ChatRecordsInvite::where('record_id', $result->id)->first([
@ -196,15 +191,14 @@ class ChatMessageConsumer extends ConsumerMessage
]);
$userInfo = User::where('id', $notifyInfo->operate_user_id)->first(['nickname', 'id']);
$membersIds = explode(',', $notifyInfo->user_ids);
$invite = [
'type' => $notifyInfo->type,
'operate_user' => ['id' => $userInfo->id, 'nickname' => $userInfo->nickname],
'users' => User::select('id', 'nickname')->whereIn('id', $membersIds)->get()->toArray()
'users' => User::select('id', 'nickname')->whereIn('id', parse_ids($notifyInfo->user_ids))->get()->toArray()
];
unset($notifyInfo, $userInfo, $membersIds);
unset($notifyInfo, $userInfo);
break;
case 4://会话记录消息
$forward = ['num' => 0, 'list' => []];
@ -224,11 +218,11 @@ class ChatMessageConsumer extends ConsumerMessage
break;
}
$msg = [
$notify = [
'send_user' => $data['data']['sender'],
'receive_user' => $data['data']['receive'],
'source_type' => $data['data']['source'],
'data' => PushMessageHelper::formatTalkMsg([
'data' => $this->formatTalkMessage([
'id' => $result->id,
'msg_type' => $result->msg_type,
'source' => $result->source,
@ -247,11 +241,7 @@ class ChatMessageConsumer extends ConsumerMessage
unset($result, $file, $code_block, $forward, $invite);
$server = server();
$notify = json_encode(['event_talk', $msg]);
foreach ($fds as $fd) {
$server->exist($fd) && $server->push($fd, $notify);
}
$this->socketPushNotify($fds, json_encode(['event_talk', $notify]));
return Result::ACK;
}
@ -263,14 +253,11 @@ class ChatMessageConsumer extends ConsumerMessage
* @param AMQPMessage $message
* @return string
*/
public function onConsumeKeyboard(array $data, AMQPMessage $message)
public function onConsumeKeyboard(array $data, AMQPMessage $message): string
{
$fds = $this->socketClientService->findUserFds($data['data']['receive_user']);
$server = server();
$notify = json_encode(['event_keyboard', $data['data']]);
foreach ($fds as $fd) {
$server->exist($fd) && $server->push($fd, $notify);
}
$this->socketPushNotify($fds, json_encode(['event_keyboard', $data['data']]));
return Result::ACK;
}
@ -282,22 +269,16 @@ class ChatMessageConsumer extends ConsumerMessage
* @param AMQPMessage $message
* @return string
*/
public function onConsumeOnlineStatus(array $data, AMQPMessage $message)
public function onConsumeOnlineStatus(array $data, AMQPMessage $message): string
{
$user_id = $data['data']['user_id'];
$friends = UsersFriend::getFriendIds(intval($user_id));
$friends = UsersFriend::getFriendIds((int)$data['data']['user_id']);
$fds = [];
foreach ($friends as $friend_id) {
$fds = array_merge($fds, $this->socketClientService->findUserFds(intval($friend_id)));
$fds = array_merge($fds, $this->socketClientService->findUserFds((int)$friend_id));
}
$fds = array_unique($fds);
$server = server();
$notify = json_encode(['event_online_status', $data['data']]);
foreach ($fds as $fd) {
$server->exist($fd) && $server->push($fd, $notify);
}
$this->socketPushNotify(array_unique($fds), json_encode(['event_online_status', $data['data']]));
return Result::ACK;
}
@ -309,7 +290,7 @@ class ChatMessageConsumer extends ConsumerMessage
* @param AMQPMessage $message
* @return string
*/
public function onConsumeRevokeTalk(array $data, AMQPMessage $message)
public function onConsumeRevokeTalk(array $data, AMQPMessage $message): string
{
/** @var ChatRecord */
$record = ChatRecord::where('id', $data['data']['record_id'])->first(['id', 'source', 'user_id', 'receive_id']);
@ -325,18 +306,15 @@ class ChatMessageConsumer extends ConsumerMessage
}
}
$fds = array_unique($fds);
$server = server();
$notify = json_encode(['event_revoke_talk', [
'record_id' => $record->id,
'source' => $record->source,
'user_id' => $record->user_id,
'receive_id' => $record->receive_id,
]]);
foreach ($fds as $fd) {
$server->exist($fd) && $server->push($fd, $notify);
}
$this->socketPushNotify(
array_unique($fds),
json_encode(['event_revoke_talk', [
'record_id' => $record->id,
'source' => $record->source,
'user_id' => $record->user_id,
'receive_id' => $record->receive_id,
]])
);
return Result::ACK;
}
@ -348,17 +326,60 @@ class ChatMessageConsumer extends ConsumerMessage
* @param AMQPMessage $message
* @return string
*/
public function onConsumeFriendApply(array $data, AMQPMessage $message)
public function onConsumeFriendApply(array $data, AMQPMessage $message): string
{
$fds = $this->socketClientService->findUserFds($data['data']['receive']);
$fds = array_unique($fds);
$server = server();
$notify = json_encode(['event_friend_apply', $data['data']]);
foreach ($fds as $fd) {
$server->exist($fd) && $server->push($fd, $notify);
}
$this->socketPushNotify(array_unique($fds), json_encode(['event_friend_apply', $data['data']]));
return Result::ACK;
}
/**
* WebSocket 消息推送
*
* @param $fds
* @param $message
*/
private function socketPushNotify($fds, $message)
{
$server = server();
foreach ($fds as $fd) {
$server->exist($fd) && $server->push($fd, $message);
}
}
/**
* 格式化对话的消息体
*
* @param array $data 对话的消息
* @return array
*/
private function formatTalkMessage(array $data)
{
$message = [
"id" => 0,//消息记录ID
"source" => 1,//消息来源[1:好友私信;2:群聊]
"msg_type" => 1,
"user_id" => 0,//发送者用户ID
"receive_id" => 0,//接收者ID[好友ID或群ID]
"content" => '',//文本消息
"is_revoke" => 0,//消息是否撤销
// 发送消息人的信息
"nickname" => "",
"avatar" => "",
// 不同的消息类型
"file" => [],
"code_block" => [],
"forward" => [],
"invite" => [],
// 消息创建时间
"created_at" => "",
];
return array_merge($message, array_intersect_key($data, $message));
}
}

View File

@ -102,7 +102,7 @@ class ArticleController extends CController
*/
public function getArticleDetail()
{
$this->validate($this->request->all(), [
$this->validate($this->request->inputs(['article_id']), [
'article_id' => 'required|integer'
]);
@ -121,7 +121,7 @@ class ArticleController extends CController
*/
public function editArticleClass()
{
$params = $this->request->all();
$params = $this->request->inputs(['class_id', 'class_name']);
$this->validate($params, [
'class_id' => 'required|integer',
'class_name' => 'required'
@ -142,7 +142,7 @@ class ArticleController extends CController
*/
public function delArticleClass()
{
$params = $this->request->all();
$params = $this->request->inputs(['class_id']);
$this->validate($params, [
'class_id' => 'required|integer'
]);
@ -161,7 +161,7 @@ class ArticleController extends CController
*/
public function articleClassSort()
{
$params = $this->request->all();
$params = $this->request->inputs(['class_id', 'sort_type']);
$this->validate($params, [
'class_id' => 'required|integer',
'sort_type' => 'required|in:1,2'
@ -191,7 +191,7 @@ class ArticleController extends CController
*/
public function mergeArticleClass()
{
$params = $this->request->all();
$params = $this->request->inputs(['class_id', 'toid']);
$this->validate($params, [
'class_id' => 'required|integer',
'toid' => 'required|integer'
@ -211,7 +211,7 @@ class ArticleController extends CController
*/
public function editArticleTags()
{
$params = $this->request->all();
$params = $this->request->inputs(['tag_id', 'tag_name']);
$this->validate($params, [
'tag_id' => 'required|integer|min:0',
'tag_name' => 'required'
@ -235,7 +235,7 @@ class ArticleController extends CController
*/
public function delArticleTags()
{
$params = $this->request->all();
$params = $this->request->inputs(['tag_id']);
$this->validate($params, [
'tag_id' => 'required|integer|min:0'
]);
@ -284,7 +284,7 @@ class ArticleController extends CController
*/
public function deleteArticle()
{
$params = $this->request->all();
$params = $this->request->inputs(['article_id']);
$this->validate($params, [
'article_id' => 'required|integer|min:0'
]);
@ -303,7 +303,7 @@ class ArticleController extends CController
*/
public function recoverArticle()
{
$params = $this->request->all();
$params = $this->request->inputs(['article_id']);
$this->validate($params, [
'article_id' => 'required|integer|min:0'
]);
@ -352,7 +352,7 @@ class ArticleController extends CController
*/
public function moveArticle()
{
$params = $this->request->all();
$params = $this->request->inputs(['article_id', 'class_id']);
$this->validate($params, [
'article_id' => 'required|integer|min:0',
'class_id' => 'required|integer|min:0'
@ -376,7 +376,7 @@ class ArticleController extends CController
*/
public function setAsteriskArticle()
{
$params = $this->request->all();
$params = $this->request->inputs(['article_id', 'type']);
$this->validate($params, [
'article_id' => 'required|integer|min:0',
'type' => 'required|in:1,2'
@ -400,7 +400,7 @@ class ArticleController extends CController
*/
public function updateArticleTag()
{
$params = $this->request->all();
$params = $this->request->inputs(['article_id', 'tags']);
$this->validate($params, [
'article_id' => 'required|integer|min:0',
'tags' => 'required|array'
@ -420,7 +420,7 @@ class ArticleController extends CController
*/
public function foreverDelArticle()
{
$params = $this->request->all();
$params = $this->request->inputs(['article_id']);
$this->validate($params, [
'article_id' => 'required|integer|min:0'
]);
@ -483,7 +483,7 @@ class ArticleController extends CController
*/
public function deleteArticleAnnex()
{
$params = $this->request->all();
$params = $this->request->inputs(['annex_id']);
$this->validate($params, [
'annex_id' => 'required|integer|min:0'
]);
@ -545,7 +545,7 @@ class ArticleController extends CController
*/
public function foreverDelAnnex()
{
$params = $this->request->all();
$params = $this->request->inputs(['annex_id']);
$this->validate($params, [
'annex_id' => 'required|integer|min:0'
]);

View File

@ -41,17 +41,14 @@ class AuthController extends CController
*/
public function login()
{
$this->validate($this->request->all(), [
$params = $this->request->inputs(['mobile', 'password', 'platform']);
$this->validate($params, [
'mobile' => "required|regex:/^1[345789][0-9]{9}$/",
'password' => 'required',
'platform' => 'required|in:h5,ios,windows,mac,web',
]);
$userInfo = $this->userService->login(
$this->request->input('mobile'),
$this->request->input('password')
);
$userInfo = $this->userService->login($params['mobile'], $params['password']);
if (!$userInfo) {
return $this->response->fail('账号不存在或密码填写错误...');
}
@ -59,7 +56,7 @@ class AuthController extends CController
try {
$token = $this->jwt->getToken([
'user_id' => $userInfo['id'],
'platform' => $this->request->input('platform'),
'platform' => $params['platform'],
]);
} catch (\Exception $exception) {
return $this->response->error('登录异常,请稍后再试...');

View File

@ -113,7 +113,7 @@ class GroupController extends CController
$this->socketRoomService->delRoom($params['group_id']);
// ... 推送群消息
// ... 推送群消息(后期添加)
return $this->response->success([], '群组解散成功...');
}

View File

@ -235,7 +235,6 @@ class TalkController extends CController
);
}
return $isTrue
? $this->response->success([], $message)
: $this->response->fail($message);
@ -292,8 +291,6 @@ class TalkController extends CController
//'receive_group_ids' => 'array',
]);
$user_id = $this->uid();
$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) {
@ -307,11 +304,9 @@ class TalkController extends CController
}, $params['receive_group_ids']);
}
$items = array_merge(
$receive_user_ids,
$receive_group_ids
);
$items = array_merge($receive_user_ids, $receive_group_ids);
$user_id = $this->uid();
if ($params['forward_mode'] == 1) {//单条转发
$ids = $this->talkService->forwardRecords($user_id, $params['receive_id'], $params['records_ids']);
} else {//合并转发
@ -646,7 +641,6 @@ class TalkController extends CController
])
);
return $this->response->success();
}
@ -667,9 +661,7 @@ class TalkController extends CController
$user_id = $this->uid();
$emoticon = EmoticonDetail::where('id', $params['emoticon_id'])->where('user_id', $user_id)->first([
'url',
'file_suffix',
'file_size'
'url','file_suffix','file_size'
]);
if (!$emoticon) {

View File

@ -79,7 +79,7 @@ class UsersController extends CController
*/
public function removeFriend()
{
$params = $this->request->all();
$params = $this->request->inputs(['friend_id']);
$this->validate($params, [
'friend_id' => 'required|integer'
]);
@ -162,7 +162,7 @@ class UsersController extends CController
$params = $this->request->inputs(['nickname', 'avatar', 'motto', 'gender']);
$this->validate($params, [
'nickname' => 'required',
'motto' => 'present',
'motto' => 'present|max:100',
'gender' => 'required|in:0,1,2',
'avatar' => 'present|url'
]);
@ -294,6 +294,7 @@ class UsersController extends CController
'remarks' => 'present'
]);
$user_id = $this->uid();
$isTrue = $this->friendService->handleFriendApply($this->uid(), (int)$params['apply_id'], $params['remarks']);
if (!$isTrue) {
return $this->response->fail('处理失败...');
@ -301,15 +302,16 @@ class UsersController extends CController
//判断对方是否在线。如果在线发送消息通知
if ($this->socketClientService->isOnlineAll((int)$params['friend_id'])) {
// $this->producer->produce(
// new ChatMessageProducer('event_friend_apply', [
// 'sender' => $user_id,
// 'receive' => (int)$params['friend_id'],
// 'type' => 1,
// 'status' => 1,
// 'remark' => ''
// ])
// );
// 待修改
$this->producer->produce(
new ChatMessageProducer('event_friend_apply', [
'sender' => $user_id,
'receive' => (int)$params['friend_id'],
'type' => 1,
'status' => 1,
'remark' => ''
])
);
}
return $this->response->success([], '处理成功...');
@ -377,7 +379,7 @@ class UsersController extends CController
$params = $this->request->inputs(['old_password', 'new_password']);
$this->validate($params, [
'old_password' => 'required',
'new_password' => 'required'
'new_password' => 'required|min:6|max:16'
]);
$userInfo = $this->userService->findById($this->uid(), ['id', 'password', 'mobile']);

View File

@ -2,6 +2,11 @@
namespace App\Helper;
/**
* Hash 密码加密辅助类
*
* @package App\Helper
*/
class Hash
{
/**

View File

@ -0,0 +1,74 @@
<?php
namespace App\Helper;
use Hashids\Hashids;
/**
* ID加密辅助类
*
* @package App\Helper
*/
class HashIdsHelper
{
/**
* 长度
*
* @var int
*/
public static $length = 10;
/**
* 为安全起见需要修改为自己的秘钥
*
* @var string
*/
public static $secretKey = 'AqBU9zgA8EfGwVv3ghSj16n4vKS9gMtTbu';
/**
* @var Hashids
*/
protected static $hashIds;
/**
* 加密
*
* @param mixed ...$numbers
* @return string
*/
public static function encode(...$numbers)
{
return self::getHashIds()->encode(...$numbers);
}
/**
* 解密
*
* @param string $hash
* @return array|mixed
* @throws \Exception
*/
public static function decode(string $hash)
{
$data = self::getHashIds()->decode($hash);
if (empty($data) || !is_array($data)) {
throw new \Exception('解密失败');
}
return count($data) == 1 ? $data[0] : $data;
}
/**
* 获取 HashIds 实例
*
* @return Hashids
*/
private static function getHashIds()
{
if (!self::$hashIds instanceof Hashids) {
self::$hashIds = new Hashids(self::$secretKey, self::$length);
}
return self::$hashIds;
}
}

View File

@ -42,10 +42,13 @@ class JWTAuthMiddleware implements MiddlewareInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$isValidToken = true;
$isValidToken = false;
// 获取请求token
$token = $request->getHeaderLine('Authorization') ?? $this->request->input('token', '');
$token = $request->getHeaderLine('Authorization');
if (empty($token)) {
$token = $this->request->input('token', '');
}
if (!empty($token)) {
try {
@ -62,7 +65,6 @@ class JWTAuthMiddleware implements MiddlewareInterface
return $this->response->withStatus(401)->json([
'code' => 401,
'message' => 'Token authentication does not pass',
'data' => []
]);
}

View File

@ -46,6 +46,7 @@ class SocketRoomService
*
* @param string|array $room 房间名
* @param string|array $members 用户ID
* @return int
*/
public function delRoomMember($room, $members)
{

View File

@ -133,9 +133,7 @@ class TalkService extends BaseService
*/
public function handleChatRecords(array $rows)
{
if (empty($rows)) {
return [];
}
if (empty($rows)) return [];
$files = $codes = $forwards = $invites = [];
foreach ($rows as $value) {
@ -205,7 +203,7 @@ class TalkService extends BaseService
];
if ($rows[$k]['invite']['type'] == 1 || $rows[$k]['invite']['type'] == 3) {
$rows[$k]['invite']['users'] = User::select('id', 'nickname')->whereIn('id', explode(',', $invites[$row['id']]['user_ids']))->get()->toArray();
$rows[$k]['invite']['users'] = User::select('id', 'nickname')->whereIn('id', parse_ids($invites[$row['id']]['user_ids']))->get()->toArray();
} else {
$rows[$k]['invite']['users'] = $rows[$k]['invite']['operate_user'];
}