Ems.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // | Original reference: https://gitee.com/karson/fastadmin
  12. // +----------------------------------------------------------------------
  13. // +----------------------------------------------------------------------
  14. // | 邮箱验证码类
  15. // +----------------------------------------------------------------------
  16. namespace app\common\library;
  17. use app\common\model\Ems as EmsModel;
  18. use think\facade\Event;
  19. use util\Random;
  20. class Ems
  21. {
  22. /**
  23. * 验证码有效时长
  24. * @var int
  25. */
  26. protected static $expire = 120;
  27. /**
  28. * 最大允许检测的次数
  29. * @var int
  30. */
  31. protected static $maxCheckNums = 10;
  32. /**
  33. * 获取最后一次邮箱发送的数据
  34. *
  35. * @param int $email 邮箱
  36. * @param string $event 事件
  37. * @return Ems
  38. */
  39. public static function get($email, $event = 'default')
  40. {
  41. $ems = EmsModel::where(['email' => $email, 'event' => $event])
  42. ->order('id', 'DESC')
  43. ->find();
  44. Event::trigger('ems_get', $ems);
  45. return $ems ?: null;
  46. }
  47. /**
  48. * 发送验证码
  49. *
  50. * @param int $email 邮箱
  51. * @param int $code 验证码,为空时将自动生成4位数字
  52. * @param string $event 事件
  53. * @return boolean
  54. */
  55. public static function send($email, $code = null, $event = 'default')
  56. {
  57. $code = is_null($code) ? Random::numeric(config('captcha.length')) : $code;
  58. $time = time();
  59. $ip = request()->ip();
  60. $ems = EmsModel::create(['event' => $event, 'email' => $email, 'code' => $code, 'ip' => $ip, 'create_time' => $time]);
  61. $result = Event::trigger('ems_send', $ems, 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. $result = Event::trigger('ems_notice', $params, true);
  84. return (bool) $result;
  85. }
  86. /**
  87. * 校验验证码
  88. *
  89. * @param int $email 邮箱
  90. * @param int $code 验证码
  91. * @param string $event 事件
  92. * @return boolean
  93. */
  94. public static function check($email, $code, $event = 'default')
  95. {
  96. $time = time() - self::$expire;
  97. $ems = EmsModel::where(['email' => $email, 'event' => $event])
  98. ->order('id', 'DESC')
  99. ->find();
  100. if ($ems) {
  101. if ($ems['create_time'] > $time && $ems['times'] <= self::$maxCheckNums) {
  102. $correct = $code == $ems['code'];
  103. if (!$correct) {
  104. $ems->times = $ems->times + 1;
  105. $ems->save();
  106. return false;
  107. } else {
  108. $result = Event::trigger('ems_check', $ems, true);
  109. return $result;
  110. }
  111. } else {
  112. // 过期则清空该邮箱验证码
  113. self::flush($email, $event);
  114. return false;
  115. }
  116. } else {
  117. return false;
  118. }
  119. }
  120. /**
  121. * 清空指定邮箱验证码
  122. *
  123. * @param int $email 邮箱
  124. * @param string $event 事件
  125. * @return boolean
  126. */
  127. public static function flush($email, $event = 'default')
  128. {
  129. EmsModel::where(['email' => $email, 'event' => $event])->delete();
  130. Event::trigger('ems_flush');
  131. return true;
  132. }
  133. }