Ems.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // | Original reference: https://gitee.com/karson/fastadmin
  12. // +----------------------------------------------------------------------
  13. // +----------------------------------------------------------------------
  14. // | 邮箱验证码接口
  15. // +----------------------------------------------------------------------
  16. namespace app\api\controller;
  17. use app\common\controller\Api;
  18. use app\common\library\Ems as Emslib;
  19. use app\common\model\User;
  20. use think\facade\Event;
  21. use think\facade\Validate;
  22. /**
  23. * 邮箱验证码接口
  24. */
  25. class Ems extends Api
  26. {
  27. protected $noNeedLogin = '*';
  28. protected $noNeedRight = '*';
  29. /**
  30. * 发送验证码
  31. */
  32. public function send()
  33. {
  34. $email = $this->request->param("email");
  35. $event = $this->request->param("event");
  36. $event = $event ?: 'register';
  37. if (!$email || !Validate::isEmail($email)) {
  38. $this->error('邮箱格式不正确!');
  39. }
  40. if (!preg_match("/^[a-z0-9_\-]{3,30}\$/i", $event)) {
  41. $this->error('事件名称错误');
  42. }
  43. $last = Emslib::get($email, $event);
  44. if ($last && time() - $last['create_time'] < 60) {
  45. $this->error('发送频繁');
  46. }
  47. if ($event) {
  48. $userinfo = User::getByEmail($email);
  49. if ($event == 'register' && $userinfo) {
  50. $this->error('已被注册');
  51. } elseif (in_array($event, ['changeemail']) && $userinfo) {
  52. $this->error('已被占用');
  53. } elseif (in_array($event, ['changepwd', 'resetpwd', 'actemail']) && !$userinfo) {
  54. $this->error('未注册');
  55. }
  56. }
  57. if (!Event::hasListener('ems_send')) {
  58. $this->error('请在后台插件管理安装邮箱验证插件');
  59. }
  60. $ret = Emslib::send($email, null, $event);
  61. if ($ret) {
  62. $this->success('发送成功');
  63. } else {
  64. $this->error('发送失败');
  65. }
  66. }
  67. /**
  68. * 检测验证码
  69. */
  70. public function check()
  71. {
  72. $email = $this->request->param("email");
  73. $event = $this->request->param("event");
  74. $event = $event ?: 'register';
  75. $captcha = $this->request->param("captcha");
  76. if (!$email || !Validate::isEmail($email)) {
  77. $this->error('邮箱格式不正确!');
  78. }
  79. if (!preg_match("/^[a-z0-9_\-]{3,30}\$/i", $event)) {
  80. $this->error('事件名称错误');
  81. }
  82. if (!preg_match("/^[a-z0-9]{4,6}\$/i", $captcha)) {
  83. $this->error('验证码格式错误');
  84. }
  85. if ($event) {
  86. $userinfo = User::getByEmail($email);
  87. if ($event == 'register' && $userinfo) {
  88. //已被注册
  89. $this->error('已被注册');
  90. } elseif (in_array($event, ['changeemail']) && $userinfo) {
  91. //被占用
  92. $this->error('已被占用');
  93. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  94. //未注册
  95. $this->error('未注册');
  96. }
  97. }
  98. $ret = Emslib::check($email, $captcha, $event);
  99. if ($ret) {
  100. $this->success('成功');
  101. } else {
  102. $this->error('验证码不正确');
  103. }
  104. }
  105. }