Ems.php 3.6 KB

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