diff --git a/app/Controller/Api/V1/AuthController.php b/app/Controller/Api/V1/AuthController.php index 294baea..276a987 100644 --- a/app/Controller/Api/V1/AuthController.php +++ b/app/Controller/Api/V1/AuthController.php @@ -1,4 +1,6 @@ request->inputs(['mobile', 'password', 'platform']); $this->validate($params, [ @@ -80,9 +84,10 @@ class AuthController extends CController /** * 退出登录接口 + * * @RequestMapping(path="logout", methods="post") */ - public function logout() + public function logout(): ResponseInterface { $this->guard()->check() && $this->guard()->logout(); @@ -91,9 +96,10 @@ class AuthController extends CController /** * 账号注册接口 + * * @RequestMapping(path="register", methods="post") */ - public function register() + public function register(): ResponseInterface { $params = $this->request->all(); $this->validate($params, [ @@ -126,9 +132,10 @@ class AuthController extends CController /** * 账号找回接口 + * * @RequestMapping(path="forget", methods="post") */ - public function forget() + public function forget(): ResponseInterface { $params = $this->request->inputs(['mobile', 'password', 'sms_code']); $this->validate($params, [ @@ -156,7 +163,7 @@ class AuthController extends CController * 授权刷新接口 * @RequestMapping(path="refresh", methods="post") */ - public function refresh() + public function refresh(): ResponseInterface { if ($this->guard()->guest()) { return $this->response->fail('token 刷新失败!'); @@ -173,9 +180,10 @@ class AuthController extends CController /** * 发送验证码 + * * @RequestMapping(path="send-verify-code", methods="post") */ - public function sendVerifyCode() + public function sendVerifyCode(): ResponseInterface { $params = $this->request->inputs(['type', 'mobile']); $this->validate($params, [ diff --git a/app/Controller/Api/V1/CController.php b/app/Controller/Api/V1/CController.php index 4bf0828..3966524 100644 --- a/app/Controller/Api/V1/CController.php +++ b/app/Controller/Api/V1/CController.php @@ -1,4 +1,6 @@ guard(); return $guard->check() ? $guard->user()->getId() : 0; } + /** + * 获取登录用户信息 + * + * @return User|null + */ + public function user(): ?User + { + $guard = $this->guard(); + + return $guard->check() ? $guard->user() : null; + } + /** * 获取授权守卫 * diff --git a/app/Controller/Api/V1/ContactsApplyController.php b/app/Controller/Api/V1/ContactsApplyController.php index 54a5dfa..5200efb 100644 --- a/app/Controller/Api/V1/ContactsApplyController.php +++ b/app/Controller/Api/V1/ContactsApplyController.php @@ -1,4 +1,5 @@ request->inputs(['friend_id', 'remark']); $this->validate($params, [ @@ -62,10 +63,11 @@ class ContactsApplyController extends CController } /** + * 好友同意接口 + * * @RequestMapping(path="accept", methods="post") - * @return ResponseInterface */ - public function accept() + public function accept(): ResponseInterface { $params = $this->request->inputs(['apply_id', 'remark']); $this->validate($params, [ @@ -74,7 +76,7 @@ class ContactsApplyController extends CController ]); $user_id = $this->uid(); - $isTrue = $this->service->accept($user_id, intval($params['apply_id']), $params['remark']); + $isTrue = $this->service->accept($user_id, (int)$params['apply_id'], $params['remark']); if (!$isTrue) { return $this->response->fail('处理失败!'); } @@ -83,10 +85,11 @@ class ContactsApplyController extends CController } /** + * 好友拒绝接口 + * * @RequestMapping(path="decline", methods="post") - * @return ResponseInterface */ - public function decline() + public function decline(): ResponseInterface { $params = $this->request->inputs(['apply_id', 'remark']); $this->validate($params, [ @@ -94,7 +97,7 @@ class ContactsApplyController extends CController 'remark' => 'present|max:20' ]); - $isTrue = $this->service->decline($this->uid(), intval($params['apply_id']), $params['remark']); + $isTrue = $this->service->decline($this->uid(), (int)$params['apply_id'], $params['remark']); if (!$isTrue) { return $this->response->fail('处理失败!'); } @@ -102,14 +105,12 @@ class ContactsApplyController extends CController return $this->response->success([], '处理成功...'); } - /** * 获取联系人申请未读数 - * @RequestMapping(path="records", methods="get") * - * @return ResponseInterface + * @RequestMapping(path="records", methods="get") */ - public function records() + public function records(): ResponseInterface { $params = $this->request->inputs(['page', 'page_size']); $this->validate($params, [ @@ -117,8 +118,8 @@ class ContactsApplyController extends CController 'page_size' => 'present|integer' ]); - $page = $this->request->input('page', 1); - $page_size = $this->request->input('page_size', 10); + $page = (int)$this->request->input('page', 1); + $page_size = (int)$this->request->input('page_size', 10); $user_id = $this->uid(); FriendApply::getInstance()->rem(strval($user_id)); diff --git a/app/Controller/Api/V1/UploadController.php b/app/Controller/Api/V1/UploadController.php index a067bb5..33b5af3 100644 --- a/app/Controller/Api/V1/UploadController.php +++ b/app/Controller/Api/V1/UploadController.php @@ -41,7 +41,7 @@ class UploadController extends CController * * @return ResponseInterface */ - public function fileStream(Filesystem $filesystem) + public function fileStream(Filesystem $filesystem): ResponseInterface { $fileStream = $this->request->post('fileStream', ''); if (empty($fileStream)) { @@ -64,7 +64,7 @@ class UploadController extends CController * * @return ResponseInterface */ - public function getFileSplitInfo() + public function getFileSplitInfo(): ResponseInterface { $params = $this->request->inputs(['file_name', 'file_size']); $this->validate($params, [ @@ -83,7 +83,7 @@ class UploadController extends CController * * @return ResponseInterface */ - public function fileSubareaUpload() + public function fileSubareaUpload(): ResponseInterface { $file = $this->request->file('file'); $params = $this->request->inputs(['name', 'hash', 'ext', 'size', 'split_index', 'split_num']); diff --git a/app/Controller/Api/V1/UsersController.php b/app/Controller/Api/V1/UsersController.php index d657f87..28db3ce 100644 --- a/app/Controller/Api/V1/UsersController.php +++ b/app/Controller/Api/V1/UsersController.php @@ -1,4 +1,6 @@ userService->findById($this->uid(), ['mobile', 'nickname', 'avatar', 'motto', 'email', 'gender']); + $userInfo = $this->user(); return $this->response->success([ 'mobile' => $userInfo->mobile, @@ -53,7 +55,7 @@ class UsersController extends CController 'avatar' => $userInfo->avatar, 'motto' => $userInfo->motto, 'email' => $userInfo->email, - 'gender' => $userInfo->gender + 'gender' => $userInfo->gender, ]); } @@ -63,9 +65,9 @@ class UsersController extends CController * * @return ResponseInterface */ - public function getUserSetting() + public function getUserSetting(): ResponseInterface { - $userInfo = $this->userService->findById($this->uid(), ['id', 'nickname', 'avatar', 'motto', 'gender']); + $userInfo = $this->user(); return $this->response->success([ 'user_info' => [ @@ -73,7 +75,7 @@ class UsersController extends CController 'nickname' => $userInfo->nickname, 'avatar' => $userInfo->avatar, 'motto' => $userInfo->motto, - 'gender' => $userInfo->gender + 'gender' => $userInfo->gender, ], 'setting' => [ 'theme_mode' => '', @@ -91,7 +93,7 @@ class UsersController extends CController * * @return ResponseInterface */ - public function editUserDetail() + public function editUserDetail(): ResponseInterface { $params = $this->request->inputs(['nickname', 'avatar', 'motto', 'gender']); $this->validate($params, [ @@ -114,7 +116,7 @@ class UsersController extends CController * * @return ResponseInterface */ - public function editAvatar() + public function editAvatar(): ResponseInterface { $params = $this->request->inputs(['avatar']); $this->validate($params, [ @@ -134,7 +136,7 @@ class UsersController extends CController * * @return ResponseInterface */ - public function search() + public function search(): ResponseInterface { $params = $this->request->inputs(['user_id']); $this->validate($params, ['user_id' => 'required|integer']); @@ -152,7 +154,7 @@ class UsersController extends CController * * @return ResponseInterface */ - public function editUserPassword() + public function editUserPassword(): ResponseInterface { $params = $this->request->inputs(['old_password', 'new_password']); $this->validate($params, [ @@ -160,7 +162,7 @@ class UsersController extends CController 'new_password' => 'required|min:6|max:16' ]); - $userInfo = $this->userService->findById($this->uid(), ['id', 'password', 'mobile']); + $userInfo = $this->user(); // 验证密码是否正确 if (!HashHelper::check($this->request->post('old_password'), $userInfo->password)) { @@ -180,7 +182,7 @@ class UsersController extends CController * @param SmsCodeService $smsCodeService * @return ResponseInterface */ - public function editUserMobile(SmsCodeService $smsCodeService) + public function editUserMobile(SmsCodeService $smsCodeService): ResponseInterface { $params = $this->request->inputs(['mobile', 'password', 'sms_code']); $this->validate($params, [ @@ -193,12 +195,11 @@ class UsersController extends CController return $this->response->fail('验证码填写错误!'); } - $user_id = $this->uid(); - if (!HashHelper::check($params['password'], User::where('id', $user_id)->value('password'))) { + if (!HashHelper::check($params['password'], $this->user()->password)) { return $this->response->fail('账号密码验证失败!'); } - [$isTrue,] = $this->userService->changeMobile($user_id, $params['mobile']); + [$isTrue,] = $this->userService->changeMobile($this->uid(), $params['mobile']); if (!$isTrue) { return $this->response->fail('手机号更换失败!'); } @@ -215,7 +216,7 @@ class UsersController extends CController * * @return ResponseInterface */ - public function editUserEmail() + public function editUserEmail(): ResponseInterface { $params = $this->request->inputs(['email', 'password', 'email_code']); $this->validate($params, [ @@ -229,13 +230,11 @@ class UsersController extends CController return $this->response->fail('验证码填写错误!'); } - $uid = $this->uid(); - $user_password = User::where('id', $uid)->value('password'); - if (!HashHelper::check($params['password'], $user_password)) { + if (!HashHelper::check($params['password'], $this->user()->password)) { return $this->response->fail('账号密码验证失败!'); } - $isTrue = User::where('id', $uid)->update(['email' => $params['email']]); + $isTrue = User::where('id', $this->uid())->update(['email' => $params['email']]); if (!$isTrue) { return $this->response->fail('邮箱设置失败!'); } @@ -252,7 +251,7 @@ class UsersController extends CController * @param SmsCodeService $smsCodeService * @return ResponseInterface */ - public function sendMobileCode(SmsCodeService $smsCodeService) + public function sendMobileCode(SmsCodeService $smsCodeService): ResponseInterface { $params = $this->request->inputs(['mobile']); $this->validate($params, [ @@ -288,7 +287,7 @@ class UsersController extends CController * @param SendEmailCode $sendEmailCode * @return ResponseInterface */ - public function sendChangeEmailCode(SendEmailCode $sendEmailCode) + public function sendChangeEmailCode(SendEmailCode $sendEmailCode): ResponseInterface { $params = $this->request->inputs(['email']); $this->validate($params, [ diff --git a/app/Repository/BaseRepository.php b/app/Repository/BaseRepository.php index 416dc7a..70d51ca 100644 --- a/app/Repository/BaseRepository.php +++ b/app/Repository/BaseRepository.php @@ -45,6 +45,16 @@ abstract class BaseRepository { use RepositoryTrait; + /** + * 获取单例 + * + * @return static + */ + public static function getInstance() + { + return di()->get(static::class); + } + /** * 查询单条数据 * @@ -96,7 +106,7 @@ abstract class BaseRepository * @param int $size 每页条数 * @return array */ - final public function paginate(array $where, $fields = ['*'], $page = 1, $size = 15): array + final public function paginate(array $where, array $fields = ['*'], int $page = 1, int $size = 15): array { $this->handleField($fields); diff --git a/app/Repository/ExampleRepository.php b/app/Repository/ExampleRepository.php index 58ea02f..631c9c0 100644 --- a/app/Repository/ExampleRepository.php +++ b/app/Repository/ExampleRepository.php @@ -280,13 +280,6 @@ class ExampleRepository extends BaseRepository public function where_case2() { - $rows = $this->get([ - 'id:gt' => 2000, - ], ['*'], [0, 2]); - - var_dump($rows); - - return; $this->get([ 'id:gt' => 3000, 'id:lt' => 4000, diff --git a/app/Support/SendEmailCode.php b/app/Support/SendEmailCode.php index 63b20e1..1226e6b 100644 --- a/app/Support/SendEmailCode.php +++ b/app/Support/SendEmailCode.php @@ -54,10 +54,10 @@ class SendEmailCode { $key = $this->getKey($type, $email); if (!$sms_code = $this->getCode($key)) { - $sms_code = mt_rand(100000, 999999); + $sms_code = (string)mt_rand(100000, 999999); } - $this->setCode($key, $sms_code);; + $this->setCode($key, $sms_code); // ...执行发送(后期使用队列) email()->send(