Sms.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\service\SmsService;
  5. use app\common\model\User;
  6. use app\common\service\UserService;
  7. use think\exception\HttpResponseException;
  8. /**
  9. * 短信验证码接口
  10. */
  11. class Sms extends Api
  12. {
  13. protected $noNeedLogin = '*';
  14. protected $noNeedRight = '*';
  15. /**
  16. * 发送验证码
  17. *
  18. * @ApiMethod (POST)
  19. * @ApiParams (name="mobile", type="string", required=true, description="手机号")
  20. * @ApiParams (name="event", type="string", required=true, description="事件名称")
  21. */
  22. public function send()
  23. {
  24. $mobile = $this->request->post("mobile");
  25. $event = $this->request->post("event");
  26. $event = $event ? $event : 'register';
  27. if (!$mobile || !preg_match("/^1[3-9]\d{9}$/", $mobile)) {
  28. $this->error(__('手机号格式错误'));
  29. }
  30. if (!preg_match("/^[a-z0-9_\-]{3,30}$/i", $event)) {
  31. $this->error(__('事件名称错误'));
  32. }
  33. //发送前验证码
  34. $captcha = $this->request->post('captcha');
  35. if (config('site.sms_captcha')) {
  36. if (!preg_match("/^[a-z0-9]{4,6}$/i", $captcha)) {
  37. $this->error(__('图形验证码格式错误'));
  38. }
  39. if (!\think\Validate::is($captcha, 'captcha')) {
  40. $this->error("验证码不正确");
  41. }
  42. }
  43. try {
  44. if ($event) {
  45. $userinfo = UserService::getOne(['mobile' => $mobile]);
  46. if ($event == 'register' && $userinfo) {
  47. //已被注册
  48. $this->error(__('已被注册'));
  49. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  50. //被占用
  51. $this->error(__('已被占用'));
  52. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  53. //未注册
  54. $this->error(__('未注册'));
  55. }
  56. }
  57. $result = \app\common\library\Sms::send($mobile, null, $event);
  58. if ($result) {
  59. $this->success(__('发送成功'));
  60. } else {
  61. $this->error(__('发送失败'));
  62. }
  63. } catch (\Exception $e) {
  64. if ($e instanceof HttpResponseException) {
  65. throw $e;
  66. }
  67. $this->error($e->getMessage());
  68. }
  69. }
  70. /**
  71. * 检测验证码
  72. *
  73. * @ApiMethod (POST)
  74. * @ApiParams (name="mobile", type="string", required=true, description="手机号")
  75. * @ApiParams (name="event", type="string", required=true, description="事件名称")
  76. * @ApiParams (name="captcha", type="string", required=true, description="验证码")
  77. */
  78. public function check(SmsService $SmsService)
  79. {
  80. $mobile = $this->request->post("mobile");
  81. $event = $this->request->post("event");
  82. $event = $event ? $event : 'register';
  83. $captcha = $this->request->post("code");
  84. if (!$mobile || !preg_match("/^1[3-9]\d{9}$/", $mobile)) {
  85. $this->error(__('手机号格式错误'));
  86. }
  87. if (!preg_match("/^[a-z0-9_\-]{3,30}$/i", $event)) {
  88. $this->error(__('事件名称错误'));
  89. }
  90. if (!preg_match("/^[0-9]{4}$/", $captcha)) {
  91. $this->error(__('验证码格式错误'));
  92. }
  93. try {
  94. $ret = $SmsService->check($mobile, $captcha);
  95. if ($ret) {
  96. $this->success(__('成功'));
  97. } else {
  98. $this->error(__('验证码不正确'));
  99. }
  100. } catch (\Exception $e) {
  101. if ($e instanceof HttpResponseException) {
  102. throw $e;
  103. }
  104. $this->error($e->getMessage());
  105. }
  106. }
  107. }