优化代码

main
gzydong 2021-09-05 15:47:01 +08:00
parent 6212d439c9
commit 86b54e48fb
8 changed files with 81 additions and 55 deletions

View File

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* This is my open source code, please do not use it for commercial applications.
* For the full copyright and license information,
@ -17,6 +19,7 @@ use Hyperf\HttpServer\Annotation\RequestMapping;
use App\Model\User;
use App\Service\UserService;
use App\Service\SmsCodeService;
use Psr\Http\Message\ResponseInterface;
/**
* 授权相关控制器
@ -38,9 +41,10 @@ class AuthController extends CController
/**
* 授权登录接口
*
* @RequestMapping(path="login", methods="post")
*/
public function login()
public function login(): ResponseInterface
{
$params = $this->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, [

View File

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* This is my open source code, please do not use it for commercial applications.
* For the full copyright and license information,
@ -13,6 +15,7 @@ namespace App\Controller\Api\V1;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;
use App\Support\Response;
use App\Model\User;
/**
* 基类控制器
@ -32,13 +35,25 @@ class CController extends AbstractController
*
* @return int
*/
public function uid()
public function uid(): int
{
$guard = $this->guard();
return $guard->check() ? $guard->user()->getId() : 0;
}
/**
* 获取登录用户信息
*
* @return User|null
*/
public function user(): ?User
{
$guard = $this->guard();
return $guard->check() ? $guard->user() : null;
}
/**
* 获取授权守卫
*

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace App\Controller\Api\V1;
@ -33,7 +34,7 @@ class ContactsApplyController extends CController
* @param UserService $userService
* @return ResponseInterface
*/
public function create(UserService $userService)
public function create(UserService $userService): ResponseInterface
{
$params = $this->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));

View File

@ -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']);

View File

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* This is my open source code, please do not use it for commercial applications.
* For the full copyright and license information,
@ -43,9 +45,9 @@ class UsersController extends CController
*
* @return ResponseInterface
*/
public function getUserDetail()
public function getUserDetail(): ResponseInterface
{
$userInfo = $this->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, [

View File

@ -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);

View File

@ -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,

View File

@ -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(