Ems.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 __construct(){
  15. $this->error('投票活动结束了');
  16. }
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. }
  21. /**
  22. * 发送验证码
  23. *
  24. * @ApiMethod (POST)
  25. * @param string $email 邮箱
  26. * @param string $event 事件名称
  27. */
  28. public function send()
  29. {
  30. $email = $this->request->post("email");
  31. $event = $this->request->post("event");
  32. $event = $event ? $event : 'register';
  33. $last = Emslib::get($email, $event);
  34. if ($last && time() - $last['createtime'] < 60) {
  35. $this->error(__('发送频繁'));
  36. }
  37. $ipSendTotal = \app\common\model\Ems::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
  38. if ($ipSendTotal >= 5) {
  39. $this->error(__('发送频繁'));
  40. }
  41. if ($event) {
  42. $userinfo = User::getByEmail($email);
  43. if ($event == 'register' && $userinfo) {
  44. //已被注册
  45. $this->error(__('已被注册'));
  46. } elseif (in_array($event, ['changeemail']) && $userinfo) {
  47. //被占用
  48. $this->error(__('已被占用'));
  49. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  50. //未注册
  51. $this->error(__('未注册'));
  52. }
  53. }
  54. $ret = Emslib::send($email, null, $event);
  55. if ($ret) {
  56. $this->success(__('发送成功'));
  57. } else {
  58. $this->error(__('发送失败'));
  59. }
  60. }
  61. /**
  62. * 检测验证码
  63. *
  64. * @ApiMethod (POST)
  65. * @param string $email 邮箱
  66. * @param string $event 事件名称
  67. * @param string $captcha 验证码
  68. */
  69. public function check()
  70. {
  71. $email = $this->request->post("email");
  72. $event = $this->request->post("event");
  73. $event = $event ? $event : 'register';
  74. $captcha = $this->request->post("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. }