初始化

main
gzydong 2020-11-24 23:23:12 +08:00
parent da44242310
commit 0a4b140253
14 changed files with 93 additions and 51 deletions

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Amqp\Consumer; namespace App\Amqp\Consumer;
use App\Model\Chat\ChatRecordsForward;
use App\Model\UsersFriend; use App\Model\UsersFriend;
use Hyperf\Amqp\Annotation\Consumer; use Hyperf\Amqp\Annotation\Consumer;
use Hyperf\Amqp\Result; use Hyperf\Amqp\Result;
@ -191,6 +192,15 @@ class ChatMessageConsumer extends ConsumerMessage
unset($notifyInfo, $userInfo, $membersIds); unset($notifyInfo, $userInfo, $membersIds);
break; break;
case 4://会话记录消息 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; break;
case 5://代码块消息 case 5://代码块消息

View File

@ -9,7 +9,7 @@ use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware; use Hyperf\HttpServer\Annotation\Middleware;
use Phper666\JWTAuth\Middleware\JWTAuthMiddleware; use App\Middleware\JWTAuthMiddleware;
/** /**
* Class ArticleController * Class ArticleController
@ -100,7 +100,7 @@ class ArticleController extends CController
return $this->response->success( return $this->response->success(
$this->articleService->getArticleDetail( $this->articleService->getArticleDetail(
$this->request->input('article_id'), (int)$this->request->input('article_id'),
$this->uid() $this->uid()
) )
); );
@ -139,7 +139,7 @@ class ArticleController extends CController
'class_id' => 'required|integer' '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('笔记分类删除失败...'); return $this->response->fail('笔记分类删除失败...');
} }
@ -163,7 +163,7 @@ class ArticleController extends CController
// 获取Redis锁 // 获取Redis锁
if (RedisLock::lock($lockKey, 0, 3)) { 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锁 // 释放Redis锁
RedisLock::release($lockKey, 0); RedisLock::release($lockKey, 0);
@ -189,7 +189,7 @@ class ArticleController extends CController
'toid' => 'required|integer' '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 return $isTrue
? $this->response->success([], '合并完成...') ? $this->response->success([], '合并完成...')
@ -211,7 +211,7 @@ class ArticleController extends CController
$id = $this->articleService->editArticleTag( $id = $this->articleService->editArticleTag(
$this->uid(), $this->uid(),
$this->request->post('tag_id', 0), (int)$this->request->post('tag_id', 0),
$this->request->post('tag_name', '') $this->request->post('tag_name', '')
); );
@ -232,7 +232,7 @@ class ArticleController extends CController
'tag_id' => 'required|integer|min:0' '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 return $isTrue
? $this->response->success([], '笔记标签删除完成...') ? $this->response->success([], '笔记标签删除完成...')
@ -255,7 +255,7 @@ class ArticleController extends CController
'md_content' => 'required', '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'], 'title' => $params['title'],
'abstract' => mb_substr(strip_tags($params['content']), 0, 200), 'abstract' => mb_substr(strip_tags($params['content']), 0, 200),
'class_id' => $params['class_id'], 'class_id' => $params['class_id'],
@ -281,7 +281,7 @@ class ArticleController extends CController
'article_id' => 'required|integer|min:0' '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 return $isTrue
? $this->response->success([], '笔记删除成功...') ? $this->response->success([], '笔记删除成功...')
@ -300,7 +300,7 @@ class ArticleController extends CController
'article_id' => 'required|integer|min:0' '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 return $isTrue
? $this->response->success([], '笔记恢复成功...') ? $this->response->success([], '笔记恢复成功...')
: $this->response->fail('笔记恢复失败...'); : $this->response->fail('笔记恢复失败...');
@ -355,8 +355,8 @@ class ArticleController extends CController
$isTrue = $this->articleService->setAsteriskArticle( $isTrue = $this->articleService->setAsteriskArticle(
$this->uid(), $this->uid(),
$params['article_id'], (int)$params['article_id'],
$params['type'] (int)$params['type']
); );
return $isTrue return $isTrue
@ -377,7 +377,7 @@ class ArticleController extends CController
'tags' => 'required|array' '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 return $isTrue
? $this->response->success([], 'success...') ? $this->response->success([], 'success...')
: $this->response->fail('编辑失败...'); : $this->response->fail('编辑失败...');
@ -395,7 +395,7 @@ class ArticleController extends CController
'article_id' => 'required|integer|min:0' '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 return $isTrue
? $this->response->success([], '笔记删除成功...') ? $this->response->success([], '笔记删除成功...')
@ -425,7 +425,7 @@ class ArticleController extends CController
'annex_id' => 'required|integer|min:0' '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 return $isTrue
? $this->response->success([], '笔记附件删除成功...') ? $this->response->success([], '笔记附件删除成功...')
@ -444,7 +444,7 @@ class ArticleController extends CController
'annex_id' => 'required|integer|min:0' '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 return $isTrue
? $this->response->success([], '笔记附件恢复成功...') ? $this->response->success([], '笔记附件恢复成功...')
@ -487,7 +487,7 @@ class ArticleController extends CController
'annex_id' => 'required|integer|min:0' '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 return $isTrue
? $this->response->success([], '笔记附件删除成功...') ? $this->response->success([], '笔记附件删除成功...')

View File

@ -11,7 +11,7 @@ use Hyperf\HttpServer\Annotation\Middleware;
use App\Service\UserService; use App\Service\UserService;
use App\Service\SmsCodeService; use App\Service\SmsCodeService;
use Phper666\JWTAuth\JWT; use Phper666\JWTAuth\JWT;
use Phper666\JWTAuth\Middleware\JWTAuthMiddleware; use App\Middleware\JWTAuthMiddleware;
/** /**
* 授权相关控制器 * 授权相关控制器

View File

@ -9,7 +9,7 @@ use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware; use Hyperf\HttpServer\Annotation\Middleware;
use Phper666\JWTAuth\Middleware\JWTAuthMiddleware; use App\Middleware\JWTAuthMiddleware;
use App\Service\EmoticonService; use App\Service\EmoticonService;
/** /**

View File

@ -9,7 +9,7 @@ use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware; use Hyperf\HttpServer\Annotation\Middleware;
use Phper666\JWTAuth\Middleware\JWTAuthMiddleware; use App\Middleware\JWTAuthMiddleware;
use App\Service\GroupService; use App\Service\GroupService;
use App\Model\UsersChatList; use App\Model\UsersChatList;
use App\Model\Group\UsersGroup; use App\Model\Group\UsersGroup;

View File

@ -17,7 +17,7 @@ use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware; use Hyperf\HttpServer\Annotation\Middleware;
use Phper666\JWTAuth\Middleware\JWTAuthMiddleware; use App\Middleware\JWTAuthMiddleware;
use Hyperf\Utils\Str; use Hyperf\Utils\Str;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use App\Amqp\Producer\ChatMessageProducer; use App\Amqp\Producer\ChatMessageProducer;

View File

@ -9,7 +9,7 @@ use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware; use Hyperf\HttpServer\Annotation\Middleware;
use Phper666\JWTAuth\Middleware\JWTAuthMiddleware; use App\Middleware\JWTAuthMiddleware;
/** /**
* 上传控制器 * 上传控制器

View File

@ -7,7 +7,7 @@ use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware; use Hyperf\HttpServer\Annotation\Middleware;
use Phper666\JWTAuth\Middleware\JWTAuthMiddleware; use App\Middleware\JWTAuthMiddleware;
use App\Constants\ResponseCode; use App\Constants\ResponseCode;
use App\Helper\Hash; use App\Helper\Hash;
use App\Model\User; use App\Model\User;

View File

@ -2,43 +2,70 @@
namespace App\Middleware; namespace App\Middleware;
use Phper666\JWTAuth\Middleware\JWTAuthMiddleware as BaseJWTAuthMiddleware; use Hyperf\HttpServer\Contract\RequestInterface;
use Phper666\JWTAuth\Exception\TokenValidException; use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse;
use Phper666\JWTAuth\Util\JWTUtil;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface; 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 * @var RequestInterface
* @param RequestHandlerInterface $handler
* @return ResponseInterface
* @throws InvalidArgumentException
* @throws \Throwable
*/ */
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 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{ {
$isValidToken = false; $isValidToken = true;
// 根据具体业务判断逻辑走向这里假设用户携带的token有效
$token = $request->getHeaderLine('Authorization') ?? ''; // 获取请求token
if (strlen($token) > 0) { $token = $request->getHeaderLine('Authorization') ?? $this->request->input('token', '');
if (!empty($token)) {
try { try {
$token = JWTUtil::handleToken($token); $token = JWTUtil::handleToken($token);
if ($token !== false && $this->jwt->checkToken($token)) { if ($token !== false && $this->jwt->checkToken($token)) {
$isValidToken = true; $isValidToken = true;
} }
} catch (InvalidArgumentException $e) { } catch (\Exception $e) {
throw new TokenValidException('Token authentication does not pass', 401); $isValidToken = false;
} }
} }
if ($isValidToken) { if (!$isValidToken) {
return $this->response->withStatus(401)->json([
'code' => 401,
'message' => 'Token authentication does not pass',
'data' => []
]);
}
return $handler->handle($request); return $handler->handle($request);
} }
throw new TokenValidException('Token authentication does not pass', 401);
}
} }

