123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace app\common\library;
- use think\Hook;
- class Ems
- {
-
- protected static $expire = 120;
-
- protected static $maxCheckNums = 10;
-
- public static function get($email, $event = 'default')
- {
- $ems = \app\common\model\Ems::
- where(['email' => $email, 'event' => $event])
- ->order('id', 'DESC')
- ->find();
- Hook::listen('ems_get', $ems, null, true);
- return $ems ? $ems : NULL;
- }
-
- public static function send($email, $code = NULL, $event = 'default')
- {
- $code = is_null($code) ? mt_rand(1000, 9999) : $code;
- $time = time();
- $ip = request()->ip();
- $ems = \app\common\model\Ems::create(['event' => $event, 'email' => $email, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
- $result = Hook::listen('ems_send', $ems, null, true);
- if (!$result)
- {
- $ems->delete();
- return FALSE;
- }
- return TRUE;
- }
-
- public static function notice($email, $msg = '', $template = NULL)
- {
- $params = [
- 'email' => $email,
- 'msg' => $msg,
- 'template' => $template
- ];
- $result = Hook::listen('ems_notice', $params, null, true);
- return $result ? TRUE : FALSE;
- }
-
- public static function check($email, $code, $event = 'default')
- {
- $time = time() - self::$expire;
- $ems = \app\common\model\Ems::where(['email' => $email, 'event' => $event])
- ->order('id', 'DESC')
- ->find();
- if ($ems)
- {
- if (strtotime($ems['createtime']) > $time && $ems['times'] <= self::$maxCheckNums)
- {
- $correct = $code == $ems['code'];
- if (!$correct)
- {
- $ems->times = $ems->times + 1;
- $ems->save();
- return FALSE;
- }
- else
- {
- $result = Hook::listen('ems_check', $ems, null, true);
- return TRUE;
- }
- }
- else
- {
-
- self::flush($email, $event);
- return FALSE;
- }
- }
- else
- {
- return FALSE;
- }
- }
-
- public static function flush($email, $event = 'default')
- {
- \app\common\model\Ems::
- where(['email' => $email, 'event' => $event])
- ->delete();
- Hook::listen('ems_flush');
- return TRUE;
- }
- }
|