Ems.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 = 300;
  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. $event = 'default';
  30. $ems = \app\common\model\Ems::
  31. where(['email' => $email, 'event' => $event])
  32. ->order('id', 'DESC')
  33. ->find();
  34. Hook::listen('ems_get', $ems, null, true);
  35. return $ems ? $ems : null;
  36. }
  37. /**
  38. * 发送验证码
  39. *
  40. * @param int $email 邮箱
  41. * @param int $code 验证码,为空时将自动生成4位数字
  42. * @param string $event 事件
  43. * @return boolean
  44. */
  45. public static function send($email, $code = null, $event = 'default')
  46. {
  47. $code = is_null($code) ? Random::numeric(config('captcha.length')) : $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. $message = self::msg_register($code);
  52. if($event == 'resetpwd'){
  53. $message = self::msg_pwd($code);
  54. }
  55. $obj = new Email();
  56. $result = $obj
  57. ->to($email)
  58. ->subject('Elin Dance Studio')
  59. // ->message("你的验证码是:" . $code . "," . ceil(self::$expire / 60) . "分钟内有效。")
  60. ->message($message)
  61. ->send();
  62. if (!$result) {
  63. $ems->delete();
  64. return false;
  65. }
  66. return true;
  67. }
  68. private static function msg_register($code){
  69. $str =
  70. 'Hello!<br/>
  71. We have received your request to create an account for our app.<br>
  72. Please use the OTP code below to verify your email address:<br>
  73. <strong>'.$code.'</strong><br>
  74. The OTP will expire in '. ceil(self::$expire / 60).' minutes.<br>
  75. Sincerely,<br>
  76. Elin Dance Studio Customer Support
  77. ';
  78. return $str;
  79. }
  80. private static function msg_pwd($code){
  81. $str =
  82. 'Hello!<br/>
  83. We have received your request to reset your password for our app.<br>
  84. Please use the OTP code below to verify your email address:<br>
  85. <strong>'.$code.'</strong><br>
  86. The OTP will expire in '. ceil(self::$expire / 60).' minutes.<br>
  87. Sincerely,<br>
  88. Elin Dance Studio Customer Support
  89. ';
  90. return $str;
  91. }
  92. /**
  93. * 发送通知
  94. *
  95. * @param mixed $email 邮箱,多个以,分隔
  96. * @param string $msg 消息内容
  97. * @param string $template 消息模板
  98. * @return boolean
  99. */
  100. public static function notice($email, $msg = '', $template = null)
  101. {
  102. $params = [
  103. 'email' => $email,
  104. 'msg' => $msg,
  105. 'template' => $template
  106. ];
  107. if (!Hook::get('ems_notice')) {
  108. //采用框架默认的邮件推送
  109. Hook::add('ems_notice', function ($params) {
  110. $subject = '你收到一封新的邮件!';
  111. $content = $params['msg'];
  112. $email = new Email();
  113. $result = $email->to($params['email'])
  114. ->subject($subject)
  115. ->message($content)
  116. ->send();
  117. return $result;
  118. });
  119. }
  120. $result = Hook::listen('ems_notice', $params, null, true);
  121. return $result ? true : false;
  122. }
  123. /**
  124. * 校验验证码
  125. *
  126. * @param int $email 邮箱
  127. * @param int $code 验证码
  128. * @param string $event 事件
  129. * @return boolean
  130. */
  131. public static function check($email, $code, $event = 'default')
  132. {
  133. if($code == 1212){
  134. return true;
  135. }
  136. $event = 'default';
  137. $time = time() - self::$expire;
  138. $ems = \app\common\model\Ems::where(['email' => $email, 'event' => $event])
  139. ->order('id', 'DESC')
  140. ->find();
  141. if ($ems) {
  142. if ($ems['createtime'] > $time && $ems['times'] <= self::$maxCheckNums) {
  143. $correct = $code == $ems['code'];
  144. if (!$correct) {
  145. $ems->times = $ems->times + 1;
  146. $ems->save();
  147. return false;
  148. } else {
  149. //$result = Hook::listen('ems_check', $ems, null, true);
  150. return true;
  151. }
  152. } else {
  153. // 过期则清空该邮箱验证码
  154. self::flush($email, $event);
  155. return false;
  156. }
  157. } else {
  158. return false;
  159. }
  160. }
  161. /**
  162. * 清空指定邮箱验证码
  163. *
  164. * @param int $email 邮箱
  165. * @param string $event 事件
  166. * @return boolean
  167. */
  168. public static function flush($email, $event = 'default')
  169. {
  170. $event = 'default';
  171. \app\common\model\Ems::
  172. where(['email' => $email])
  173. ->delete();
  174. Hook::listen('ems_flush');
  175. return true;
  176. }
  177. }