From c3b17a583b32a85abb1658f74aa027a46733896a Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Sat, 22 Jan 2022 12:48:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=85=BC=E5=AE=B9=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/Api/V1/EmoticonController.php | 33 ++++++++----------- .../Api/V1/Talk/MessageController.php | 6 +++- app/Controller/Api/V1/UploadController.php | 7 ++-- app/Controller/Api/V1/UsersController.php | 12 ++++--- app/Model/Emoticon/EmoticonItem.php | 4 ++- app/Model/Talk/TalkRecordsFile.php | 1 + app/Service/EmoticonService.php | 7 +--- app/Service/Message/FormatMessageService.php | 4 +-- app/Support/Response.php | 18 ++++++++-- 9 files changed, 54 insertions(+), 38 deletions(-) diff --git a/app/Controller/Api/V1/EmoticonController.php b/app/Controller/Api/V1/EmoticonController.php index 110bd3d..4496715 100644 --- a/app/Controller/Api/V1/EmoticonController.php +++ b/app/Controller/Api/V1/EmoticonController.php @@ -1,4 +1,5 @@ 'required|in:1,2' ]); + $params['emoticon_id'] = intval($params['emoticon_id']); + $user_id = $this->uid(); // 移除表情包 @@ -132,7 +134,8 @@ class EmoticonController extends CController /** * 自定义上传表情包 * - * @RequestMapping(path="upload-emoticon", methods="post") + * @RequestMapping(path="customize/create", methods="post") + * * @param Filesystem $filesystem * @return ResponseInterface */ @@ -140,24 +143,16 @@ class EmoticonController extends CController { $file = $this->request->file('emoticon'); if (!$file->isValid()) { - return $this->response->fail( - '图片上传失败,请稍后再试!', - [], - ResponseCode::VALIDATION_ERROR - ); + return $this->response->invalidParams('上传文件验证失败!'); } $ext = $file->getExtension(); if (!in_array($ext, ['jpg', 'png', 'jpeg', 'gif', 'webp'])) { - return $this->response->fail( - '图片格式错误,目前仅支持jpg、png、jpeg、gif和webp', - [], - ResponseCode::VALIDATION_ERROR - ); + return $this->response->invalidParams('图片格式错误,目前仅支持jpg、png、jpeg、gif和webp'); } try { - $path = 'media/images/emoticon/' . date('Ymd') . '/' . create_image_name($ext, getimagesize($file->getRealPath())); + $path = 'public/media/images/emoticon/' . date('Ymd') . '/' . create_image_name($ext, getimagesize($file->getRealPath())); $filesystem->write($path, file_get_contents($file->getRealPath())); } catch (\Exception $e) { return $this->response->fail('图片上传失败!'); @@ -165,10 +160,9 @@ class EmoticonController extends CController $result = EmoticonItem::create([ 'user_id' => $this->uid(), - 'url' => $path, + 'url' => get_media_url($path), 'file_suffix' => $ext, 'file_size' => $file->getSize(), - 'created_at' => date('Y-m-d H:i:s') ]); if (!$result) { @@ -177,14 +171,15 @@ class EmoticonController extends CController return $this->response->success([ 'media_id' => $result->id, - 'src' => get_media_url($result->url) + 'src' => $result->url, ]); } /** * 移除收藏的表情包 * - * @RequestMapping(path="del-collect-emoticon", methods="post") + * @RequestMapping(path="customize/delete", methods="post") + * @throws \Exception */ public function delCollectEmoticon(): ResponseInterface { diff --git a/app/Controller/Api/V1/Talk/MessageController.php b/app/Controller/Api/V1/Talk/MessageController.php index 46af461..0a0458a 100644 --- a/app/Controller/Api/V1/Talk/MessageController.php +++ b/app/Controller/Api/V1/Talk/MessageController.php @@ -136,7 +136,7 @@ class MessageController extends CController } try { - $path = 'media/images/talks/' . date('Ymd') . '/' . create_image_name($ext, getimagesize($file->getRealPath())); + $path = 'public/media/images/talks/' . date('Ymd') . '/' . create_image_name($ext, getimagesize($file->getRealPath())); $filesystem->write($path, file_get_contents($file->getRealPath())); } catch (\Exception $e) { return $this->response->fail(); @@ -152,6 +152,7 @@ class MessageController extends CController 'suffix' => $ext, 'size' => $file->getSize(), 'path' => $path, + 'url' => get_media_url($path), 'original_name' => $file->getClientFilename(), ]); @@ -254,6 +255,7 @@ class MessageController extends CController public function emoticon(): ResponseInterface { $params = $this->request->inputs(['talk_type', 'receiver_id', 'emoticon_id']); + $this->validate($params, [ 'talk_type' => 'required|in:1,2', 'receiver_id' => 'required|integer|min:1', @@ -269,6 +271,8 @@ class MessageController extends CController if (!$emoticon) return $this->response->fail('表情不存在!'); + var_dump($emoticon->toArray()); + $isTrue = $this->talkMessageService->insertFile([ 'talk_type' => $params['talk_type'], 'user_id' => $user_id, diff --git a/app/Controller/Api/V1/UploadController.php b/app/Controller/Api/V1/UploadController.php index a57fd56..b7853b3 100644 --- a/app/Controller/Api/V1/UploadController.php +++ b/app/Controller/Api/V1/UploadController.php @@ -34,7 +34,7 @@ class UploadController extends CController * @param Filesystem $filesystem * @return ResponseInterface */ - public function fileStream(Filesystem $filesystem): ResponseInterface + public function avatar(Filesystem $filesystem): ResponseInterface { $file = $this->request->file("file"); @@ -60,6 +60,7 @@ class UploadController extends CController public function initiateMultipart(): ResponseInterface { $params = $this->request->inputs(['file_name', 'file_size']); + $this->validate($params, [ 'file_name' => "required", 'file_size' => 'required|integer' @@ -79,14 +80,16 @@ class UploadController extends CController */ public function fileSubareaUpload(): ResponseInterface { - $file = $this->request->file('file'); + $params = $this->request->inputs(['upload_id', 'split_index', 'split_num']); + $this->validate($params, [ 'upload_id' => 'required', 'split_index' => 'required', 'split_num' => 'required' ]); + $file = $this->request->file('file'); if (!$file || !$file->isValid()) { return $this->response->fail(); } diff --git a/app/Controller/Api/V1/UsersController.php b/app/Controller/Api/V1/UsersController.php index ac03736..6075f05 100644 --- a/app/Controller/Api/V1/UsersController.php +++ b/app/Controller/Api/V1/UsersController.php @@ -81,7 +81,8 @@ class UsersController extends CController /** * 编辑我的信息 - * @RequestMapping(path="edit/detail", methods="post") + * + * @RequestMapping(path="change/detail", methods="post") * * @return ResponseInterface */ @@ -103,7 +104,8 @@ class UsersController extends CController /** * 修改我的密码 - * @RequestMapping(path="edit/password", methods="post") + * + * @RequestMapping(path="change/password", methods="post") * * @return ResponseInterface */ @@ -133,7 +135,8 @@ class UsersController extends CController /** * 更换用户手机号 - * @RequestMapping(path="edit/mobile", methods="post") + * + * @RequestMapping(path="change/mobile", methods="post") * * @param SmsCodeService $smsCodeService * @return ResponseInterface @@ -168,7 +171,8 @@ class UsersController extends CController /** * 修改用户邮箱接口 - * @RequestMapping(path="edit/email", methods="post") + * + * @RequestMapping(path="change/email", methods="post") * * @param \App\Support\SendEmailCode $emailCode * @return \Psr\Http\Message\ResponseInterface diff --git a/app/Model/Emoticon/EmoticonItem.php b/app/Model/Emoticon/EmoticonItem.php index 709e570..964a19d 100644 --- a/app/Model/Emoticon/EmoticonItem.php +++ b/app/Model/Emoticon/EmoticonItem.php @@ -1,5 +1,4 @@ get(['id as media_id', 'url as src'])->toArray(); - foreach ($items as $k => $item) { - $items[$k]['src'] = get_media_url($item['src']); - } - - return $items; + return EmoticonItem::where($where)->get(['id as media_id', 'url as src'])->toArray(); } } diff --git a/app/Service/Message/FormatMessageService.php b/app/Service/Message/FormatMessageService.php index 96572b9..88970f5 100644 --- a/app/Service/Message/FormatMessageService.php +++ b/app/Service/Message/FormatMessageService.php @@ -97,7 +97,7 @@ class FormatMessageService if ($files) { $files = TalkRecordsFile::whereIn('record_id', $files)->get([ 'id', 'record_id', 'user_id', 'source', 'type', 'drive', - 'original_name', 'suffix', 'size', 'path' + 'original_name', 'suffix', 'size', 'path', 'url' ])->keyBy('record_id')->toArray(); } @@ -149,7 +149,7 @@ class FormatMessageService case TalkMessageType::FILE_MESSAGE: $rows[$k]['file'] = $files[$row['id']] ?? []; if ($rows[$k]['file']) { - $rows[$k]['file']['file_url'] = get_media_url($rows[$k]['file']['path']); + $rows[$k]['file']['file_url'] = $rows[$k]['file']['url']; } break; diff --git a/app/Support/Response.php b/app/Support/Response.php index 67c3202..e5765bb 100644 --- a/app/Support/Response.php +++ b/app/Support/Response.php @@ -39,10 +39,9 @@ class Response $resp = [ "code" => ResponseCode::SUCCESS, "message" => $message, - "data" => $data, ]; - // if ($data) $resp["data"] = $data; + if ($data) $resp["data"] = $data; return $this->response->json($resp); } @@ -55,7 +54,7 @@ class Response * @param int $code 错误码 * @return PsrResponseInterface */ - public function fail(string $message = 'fail', array $data = [], $code = ResponseCode::FAIL): PsrResponseInterface + public function fail(string $message = 'fail', array $data = [], int $code = ResponseCode::FAIL): PsrResponseInterface { $resp = [ "code" => $code, @@ -67,6 +66,19 @@ class Response return $this->response->json($resp); } + /** + * 参数验证错误 + * @param string $message 错误信息 + * @return PsrResponseInterface + */ + public function invalidParams(string $message = 'fail'): PsrResponseInterface + { + return $this->response->json([ + "code" => ResponseCode::VALIDATION_ERROR, + "message" => $message, + ]); + } + /** * 处理错误信息返回 *