优化代码

main
gzydong 2021-04-22 16:54:01 +08:00
parent 52410df8a6
commit 1140a30aa8
20 changed files with 97 additions and 113 deletions

View File

@ -167,6 +167,7 @@ class ArticleController extends CController
* @RequestMapping(path="article-class-sort", methods="post")
*
* @return ResponseInterface
* @throws \Exception
*/
public function articleClassSort()
{
@ -344,7 +345,6 @@ class ArticleController extends CController
return $this->response->fail('图片格式错误目前仅支持jpg、png、jpeg、gif和webp');
}
//获取图片信息
$imgInfo = getimagesize($file->getRealPath());
$path = $this->uploadService->media($file, 'media/images/notes/', create_image_name($ext, $imgInfo[0], $imgInfo[1]));
@ -433,6 +433,7 @@ class ArticleController extends CController
* @RequestMapping(path="forever-delete-article", methods="post")
*
* @return ResponseInterface
* @throws \Exception
*/
public function foreverDelArticle()
{

View File

@ -15,7 +15,6 @@ use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\JWTAuthMiddleware;
use App\Constants\ResponseCode;
use App\Model\User;
use App\Service\UserService;
use App\Service\SmsCodeService;

View File

@ -102,10 +102,9 @@ class ContactsController extends CController
// 好友申请未读消息数自增
ApplyNumCache::setInc(intval($params['friend_id']));
//判断对方是否在线。如果在线发送消息通知
// 判断对方是否在线。如果在线发送消息通知
if ($this->socketClientService->isOnlineAll(intval($params['friend_id']))) {
$this->producer->produce(
// 消息待完善
new ChatMessageProducer(SocketConstants::EVENT_FRIEND_APPLY, [
'sender' => $user_id,
'receive' => intval($params['friend_id']),
@ -137,11 +136,11 @@ class ContactsController extends CController
return $this->response->fail('好友关系解除失败...');
}
//删除好友会话列表
// 删除好友会话列表
UsersChatList::delItem($user_id, $params['friend_id'], 2);
UsersChatList::delItem($params['friend_id'], $user_id, 2);
// ... 推送消息(待完善)
// ... TODO 推送消息(待完善)
return $this->response->success([], '好友关系解除成功...');
}
@ -170,7 +169,7 @@ class ContactsController extends CController
->where('friend_id', $user_id)
->value('user_id');
//判断对方是否在线。如果在线发送消息通知
// 判断对方是否在线。如果在线发送消息通知
if ($this->socketClientService->isOnlineAll($friend_id)) {
// 待完善
$this->producer->produce(

View File

@ -222,6 +222,7 @@ class EmoticonController extends CController
* @RequestMapping(path="del-collect-emoticon", methods="post")
*
* @return ResponseInterface
* @throws \Exception
*/
public function delCollectEmoticon()
{

View File

@ -122,7 +122,7 @@ class GroupController extends CController
$this->socketRoomService->delRoom($params['group_id']);
// ... 推送群消息(预留)
// ... TODO 推送群消息(预留)
return $this->response->success([], '群组解散成功...');
}
@ -224,7 +224,7 @@ class GroupController extends CController
'avatar' => $params['avatar']
]);
// ... 推送消息通知(预留)
// ... TODO 推送消息通知(预留)
return $result
? $this->response->success([], '群组信息修改成功...')
@ -496,7 +496,7 @@ class GroupController extends CController
return $this->response->fail('添加群公告信息失败...');
}
// ... 推送群消息(预留)
// ... TODO 推送群消息(预留)
return $this->response->success([], '添加群公告信息成功...');
}

View File

@ -267,7 +267,7 @@ class TalkController extends CController
{
$params = $this->request->inputs(['source', 'record_id', 'receive_id']);
$this->validate($params, [
'source' => 'required|in:1,2',//消息来源1好友消息 2群聊消息
'source' => 'required|in:1,2',// 消息来源[1:好友消息;2:群聊消息;]
'record_id' => 'required|ids',
'receive_id' => 'required|integer|min:0'
]);
@ -296,17 +296,17 @@ class TalkController extends CController
{
$params = $this->request->inputs(['source', 'records_ids', 'receive_id', 'forward_mode', 'receive_user_ids', 'receive_group_ids']);
$this->validate($params, [
//消息来源[1好友消息 2群聊消息]
// 消息来源[1:好友消息;2:群聊消息;]
'source' => 'required|in:1,2',
//聊天记录ID多个逗号拼接
// 聊天记录ID多个逗号拼接
'records_ids' => 'required',
//接收者ID好友ID或者群聊ID
// 接收者ID好友ID或者群聊ID
'receive_id' => 'required|integer|min:0',
//转发方方式[1:逐条转发;2:合并转发]
// 转发方方式[1:逐条转发;2:合并转发;]
'forward_mode' => 'required|in:1,2',
//转发的好友的ID
// 转发的好友的ID
//'receive_user_ids' => 'array',
//转发的群聊ID
// 转发的群聊ID
//'receive_group_ids' => 'array',
]);
@ -326,9 +326,9 @@ class TalkController extends CController
$items = array_merge($receive_user_ids, $receive_group_ids);
$user_id = $this->uid();
if ($params['forward_mode'] == 1) {//单条转发
if ($params['forward_mode'] == 1) {// 单条转发
$ids = $this->talkService->forwardRecords($user_id, $params['receive_id'], $params['records_ids']);
} else {//合并转发
} else {// 合并转发
$ids = $this->talkService->mergeForwardRecords($user_id, $params['receive_id'], $params['source'], $params['records_ids'], $items);
}
@ -342,13 +342,13 @@ class TalkController extends CController
}
}
// ...消息推送队列
// ... 消息推送队列
foreach ($ids as $value) {
$this->producer->produce(
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
'sender' => $user_id, //发送者ID
'receive' => intval($value['receive_id']), //接收者ID
'source' => intval($value['source']), //接收者类型 1:好友;2:群组
'sender' => $user_id, // 发送者ID
'receive' => intval($value['receive_id']), // 接收者ID
'source' => intval($value['source']), // 接收者类型 1:好友;2:群组
'record_id' => $value['record_id']
])
);
@ -367,7 +367,7 @@ class TalkController extends CController
{
$params = $this->request->inputs(['record_id', 'source', 'receive_id']);
$this->validate($params, [
'source' => 'required|in:1,2',//消息来源1好友消息 2群聊消息
'source' => 'required|in:1,2',// 消息来源[1:好友消息;2:群聊消息;]
'record_id' => 'required|integer|min:0',
'receive_id' => 'required|integer|min:1',
]);
@ -412,10 +412,7 @@ class TalkController extends CController
'records_id' => 'required|integer|min:0'
]);
$rows = $this->talkService->getForwardRecords(
$this->uid(),
$params['records_id']
);
$rows = $this->talkService->getForwardRecords($this->uid(), $params['records_id']);
return $this->response->success(['rows' => $rows]);
}
@ -430,7 +427,7 @@ class TalkController extends CController
{
$params = $this->request->inputs(['record_id', 'source', 'receive_id', 'msg_type']);
$this->validate($params, [
'source' => 'required|in:1,2',//消息来源1好友消息 2群聊消息
'source' => 'required|in:1,2',// 消息来源[1:好友消息;2:群聊消息;]
'record_id' => 'required|integer|min:0',
'receive_id' => 'required|integer|min:1',
'msg_type' => 'required|in:0,1,2,3,4,5,6',
@ -503,8 +500,7 @@ class TalkController extends CController
{
$params = $this->request->inputs(['source', 'receive_id']);
$this->validate($params, [
//消息来源1好友消息 2群聊消息
'source' => 'required|in:1,2',
'source' => 'required|in:1,2',// 消息来源[1:好友消息;2:群聊消息;]
'receive_id' => 'required|integer|min:1'
]);
@ -518,7 +514,7 @@ class TalkController extends CController
return $this->response->fail('图片格式错误目前仅支持jpg、png、jpeg、gif和webp');
}
//获取图片信息
// 获取图片信息
$imgInfo = getimagesize($file->getRealPath());
$path = $uploadService->media($file, 'media/images/talks', create_image_name($ext, $imgInfo[0], $imgInfo[1]));
@ -547,12 +543,12 @@ class TalkController extends CController
return $this->response->fail('图片上传失败');
}
// ...消息推送队列
// ... 消息推送队列
$this->producer->produce(
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
'sender' => $user_id, //发送者ID
'receive' => intval($params['receive_id']), //接收者ID
'source' => intval($params['source']), //接收者类型 1:好友;2:群组
'sender' => $user_id, // 发送者ID
'receive' => intval($params['receive_id']), // 接收者ID
'source' => intval($params['source']), // 接收者类型[1:好友;2:群组;]
'record_id' => $record_id
])
);
@ -575,8 +571,7 @@ class TalkController extends CController
{
$params = $this->request->inputs(['source', 'receive_id', 'lang', 'code']);
$this->validate($params, [
//消息来源1好友消息 2群聊消息
'source' => 'required|in:1,2',
'source' => 'required|in:1,2',// 消息来源[1:好友消息;2:群聊消息;]
'receive_id' => 'required|integer|min:1',
'lang' => 'required',
'code' => 'required'
@ -601,9 +596,9 @@ class TalkController extends CController
// ...消息推送队列
$this->producer->produce(
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
'sender' => $user_id, //发送者ID
'receive' => intval($params['receive_id']), //接收者ID
'source' => intval($params['source']), //接收者类型 1:好友;2:群组
'sender' => $user_id, // 发送者ID
'receive' => intval($params['receive_id']), // 接收者ID
'source' => intval($params['source']), // 接收者类型[1:好友;2:群组;]
'record_id' => $record_id
])
);
@ -627,8 +622,7 @@ class TalkController extends CController
{
$params = $this->request->inputs(['hash_name', 'receive_id', 'source']);
$this->validate($params, [
//消息来源1好友消息 2群聊消息
'source' => 'required|in:1,2',
'source' => 'required|in:1,2',// 消息来源[1:好友消息;2:群聊消息;]
'receive_id' => 'required|integer|min:1',
'hash_name' => 'required',
]);
@ -645,7 +639,6 @@ class TalkController extends CController
$uploadService->makeDirectory($uploadService->driver("files/talks/" . date('Ymd')));
// Copy Files
@copy($uploadService->driver($file->save_dir), $uploadService->driver($save_dir));
$record_id = $this->talkService->createFileMessage([
@ -667,12 +660,12 @@ class TalkController extends CController
return $this->response->fail('表情发送失败');
}
// ...消息推送队列
// ... 消息推送队列
$this->producer->produce(
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
'sender' => $user_id, //发送者ID
'receive' => intval($params['receive_id']), //接收者ID
'source' => intval($params['source']), //接收者类型 1:好友;2:群组
'sender' => $user_id, // 发送者ID
'receive' => intval($params['receive_id']), // 接收者ID
'source' => intval($params['source']), // 接收者类型[1:好友;2:群组;]
'record_id' => $record_id
])
);
@ -695,8 +688,7 @@ class TalkController extends CController
{
$params = $this->request->inputs(['source', 'receive_id', 'emoticon_id']);
$this->validate($params, [
//消息来源1好友消息 2群聊消息
'source' => 'required|in:1,2',
'source' => 'required|in:1,2',// 消息来源[1:好友消息;2:群聊消息;]
'receive_id' => 'required|integer|min:1',
'emoticon_id' => 'required|integer|min:1',
]);
@ -728,12 +720,12 @@ class TalkController extends CController
return $this->response->fail('表情发送失败');
}
// ...消息推送队列
// ... 消息推送队列
$this->producer->produce(
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
'sender' => $user_id, //发送者ID
'receive' => intval($params['receive_id']), //接收者ID
'source' => intval($params['source']), //接收者类型 1:好友;2:群组
'sender' => $user_id, // 发送者ID
'receive' => intval($params['receive_id']), // 接收者ID
'source' => intval($params['source']), // 接收者类型[1:好友;2:群组;]
'record_id' => $record_id
])
);

