Ems.php 3.3 KB

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