Ems.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\common\library;
  3. use think\Hook;
  4. /**
  5. * 邮箱验证码类
  6. */
  7. class Ems
  8. {
  9. /**
  10. * 验证码有效时长
  11. * @var int
  12. */
  13. protected static $expire = 120;
  14. /**
  15. * 最大允许检测的次数
  16. * @var int
  17. */
  18. protected static $maxCheckNums = 10;
  19. /**
  20. * 获取最后一次邮箱发送的数据
  21. *
  22. * @param int $email 邮箱
  23. * @param string $event 事件
  24. * @return Ems
  25. */
  26. public static function get($email, $event = 'default')
  27. {
  28. $event = 'default';
  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. $event = 'default';
  47. $code = is_null($code) ? mt_rand(1000, 9999) : $code;
  48. $time = time();
  49. $ip = request()->ip();
  50. $ems = \app\common\model\Ems::create(['event' => $event, 'email' => $email, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
  51. $result = Hook::listen('ems_send', $ems, null, true);
  52. if (!$result) {
  53. $ems->delete();
  54. return false;
  55. }
  56. return true;
  57. }
  58. /**
  59. * 发送通知
  60. *
  61. * @param mixed $email 邮箱,多个以,分隔
  62. * @param string $msg 消息内容
  63. * @param string $template 消息模板
  64. * @return boolean
  65. */
  66. public static function notice($email, $msg = '', $template = null)
  67. {
  68. $params = [
  69. 'email' => $email,
  70. 'msg' => $msg,
  71. 'template' => $template
  72. ];
  73. $result = Hook::listen('ems_notice', $params, null, true);
  74. return $result ? true : false;
  75. }
  76. /**
  77. * 校验验证码
  78. *
  79. * @param int $email 邮箱
  80. * @param int $code 验证码
  81. * @param string $event 事件
  82. * @return boolean
  83. */
  84. public static function check($email, $code, $event = 'default')
  85. {
  86. $event = 'default';
  87. $time = time() - self::$expire;
  88. $ems = \app\common\model\Ems::where(['email' => $email, 'event' => $event])
  89. ->order('id', 'DESC')
  90. ->find();
  91. if ($ems) {
  92. if ($ems['createtime'] > $time && $ems['times'] <= self::$maxCheckNums) {
  93. $correct = $code == $ems['code'];
  94. if (!$correct) {
  95. $ems->times = $ems->times + 1;
  96. $ems->save();
  97. return false;
  98. } else {
  99. $result = Hook::listen('ems_check', $ems, null, true);
  100. return true;
  101. }
  102. } else {
  103. // 过期则清空该邮箱验证码
  104. self::flush($email, $event);
  105. return false;
  106. }
  107. } else {
  108. return false;
  109. }
  110. }
  111. /**
  112. * 清空指定邮箱验证码
  113. *
  114. * @param int $email 邮箱
  115. * @param string $event 事件
  116. * @return boolean
  117. */
  118. public static function flush($email, $event = 'default')
  119. {
  120. $event = 'default';
  121. \app\common\model\Ems::
  122. where(['email' => $email, 'event' => $event])
  123. ->delete();
  124. Hook::listen('ems_flush');
  125. return true;
  126. }
  127. }