Sms.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\api\controller;
  3. use ba\Captcha;
  4. use think\Exception;
  5. use app\common\model\User;
  6. use modules\sms\Sms as smsLib;
  7. use app\common\controller\Frontend;
  8. use think\facade\Event;
  9. class Sms extends Frontend
  10. {
  11. protected $noNeedLogin = ['send'];
  12. public function initialize()
  13. {
  14. parent::initialize();
  15. }
  16. public function send()
  17. {
  18. $mobile = $this->request->post("mobile");
  19. $templateCode = $this->request->post("template_code");
  20. if (!$mobile) {
  21. $this->error(__('Mobile format error'));
  22. }
  23. if (!$templateCode) {
  24. $this->error(__('Parameter error'));
  25. }
  26. // 检查频繁发送
  27. $captcha = (new Captcha())->getCaptchaData($mobile . $templateCode);
  28. if ($captcha && time() - $captcha['createtime'] < 60) {
  29. $this->error(__('Frequent SMS sending'));
  30. }
  31. // 检查号码占用
  32. $userInfo = User::where('mobile', $mobile)->find();
  33. if ($templateCode == 'user_register' && $userInfo) {
  34. $this->error(__('Mobile number has been registered, please log in directly'));
  35. } elseif ($templateCode == 'user_change_mobile' && $userInfo) {
  36. $this->error(__('The mobile number has been occupied'));
  37. } elseif (in_array($templateCode, ['user_retrieve_pwd', 'user_mobile_verify']) && !$userInfo) {
  38. $this->error(__('Mobile number not registered'));
  39. }
  40. // 通过手机号验证账户
  41. if ($templateCode == 'user_mobile_verify') {
  42. if (!$this->auth->isLogin()) {
  43. $this->error(__('Please login first'));
  44. }
  45. if ($this->auth->mobile != $mobile) {
  46. $this->error(__('Please use the account registration mobile to send the verification code'));
  47. }
  48. // 验证账户密码
  49. $password = $this->request->post('password');
  50. if ($this->auth->password != encrypt_password($password, $this->auth->salt)) {
  51. $this->error(__('Password error'));
  52. }
  53. }
  54. // 监听短信模板分析完成
  55. Event::listen('TemplateAnalysisAfter', function ($templateData) use ($mobile, $templateCode) {
  56. // 存储验证码
  57. if (array_key_exists('code', $templateData['variables'])) {
  58. (new Captcha())->create($mobile . $templateCode, $templateData['variables']['code']);
  59. }
  60. if (array_key_exists('alnum', $templateData['variables'])) {
  61. (new Captcha())->create($mobile . $templateCode, $templateData['variables']['alnum']);
  62. }
  63. });
  64. try {
  65. smsLib::send($templateCode, $mobile);
  66. } catch (Exception $e) {
  67. if (!env('APP_DEBUG', false)) {
  68. $this->error(__('Failed to send SMS. Please contact the website administrator'));
  69. } else {
  70. // throw new Exception($e->getMessage());
  71. $this->error(__($e->getMessage()));
  72. }
  73. }
  74. $this->success(__('SMS sent successfully'));
  75. }
  76. }