View File

@ -56,6 +56,7 @@ class UploadController extends CController
$path = '/media/images/avatar/' . date('Ymd') . '/' . uniqid() . date('His') . '.png';
$this->uploadService->makeDirectory($this->uploadService->driver('/media/images/avatar/' . date('Ymd') . '/'));
@file_put_contents($this->uploadService->driver($path), $data);
return $this->response->success(['avatar' => get_media_url($path)]);
}

View File

@ -46,6 +46,7 @@ class UsersController extends CController
public function getUserDetail()
{
$userInfo = $this->userService->findById($this->uid(), ['mobile', 'nickname', 'avatar', 'motto', 'email', 'gender']);
return $this->response->success([
'mobile' => $userInfo->mobile,
'nickname' => $userInfo->nickname,
@ -65,6 +66,7 @@ class UsersController extends CController
public function getUserSetting()
{
$userInfo = $this->userService->findById($this->uid(), ['id', 'nickname', 'avatar', 'motto', 'gender']);
return $this->response->success([
'user_info' => [
'uid' => $userInfo->id,

View File

@ -1,28 +1,14 @@
<?php
declare(strict_types=1);
/**
* 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;
use App\Model\UsersEmoticon;
use Hyperf\HttpServer\Contract\ResponseInterface;
class IndexController extends AbstractController
{
public function index(ResponseInterface $response)
{
$res = (bool)UsersEmoticon::where('user_id', 9999999)->delete();
var_dump($res);
$user = $this->request->input('user', 'Hyperf');
$method = $this->request->getMethod();

View File

@ -156,7 +156,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
stdout_log()->notice("客户端FD:{$fd} 已关闭连接 用户ID为【{$user_id}】,关闭时间:" . date('Y-m-d H:i:s'));
// 解除fd关系
// 删除 fd 绑定关系
$this->socketClientService->removeRelation($fd);
// 判断是否存在异地登录

View File

@ -33,7 +33,7 @@ class AppExceptionHandler extends ExceptionHandler
'message' => 'Internal Server Error.'
], JSON_UNESCAPED_UNICODE);
return $response->withHeader('Server', 'Hyperf')->withStatus(500)->withBody(new SwooleStream($data));
return $response->withHeader('Server', 'Lumen IM')->withStatus(500)->withBody(new SwooleStream($data));
}
/**

View File

@ -29,6 +29,7 @@ class JwtAuthExceptionHandler extends ExceptionHandler
// 阻止异常冒泡
$this->stopPropagation();
return $response->withAddedHeader('content-type', 'application/json; charset=utf-8')->withStatus(401)->withBody(new SwooleStream($data));
}

View File

@ -10,9 +10,11 @@ namespace App\Model;
* @property integer $id 用户ID
* @property string $nickname 用户昵称
* @property string $mobile 登录手机号
* @property string $email 邮箱地址
* @property string $password 登录密码
* @property string $avatar 头像
* @property integer $gender 性别
* @property string $motto 座右铭
* @property integer $created_at 注册时间
* @package App\Model
*/

View File

@ -213,6 +213,7 @@ class ArticleService extends BaseService
* @param int $uid 用户ID
* @param int $class_id 分类ID
* @return bool
* @throws Exception
*/
public function delArticleClass(int $uid, int $class_id)
{

View File

@ -153,7 +153,6 @@ SQL;
$active = ($friendResult->user1 == $info->user_id && $friendResult->user2 == $info->friend_id) ? 1 : 2;
UsersFriend::where('id', $friendResult->id)->update(['active' => $active, 'status' => 1]);
} else {
//好友昵称
$friend_nickname = User::where('id', $info->friend_id)->value('nickname');
UsersFriend::create([

View File

@ -27,7 +27,7 @@ class MailerService
* @param string $template 对应邮件模板
* @return bool
*/
public function send($email, $subject, $template)
public function send(string $email, string $subject, string $template)
{
if ($this->queueSwitch) {
@ -44,7 +44,7 @@ class MailerService
* @param string $template 对应邮件模板
* @return bool
*/
public function realSend($email, $subject, $template)
public function realSend(string $email, string $subject, string $template)
{
try {
return $this->mail($email, $subject, $template);

View File

@ -49,19 +49,19 @@ class MessageHandleService
return;
}
//验证消息类型 私聊|群聊
// 验证消息类型 私聊|群聊
if (!in_array($data['source_type'], [1, 2])) {
return;
}
//验证发送消息用户与接受消息用户之间是否存在好友或群聊关系(后期走缓存)
if ($data['source_type'] == 1) {//私信
//判断发送者和接受者是否是好友关系
// 验证发送消息用户与接受消息用户之间是否存在好友或群聊关系(后期走缓存)
if ($data['source_type'] == 1) {// 私信
// 判断发送者和接受者是否是好友关系
if (!UsersFriend::isFriend((int)$data['send_user'], (int)$data['receive_user'], true)) {
return;
}
} else if ($data['source_type'] == 2) {//群聊
//判断是否属于群成员
} else if ($data['source_type'] == 2) {// 群聊
// 判断是否属于群成员
if (!Group::isMember((int)$data['receive_user'], (int)$data['send_user'])) {
return;
}
@ -94,9 +94,9 @@ class MessageHandleService
$this->producer->produce(
new ChatMessageProducer('event_talk', [
'sender' => intval($data['send_user']), //发送者ID
'receive' => intval($data['receive_user']), //接收者ID
'source' => intval($data['source_type']), //接收者类型 1:好友;2:群组
'sender' => intval($data['send_user']), // 发送者ID
'receive' => intval($data['receive_user']), // 接收者ID
'source' => intval($data['source_type']), // 接收者类型[1:好友;2:群组;]
'record_id' => $result->id
])
);
@ -113,8 +113,8 @@ class MessageHandleService
{
$this->producer->produce(
new ChatMessageProducer('event_keyboard', [
'send_user' => intval($data['send_user']), //发送者ID
'receive_user' => intval($data['receive_user']), //接收者ID
'send_user' => intval($data['send_user']), // 发送者ID
'receive_user' => intval($data['receive_user']), // 接收者ID
])
);
}

View File

@ -175,13 +175,13 @@ class TalkService extends BaseService
$rows[$k]['invite'] = [];
switch ($row['msg_type']) {
case 2://2:文件消息
case 2:// 文件消息
$rows[$k]['file'] = $files[$row['id']] ?? [];
if ($rows[$k]['file']) {
$rows[$k]['file']['file_url'] = get_media_url($rows[$k]['file']['save_dir']);
}
break;
case 3://3:入群消息/退群消息
case 3:// 入群消息/退群消息
if (isset($invites[$row['id']])) {
$rows[$k]['invite'] = [
'type' => $invites[$row['id']]['type'],
@ -199,7 +199,7 @@ class TalkService extends BaseService
}
}
break;
case 4://4:会话记录消息
case 4:// 会话记录消息
if (isset($forwards[$row['id']])) {
$rows[$k]['forward'] = [
'num' => substr_count($forwards[$row['id']]['records_id'], ',') + 1,
@ -207,7 +207,7 @@ class TalkService extends BaseService
];
}
break;
case 5://5:代码块消息
case 5:// 代码块消息
$rows[$k]['code_block'] = $codes[$row['id']] ?? [];
if ($rows[$k]['code_block']) {
$rows[$k]['code_block']['code'] = htmlspecialchars_decode($rows[$k]['code_block']['code']);
@ -298,7 +298,7 @@ class TalkService extends BaseService
'id', 'source', 'msg_type', 'user_id', 'receive_id', 'content', 'is_revoke', 'created_at'
]);
//判断是否有权限查看
// 判断是否有权限查看
if ($result->source == 1 && ($result->user_id != $user_id && $result->receive_id != $user_id)) {
return [];
} else if ($result->source == 2 && !Group::isMember($result->receive_id, $user_id)) {
@ -338,11 +338,11 @@ class TalkService extends BaseService
*/
public function removeRecords(int $user_id, int $source, int $receive_id, array $record_ids)
{
if ($source == 1) {//私聊信息
if ($source == 1) {// 私聊信息
$ids = ChatRecord::whereIn('id', $record_ids)->where(function ($query) use ($user_id, $receive_id) {
$query->where([['user_id', '=', $user_id], ['receive_id', '=', $receive_id]])->orWhere([['user_id', '=', $receive_id], ['receive_id', '=', $user_id]]);
})->where('source', 1)->pluck('id');
} else {//群聊信息
} else {// 群聊信息
$ids = ChatRecord::whereIn('id', $record_ids)->where('source', 2)->pluck('id');
}
@ -379,7 +379,7 @@ class TalkService extends BaseService
$result = ChatRecord::where('id', $record_id)->first(['id', 'source', 'user_id', 'receive_id', 'created_at']);
if (!$result) return [false, '消息记录不存在'];
//判断是否在两分钟之内撤回消息超过2分钟不能撤回消息
// 判断是否在两分钟之内撤回消息超过2分钟不能撤回消息
if ((time() - strtotime($result->created_at) > 120)) {
return [false, '已超过有效的撤回时间', []];
}

View File

@ -64,11 +64,11 @@ class Mail
/**
* @param string $engine
* @param $template
* @param string $template
* @param array $params
* @return string
*/
private function view(string $engine, $template, $params = []): string
private function view(string $engine, string $template, array $params = []): string
{
$config = config('view.config', []);
return container()->get($engine)->render($template, $params, $config);

View File

@ -18,7 +18,7 @@ class RedisLock
/**
* 获取Redis连接
*
* @return mixed|\Redis
* @return \Hyperf\Redis\Redis
*/
public static function getRedis()
{
@ -27,8 +27,8 @@ class RedisLock
/**
* 获得锁,如果锁被占用,阻塞,直到获得锁或者超时。
* -- 1、如果 $timeout 参数为 0,则立即返回锁。
* -- 2、建议 timeout 设置为 0,避免 redis 因为阻塞导致性能下降。请根据实际需求进行设置。
* 1、如果 $timeout 参数为 0,则立即返回锁。
* 2、建议 timeout 设置为 0,避免 redis 因为阻塞导致性能下降。请根据实际需求进行设置。
*
* @param string $key 缓存KEY
* @param string|int $requestId 客户端请求唯一ID
@ -60,7 +60,7 @@ class RedisLock
\Swoole\Coroutine\System::sleep($sleep);
} while (!is_numeric($timeout) || (self::getMicroTime()) < ($start + ($timeout * 1000000)));
return $acquired ? true : false;
return (bool)$acquired;
}
/**