1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- declare(strict_types=1);
- namespace App\Controller\Api\v1;
- use App\Controller\AbstractController;
- use App\Model\Arts\SmsCodeModel;
- use App\Model\Arts\UserModel;
- use App\Request\Api\v1\Passport\LoginMobileRequest;
- use App\Utils\AppResult;
- use App\Utils\Encrypt\TokenFast;
- use Hyperf\Stringable\Str;
- /**
- * 通行证
- * 示例
- */
- class PassportController extends AbstractController
- {
- // 日志模块名称
- const LOG_MODULE = 'v1/PassportController';
- /**
- * 手机号登录注册
- * @param LoginMobileRequest $request
- * @return \Psr\Http\Message\MessageInterface|\Psr\Http\Message\ResponseInterface
- * @throws \Psr\Container\ContainerExceptionInterface
- * @throws \Psr\Container\NotFoundExceptionInterface
- */
- public function login_mobile(LoginMobileRequest $request)
- {
- $params = $request->validated();
- // 校验验证码
- $sms = new SmsCodeModel();
- if (!$sms->check($params['mobile'], $params['captcha'], $params['event'], 50)) {
- return AppResult::error($sms->getMessage());
- }
- $UserModel = new UserModel();
- if ($user = $UserModel->getByMobile($params['mobile'])){
- if ($user['status'] != 1){
- return AppResult::error('账号被锁定,无法登录');
- }
- }else{
- if (!$UserModel->register(mobile: $params['mobile'])){
- return AppResult::error('注册失败');
- }
- $user = $UserModel->getData();
- }
- $token = (string)Str::uuid();
- if (!TokenFast::set($token,$user['id'],2592000)){
- return AppResult::error('获取token失败');
- }
- $sms->flush($params['mobile'],$params['event']);
- return AppResult::success('登录成功',[
- 'token' => $token
- ]);
- }
- }
|