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.
 
 
 
 
 
 

134 lines
3.5 KiB

  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. $ems = \app\common\model\Ems::
  29. 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) ? mt_rand(1000, 9999) : $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. $result = Hook::listen('ems_send', $ems, null, true);
  50. if (!$result) {
  51. $ems->delete();
  52. return false;
  53. }
  54. return true;
  55. }
  56. /**
  57. * 发送通知
  58. *
  59. * @param mixed $email 邮箱,多个以,分隔
  60. * @param string $msg 消息内容
  61. * @param string $template 消息模板
  62. * @return boolean
  63. */
  64. public static function notice($email, $msg = '', $template = null)
  65. {
  66. $params = [
  67. 'email' => $email,
  68. 'msg' => $msg,
  69. 'template' => $template
  70. ];
  71. $result = Hook::listen('ems_notice', $params, null, true);
  72. return $result ? true : false;
  73. }
  74. /**
  75. * 校验验证码
  76. *
  77. * @param int $email 邮箱
  78. * @param int $code 验证码
  79. * @param string $event 事件
  80. * @return boolean
  81. */
  82. public static function check($email, $code, $event = 'default')
  83. {
  84. $time = time() - self::$expire;
  85. $ems = \app\common\model\Ems::where(['email' => $email, 'event' => $event])
  86. ->order('id', 'DESC')
  87. ->find();
  88. if ($ems) {
  89. if ($ems['createtime'] > $time && $ems['times'] <= self::$maxCheckNums) {
  90. $correct = $code == $ems['code'];
  91. if (!$correct) {
  92. $ems->times = $ems->times + 1;
  93. $ems->save();
  94. return false;
  95. } else {
  96. $result = Hook::listen('ems_check', $ems, null, true);
  97. return true;
  98. }
  99. } else {
  100. // 过期则清空该邮箱验证码
  101. self::flush($email, $event);
  102. return false;
  103. }
  104. } else {
  105. return false;
  106. }
  107. }
  108. /**
  109. * 清空指定邮箱验证码
  110. *
  111. * @param int $email 邮箱
  112. * @param string $event 事件
  113. * @return boolean
  114. */
  115. public static function flush($email, $event = 'default')
  116. {
  117. \app\common\model\Ems::
  118. where(['email' => $email, 'event' => $event])
  119. ->delete();
  120. Hook::listen('ems_flush');
  121. return true;
  122. }
  123. }