Ems.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. //发送前验证码
  32. if (config('fastadmin.user_api_captcha')) {
  33. if (!\think\Validate::is($captcha, 'captcha')) {
  34. $this->error("验证码不正确");
  35. }
  36. }
  37. $last = Emslib::get($email, $event);
  38. if ($last && time() - $last['createtime'] < 60) {
  39. $this->error(__('发送频繁'));
  40. }
  41. $ipSendTotal = \app\common\model\Ems::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
  42. if ($ipSendTotal >= 5) {
  43. $this->error(__('发送频繁'));
  44. }
  45. if ($event) {
  46. $userinfo = User::getByEmail($email);
  47. if ($event == 'register' && $userinfo) {
  48. //已被注册
  49. $this->error(__('已被注册'));
  50. } elseif (in_array($event, ['changeemail']) && $userinfo) {
  51. //被占用
  52. $this->error(__('已被占用'));
  53. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  54. //未注册
  55. $this->error(__('未注册'));
  56. }
  57. }
  58. $ret = Emslib::send($email, null, $event);
  59. if ($ret) {
  60. $this->success(__('发送成功'));
  61. } else {
  62. $this->error(__('发送失败'));
  63. }
  64. }
  65. /**
  66. * 检测验证码
  67. *
  68. * @ApiMethod (POST)
  69. * @param string $email 邮箱
  70. * @param string $event 事件名称
  71. * @param string $captcha 验证码
  72. */
  73. public function check()
  74. {
  75. $email = $this->request->post("email");
  76. $event = $this->request->post("event");
  77. $event = $event ? $event : 'register';
  78. $captcha = $this->request->post("captcha");
  79. if ($event) {
  80. $userinfo = User::getByEmail($email);
  81. if ($event == 'register' && $userinfo) {
  82. //已被注册
  83. $this->error(__('已被注册'));
  84. } elseif (in_array($event, ['changeemail']) && $userinfo) {
  85. //被占用
  86. $this->error(__('已被占用'));
  87. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  88. //未注册
  89. $this->error(__('未注册'));
  90. }
  91. }
  92. $ret = Emslib::check($email, $captcha, $event);
  93. if ($ret) {
  94. $this->success(__('成功'));
  95. } else {
  96. $this->error(__('验证码不正确'));
  97. }
  98. }
  99. }