diff --git a/app/Amqp/Consumer/ChatMessageConsumer.php b/app/Amqp/Consumer/ChatMessageConsumer.php index e682242..c8d6397 100644 --- a/app/Amqp/Consumer/ChatMessageConsumer.php +++ b/app/Amqp/Consumer/ChatMessageConsumer.php @@ -1,6 +1,15 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Amqp\Consumer; diff --git a/app/Amqp/Producer/ChatMessageProducer.php b/app/Amqp/Producer/ChatMessageProducer.php index 26fe655..08635ba 100644 --- a/app/Amqp/Producer/ChatMessageProducer.php +++ b/app/Amqp/Producer/ChatMessageProducer.php @@ -1,6 +1,15 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Amqp\Producer; diff --git a/app/Bootstrap/ServerStart.php b/app/Bootstrap/ServerStart.php index eb0142e..3ee8bc5 100644 --- a/app/Bootstrap/ServerStart.php +++ b/app/Bootstrap/ServerStart.php @@ -1,5 +1,14 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Bootstrap; use Hyperf\Framework\Bootstrap\ServerStartCallback; diff --git a/app/Command/RemoveWsCacheCommand.php b/app/Command/RemoveWsCacheCommand.php index 79a5e22..5f591f3 100644 --- a/app/Command/RemoveWsCacheCommand.php +++ b/app/Command/RemoveWsCacheCommand.php @@ -1,6 +1,15 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Command; diff --git a/app/Command/SendEmailCommand.php b/app/Command/SendEmailCommand.php deleted file mode 100644 index 800951c..0000000 --- a/app/Command/SendEmailCommand.php +++ /dev/null @@ -1,41 +0,0 @@ -container = $container; - - parent::__construct('ws:send-email'); - } - - public function configure() - { - parent::configure(); - } - - public function handle() - { - $mail = new Mail(); - $mail->sendEmailCode('837215079@qq.com', '878123', '邮件验证码标题'); - } -} diff --git a/app/Constants/ResponseCode.php b/app/Constants/ResponseCode.php index 317a6b4..72feafa 100644 --- a/app/Constants/ResponseCode.php +++ b/app/Constants/ResponseCode.php @@ -1,5 +1,14 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Constants; /** diff --git a/app/Constants/SocketConstants.php b/app/Constants/SocketConstants.php index c526170..67b9d9d 100644 --- a/app/Constants/SocketConstants.php +++ b/app/Constants/SocketConstants.php @@ -1,4 +1,14 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Constants; diff --git a/app/Controller/AbstractController.php b/app/Controller/AbstractController.php index e5a33df..9ef50b0 100644 --- a/app/Controller/AbstractController.php +++ b/app/Controller/AbstractController.php @@ -1,14 +1,14 @@ + * @link https://github.com/gzydong/hyperf-chat */ namespace App\Controller; diff --git a/app/Controller/Api/V1/ArticleController.php b/app/Controller/Api/V1/ArticleController.php index a0e54ee..7eb95a1 100644 --- a/app/Controller/Api/V1/ArticleController.php +++ b/app/Controller/Api/V1/ArticleController.php @@ -1,4 +1,14 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Controller\Api\V1; @@ -456,13 +466,11 @@ class ArticleController extends CController } $annex['save_dir'] = $path; - $insId = $this->articleService->insertArticleAnnex($this->uid(), (int)$params['article_id'], $annex); - if (!$insId) { + $annex['id'] = $this->articleService->insertArticleAnnex($this->uid(), (int)$params['article_id'], $annex); + if (!$annex['id']) { return $this->response->fail('附件上传失败,请稍后再试...'); } - $annex['id'] = $insId; - return $this->response->success($annex, '笔记附件上传成功...'); } @@ -492,7 +500,7 @@ class ArticleController extends CController */ public function recoverArticleAnnex() { - $params = $this->request->all(); + $params = $this->request->inputs(['annex_id']); $this->validate($params, [ 'annex_id' => 'required|integer|min:0' ]); diff --git a/app/Controller/Api/V1/AuthController.php b/app/Controller/Api/V1/AuthController.php index 7b4afcf..cc507c6 100644 --- a/app/Controller/Api/V1/AuthController.php +++ b/app/Controller/Api/V1/AuthController.php @@ -1,4 +1,14 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Controller\Api\V1; @@ -11,6 +21,7 @@ use App\Constants\ResponseCode; use App\Model\User; use App\Service\UserService; use App\Service\SmsCodeService; +use Phper666\JWTAuth\JWT; /** * 授权相关控制器 @@ -31,13 +42,16 @@ class AuthController extends CController */ private $smsCodeService; + /** + * @Inject + * @var JWT + */ + protected $jwt; + /** * 授权登录接口 * * @RequestMapping(path="login", methods="post") - * - * @return \Psr\Http\Message\ResponseInterface - * @throws \Psr\SimpleCache\InvalidArgumentException */ public function login() { @@ -94,8 +108,6 @@ class AuthController extends CController * 账号注册接口 * * @RequestMapping(path="register", methods="post") - * - * @return \Psr\Http\Message\ResponseInterface */ public function register() { @@ -177,8 +189,6 @@ class AuthController extends CController * 发送验证码 * * @RequestMapping(path="send-verify-code", methods="post") - * - * @return \Psr\Http\Message\ResponseInterface */ public function sendVerifyCode() { diff --git a/app/Controller/Api/V1/CController.php b/app/Controller/Api/V1/CController.php index 97a1843..0db9d51 100644 --- a/app/Controller/Api/V1/CController.php +++ b/app/Controller/Api/V1/CController.php @@ -1,11 +1,20 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Controller\Api\V1; use Hyperf\Di\Annotation\Inject; use App\Controller\AbstractController; use App\Support\Response; -use Phper666\JWTAuth\JWT; /** * 基类控制器 @@ -21,12 +30,6 @@ class CController extends AbstractController */ protected $response; - /** - * @Inject - * @var JWT - */ - protected $jwt; - /** * 获取当前登录用户ID * @@ -34,8 +37,8 @@ class CController extends AbstractController */ public function uid() { - $token = request()->getQueryParams()['token'] ?? null; - $data = $this->jwt->getParserData($token); - return $data['user_id']; + $data = $this->request->getAttribute('auth_data'); + + return $data['user_id'] ?? 0; } } diff --git a/app/Controller/Api/V1/DownloadController.php b/app/Controller/Api/V1/DownloadController.php index ea761f1..bcd2925 100644 --- a/app/Controller/Api/V1/DownloadController.php +++ b/app/Controller/Api/V1/DownloadController.php @@ -1,4 +1,14 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Controller\Api\V1; @@ -18,6 +28,7 @@ use Hyperf\HttpServer\Contract\ResponseInterface; * Class DownloadController * * @Controller(path="/api/v1/download") + * @Middleware(JWTAuthMiddleware::class) * * @package App\Controller\Api\V1 */ diff --git a/app/Controller/Api/V1/EmoticonController.php b/app/Controller/Api/V1/EmoticonController.php index 06ab830..5fd3a9e 100644 --- a/app/Controller/Api/V1/EmoticonController.php +++ b/app/Controller/Api/V1/EmoticonController.php @@ -1,5 +1,14 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Controller\Api\V1; use Hyperf\Di\Annotation\Inject; @@ -12,6 +21,7 @@ use App\Model\Emoticon; use App\Model\EmoticonDetail; use App\Service\EmoticonService; use App\Service\UploadService; +use Psr\Http\Message\ResponseInterface; /** * Class EmoticonController @@ -134,7 +144,7 @@ class EmoticonController extends CController * @RequestMapping(path="upload-emoticon", methods="post") * * @param UploadService $uploadService - * @return \Psr\Http\Message\ResponseInterface + * @return ResponseInterface */ public function uploadEmoticon(UploadService $uploadService) { @@ -156,7 +166,7 @@ class EmoticonController extends CController // 读取图片信息 $imgInfo = @getimagesize($file->getRealPath()); - if(!$imgInfo){ + if (!$imgInfo) { return $this->response->fail('表情包上传失败...'); } diff --git a/app/Controller/Api/V1/GroupController.php b/app/Controller/Api/V1/GroupController.php index a147a39..3ce656f 100644 --- a/app/Controller/Api/V1/GroupController.php +++ b/app/Controller/Api/V1/GroupController.php @@ -1,5 +1,14 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Controller\Api\V1; use Hyperf\Di\Annotation\Inject; diff --git a/app/Controller/Api/V1/TalkController.php b/app/Controller/Api/V1/TalkController.php index 5093c85..1d80aae 100644 --- a/app/Controller/Api/V1/TalkController.php +++ b/app/Controller/Api/V1/TalkController.php @@ -1,5 +1,14 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Controller\Api\V1; use Hyperf\Di\Annotation\Inject; diff --git a/app/Controller/Api/V1/UploadController.php b/app/Controller/Api/V1/UploadController.php index 1533661..38b4315 100644 --- a/app/Controller/Api/V1/UploadController.php +++ b/app/Controller/Api/V1/UploadController.php @@ -1,4 +1,14 @@ + * @link https://github.com/gzydong/hyperf-chat + */ declare(strict_types=1); namespace App\Controller\Api\V1; @@ -10,6 +20,7 @@ use Hyperf\HttpServer\Annotation\Middleware; use App\Middleware\JWTAuthMiddleware; use App\Service\SplitUploadService; use App\Service\UploadService; +use Psr\Http\Message\ResponseInterface; /** * 上传控制器 @@ -25,12 +36,14 @@ class UploadController extends CController { /** * @inject + * * @var UploadService */ private $uploadService; /** * @inject + * * @var SplitUploadService */ private $splitUploadService; @@ -73,8 +86,6 @@ class UploadController extends CController * 文件拆分上传接口 * * @RequestMapping(path="file-subarea-upload", methods="post") - * - * @return \Psr\Http\Message\ResponseInterface */ public function fileSubareaUpload() { diff --git a/app/Controller/Api/V1/UsersController.php b/app/Controller/Api/V1/UsersController.php index 888f883..eb078c9 100644 --- a/app/Controller/Api/V1/UsersController.php +++ b/app/Controller/Api/V1/UsersController.php @@ -1,27 +1,37 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Controller\Api\V1; -use App\Cache\FriendRemarkCache; -use App\Constants\SocketConstants; 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 Hyperf\Amqp\Producer; -use App\Constants\ResponseCode; -use App\Helper\Hash; +use App\Amqp\Producer\ChatMessageProducer; use App\Model\User; use App\Model\UsersChatList; use App\Model\UsersFriend; use App\Support\SendEmailCode; +use App\Helper\Hash; use App\Service\FriendService; use App\Service\UserService; use App\Service\SocketClientService; use App\Service\SmsCodeService; -use App\Amqp\Producer\ChatMessageProducer; use App\Cache\ApplyNumCache; +use App\Cache\FriendRemarkCache; +use App\Constants\SocketConstants; +use App\Constants\ResponseCode; +use Psr\Http\Message\ResponseInterface; /** * Class UsersController @@ -410,7 +420,7 @@ class UsersController extends CController * @RequestMapping(path="change-mobile", methods="post") * * @param SmsCodeService $smsCodeService - * @return \Psr\Http\Message\ResponseInterface + * @return ResponseInterface */ public function editUserMobile(SmsCodeService $smsCodeService) { @@ -482,7 +492,7 @@ class UsersController extends CController * @RequestMapping(path="send-mobile-code", methods="post") * * @param SmsCodeService $smsCodeService - * @return \Psr\Http\Message\ResponseInterface + * @return ResponseInterface */ public function sendMobileCode(SmsCodeService $smsCodeService) { @@ -519,7 +529,7 @@ class UsersController extends CController * @RequestMapping(path="send-change-email-code", methods="post") * * @param SendEmailCode $sendEmailCode - * @return \Psr\Http\Message\ResponseInterface + * @return ResponseInterface */ public function sendChangeEmailCode(SendEmailCode $sendEmailCode) { diff --git a/app/Controller/IndexController.php b/app/Controller/IndexController.php index 8dad321..c990a1e 100644 --- a/app/Controller/IndexController.php +++ b/app/Controller/IndexController.php @@ -1,14 +1,14 @@ + * @link https://github.com/gzydong/hyperf-chat */ namespace App\Controller; diff --git a/app/Controller/WebSocketController.php b/app/Controller/WebSocketController.php index 35b8ff4..c3ed237 100644 --- a/app/Controller/WebSocketController.php +++ b/app/Controller/WebSocketController.php @@ -1,5 +1,15 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Controller; @@ -57,7 +67,9 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos */ private $messageHandleService; - // 消息事件绑定 + /** + * 消息事件绑定 + */ const EVENTS = [ SocketConstants::EVENT_TALK => 'onTalk', SocketConstants::EVENT_KEYBOARD => 'onKeyboard', @@ -78,7 +90,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos // 判断是否存在异地登录 $isOnline = $this->socketClientService->isOnlineAll(intval($userInfo['user_id'])); - // 若开启单点登录,则主动关闭当前连接 + // 若开启单点登录,则主动关闭之前登录的连接 if ($isOnline) { // ... 预留 } @@ -116,7 +128,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos if ($frame->data == 'PING') return; //$result = SocketIOParser::decode($frame->data); - $result = json_decode($frame->data,true); + $result = json_decode($frame->data, true); if (!isset(self::EVENTS[$result['event']])) { return; } diff --git a/app/Exception/ValidateException.php b/app/Exception/ValidateException.php index d4f7bae..0650a06 100644 --- a/app/Exception/ValidateException.php +++ b/app/Exception/ValidateException.php @@ -1,5 +1,14 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Exception; use Hyperf\Server\Exception\ServerException; diff --git a/app/Helper/Hash.php b/app/Helper/Hash.php index 116b35e..e8bf95a 100644 --- a/app/Helper/Hash.php +++ b/app/Helper/Hash.php @@ -1,4 +1,14 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Helper; @@ -12,7 +22,7 @@ class Hash /** * Hash the given value. * - * @param string $value + * @param string $value * @return string */ public static function make(string $value) @@ -23,8 +33,8 @@ class Hash /** * Check the given plain value against a hash. * - * @param string $value - * @param string $hashedValue + * @param string $value + * @param string $hashedValue * @return bool */ public static function check(string $value, string $hashedValue) diff --git a/app/Helper/HashIdsHelper.php b/app/Helper/HashIdsHelper.php index 8cd5337..2a4660d 100644 --- a/app/Helper/HashIdsHelper.php +++ b/app/Helper/HashIdsHelper.php @@ -1,4 +1,14 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Helper; diff --git a/app/Helper/StringHelper.php b/app/Helper/StringHelper.php index 74bce09..bd77e1a 100644 --- a/app/Helper/StringHelper.php +++ b/app/Helper/StringHelper.php @@ -1,6 +1,15 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Helper; diff --git a/app/Middleware/CorsMiddleware.php b/app/Middleware/CorsMiddleware.php index ac22250..3028007 100644 --- a/app/Middleware/CorsMiddleware.php +++ b/app/Middleware/CorsMiddleware.php @@ -1,6 +1,15 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Middleware; diff --git a/app/Middleware/JWTAuthMiddleware.php b/app/Middleware/JWTAuthMiddleware.php index 0c657ab..e99aebe 100644 --- a/app/Middleware/JWTAuthMiddleware.php +++ b/app/Middleware/JWTAuthMiddleware.php @@ -1,5 +1,14 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Middleware; use Hyperf\HttpServer\Contract\RequestInterface; @@ -10,6 +19,7 @@ use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Phper666\JWTAuth\JWT; use Phper666\JWTAuth\Util\JWTUtil; +use Hyperf\Utils\Context; /** * Http Token 授权验证中间件 @@ -68,6 +78,20 @@ class JWTAuthMiddleware implements MiddlewareInterface ]); } + $request = $this->setRequestContext($token); return $handler->handle($request); } + + private function setRequestContext(string $token): ServerRequestInterface + { + $request = Context::get(ServerRequestInterface::class); + + $jwtData = $this->jwt->getParserData($token); + + $request = $request->withAttribute('auth_data', $jwtData); + + Context::set(ServerRequestInterface::class, $request); + + return $request; + } } diff --git a/app/Middleware/WebSocketAuthMiddleware.php b/app/Middleware/WebSocketAuthMiddleware.php index e8c8254..431530c 100644 --- a/app/Middleware/WebSocketAuthMiddleware.php +++ b/app/Middleware/WebSocketAuthMiddleware.php @@ -1,6 +1,15 @@ + * @link https://github.com/gzydong/hyperf-chat + */ namespace App\Middleware; diff --git a/app/Service/ArticleService.php b/app/Service/ArticleService.php index 2562fad..5b9e1b8 100644 --- a/app/Service/ArticleService.php +++ b/app/Service/ArticleService.php @@ -585,6 +585,12 @@ class ArticleService extends BaseService * @param int $uid 用户ID * @param int $annex_id 笔记附件ID * @return bool|int|mixed|null + */ + + /** + * @param int $uid + * @param int $annex_id + * @return bool|int|mixed|null * @throws Exception */ public function foreverDelAnnex(int $uid, int $annex_id) diff --git a/app/Support/Mail.php b/app/Support/Mail.php index 87fd70c..9f4b3e2 100644 --- a/app/Support/Mail.php +++ b/app/Support/Mail.php @@ -14,21 +14,21 @@ class Mail /** * 发送邮件验证码 * - * @param string $eamil 邮箱地址 + * @param string $email 邮箱地址 * @param string $sms_code 验证码 * @param string $title 邮件标题 * @return bool */ - public function sendEmailCode(string $eamil, string $sms_code, string $title) + public function sendEmailCode(string $email, string $sms_code, string $title) { $view = $this->view(config('view.engine'), 'emails.verify-code', [ - 'service_name' => "Lumen IM", + 'service_name' => "邮箱绑定", 'sms_code' => $sms_code, - 'domain' => 'adsfas/asdfa' + 'domain' => config('domain.web_url') ]); try { - return $this->mail($eamil, $title, $view); + return $this->mail($email, $title, $view); } catch (\Exception $e) { return false; } diff --git a/app/Support/SendEmailCode.php b/app/Support/SendEmailCode.php index 08d4b6c..a9ad396 100644 --- a/app/Support/SendEmailCode.php +++ b/app/Support/SendEmailCode.php @@ -55,9 +55,10 @@ class SendEmailCode $sms_code = mt_rand(100000, 999999); } - $this->setCode($key, $sms_code); + $this->setCode($key, $sms_code);; - // ...执行发送 + // ...执行发送(后期使用队列) + container()->get(Mail::class)->sendEmailCode($email, $sms_code, 'Lumen IM(绑定邮箱验证码)'); return true; } diff --git a/app/helper.php b/app/helper.php index fcea62d..5da77b4 100644 --- a/app/helper.php +++ b/app/helper.php @@ -1,4 +1,14 @@ + * @link https://github.com/gzydong/hyperf-chat + */ /* |-------------------------------------------------------------------------- diff --git a/config/config.php b/config/config.php index 5fb6de9..c9af020 100644 --- a/config/config.php +++ b/config/config.php @@ -36,7 +36,7 @@ return [ 'log_level' => [ LogLevel::ALERT, LogLevel::CRITICAL, - LogLevel::DEBUG, + //LogLevel::DEBUG, LogLevel::EMERGENCY, LogLevel::ERROR, LogLevel::INFO, diff --git a/storage/view/emails/verify-code.blade.php b/storage/view/emails/verify-code.blade.php index 82eb6f9..034de3b 100644 --- a/storage/view/emails/verify-code.blade.php +++ b/storage/view/emails/verify-code.blade.php @@ -42,7 +42,7 @@

此为系统邮件,请勿回复
请保管好您的邮箱,避免账号被他人盗用

-

Hyperf Chat在线聊天

+

Lumen IM 在线聊天