Ems.php 2.7 KB

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