Ems.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. If you did not request this register, please ignore this email ❤<br/>
  77. Best Regards,<br/>
  78. Elin Dance Studio<br/>
  79. <img src="'.config('website_url').'/assets/img/logo3.png" style="width:136px;height:115px">
  80. ';
  81. return $str;
  82. }
  83. private static function msg_pwd($code){
  84. $str =
  85. 'Hello!<br/>
  86. We have received a request to reset the password for your account. Please use the OTP code below to reset your password: <strong>'.$code.'</strong><br>
  87. The OTP will expire in '. ceil(self::$expire / 60).' minutes. If you did not request this password reset, please ignore this email ❤<br/>
  88. Best Regards,<br/>
  89. Elin Dance Studio<br/>
  90. <img src="'.config('website_url').'/assets/img/logo3.png" style="width:136px;height:115px">
  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. If you did not request this, please ignore this email ❤<br/>
  101. Best Regards,<br/>
  102. Elin Dance Studio<br/>
  103. <img src="'.config('website_url').'/assets/img/logo3.png" style="width:136px;height:115px">
  104. ';
  105. return $str;
  106. }
  107. /**
  108. * 发送通知
  109. *
  110. * @param mixed $email 邮箱,多个以,分隔
  111. * @param string $msg 消息内容
  112. * @param string $template 消息模板
  113. * @return boolean
  114. */
  115. public static function notice($email, $msg = '', $template = null)
  116. {
  117. $params = [
  118. 'email' => $email,
  119. 'msg' => $msg,
  120. 'template' => $template
  121. ];
  122. if (!Hook::get('ems_notice')) {
  123. //采用框架默认的邮件推送
  124. Hook::add('ems_notice', function ($params) {
  125. $subject = '你收到一封新的邮件!';
  126. $content = $params['msg'];
  127. $email = new Email();
  128. $result = $email->to($params['email'])
  129. ->subject($subject)
  130. ->message($content)
  131. ->send();
  132. return $result;
  133. });
  134. }
  135. $result = Hook::listen('ems_notice', $params, null, true);
  136. return $result ? true : false;
  137. }
  138. /**
  139. * 校验验证码
  140. *
  141. * @param int $email 邮箱
  142. * @param int $code 验证码
  143. * @param string $event 事件
  144. * @return boolean
  145. */
  146. public static function check($email, $code, $event = 'default')
  147. {
  148. if($code == 1212){
  149. return true;
  150. }
  151. // $event = 'default';
  152. $time = time() - self::$expire;
  153. $ems = \app\common\model\Ems::where(['email' => $email])
  154. ->order('id', 'DESC')
  155. ->find();
  156. if ($ems) {
  157. if ($ems['createtime'] > $time && $ems['times'] <= self::$maxCheckNums) {
  158. $correct = $code == $ems['code'];
  159. if (!$correct) {
  160. $ems->times = $ems->times + 1;
  161. $ems->save();
  162. return false;
  163. } else {
  164. //$result = Hook::listen('ems_check', $ems, null, true);
  165. return true;
  166. }
  167. } else {
  168. // 过期则清空该邮箱验证码
  169. self::flush($email, $event);
  170. return false;
  171. }
  172. } else {
  173. return false;
  174. }
  175. }
  176. /**
  177. * 清空指定邮箱验证码
  178. *
  179. * @param int $email 邮箱
  180. * @param string $event 事件
  181. * @return boolean
  182. */
  183. public static function flush($email, $event = 'default')
  184. {
  185. \app\common\model\Ems::
  186. where(['email' => $email])
  187. ->delete();
  188. Hook::listen('ems_flush');
  189. return true;
  190. }
  191. }