From 84b9900f7dd83eb733217b908ede3a2b1c505cdb Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Sun, 29 Nov 2020 17:39:24 +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 --- README.md | 1 + app/Controller/Api/V1/ArticleController.php | 8 ++-- app/Controller/Api/V1/AuthController.php | 17 +++----- app/Controller/Api/V1/CController.php | 13 +++++-- app/Controller/Api/V1/DownloadController.php | 4 +- app/Controller/Api/V1/EmoticonController.php | 36 +++++++++-------- app/Controller/Api/V1/GroupController.php | 20 +++++----- app/Controller/Api/V1/TalkController.php | 4 +- app/Controller/Api/V1/UploadController.php | 3 +- app/Controller/Api/V1/UsersController.php | 39 +++++++++++-------- .../ValidatorFactoryResolvedListener.php | 38 ++++++++++++++++++ app/Support/Response.php | 2 +- app/helper.php | 19 +++++++++ storage/languages/en/validation.php | 2 + storage/languages/zh_CN/validation.php | 2 + 15 files changed, 139 insertions(+), 69 deletions(-) create mode 100644 app/Listener/ValidatorFactoryResolvedListener.php diff --git a/README.md b/README.md index 1f276be..cb71b3f 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ - JSON - PDO - Redis >= 5.0.0 + - AMQP ## 4、项目安装 [接口文档](https://docs.apipost.cn/view/9c75130d7006e6e5#3184466) diff --git a/app/Controller/Api/V1/ArticleController.php b/app/Controller/Api/V1/ArticleController.php index f7ae74e..84db004 100644 --- a/app/Controller/Api/V1/ArticleController.php +++ b/app/Controller/Api/V1/ArticleController.php @@ -103,7 +103,7 @@ class ArticleController extends CController public function getArticleDetail() { $this->validate($this->request->all(), [ - 'article_id' => 'required|integer', + 'article_id' => 'required|integer' ]); return $this->response->success( @@ -124,7 +124,7 @@ class ArticleController extends CController $params = $this->request->all(); $this->validate($params, [ 'class_id' => 'required|integer', - 'class_name' => 'required', + 'class_name' => 'required' ]); $class_id = $this->articleService->editArticleClass($this->uid(), $params['class_id'], $params['class_name']); @@ -260,7 +260,7 @@ class ArticleController extends CController 'class_id' => 'required|integer|min:0', 'title' => 'required|max:255', 'content' => 'required', - 'md_content' => 'required', + 'md_content' => 'required' ]); $id = $this->articleService->editArticle($this->uid(), (int)$params['article_id'], [ @@ -309,6 +309,7 @@ class ArticleController extends CController ]); $isTrue = $this->articleService->updateArticleStatus($this->uid(), (int)$params['article_id'], 1); + return $isTrue ? $this->response->success([], '笔记恢复成功...') : $this->response->fail('笔记恢复失败...'); @@ -406,6 +407,7 @@ class ArticleController extends CController ]); $isTrue = $this->articleService->updateArticleTag($this->uid(), (int)$params['article_id'], $params['tags']); + return $isTrue ? $this->response->success([], 'success...') : $this->response->fail('编辑失败...'); diff --git a/app/Controller/Api/V1/AuthController.php b/app/Controller/Api/V1/AuthController.php index cc87a1e..7372990 100644 --- a/app/Controller/Api/V1/AuthController.php +++ b/app/Controller/Api/V1/AuthController.php @@ -7,7 +7,6 @@ use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\Middleware; use App\Middleware\JWTAuthMiddleware; -use Phper666\JWTAuth\JWT; use App\Constants\ResponseCode; use App\Model\User; use App\Service\UserService; @@ -26,12 +25,6 @@ class AuthController extends CController */ private $userService; - /** - * @Inject - * @var JWT - */ - private $jwt; - /** * @Inject * @var SmsCodeService @@ -43,7 +36,6 @@ class AuthController extends CController * * @RequestMapping(path="login", methods="post") * - * @param JWT $jwt * @return \Psr\Http\Message\ResponseInterface * @throws \Psr\SimpleCache\InvalidArgumentException */ @@ -61,7 +53,7 @@ class AuthController extends CController ); if (!$userInfo) { - return $this->response->fail('账号不存在或密码填写错误...', ResponseCode::FAIL); + return $this->response->fail('账号不存在或密码填写错误...'); } try { @@ -85,7 +77,7 @@ class AuthController extends CController 'motto' => $userInfo['motto'], 'email' => $userInfo['email'], ] - ], '登录成功...'); + ]); } /** @@ -154,12 +146,12 @@ class AuthController extends CController ]); if (!$this->smsCodeService->check('forget_password', $params['mobile'], $params['sms_code'])) { - return $this->response->fail('验证码填写错误...', ResponseCode::FAIL); + return $this->response->fail('验证码填写错误'); } $isTrue = $this->userService->resetPassword($params['mobile'], $params['password']); if (!$isTrue) { - return $this->response->fail('重置密码失败...', ResponseCode::FAIL); + return $this->response->fail('重置密码失败'); } // 删除验证码缓存 @@ -217,6 +209,7 @@ class AuthController extends CController [$isTrue, $result] = $this->smsCodeService->send($params['type'], $params['mobile']); if (!$isTrue) { // ... 处理发送失败逻辑,当前默认发送成功 + return $this->response->fail('验证码发送失败'); } // 测试环境下直接返回验证码 diff --git a/app/Controller/Api/V1/CController.php b/app/Controller/Api/V1/CController.php index 0bc97ac..97a1843 100644 --- a/app/Controller/Api/V1/CController.php +++ b/app/Controller/Api/V1/CController.php @@ -21,14 +21,21 @@ class CController extends AbstractController */ protected $response; + /** + * @Inject + * @var JWT + */ + protected $jwt; + /** * 获取当前登录用户ID * * @return int */ - public function uid(){ - $token = request()->getQueryParams()['token']??null; - $data = container()->get(JWT::class)->getParserData($token); + public function uid() + { + $token = request()->getQueryParams()['token'] ?? null; + $data = $this->jwt->getParserData($token); return $data['user_id']; } } diff --git a/app/Controller/Api/V1/DownloadController.php b/app/Controller/Api/V1/DownloadController.php index 4ffdf87..7d49914 100644 --- a/app/Controller/Api/V1/DownloadController.php +++ b/app/Controller/Api/V1/DownloadController.php @@ -37,7 +37,7 @@ class DownloadController extends CController { $params = $this->request->inputs(['cr_id']); $this->validate($params, [ - 'cr_id' => 'required|integer', + 'cr_id' => 'required|integer' ]); $recordsInfo = ChatRecord::select(['msg_type', 'source', 'user_id', 'receive_id'])->where('id', $params['cr_id'])->first(); @@ -81,7 +81,7 @@ class DownloadController extends CController { $params = $this->request->inputs(['annex_id']); $this->validate($params, [ - 'annex_id' => 'required|integer', + 'annex_id' => 'required|integer' ]); $info = ArticleAnnex::select(['save_dir', 'original_name']) diff --git a/app/Controller/Api/V1/EmoticonController.php b/app/Controller/Api/V1/EmoticonController.php index 2fd63ef..c3f602a 100644 --- a/app/Controller/Api/V1/EmoticonController.php +++ b/app/Controller/Api/V1/EmoticonController.php @@ -11,6 +11,7 @@ use App\Constants\ResponseCode; use App\Model\Emoticon; use App\Model\EmoticonDetail; use App\Service\EmoticonService; +use App\Service\UploadService; /** * Class EmoticonController @@ -26,7 +27,7 @@ class EmoticonController extends CController * @Inject * @var EmoticonService */ - public $emoticonService; + private $emoticonService; /** * 获取用户表情包列表 @@ -91,7 +92,7 @@ class EmoticonController extends CController $params = $this->request->all(); $this->validate($params, [ 'emoticon_id' => 'required|integer', - 'type' => 'required|in:1,2', + 'type' => 'required|in:1,2' ]); $user_id = $this->uid(); @@ -131,8 +132,11 @@ class EmoticonController extends CController * 自定义上传表情包 * * @RequestMapping(path="upload-emoticon", methods="post") + * + * @param UploadService $uploadService + * @return \Psr\Http\Message\ResponseInterface */ - public function uploadEmoticon() + public function uploadEmoticon(UploadService $uploadService) { $file = $this->request->file('emoticon'); if (!$file->isValid()) { @@ -150,14 +154,16 @@ class EmoticonController extends CController ); } - $save_path = ''; - - $user_id = $this->uid(); + // 读取图片信息 $imgInfo = getimagesize($file->getPath()); - $filename = create_image_name($ext, $imgInfo[0], $imgInfo[1]); + + $save_path = $uploadService->media($file, 'media/images/emoticon', create_image_name($ext, $imgInfo[0], $imgInfo[1])); + if (!$save_path) { + return $this->response->fail('图片上传失败'); + } $result = EmoticonDetail::create([ - 'user_id' => $user_id, + 'user_id' => $this->uid(), 'url' => $save_path, 'file_suffix' => $ext, 'file_size' => $file->getSize(), @@ -181,7 +187,7 @@ class EmoticonController extends CController */ public function collectEmoticon() { - $params = $this->request->all(); + $params = $this->request->inputs(['record_id']); $this->validate($params, [ 'record_id' => 'required|integer' ]); @@ -204,15 +210,13 @@ class EmoticonController extends CController */ public function delCollectEmoticon() { - $params = $this->request->all(); + $params = $this->request->inputs(['ids']); $this->validate($params, [ - 'ids' => 'required' + 'ids' => 'required|ids' ]); - $ids = explode(',', trim($params['ids'])); - - return $this->emoticonService->deleteCollect($this->uid(), $ids) ? - $this->response->success([], 'success') : - $this->response->fail('fail'); + return $this->emoticonService->deleteCollect($this->uid(), parse_ids($params['ids'])) ? + $this->response->success([]) : + $this->response->fail(); } } diff --git a/app/Controller/Api/V1/GroupController.php b/app/Controller/Api/V1/GroupController.php index 1304e5b..d7c72d1 100644 --- a/app/Controller/Api/V1/GroupController.php +++ b/app/Controller/Api/V1/GroupController.php @@ -31,7 +31,7 @@ class GroupController extends CController * @Inject * @var GroupService */ - public $groupService; + private $groupService; /** * @Inject @@ -57,11 +57,10 @@ class GroupController extends CController $params = $this->request->inputs(['group_name', 'uids']); $this->validate($params, [ 'group_name' => 'required', - 'uids' => 'required', + 'uids' => 'required|ids' ]); - $friend_ids = array_filter(explode(',', $params['uids'])); - $friend_ids = array_unique($friend_ids); + $friend_ids = parse_ids($params['uids']); $user_id = $this->uid(); [$isTrue, $data] = $this->groupService->create($user_id, [ @@ -92,7 +91,7 @@ class GroupController extends CController return $this->response->success([ 'group_id' => $data['group_id'] - ], '群聊创建成功...'); + ]); } /** @@ -104,7 +103,7 @@ class GroupController extends CController { $params = $this->request->inputs(['group_id']); $this->validate($params, [ - 'group_id' => 'required|integer', + 'group_id' => 'required|integer' ]); $isTrue = $this->groupService->dismiss($params['group_id'], $this->uid()); @@ -129,11 +128,10 @@ class GroupController extends CController $params = $this->request->inputs(['group_id', 'uids']); $this->validate($params, [ 'group_id' => 'required|integer', - 'uids' => 'required', + 'uids' => 'required|ids' ]); - $uids = array_filter(explode(',', $params['uids'])); - $uids = array_unique($uids); + $uids = parse_ids($params['uids']); $user_id = $this->uid(); [$isTrue, $record_id] = $this->groupService->invite($user_id, $params['group_id'], $uids); @@ -205,7 +203,7 @@ class GroupController extends CController 'group_id' => 'required|integer', 'group_name' => 'required', 'group_profile' => 'required', - 'avatar' => 'required', + 'avatar' => 'required' ]); $result = UsersGroup::where('id', $params['group_id'])->where('user_id', $this->uid())->update([ @@ -421,7 +419,7 @@ class GroupController extends CController 'group_id' => 'required|integer', 'notice_id' => 'required|integer', 'title' => 'required', - 'content' => 'required', + 'content' => 'required' ]); $user_id = $this->uid(); diff --git a/app/Controller/Api/V1/TalkController.php b/app/Controller/Api/V1/TalkController.php index c6b1a6a..154cc2f 100644 --- a/app/Controller/Api/V1/TalkController.php +++ b/app/Controller/Api/V1/TalkController.php @@ -287,9 +287,9 @@ class TalkController extends CController //转发方方式[1:逐条转发;2:合并转发] 'forward_mode' => 'required|in:1,2', //转发的好友的ID -// 'receive_user_ids' => 'array', + //'receive_user_ids' => 'array', //转发的群聊ID -// 'receive_group_ids' => 'array', + //'receive_group_ids' => 'array', ]); $user_id = $this->uid(); diff --git a/app/Controller/Api/V1/UploadController.php b/app/Controller/Api/V1/UploadController.php index 2113749..1533661 100644 --- a/app/Controller/Api/V1/UploadController.php +++ b/app/Controller/Api/V1/UploadController.php @@ -39,7 +39,6 @@ class UploadController extends CController * 图片文件流上传接口 * * @RequestMapping(path="file-stream", methods="post") - * */ public function fileStream() { @@ -56,7 +55,6 @@ class UploadController extends CController * 获取拆分文件信息 * * @RequestMapping(path="get-file-split-info", methods="get") - * */ public function getFileSplitInfo() { @@ -72,6 +70,7 @@ class UploadController extends CController } /** + * 文件拆分上传接口 * * @RequestMapping(path="file-subarea-upload", methods="post") * diff --git a/app/Controller/Api/V1/UsersController.php b/app/Controller/Api/V1/UsersController.php index 4b2fd3c..cd031a3 100644 --- a/app/Controller/Api/V1/UsersController.php +++ b/app/Controller/Api/V1/UsersController.php @@ -35,13 +35,13 @@ class UsersController extends CController * @Inject * @var FriendService */ - protected $friendService; + private $friendService; /** * @Inject * @var UserService */ - protected $userService; + private $userService; /** * @inject @@ -140,7 +140,7 @@ class UsersController extends CController 'nickname' => $userInfo->nickname, 'avatar' => $userInfo->avatar, 'motto' => $userInfo->motto, - 'gender' => $userInfo->gender, + 'gender' => $userInfo->gender ], 'setting' => [ 'theme_mode' => '', @@ -187,6 +187,7 @@ class UsersController extends CController ]); $isTrue = User::where('id', $this->uid())->update(['avatar' => $params['avatar']]); + return $isTrue ? $this->response->success([], '头像修改成功...') : $this->response->fail('头像修改失败...'); @@ -228,10 +229,11 @@ class UsersController extends CController $params = $this->request->inputs(['friend_id', 'remarks']); $this->validate($params, [ 'friend_id' => 'required|integer', - 'remarks' => "required", + 'remarks' => "required" ]); $isTrue = $this->friendService->editFriendRemark($this->uid(), $params['friend_id'], $params['remarks']); + return $isTrue ? $this->response->success([], '备注修改成功...') : $this->response->fail('备注修改失败...'); @@ -247,7 +249,7 @@ class UsersController extends CController $params = $this->request->inputs(['friend_id', 'remarks']); $this->validate($params, [ 'friend_id' => 'required|integer', - 'remarks' => 'present', + 'remarks' => 'present' ]); $user = $this->userService->findById($params['friend_id']); @@ -276,7 +278,6 @@ class UsersController extends CController ); } - return $this->response->success([], '发送好友申请成功...'); } @@ -290,7 +291,7 @@ class UsersController extends CController $params = $this->request->inputs(['apply_id', 'remarks']); $this->validate($params, [ 'apply_id' => 'required|integer', - 'remarks' => 'present', + 'remarks' => 'present' ]); $isTrue = $this->friendService->handleFriendApply($this->uid(), (int)$params['apply_id'], $params['remarks']); @@ -323,7 +324,7 @@ class UsersController extends CController { $params = $this->request->inputs(['apply_id']); $this->validate($params, [ - 'apply_id' => 'required|integer', + 'apply_id' => 'required|integer' ]); $isTrue = $this->friendService->delFriendApply($this->uid(), (int)$params['apply_id']); @@ -342,7 +343,7 @@ class UsersController extends CController $params = $this->request->inputs(['page', 'page_size']); $this->validate($params, [ 'page' => 'present|integer', - 'page_size' => 'present|integer', + 'page_size' => 'present|integer' ]); $page = $this->request->input('page', 1); @@ -376,7 +377,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' ]); $userInfo = $this->userService->findById($this->uid(), ['id', 'password', 'mobile']); @@ -406,7 +407,7 @@ class UsersController extends CController $this->validate($params, [ 'mobile' => "required|regex:/^1[345789][0-9]{9}$/", 'password' => 'required', - 'sms_code' => 'required|digits:6', + 'sms_code' => 'required|digits:6' ]); if (!$smsCodeService->check('change_mobile', $params['mobile'], $params['sms_code'])) { @@ -425,6 +426,7 @@ class UsersController extends CController // 清除缓存信息 $smsCodeService->delCode('change_mobile', $params['mobile']); + return $this->response->success([], '手机号更换成功...'); } @@ -439,7 +441,7 @@ class UsersController extends CController $this->validate($params, [ 'email' => 'required|email', 'password' => 'required', - 'email_code' => 'required|digits:6', + 'email_code' => 'required|digits:6' ]); $sendEmailCode = new SendEmailCode(); @@ -459,6 +461,7 @@ class UsersController extends CController } $sendEmailCode->delCode(SendEmailCode::CHANGE_EMAIL, $params['email']); + return $this->response->success([], '邮箱设置成功...'); } @@ -474,7 +477,7 @@ class UsersController extends CController { $params = $this->request->inputs(['mobile']); $this->validate($params, [ - 'mobile' => "present|regex:/^1[345789][0-9]{9}$/", + 'mobile' => "present|regex:/^1[345789][0-9]{9}$/" ]); $user_id = $this->uid(); @@ -488,12 +491,14 @@ class UsersController extends CController $data = ['is_debug' => true]; [$isTrue, $result] = $smsCodeService->send('change_mobile', $params['mobile']); - if ($isTrue) { - $data['sms_code'] = $result['data']['code']; - } else { + if (!$isTrue) { // ... 处理发送失败逻辑,当前默认发送成功 + return $this->response->fail('验证码发送失败'); } + // 测试环境下直接返回验证码 + $data['sms_code'] = $result['data']['code']; + return $this->response->success($data, '验证码发送成功...'); } @@ -509,7 +514,7 @@ class UsersController extends CController { $params = $this->request->inputs(['email']); $this->validate($params, [ - 'email' => "required|email", + 'email' => "required|email" ]); $isTrue = $sendEmailCode->send(SendEmailCode::CHANGE_EMAIL, '绑定邮箱', $params['email']); diff --git a/app/Listener/ValidatorFactoryResolvedListener.php b/app/Listener/ValidatorFactoryResolvedListener.php new file mode 100644 index 0000000..6142788 --- /dev/null +++ b/app/Listener/ValidatorFactoryResolvedListener.php @@ -0,0 +1,38 @@ +validatorFactory; + + // 注册了 ids 验证器(验证英文逗号拼接的整形数字字符串 例如:[1,2,3,4,5]) + $validatorFactory->extend('ids', function ($attribute, $value, $parameters, $validator) { + $arr = explode(',', $value); + foreach ($arr as $id) { + if (!check_int($id)) return false; + } + + return true; + }); + } +} diff --git a/app/Support/Response.php b/app/Support/Response.php index 2a6c208..3ee0686 100644 --- a/app/Support/Response.php +++ b/app/Support/Response.php @@ -46,7 +46,7 @@ class Response * * @return PsrResponseInterface */ - public function fail($message = 'FAIL', $data = [], $code = ResponseCode::FAIL) + public function fail($message = 'fail', $data = [], $code = ResponseCode::FAIL) { return $this->response->json(compact('code', 'message', 'data')); } diff --git a/app/helper.php b/app/helper.php index f0b7ad2..2755aee 100644 --- a/app/helper.php +++ b/app/helper.php @@ -198,3 +198,22 @@ function arraysSort(array $array, $field, $sort = SORT_DESC) array_multisort(array_column($array, $field), $sort, $array); return $array; } + + +/** + * 判断0或正整数 + * + * @param string $int 验证字符串 + * @param bool $isZero 判断是否可为0 + * @return bool + */ +function check_int($int, $isZero = false) +{ + $reg = $isZero ? '/^[+]{0,1}(\d+)$/' : '/^[1-9]\d*$/'; + return is_numeric($int) && preg_match($reg, $int); +} + +function parse_ids($ids) +{ + return array_unique(explode(',', trim($ids))); +} diff --git a/storage/languages/en/validation.php b/storage/languages/en/validation.php index 5e128a6..6ba00d1 100644 --- a/storage/languages/en/validation.php +++ b/storage/languages/en/validation.php @@ -174,4 +174,6 @@ return [ 'chinese_word' => 'The :attribute must contain valid characters(chinese/english character, number, underscore)', 'sequential_array' => 'The :attribute must be sequential array', + + 'ids' => ' :attribute 字段 ids 格式不正确', ]; diff --git a/storage/languages/zh_CN/validation.php b/storage/languages/zh_CN/validation.php index 8093224..7610b33 100644 --- a/storage/languages/zh_CN/validation.php +++ b/storage/languages/zh_CN/validation.php @@ -174,4 +174,6 @@ return [ 'chinese_word' => ':attribute 必须包含以下有效字符 (中文/英文,数字, 下划线)', 'sequential_array' => ':attribute 必须是一个有序数组', + + 'ids' => ' :attribute 字段 ids 格式不正确', ];