hyperf-chat/app/Controller/Api/V1/CommonController.php

110 lines
2.8 KiB
PHP
Raw Permalink Normal View History

2022-01-16 10:29:16 +08:00
<?php
declare(strict_types=1);
namespace App\Controller\Api\V1;
2022-01-22 16:04:54 +08:00
use App\Repository\UserRepository;
2022-01-21 21:46:17 +08:00
use App\Support\SendEmailCode;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use App\Service\SmsCodeService;
2022-01-22 20:08:19 +08:00
use App\Constant\SmsConstant;
2022-01-21 21:46:17 +08:00
/**
* class CommonController
*
* @Controller(prefix="/api/v1/common")
*/
2022-01-16 10:29:16 +08:00
class CommonController extends CController
{
2022-01-21 21:46:17 +08:00
/**
* @var SmsCodeService
*/
private $sms;
2022-01-22 21:02:53 +08:00
public function __construct(SmsCodeService $service)
{
parent::__construct();
$this->sms = $service;
}
2022-01-16 10:29:16 +08:00
/**
* 发送短信验证码
2022-01-21 21:46:17 +08:00
*
* @RequestMapping(path="sms-code", methods="post")
2022-01-16 10:29:16 +08:00
*/
2022-01-22 16:04:54 +08:00
public function SmsCode(UserRepository $userRepository)
2022-01-21 21:46:17 +08:00
{
$params = $this->request->all();
$this->validate($params, [
'channel' => "required|in:login,register,forget_account,change_account",
'mobile' => "required|phone"
]);
2022-01-16 10:29:16 +08:00
2022-01-21 21:46:17 +08:00
switch ($params['channel']) {
case SmsConstant::SmsLoginChannel:
case SmsConstant::SmsForgetAccountChannel:
2022-01-22 16:04:54 +08:00
if (!$userRepository->isExistMobile($params['mobile'])) {
2022-01-21 21:46:17 +08:00
return $this->response->fail("账号不存在或密码错误!");
}
break;
case SmsConstant::SmsRegisterChannel:
case SmsConstant::SmsChangeAccountChannel:
2022-01-22 16:04:54 +08:00
if ($userRepository->isExistMobile($params['mobile'])) {
2022-01-21 21:46:17 +08:00
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);
2022-01-16 10:29:16 +08:00
}
/**
* 发送邮件验证码
2022-01-21 21:46:17 +08:00
*
* @RequestMapping(path="email-code", methods="post")
2022-01-16 10:29:16 +08:00
*/
2022-01-21 21:46:17 +08:00
public function EmailCode(SendEmailCode $sendEmailCode)
{
$params = $this->request->inputs(['email']);
2022-01-23 15:13:58 +08:00
2022-01-21 21:46:17 +08:00
$this->validate($params, [
'email' => "required|email"
]);
$isTrue = $sendEmailCode->send(SendEmailCode::CHANGE_EMAIL, '绑定邮箱', $params['email']);
if (!$isTrue) {
return $this->response->fail('验证码发送失败!');
}
2022-01-16 10:29:16 +08:00
2022-01-21 21:46:17 +08:00
return $this->response->success([], '验证码发送成功...');
2022-01-16 10:29:16 +08:00
}
/**
* 公共设置
2022-01-21 21:46:17 +08:00
*
* @RequestMapping(path="setting", methods="post")
2022-01-16 10:29:16 +08:00
*/
2022-01-21 21:46:17 +08:00
public function Setting()
{
2022-01-16 10:29:16 +08:00
}
}