Ems.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems as Emslib;
  5. use app\common\model\User;
  6. use think\Hook;
  7. /**
  8. * 邮箱验证码接口
  9. */
  10. class Ems extends Api
  11. {
  12. protected $noNeedLogin = '*';
  13. protected $noNeedRight = '*';
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. }
  18. /**
  19. * 发送验证码
  20. *
  21. * @ApiMethod (POST)
  22. * @param string $email 邮箱
  23. * @param string $event 事件名称
  24. */
  25. public function send()
  26. {
  27. $email = $this->request->post("email");
  28. $captcha = $this->request->post("captcha");
  29. $event = $this->request->post("event");
  30. $event = $event ? $event : 'register';
  31. if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  32. $this->error(__('邮箱格式错误'));
  33. }
  34. if (!preg_match("/^[a-z0-9_\-]{3,30}\$/i", $event)) {
  35. $this->error(__('事件名称错误'));
  36. }
  37. //发送前验证码
  38. if (config('fastadmin.user_api_captcha')) {
  39. if (!preg_match("/^[a-z0-9]{4,6}\$/i", $captcha)) {
  40. $this->error(__('验证码格式错误'));
  41. }
  42. if (!\think\Validate::is($captcha, 'captcha')) {
  43. $this->error("验证码不正确");
  44. }
  45. }
  46. $last = Emslib::get($email, $event);
  47. if ($last && time() - $last['createtime'] < 60) {
  48. $this->error(__('发送频繁'));
  49. }
  50. $ipSendTotal = \app\common\model\Ems::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
  51. if ($ipSendTotal >= 5) {
  52. $this->error(__('发送频繁'));
  53. }
  54. if ($event) {
  55. $userinfo = User::getByEmail($email);
  56. if ($event == 'register' && $userinfo) {
  57. //已被注册
  58. $this->error(__('已被注册'));
  59. } elseif (in_array($event, ['changeemail']) && $userinfo) {
  60. //被占用
  61. $this->error(__('已被占用'));
  62. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  63. //未注册
  64. $this->error(__('未注册'));
  65. }
  66. }
  67. $ret = Emslib::send($email, null, $event);
  68. if ($ret) {
  69. $this->success(__('发送成功'));
  70. } else {
  71. $this->error(__('发送失败'));
  72. }
  73. }
  74. /**
  75. * 检测验证码
  76. *
  77. * @ApiMethod (POST)
  78. * @param string $email 邮箱
  79. * @param string $event 事件名称
  80. * @param string $captcha 验证码
  81. */
  82. public function check()
  83. {
  84. $email = $this->request->post("email");
  85. $event = $this->request->post("event");
  86. $event = $event ? $event : 'register';
  87. $captcha = $this->request->post("captcha");
  88. if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  89. $this->error(__('邮箱格式错误'));
  90. }
  91. if (!preg_match("/^[a-z0-9_\-]{3,30}\$/i", $event)) {
  92. $this->error(__('事件名称错误'));
  93. }
  94. if (!preg_match("/^[a-z0-9]{4,6}\$/i", $captcha)) {
  95. $this->error(__('验证码格式错误'));
  96. }
  97. if ($event) {
  98. $userinfo = User::getByEmail($email);
  99. if ($event == 'register' && $userinfo) {
  100. //已被注册
  101. $this->error(__('已被注册'));
  102. } elseif (in_array($event, ['changeemail']) && $userinfo) {
  103. //被占用
  104. $this->error(__('已被占用'));
  105. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  106. //未注册
  107. $this->error(__('未注册'));
  108. }
  109. }
  110. $ret = Emslib::check($email, $captcha, $event);
  111. if ($ret) {
  112. $this->success(__('成功'));
  113. } else {
  114. $this->error(__('验证码不正确'));
  115. }
  116. }
  117. }