Ems.php 4.5 KB

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