Ems.php 4.1 KB

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