酒店预订平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

105 lines
3.0 KiB

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