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