Ems.php 5.8 KB

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