feat:兼容开发

main
gzydong 2022-01-21 21:46:17 +08:00
parent 237e172bd8
commit f26f899f3d
7 changed files with 132 additions and 137 deletions

View File

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace App\Constants;
class SmsConstant
{
const SmsLoginChannel = "login";
const SmsRegisterChannel = "register";
const SmsForgetAccountChannel = "forget_account";
const SmsChangeAccountChannel = "change_account";
}

View File

@ -3,26 +3,107 @@ declare(strict_types=1);
namespace App\Controller\Api\V1; namespace App\Controller\Api\V1;
use App\Support\SendEmailCode;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use App\Service\SmsCodeService;
use App\Constants\SmsConstant;
use App\Service\UserService;
/**
* class CommonController
*
* @Controller(prefix="/api/v1/common")
*/
class CommonController extends CController class CommonController extends CController
{ {
/** /**
* 发送短信验证码 * @Inject
* @var UserService
*/ */
public function SmsCode(){ private $userService;
/**
* @Inject
* @var SmsCodeService
*/
private $sms;
/**
* 发送短信验证码
*
* @RequestMapping(path="sms-code", methods="post")
*/
public function SmsCode()
{
$params = $this->request->all();
$this->validate($params, [
'channel' => "required|in:login,register,forget_account,change_account",
'mobile' => "required|phone"
]);
switch ($params['channel']) {
case SmsConstant::SmsLoginChannel:
case SmsConstant::SmsForgetAccountChannel:
if (!$this->userService->isMobileExist($params['mobile'])) {
return $this->response->fail("账号不存在或密码错误!");
}
break;
case SmsConstant::SmsRegisterChannel:
case SmsConstant::SmsChangeAccountChannel:
if ($this->userService->isMobileExist($params['mobile'])) {
return $this->response->fail("账号已被他(她)人使用!");
}
break;
default:
return $this->response->fail("发送异常!");
}
[$isTrue, $result] = $this->sms->send($params['channel'], $params['mobile']);
if (!$isTrue) {
return $this->response->fail($result['msg']);
}
// 可自行去掉
$data = [];
$data['is_debug'] = true;
$data['sms_code'] = $result['data']['code'];
return $this->response->success($data);
} }
/** /**
* 发送邮件验证码 * 发送邮件验证码
*
* @RequestMapping(path="email-code", methods="post")
*/ */
public function EmailCode(){ public function EmailCode(SendEmailCode $sendEmailCode)
{
$params = $this->request->inputs(['email']);
$this->validate($params, [
'email' => "required|email"
]);
$isTrue = $sendEmailCode->send(SendEmailCode::CHANGE_EMAIL, '绑定邮箱', $params['email']);
if (!$isTrue) {
return $this->response->fail('验证码发送失败!');
}
return $this->response->success([], '验证码发送成功...');
} }
/** /**
* 公共设置 * 公共设置
*
* @RequestMapping(path="setting", methods="post")
*/ */
public function Setting(){ public function Setting()
{
} }
} }

View File

@ -8,7 +8,7 @@ use App\Constants\TalkEventConstant;
use App\Constants\TalkModeConstant; use App\Constants\TalkModeConstant;
use App\Controller\Api\V1\CController; use App\Controller\Api\V1\CController;
use App\Event\TalkEvent; use App\Event\TalkEvent;
use App\Model\EmoticonItem; use App\Model\Emoticon\EmoticonItem;
use App\Model\SplitUpload; use App\Model\SplitUpload;
use App\Service\TalkForwardService; use App\Service\TalkForwardService;
use App\Service\TalkMessageService; use App\Service\TalkMessageService;

View File

@ -67,7 +67,6 @@ class UploadController extends CController
$data = $this->splitUploadService->create($this->uid(), $params['file_name'], $params['file_size']); $data = $this->splitUploadService->create($this->uid(), $params['file_name'], $params['file_size']);
$data['hash_name'] = $data["upload_id"]; $data['hash_name'] = $data["upload_id"];
return $data ? $this->response->success($data) : $this->response->fail('获取文件拆分信息失败!'); return $data ? $this->response->success($data) : $this->response->fail('获取文件拆分信息失败!');

