From 0a4b140253d36e1d553cbe48b43cdb2fd93a328d Mon Sep 17 00:00:00 2001 From: gzydong Date: Tue, 24 Nov 2020 23:23:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Amqp/Consumer/ChatMessageConsumer.php | 10 +++ app/Controller/Api/V1/ArticleController.php | 34 +++++----- app/Controller/Api/V1/AuthController.php | 2 +- app/Controller/Api/V1/EmoticonController.php | 2 +- app/Controller/Api/V1/GroupController.php | 2 +- app/Controller/Api/V1/TalkController.php | 2 +- app/Controller/Api/V1/UploadController.php | 4 +- app/Controller/Api/V1/UsersController.php | 2 +- app/Middleware/JWTAuthMiddleware.php | 65 ++++++++++++++------ app/Middleware/WebSocketAuthMiddleware.php | 5 ++ app/Service/ArticleService.php | 4 +- app/Support/RedisLock.php | 8 +-- config/autoload/middlewares.php | 2 +- config/autoload/server.php | 2 +- 14 files changed, 93 insertions(+), 51 deletions(-) diff --git a/app/Amqp/Consumer/ChatMessageConsumer.php b/app/Amqp/Consumer/ChatMessageConsumer.php index 3b3c714..69d1961 100644 --- a/app/Amqp/Consumer/ChatMessageConsumer.php +++ b/app/Amqp/Consumer/ChatMessageConsumer.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Amqp\Consumer; +use App\Model\Chat\ChatRecordsForward; use App\Model\UsersFriend; use Hyperf\Amqp\Annotation\Consumer; use Hyperf\Amqp\Result; @@ -191,6 +192,15 @@ class ChatMessageConsumer extends ConsumerMessage unset($notifyInfo, $userInfo, $membersIds); break; case 4://会话记录消息 + $forward = ['num' => 0,'list' => []]; + + $forwardInfo = ChatRecordsForward::where('record_id', $result->id)->first(['records_id', 'text']); + if ($forwardInfo) { + $forward = [ + 'num' => substr_count($forwardInfo->records_id, ',') + 1, + 'list' => json_decode($forwardInfo->text, true) ?? [] + ]; + } break; case 5://代码块消息 diff --git a/app/Controller/Api/V1/ArticleController.php b/app/Controller/Api/V1/ArticleController.php index f35e66d..60163a2 100644 --- a/app/Controller/Api/V1/ArticleController.php +++ b/app/Controller/Api/V1/ArticleController.php @@ -9,7 +9,7 @@ use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\Middleware; -use Phper666\JWTAuth\Middleware\JWTAuthMiddleware; +use App\Middleware\JWTAuthMiddleware; /** * Class ArticleController @@ -100,7 +100,7 @@ class ArticleController extends CController return $this->response->success( $this->articleService->getArticleDetail( - $this->request->input('article_id'), + (int)$this->request->input('article_id'), $this->uid() ) ); @@ -139,7 +139,7 @@ class ArticleController extends CController 'class_id' => 'required|integer' ]); - if (!$this->articleService->delArticleClass($this->uid(), $params['class_id'])) { + if (!$this->articleService->delArticleClass($this->uid(), (int)$params['class_id'])) { return $this->response->fail('笔记分类删除失败...'); } @@ -163,7 +163,7 @@ class ArticleController extends CController // 获取Redis锁 if (RedisLock::lock($lockKey, 0, 3)) { - $isTrue = $this->articleService->articleClassSort($this->uid(), $params['class_id'], $params['sort_type']); + $isTrue = $this->articleService->articleClassSort($this->uid(), (int)$params['class_id'], (int)$params['sort_type']); // 释放Redis锁 RedisLock::release($lockKey, 0); @@ -189,7 +189,7 @@ class ArticleController extends CController 'toid' => 'required|integer' ]); - $isTrue = $this->articleService->mergeArticleClass($this->uid(), $params['class_id'], $params['toid']); + $isTrue = $this->articleService->mergeArticleClass($this->uid(), (int)$params['class_id'], (int)$params['toid']); return $isTrue ? $this->response->success([], '合并完成...') @@ -211,7 +211,7 @@ class ArticleController extends CController $id = $this->articleService->editArticleTag( $this->uid(), - $this->request->post('tag_id', 0), + (int)$this->request->post('tag_id', 0), $this->request->post('tag_name', '') ); @@ -232,7 +232,7 @@ class ArticleController extends CController 'tag_id' => 'required|integer|min:0' ]); - $isTrue = $this->articleService->delArticleTags($this->uid(), $params['tag_id']); + $isTrue = $this->articleService->delArticleTags($this->uid(), (int)$params['tag_id']); return $isTrue ? $this->response->success([], '笔记标签删除完成...') @@ -255,7 +255,7 @@ class ArticleController extends CController 'md_content' => 'required', ]); - $id = $this->articleService->editArticle($this->uid(), $params['article_id'], [ + $id = $this->articleService->editArticle($this->uid(), (int)$params['article_id'], [ 'title' => $params['title'], 'abstract' => mb_substr(strip_tags($params['content']), 0, 200), 'class_id' => $params['class_id'], @@ -281,7 +281,7 @@ class ArticleController extends CController 'article_id' => 'required|integer|min:0' ]); - $isTrue = $this->articleService->updateArticleStatus($this->uid(), $params['article_id'], 2); + $isTrue = $this->articleService->updateArticleStatus($this->uid(), (int)$params['article_id'], 2); return $isTrue ? $this->response->success([], '笔记删除成功...') @@ -300,7 +300,7 @@ class ArticleController extends CController 'article_id' => 'required|integer|min:0' ]); - $isTrue = $this->articleService->updateArticleStatus($this->uid(), $params['article_id'], 1); + $isTrue = $this->articleService->updateArticleStatus($this->uid(), (int)$params['article_id'], 1); return $isTrue ? $this->response->success([], '笔记恢复成功...') : $this->response->fail('笔记恢复失败...'); @@ -355,8 +355,8 @@ class ArticleController extends CController $isTrue = $this->articleService->setAsteriskArticle( $this->uid(), - $params['article_id'], - $params['type'] + (int)$params['article_id'], + (int)$params['type'] ); return $isTrue @@ -377,7 +377,7 @@ class ArticleController extends CController 'tags' => 'required|array' ]); - $isTrue = $this->articleService->updateArticleTag($this->uid(), $params['article_id'], $params['tags']); + $isTrue = $this->articleService->updateArticleTag($this->uid(), (int)$params['article_id'], $params['tags']); return $isTrue ? $this->response->success([], 'success...') : $this->response->fail('编辑失败...'); @@ -395,7 +395,7 @@ class ArticleController extends CController 'article_id' => 'required|integer|min:0' ]); - $isTrue = $this->articleService->foreverDelArticle($this->uid(), $params['article_id']); + $isTrue = $this->articleService->foreverDelArticle($this->uid(), (int)$params['article_id']); return $isTrue ? $this->response->success([], '笔记删除成功...') @@ -425,7 +425,7 @@ class ArticleController extends CController 'annex_id' => 'required|integer|min:0' ]); - $isTrue = $this->articleService->updateArticleAnnexStatus($this->uid(), $params['annex_id'], 2); + $isTrue = $this->articleService->updateArticleAnnexStatus($this->uid(), (int)$params['annex_id'], 2); return $isTrue ? $this->response->success([], '笔记附件删除成功...') @@ -444,7 +444,7 @@ class ArticleController extends CController 'annex_id' => 'required|integer|min:0' ]); - $isTrue = $this->articleService->updateArticleAnnexStatus($this->uid(), $params['annex_id'], 1); + $isTrue = $this->articleService->updateArticleAnnexStatus($this->uid(), (int)$params['annex_id'], 1); return $isTrue ? $this->response->success([], '笔记附件恢复成功...') @@ -487,7 +487,7 @@ class ArticleController extends CController 'annex_id' => 'required|integer|min:0' ]); - $isTrue = $this->articleService->foreverDelAnnex($this->uid(), $params['annex_id']); + $isTrue = $this->articleService->foreverDelAnnex($this->uid(), (int)$params['annex_id']); return $isTrue ? $this->response->success([], '笔记附件删除成功...') diff --git a/app/Controller/Api/V1/AuthController.php b/app/Controller/Api/V1/AuthController.php index fd43516..b4dd7b0 100644 --- a/app/Controller/Api/V1/AuthController.php +++ b/app/Controller/Api/V1/AuthController.php @@ -11,7 +11,7 @@ use Hyperf\HttpServer\Annotation\Middleware; use App\Service\UserService; use App\Service\SmsCodeService; use Phper666\JWTAuth\JWT; -use Phper666\JWTAuth\Middleware\JWTAuthMiddleware; +use App\Middleware\JWTAuthMiddleware; /** * 授权相关控制器 diff --git a/app/Controller/Api/V1/EmoticonController.php b/app/Controller/Api/V1/EmoticonController.php index fa39905..be49232 100644 --- a/app/Controller/Api/V1/EmoticonController.php +++ b/app/Controller/Api/V1/EmoticonController.php @@ -9,7 +9,7 @@ use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\Middleware; -use Phper666\JWTAuth\Middleware\JWTAuthMiddleware; +use App\Middleware\JWTAuthMiddleware; use App\Service\EmoticonService; /** diff --git a/app/Controller/Api/V1/GroupController.php b/app/Controller/Api/V1/GroupController.php index 7d22cf7..fd57419 100644 --- a/app/Controller/Api/V1/GroupController.php +++ b/app/Controller/Api/V1/GroupController.php @@ -9,7 +9,7 @@ use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\Middleware; -use Phper666\JWTAuth\Middleware\JWTAuthMiddleware; +use App\Middleware\JWTAuthMiddleware; use App\Service\GroupService; use App\Model\UsersChatList; use App\Model\Group\UsersGroup; diff --git a/app/Controller/Api/V1/TalkController.php b/app/Controller/Api/V1/TalkController.php index eac6dd2..94e0ba3 100644 --- a/app/Controller/Api/V1/TalkController.php +++ b/app/Controller/Api/V1/TalkController.php @@ -17,7 +17,7 @@ use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\Middleware; -use Phper666\JWTAuth\Middleware\JWTAuthMiddleware; +use App\Middleware\JWTAuthMiddleware; use Hyperf\Utils\Str; use Psr\Http\Message\ResponseInterface; use App\Amqp\Producer\ChatMessageProducer; diff --git a/app/Controller/Api/V1/UploadController.php b/app/Controller/Api/V1/UploadController.php index 47d3a8a..b394a83 100644 --- a/app/Controller/Api/V1/UploadController.php +++ b/app/Controller/Api/V1/UploadController.php @@ -9,7 +9,7 @@ use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\Middleware; -use Phper666\JWTAuth\Middleware\JWTAuthMiddleware; +use App\Middleware\JWTAuthMiddleware; /** * 上传控制器 @@ -51,7 +51,7 @@ class UploadController extends CController @file_put_contents($this->uploadService->driver($path), $data); return $this->response->success(['avatar' => get_media_url($path)]); } - + /** * 获取拆分文件信息 * diff --git a/app/Controller/Api/V1/UsersController.php b/app/Controller/Api/V1/UsersController.php index 6347d3e..3bf4e34 100644 --- a/app/Controller/Api/V1/UsersController.php +++ b/app/Controller/Api/V1/UsersController.php @@ -7,7 +7,7 @@ use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\Middleware; -use Phper666\JWTAuth\Middleware\JWTAuthMiddleware; +use App\Middleware\JWTAuthMiddleware; use App\Constants\ResponseCode; use App\Helper\Hash; use App\Model\User; diff --git a/app/Middleware/JWTAuthMiddleware.php b/app/Middleware/JWTAuthMiddleware.php index cbdf166..83ca45f 100644 --- a/app/Middleware/JWTAuthMiddleware.php +++ b/app/Middleware/JWTAuthMiddleware.php @@ -2,43 +2,70 @@ namespace App\Middleware; -use Phper666\JWTAuth\Middleware\JWTAuthMiddleware as BaseJWTAuthMiddleware; -use Phper666\JWTAuth\Exception\TokenValidException; -use Phper666\JWTAuth\Util\JWTUtil; +use Hyperf\HttpServer\Contract\RequestInterface; +use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; -use Psr\SimpleCache\InvalidArgumentException; +use Phper666\JWTAuth\JWT; +use Phper666\JWTAuth\Util\JWTUtil; -class JWTAuthMiddleware extends BaseJWTAuthMiddleware +/** + * Http Token 授权验证中间件 + * + * @package App\Middleware + */ +class JWTAuthMiddleware implements MiddlewareInterface { /** - * @param ServerRequestInterface $request - * @param RequestHandlerInterface $handler - * @return ResponseInterface - * @throws InvalidArgumentException - * @throws \Throwable + * @var RequestInterface */ + protected $request; + + /** + * @var HttpResponse + */ + protected $response; + + /** + * @var JWT + */ + protected $jwt; + + public function __construct(HttpResponse $response, RequestInterface $request, JWT $jwt) + { + $this->response = $response; + $this->request = $request; + $this->jwt = $jwt; + } + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { - $isValidToken = false; - // 根据具体业务判断逻辑走向,这里假设用户携带的token有效 - $token = $request->getHeaderLine('Authorization') ?? ''; - if (strlen($token) > 0) { + $isValidToken = true; + + // 获取请求token + $token = $request->getHeaderLine('Authorization') ?? $this->request->input('token', ''); + + if (!empty($token)) { try { $token = JWTUtil::handleToken($token); if ($token !== false && $this->jwt->checkToken($token)) { $isValidToken = true; } - } catch (InvalidArgumentException $e) { - throw new TokenValidException('Token authentication does not pass', 401); + } catch (\Exception $e) { + $isValidToken = false; } } - if ($isValidToken) { - return $handler->handle($request); + if (!$isValidToken) { + return $this->response->withStatus(401)->json([ + 'code' => 401, + 'message' => 'Token authentication does not pass', + 'data' => [] + ]); } - throw new TokenValidException('Token authentication does not pass', 401); + return $handler->handle($request); } } diff --git a/app/Middleware/WebSocketAuthMiddleware.php b/app/Middleware/WebSocketAuthMiddleware.php index caaa0e9..a09ea30 100644 --- a/app/Middleware/WebSocketAuthMiddleware.php +++ b/app/Middleware/WebSocketAuthMiddleware.php @@ -12,6 +12,11 @@ use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; +/** + * WebSocket token 授权验证中间件 + * + * @package App\Middleware + */ class WebSocketAuthMiddleware implements MiddlewareInterface { /** diff --git a/app/Service/ArticleService.php b/app/Service/ArticleService.php index e5fb7d8..9f86468 100644 --- a/app/Service/ArticleService.php +++ b/app/Service/ArticleService.php @@ -164,11 +164,11 @@ class ArticleService extends BaseService * 编辑笔记分类 * * @param int $uid 用户ID - * @param int $class_id 分类ID + * @param int|string $class_id 分类ID * @param string $class_name 分类名 * @return bool|int */ - public function editArticleClass(int $uid, int $class_id, string $class_name) + public function editArticleClass(int $uid, $class_id, string $class_name) { if ($class_id) { if (!ArticleClass::where('id', $class_id)->where('user_id', $uid)->where('is_default', 0)->update(['class_name' => $class_name])) { diff --git a/app/Support/RedisLock.php b/app/Support/RedisLock.php index ea68c83..23a2b64 100644 --- a/app/Support/RedisLock.php +++ b/app/Support/RedisLock.php @@ -30,14 +30,14 @@ class RedisLock * -- 2、建议 timeout 设置为 0,避免 redis 因为阻塞导致性能下降。请根据实际需求进行设置。 * * @param string $key 缓存KEY - * @param string $requestId 客户端请求唯一ID + * @param string|int $requestId 客户端请求唯一ID * @param integer $lockSecond 锁定时间 单位(秒) * @param integer $timeout 取锁超时时间。单位(秒)。等于0,如果当前锁被占用,则立即返回失败。如果大于0,则反复尝试获取锁直到达到该超时时间。 * @param integer|float $sleep 取锁间隔时间 单位(秒)。当锁为占用状态时。每隔多久尝试去取锁。默认 0.1 秒一次取锁。 * @return bool * @throws \Exception */ - public static function lock(string $key, string $requestId, $lockSecond = 20, $timeout = 0, $sleep = 0.1) + public static function lock(string $key,$requestId, $lockSecond = 20, $timeout = 0, $sleep = 0.1) { if (empty($key)) { throw new \Exception('获取锁的KEY值没有设置'); @@ -66,10 +66,10 @@ class RedisLock * 释放锁 * * @param string $key 被加锁的KEY - * @param string $requestId 客户端请求唯一ID + * @param string|int $requestId 客户端请求唯一ID * @return bool */ - public static function release(string $key, string $requestId) + public static function release(string $key,$requestId) { if (strlen($key) === 0) { return false; diff --git a/config/autoload/middlewares.php b/config/autoload/middlewares.php index 0891d9d..762a01e 100644 --- a/config/autoload/middlewares.php +++ b/config/autoload/middlewares.php @@ -19,6 +19,6 @@ return [ CorsMiddleware::class ], 'ws' => [ - + WebSocketAuthMiddleware::class ] ]; diff --git a/config/autoload/server.php b/config/autoload/server.php index 3248d6a..1e4513f 100644 --- a/config/autoload/server.php +++ b/config/autoload/server.php @@ -46,7 +46,7 @@ return [ ], 'settings' => [ 'enable_coroutine' => true, - 'worker_num' => swoole_cpu_num(), + 'worker_num' => 1, 'pid_file' => BASE_PATH . '/runtime/hyperf.pid', 'open_tcp_nodelay' => true, 'max_coroutine' => 100000,