Ems.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace app\common\library;
  3. use fast\Random;
  4. use think\Hook;
  5. /**
  6. * 邮箱验证码类
  7. */
  8. class Ems
  9. {
  10. /**
  11. * 验证码有效时长
  12. * @var int
  13. */
  14. protected static $expire = 120;
  15. /**
  16. * 最大允许检测的次数
  17. * @var int
  18. */
  19. protected static $maxCheckNums = 10;
  20. /**
  21. * 获取最后一次邮箱发送的数据
  22. *
  23. * @param int $email 邮箱
  24. * @param string $event 事件
  25. * @return Ems
  26. */
  27. public static function get($email, $event = 'default')
  28. {
  29. $ems = \app\common\model\Ems::where(['email' => $email, 'event' => $event])
  30. ->order('id', 'DESC')
  31. ->find();
  32. Hook::listen('ems_get', $ems, null, true);
  33. return $ems ? $ems : null;
  34. }
  35. /**
  36. * 发送验证码
  37. *
  38. * @param int $email 邮箱
  39. * @param int $code 验证码,为空时将自动生成4位数字
  40. * @param string $event 事件
  41. * @return boolean
  42. */
  43. public static function send($email, $code = null, $event = 'default')
  44. {
  45. $code = is_null($code) ? Random::numeric(config('captcha.length')) : $code;
  46. $time = time();
  47. $ip = request()->ip();
  48. $ems = \app\common\model\Ems::create(['event' => $event, 'email' => $email, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
  49. if (!Hook::get('ems_send')) {
  50. //采用框架默认的邮件推送
  51. Hook::add('ems_send', function ($params) {
  52. $obj = new Email();
  53. $result = $obj
  54. ->to($params->email)
  55. ->subject('请查收你的验证码!')
  56. ->message("你的验证码是:" . $params->code . "," . ceil(self::$expire / 60) . "分钟内有效。")
  57. ->send();
  58. return $result;
  59. });
  60. }
  61. $result = Hook::listen('ems_send', $ems, null, true);
  62. if (!$result) {
  63. $ems->delete();
  64. return false;
  65. }
  66. return true;
  67. }
  68. /**
  69. * 发送通知
  70. *
  71. * @param mixed $email 邮箱,多个以,分隔
  72. * @param string $msg 消息内容
  73. * @param string $template 消息模板
  74. * @return boolean
  75. */
  76. public static function notice($email, $msg = '', $template = null)
  77. {
  78. $params = [
  79. 'email' => $email,
  80. 'msg' => $msg,
  81. 'template' => $template
  82. ];
  83. if (!Hook::get('ems_notice')) {
  84. //采用框架默认的邮件推送
  85. Hook::add('ems_notice', function ($params) {
  86. $subject = '你收到一封新的邮件!';
  87. $content = $params['msg'];
  88. $email = new Email();
  89. $result = $email->to($params['email'])
  90. ->subject($subject)
  91. ->message($content)
  92. ->send();
  93. return $result;
  94. });
  95. }
  96. $result = Hook::listen('ems_notice', $params, null, true);
  97. return (bool)$result;
  98. }
  99. /**
  100. * 校验验证码
  101. *
  102. * @param int $email 邮箱
  103. * @param int $code 验证码
  104. * @param string $event 事件
  105. * @return boolean
  106. */
  107. public static function check($email, $code, $event = 'default')
  108. {
  109. $time = time() - self::$expire;
  110. $ems = \app\common\model\Ems::where(['email' => $email, 'event' => $event])
  111. ->order('id', 'DESC')
  112. ->find();
  113. if ($ems) {
  114. if ($ems['createtime'] > $time && $ems['times'] <= self::$maxCheckNums) {
  115. $correct = $code == $ems['code'];
  116. if (!$correct) {
  117. $ems->times = $ems->times + 1;
  118. $ems->save();
  119. return false;
  120. } else {
  121. $result = Hook::listen('ems_check', $ems, null, true);
  122. return true;
  123. }
  124. } else {
  125. // 过期则清空该邮箱验证码
  126. self::flush($email, $event);
  127. return false;
  128. }
  129. } else {
  130. return false;
  131. }
  132. }
  133. /**
  134. * 清空指定邮箱验证码
  135. *
  136. * @param int $email 邮箱
  137. * @param string $event 事件
  138. * @return boolean
  139. */
  140. public static function flush($email, $event = 'default')
  141. {
  142. \app\common\model\Ems::where(['email' => $email, 'event' => $event])
  143. ->delete();
  144. Hook::listen('ems_flush');
  145. return true;
  146. }
  147. }