View File

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace App\Controller\Api\V1; namespace App\Controller\Api\V1;
use App\Constants\SmsConstant;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\RequestMapping;
@ -36,7 +37,7 @@ class UsersController extends CController
* *
* @return ResponseInterface * @return ResponseInterface
*/ */
public function getUserDetail(): ResponseInterface public function getDetail(): ResponseInterface
{ {
$userInfo = $this->user(); $userInfo = $this->user();
@ -56,7 +57,7 @@ class UsersController extends CController
* *
* @return ResponseInterface * @return ResponseInterface
*/ */
public function getUserSetting(): ResponseInterface public function getSetting(): ResponseInterface
{ {
$userInfo = $this->user(); $userInfo = $this->user();
@ -80,13 +81,14 @@ class UsersController extends CController
/** /**
* 编辑我的信息 * 编辑我的信息
* @RequestMapping(path="edit-user-detail", methods="post") * @RequestMapping(path="edit/detail", methods="post")
* *
* @return ResponseInterface * @return ResponseInterface
*/ */
public function editUserDetail(): ResponseInterface public function editDetail(): ResponseInterface
{ {
$params = $this->request->inputs(['nickname', 'avatar', 'motto', 'gender']); $params = $this->request->inputs(['nickname', 'avatar', 'motto', 'gender']);
$this->validate($params, [ $this->validate($params, [
'nickname' => 'required', 'nickname' => 'required',
'motto' => 'present|max:100', 'motto' => 'present|max:100',
@ -94,43 +96,21 @@ class UsersController extends CController
'avatar' => 'present|url' 'avatar' => 'present|url'
]); ]);
$params['updated_at'] = date("Y-m-d H:i:s");
User::where('id', $this->uid())->update($params); User::where('id', $this->uid())->update($params);
return $this->response->success([], '个人信息修改成功...'); return $this->response->success([], '个人信息修改成功...');
} }
/**
* 修改用户头像
* @RequestMapping(path="edit-avatar", methods="post")
*
* @return ResponseInterface
*/
public function editAvatar(): ResponseInterface
{
$params = $this->request->inputs(['avatar']);
$this->validate($params, [
'avatar' => 'required|url'
]);
User::where('id', $this->uid())->update([
'avatar' => $params['avatar'],
'updated_at' => date("Y-m-d H:i:s")
]);
return $this->response->success([], '头像修改成功...');
}
/** /**
* 修改我的密码 * 修改我的密码
* @RequestMapping(path="change-password", methods="post") * @RequestMapping(path="edit/password", methods="post")
* *
* @return ResponseInterface * @return ResponseInterface
*/ */
public function editUserPassword(): ResponseInterface public function editPassword(): ResponseInterface
{ {
$params = $this->request->inputs(['old_password', 'new_password']); $params = $this->request->inputs(['old_password', 'new_password']);
$this->validate($params, [ $this->validate($params, [
'old_password' => 'required', 'old_password' => 'required',
'new_password' => 'required|min:6|max:16' 'new_password' => 'required|min:6|max:16'
@ -153,21 +133,22 @@ class UsersController extends CController
/** /**
* 更换用户手机号 * 更换用户手机号
* @RequestMapping(path="change-mobile", methods="post") * @RequestMapping(path="edit/mobile", methods="post")
* *
* @param SmsCodeService $smsCodeService * @param SmsCodeService $smsCodeService
* @return ResponseInterface * @return ResponseInterface
*/ */
public function editUserMobile(SmsCodeService $smsCodeService): ResponseInterface public function editMobile(SmsCodeService $smsCodeService): ResponseInterface
{ {
$params = $this->request->inputs(['mobile', 'password', 'sms_code']); $params = $this->request->inputs(['mobile', 'password', 'sms_code']);
$this->validate($params, [ $this->validate($params, [
'mobile' => "required|phone", 'mobile' => "required|phone",
'password' => 'required', 'password' => 'required',
'sms_code' => 'required|digits:6' 'sms_code' => 'required|digits:6'
]); ]);
if (!$smsCodeService->check('change_mobile', $params['mobile'], (string)$params['sms_code'])) { if (!$smsCodeService->check(SmsConstant::SmsChangeAccountChannel, $params['mobile'], (string)$params['sms_code'])) {
return $this->response->fail('验证码填写错误!'); return $this->response->fail('验证码填写错误!');
} }
@ -180,29 +161,29 @@ class UsersController extends CController
return $this->response->fail('手机号更换失败!'); return $this->response->fail('手机号更换失败!');
} }
// 清除缓存信息 $smsCodeService->delCode(SmsConstant::SmsChangeAccountChannel, $params['mobile']);
$smsCodeService->delCode('change_mobile', $params['mobile']);
return $this->response->success([], '手机号更换成功...'); return $this->response->success([], '手机号更换成功...');
} }
/** /**
* 修改用户邮箱接口 * 修改用户邮箱接口
* @RequestMapping(path="change-email", methods="post") * @RequestMapping(path="edit/email", methods="post")
* *
* @return ResponseInterface * @param \App\Support\SendEmailCode $emailCode
* @return \Psr\Http\Message\ResponseInterface
*/ */
public function editUserEmail(): ResponseInterface public function editEmail(SendEmailCode $emailCode): ResponseInterface
{ {
$params = $this->request->inputs(['email', 'password', 'email_code']); $params = $this->request->inputs(['email', 'password', 'email_code']);
$this->validate($params, [ $this->validate($params, [
'email' => 'required|email', 'email' => 'required|email',
'password' => 'required', 'password' => 'required',
'email_code' => 'required|digits:6' 'email_code' => 'required|digits:6'
]); ]);
$sendEmailCode = new SendEmailCode(); if (!$emailCode->check(SendEmailCode::CHANGE_EMAIL, $params['email'], (string)$params['email_code'])) {
if (!$sendEmailCode->check(SendEmailCode::CHANGE_EMAIL, $params['email'], (string)$params['email_code'])) {
return $this->response->fail('验证码填写错误!'); return $this->response->fail('验证码填写错误!');
} }
@ -215,66 +196,8 @@ class UsersController extends CController
return $this->response->fail('邮箱设置失败!'); return $this->response->fail('邮箱设置失败!');
} }
$sendEmailCode->delCode(SendEmailCode::CHANGE_EMAIL, $params['email']); $emailCode->delCode(SendEmailCode::CHANGE_EMAIL, $params['email']);
return $this->response->success([], '邮箱设置成功...'); return $this->response->success([], '邮箱设置成功...');
} }
/**
* 修改手机号发送验证码
* @RequestMapping(path="send-mobile-code", methods="post")
*
* @param SmsCodeService $smsCodeService
* @return ResponseInterface
*/
public function sendMobileCode(SmsCodeService $smsCodeService): ResponseInterface
{
$params = $this->request->inputs(['mobile']);
$this->validate($params, [
'mobile' => "present|phone"
]);
$user_id = $this->uid();
if (in_array($user_id, [2054, 2055])) {
return $this->response->fail('测试账号不支持修改手机号!');
}
if (User::where('mobile', $params['mobile'])->exists()) {
return $this->response->fail('手机号已被他人注册!');
}
$data = ['is_debug' => true];
[$isTrue, $result] = $smsCodeService->send('change_mobile', $params['mobile']);
if (!$isTrue) {
// ... 处理发送失败逻辑,当前默认发送成功
return $this->response->fail('验证码发送失败!');
}
// 测试环境下直接返回验证码
$data['sms_code'] = $result['data']['code'];
return $this->response->success($data, '验证码发送成功...');
}
/**
* 发送绑定邮箱的验证码
* @RequestMapping(path="send-change-email-code", methods="post")
*
* @param SendEmailCode $sendEmailCode
* @return ResponseInterface
*/
public function sendChangeEmailCode(SendEmailCode $sendEmailCode): ResponseInterface
{
$params = $this->request->inputs(['email']);
$this->validate($params, [
'email' => "required|email"
]);
$isTrue = $sendEmailCode->send(SendEmailCode::CHANGE_EMAIL, '绑定邮箱', $params['email']);
if (!$isTrue) {
return $this->response->fail('验证码发送失败!');
}
return $this->response->success([], '验证码发送成功...');
}
} }

View File

@ -9,15 +9,6 @@ namespace App\Service;
*/ */
class SmsCodeService class SmsCodeService
{ {
/**
* 短信验证码用途渠道
*/
const SMS_USAGE = [
'user_register', // 注册账号
'forget_password', // 找回密码验
'change_mobile', // 修改手机
];
/** /**
* 获取Redis连接 * 获取Redis连接
* *
@ -35,7 +26,7 @@ class SmsCodeService
* @param string $mobile 手机号 * @param string $mobile 手机号
* @return string * @return string
*/ */
private function getKey(string $type, string $mobile) private function key(string $type, string $mobile)
{ {
return "sms_code:{$type}:{$mobile}"; return "sms_code:{$type}:{$mobile}";
} }
@ -50,7 +41,7 @@ class SmsCodeService
*/ */
public function check(string $type, string $mobile, string $code) public function check(string $type, string $mobile, string $code)
{ {
$sms_code = $this->redis()->get($this->getKey($type, $mobile)); $sms_code = $this->redis()->get($this->key($type, $mobile));
return $sms_code == $code; return $sms_code == $code;
} }
@ -62,16 +53,9 @@ class SmsCodeService
* @param string $mobile 手机号 * @param string $mobile 手机号
* @return array|bool * @return array|bool
*/ */
public function send(string $usage, string $mobile) public function send(string $usage, string $mobile): array
{ {
if (!$this->isUsages($usage)) { $key = $this->key($usage, $mobile);
return [false, [
'msg' => "[{$usage}]:此类短信验证码不支持发送",
'data' => []
]];
}
$key = $this->getKey($usage, $mobile);
// 为防止刷短信行为,此处可进行过滤处理 // 为防止刷短信行为,此处可进行过滤处理
[$isTrue, $data] = $this->filter($usage, $mobile); [$isTrue, $data] = $this->filter($usage, $mobile);
@ -127,7 +111,7 @@ class SmsCodeService
*/ */
public function delCode(string $usage, string $mobile) public function delCode(string $usage, string $mobile)
{ {
return $this->redis()->del($this->getKey($usage, $mobile)); return $this->redis()->del($this->key($usage, $mobile));
} }
/** /**
@ -152,15 +136,4 @@ class SmsCodeService
'data' => [] 'data' => []
]]; ]];
} }
/**
* 判断验证码用途渠道是否注册
*
* @param string $usage
* @return bool
*/
public function isUsages(string $usage)
{
return in_array($usage, self::SMS_USAGE);
}
} }

View File

@ -11,6 +11,12 @@ use Hyperf\DbConnection\Db;
class UserService extends BaseService class UserService extends BaseService
{ {
public function isMobileExist(string $mobile): bool
{
return User::where('mobile', $mobile)->exists();
}
/** /**
* 获取用户信息 * 获取用户信息
* *
@ -28,10 +34,11 @@ class UserService extends BaseService
* *
* @param string $mobile 手机号 * @param string $mobile 手机号
* @param string $password 登录密码 * @param string $password 登录密码
* @return User|bool * @return false|User
*/ */
public function login(string $mobile, string $password) public function login(string $mobile, string $password)
{ {
$user = User::where('mobile', $mobile)->first();
if (!$user = User::where('mobile', $mobile)->first()) { if (!$user = User::where('mobile', $mobile)->first()) {
return false; return false;
} }