| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- namespace app\controller\common;
- use app\extra\basic\Base;
- use app\extra\service\basic\SmsService;
- use app\middleware\AuthMiddleware;
- use LinFly\Annotation\Route\Controller;
- use LinFly\Annotation\Route\Route;
- use support\Request;
- use support\Response;
- use Tinywan\Captcha\Captcha;
- use Webman\Annotation\Middleware;
- #[Controller(prefix: "/api/sms"),Middleware(AuthMiddleware::class)]
- class Sms extends Base
- {
- /**
- * 图片验证码
- */
- #[Route(path: "captcha",methods: "get")]
- public function getCaptcha(): Response
- {
- try {
- $captcha = Captcha::base64();
- return successTrans("success.data",$captcha);
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- /**
- * 发送登录/注册验证码
- * @param Request $request
- * @return Response
- */
- #[Route(path: "send",methods: "post")]
- public function getSms(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "mobile.require" => trans("empty.mobile"),
- "mobile.mobile" => trans("error.mobile"),
- "code.require" => trans("empty.code"),
- "key.require" => trans("empty.data"),
- "scene.require" => trans("empty.data"),
- ],"post");
- if (!is_array($param)) return error($param);
- if (Captcha::check($param['code'],$param['key']) === false) return errorTrans("error.captcha");
- [$state,$msg] = (new SmsService)->sendSceneSms($param['mobile'],$param['scene'], $param['scene']=='register');
- if (!$state) return error($msg);
- return success($msg);
- } catch (\Throwable $throwable) {
- echo $throwable->getMessage()."\n";
- echo $throwable->getFile()."\n";
- echo $throwable->getLine()."\n";
- return error($throwable->getMessage());
- }
- }
- }
|