Ems.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace addons\cms\controller\api;
  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. $last = Emslib::get($email, $event);
  39. if ($last && time() - $last['createtime'] < 60) {
  40. $this->error(__('发送频繁'));
  41. }
  42. if ($event) {
  43. $userinfo = User::getByEmail($email);
  44. if ($event == 'register' && $userinfo) {
  45. //已被注册
  46. $this->error(__('已被注册'));
  47. } elseif (in_array($event, ['changeemail']) && $userinfo) {
  48. //被占用
  49. $this->error(__('已被占用'));
  50. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  51. //未注册
  52. $this->error(__('未注册'));
  53. }
  54. }
  55. $ret = Emslib::send($email, null, $event);
  56. if ($ret) {
  57. $this->success(__('发送成功'));
  58. } else {
  59. $this->error(__('发送失败'));
  60. }
  61. }
  62. /**
  63. * 检测验证码
  64. *
  65. * @param string $email 邮箱
  66. * @param string $event 事件名称
  67. * @param string $captcha 验证码
  68. */
  69. public function check()
  70. {
  71. $email = $this->request->param("email");
  72. $event = $this->request->param("event");
  73. $event = $event ? $event : 'register';
  74. $captcha = $this->request->param("captcha");
  75. if ($event) {
  76. $userinfo = User::getByEmail($email);
  77. if ($event == 'register' && $userinfo) {
  78. //已被注册
  79. $this->error(__('已被注册'));
  80. } elseif (in_array($event, ['changeemail']) && $userinfo) {
  81. //被占用
  82. $this->error(__('已被占用'));
  83. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  84. //未注册
  85. $this->error(__('未注册'));
  86. }
  87. }
  88. $ret = Emslib::check($email, $captcha, $event);
  89. if ($ret) {
  90. $this->success(__('成功'));
  91. } else {
  92. $this->error(__('验证码不正确'));
  93. }
  94. }
  95. }