説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Ems.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 邮箱验证码类
  13. // +----------------------------------------------------------------------
  14. namespace app\common\library;
  15. use app\common\model\Ems as EmsModel;
  16. use util\Random;
  17. class Ems
  18. {
  19. /**
  20. * 验证码有效时长
  21. * @var int
  22. */
  23. protected static $expire = 120;
  24. /**
  25. * 最大允许检测的次数
  26. * @var int
  27. */
  28. protected static $maxCheckNums = 10;
  29. /**
  30. * 获取最后一次邮箱发送的数据
  31. *
  32. * @param int $email 邮箱
  33. * @param string $event 事件
  34. * @return Ems
  35. */
  36. public static function get($email, $event = 'default')
  37. {
  38. $ems = EmsModel::where(['email' => $email, 'event' => $event])
  39. ->order('id', 'DESC')
  40. ->find();
  41. hook('ems_get', $ems);
  42. return $ems ?: null;
  43. }
  44. /**
  45. * 发送验证码
  46. *
  47. * @param int $email 邮箱
  48. * @param int $code 验证码,为空时将自动生成4位数字
  49. * @param string $event 事件
  50. * @return boolean
  51. */
  52. public static function send($email, $code = null, $event = 'default')
  53. {
  54. $code = is_null($code) ? Random::numeric(config('captcha.length')) : $code;
  55. $time = time();
  56. $ems = EmsModel::create(['event' => $event, 'email' => $email, 'code' => $code, 'create_time' => $time]);
  57. $result = hook('ems_send', $ems, true, true);
  58. if (!$result) {
  59. $ems->delete();
  60. return false;
  61. }
  62. return true;
  63. }
  64. /**
  65. * 发送通知
  66. *
  67. * @param mixed $email 邮箱,多个以,分隔
  68. * @param string $msg 消息内容
  69. * @param string $template 消息模板
  70. * @return boolean
  71. */
  72. public static function notice($email, $msg = '', $template = null)
  73. {
  74. $params = [
  75. 'email' => $email,
  76. 'msg' => $msg,
  77. 'template' => $template,
  78. ];
  79. $result = hook('ems_notice', $params, true, true);
  80. return (bool) $result;
  81. }
  82. /**
  83. * 校验验证码
  84. *
  85. * @param int $email 邮箱
  86. * @param int $code 验证码
  87. * @param string $event 事件
  88. * @return boolean
  89. */
  90. public static function check($email, $code, $event = 'default')
  91. {
  92. $time = time() - self::$expire;
  93. $ems = EmsModel::where(['email' => $email, 'event' => $event])
  94. ->order('id', 'DESC')
  95. ->find();
  96. if ($ems) {
  97. if ($ems['create_time'] > $time && $ems['times'] <= self::$maxCheckNums) {
  98. $correct = $code == $ems['code'];
  99. if (!$correct) {
  100. $ems->times = $ems->times + 1;
  101. $ems->save();
  102. return false;
  103. } else {
  104. $result = hook('ems_check', $ems, true, true);
  105. return $result;
  106. }
  107. } else {
  108. // 过期则清空该邮箱验证码
  109. self::flush($email, $event);
  110. return false;
  111. }
  112. } else {
  113. return false;
  114. }
  115. }
  116. /**
  117. * 清空指定邮箱验证码
  118. *
  119. * @param int $email 邮箱
  120. * @param string $event 事件
  121. * @return boolean
  122. */
  123. public static function flush($email, $event = 'default')
  124. {
  125. EmsModel::where(['email' => $email, 'event' => $event])->delete();
  126. hook('ems_flush');
  127. return true;
  128. }
  129. }