Ems.php 2.9 KB

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