View File

@ -12,6 +12,11 @@ use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Server\RequestHandlerInterface;
/**
* WebSocket token 授权验证中间件
*
* @package App\Middleware
*/
class WebSocketAuthMiddleware implements MiddlewareInterface class WebSocketAuthMiddleware implements MiddlewareInterface
{ {
/** /**

View File

@ -164,11 +164,11 @@ class ArticleService extends BaseService
* 编辑笔记分类 * 编辑笔记分类
* *
* @param int $uid 用户ID * @param int $uid 用户ID
* @param int $class_id 分类ID * @param int|string $class_id 分类ID
* @param string $class_name 分类名 * @param string $class_name 分类名
* @return bool|int * @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 ($class_id) {
if (!ArticleClass::where('id', $class_id)->where('user_id', $uid)->where('is_default', 0)->update(['class_name' => $class_name])) { if (!ArticleClass::where('id', $class_id)->where('user_id', $uid)->where('is_default', 0)->update(['class_name' => $class_name])) {

View File

@ -30,14 +30,14 @@ class RedisLock
* -- 2、建议 timeout 设置为 0,避免 redis 因为阻塞导致性能下降。请根据实际需求进行设置。 * -- 2、建议 timeout 设置为 0,避免 redis 因为阻塞导致性能下降。请根据实际需求进行设置。
* *
* @param string $key 缓存KEY * @param string $key 缓存KEY
* @param string $requestId 客户端请求唯一ID * @param string|int $requestId 客户端请求唯一ID
* @param integer $lockSecond 锁定时间 单位() * @param integer $lockSecond 锁定时间 单位()
* @param integer $timeout 取锁超时时间。单位()。等于0,如果当前锁被占用,则立即返回失败。如果大于0,则反复尝试获取锁直到达到该超时时间。 * @param integer $timeout 取锁超时时间。单位()。等于0,如果当前锁被占用,则立即返回失败。如果大于0,则反复尝试获取锁直到达到该超时时间。
* @param integer|float $sleep 取锁间隔时间 单位()。当锁为占用状态时。每隔多久尝试去取锁。默认 0.1 秒一次取锁。 * @param integer|float $sleep 取锁间隔时间 单位()。当锁为占用状态时。每隔多久尝试去取锁。默认 0.1 秒一次取锁。
* @return bool * @return bool
* @throws \Exception * @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)) { if (empty($key)) {
throw new \Exception('获取锁的KEY值没有设置'); throw new \Exception('获取锁的KEY值没有设置');
@ -66,10 +66,10 @@ class RedisLock
* 释放锁 * 释放锁
* *
* @param string $key 被加锁的KEY * @param string $key 被加锁的KEY
* @param string $requestId 客户端请求唯一ID * @param string|int $requestId 客户端请求唯一ID
* @return bool * @return bool
*/ */
public static function release(string $key, string $requestId) public static function release(string $key,$requestId)
{ {
if (strlen($key) === 0) { if (strlen($key) === 0) {
return false; return false;

View File

@ -19,6 +19,6 @@ return [
CorsMiddleware::class CorsMiddleware::class
], ],
'ws' => [ 'ws' => [
WebSocketAuthMiddleware::class
] ]
]; ];

View File

@ -46,7 +46,7 @@ return [
], ],
'settings' => [ 'settings' => [
'enable_coroutine' => true, 'enable_coroutine' => true,
'worker_num' => swoole_cpu_num(), 'worker_num' => 1,
'pid_file' => BASE_PATH . '/runtime/hyperf.pid', 'pid_file' => BASE_PATH . '/runtime/hyperf.pid',
'open_tcp_nodelay' => true, 'open_tcp_nodelay' => true,
'max_coroutine' => 100000, 'max_coroutine' => 100000,