Sms.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace app\controller\common;
  3. use app\extra\basic\Base;
  4. use app\extra\service\basic\SmsService;
  5. use app\middleware\AuthMiddleware;
  6. use LinFly\Annotation\Route\Controller;
  7. use LinFly\Annotation\Route\Route;
  8. use support\Request;
  9. use support\Response;
  10. use Tinywan\Captcha\Captcha;
  11. use Webman\Annotation\Middleware;
  12. #[Controller(prefix: "/api/sms"),Middleware(AuthMiddleware::class)]
  13. class Sms extends Base
  14. {
  15. /**
  16. * 图片验证码
  17. */
  18. #[Route(path: "captcha",methods: "get")]
  19. public function getCaptcha(): Response
  20. {
  21. try {
  22. $captcha = Captcha::base64();
  23. return successTrans("success.data",$captcha);
  24. } catch (\Throwable $throwable) {
  25. return error($throwable->getMessage());
  26. }
  27. }
  28. /**
  29. * 发送登录/注册验证码
  30. * @param Request $request
  31. * @return Response
  32. */
  33. #[Route(path: "send",methods: "post")]
  34. public function getSms(Request $request): Response
  35. {
  36. try {
  37. $param = $this->_valid([
  38. "mobile.require" => trans("empty.mobile"),
  39. "mobile.mobile" => trans("error.mobile"),
  40. "code.require" => trans("empty.code"),
  41. "key.require" => trans("empty.data"),
  42. "scene.require" => trans("empty.data"),
  43. ],"post");
  44. if (!is_array($param)) return error($param);
  45. if (Captcha::check($param['code'],$param['key']) === false) return errorTrans("error.captcha");
  46. [$state,$msg] = (new SmsService)->sendSceneSms($param['mobile'],$param['scene'], $param['scene']=='register');
  47. if (!$state) return error($msg);
  48. return success($msg);
  49. } catch (\Throwable $throwable) {
  50. echo $throwable->getMessage()."\n";
  51. echo $throwable->getFile()."\n";
  52. echo $throwable->getLine()."\n";
  53. return error($throwable->getMessage());
  54. }
  55. }
  56. }