Browse Source

优化邮件发送插件检测

Karson 3 years ago
parent
commit
8b55020b3a
2 changed files with 39 additions and 22 deletions
  1. 1 9
      application/api/controller/Ems.php
  2. 38 13
      application/common/library/Ems.php

+ 1 - 9
application/api/controller/Ems.php

@@ -5,6 +5,7 @@ namespace app\api\controller;
 use app\common\controller\Api;
 use app\common\controller\Api;
 use app\common\library\Ems as Emslib;
 use app\common\library\Ems as Emslib;
 use app\common\model\User;
 use app\common\model\User;
+use think\Hook;
 
 
 /**
 /**
  * 邮箱验证码接口
  * 邮箱验证码接口
@@ -17,15 +18,6 @@ class Ems extends Api
     public function _initialize()
     public function _initialize()
     {
     {
         parent::_initialize();
         parent::_initialize();
-        \think\Hook::add('ems_send', function ($params) {
-            $obj = \app\common\library\Email::instance();
-            $result = $obj
-                ->to($params->email)
-                ->subject('验证码')
-                ->message("你的验证码是:" . $params->code)
-                ->send();
-            return $result;
-        });
     }
     }
 
 
     /**
     /**

+ 38 - 13
application/common/library/Ems.php

@@ -26,8 +26,8 @@ class Ems
     /**
     /**
      * 获取最后一次邮箱发送的数据
      * 获取最后一次邮箱发送的数据
      *
      *
-     * @param   int    $email 邮箱
-     * @param   string $event 事件
+     * @param int    $email 邮箱
+     * @param string $event 事件
      * @return  Ems
      * @return  Ems
      */
      */
     public static function get($email, $event = 'default')
     public static function get($email, $event = 'default')
@@ -43,9 +43,9 @@ class Ems
     /**
     /**
      * 发送验证码
      * 发送验证码
      *
      *
-     * @param   int    $email 邮箱
-     * @param   int    $code  验证码,为空时将自动生成4位数字
-     * @param   string $event 事件
+     * @param int    $email 邮箱
+     * @param int    $code  验证码,为空时将自动生成4位数字
+     * @param string $event 事件
      * @return  boolean
      * @return  boolean
      */
      */
     public static function send($email, $code = null, $event = 'default')
     public static function send($email, $code = null, $event = 'default')
@@ -54,6 +54,18 @@ class Ems
         $time = time();
         $time = time();
         $ip = request()->ip();
         $ip = request()->ip();
         $ems = \app\common\model\Ems::create(['event' => $event, 'email' => $email, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
         $ems = \app\common\model\Ems::create(['event' => $event, 'email' => $email, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
+        if (!Hook::get('ems_send')) {
+            //采用框架默认的邮件推送
+            Hook::add('ems_send', function ($params) {
+                $obj = new Email();
+                $result = $obj
+                    ->to($params->email)
+                    ->subject('请查收你的验证码!')
+                    ->message("你的验证码是:" . $params->code . "," . ceil(self::$expire / 60) . "分钟内有效。")
+                    ->send();
+                return $result;
+            });
+        }
         $result = Hook::listen('ems_send', $ems, null, true);
         $result = Hook::listen('ems_send', $ems, null, true);
         if (!$result) {
         if (!$result) {
             $ems->delete();
             $ems->delete();
@@ -65,9 +77,9 @@ class Ems
     /**
     /**
      * 发送通知
      * 发送通知
      *
      *
-     * @param   mixed  $email    邮箱,多个以,分隔
-     * @param   string $msg      消息内容
-     * @param   string $template 消息模板
+     * @param mixed  $email    邮箱,多个以,分隔
+     * @param string $msg      消息内容
+     * @param string $template 消息模板
      * @return  boolean
      * @return  boolean
      */
      */
     public static function notice($email, $msg = '', $template = null)
     public static function notice($email, $msg = '', $template = null)
@@ -77,6 +89,19 @@ class Ems
             'msg'      => $msg,
             'msg'      => $msg,
             'template' => $template
             'template' => $template
         ];
         ];
+        if (!Hook::get('ems_notice')) {
+            //采用框架默认的邮件推送
+            Hook::add('ems_notice', function ($params) {
+                $subject = '你收到一封新的邮件!';
+                $content = $params['msg'];
+                $email = new Email();
+                $result = $email->to($params['email'])
+                    ->subject($subject)
+                    ->message($content)
+                    ->send();
+                return $result;
+            });
+        }
         $result = Hook::listen('ems_notice', $params, null, true);
         $result = Hook::listen('ems_notice', $params, null, true);
         return $result ? true : false;
         return $result ? true : false;
     }
     }
@@ -84,9 +109,9 @@ class Ems
     /**
     /**
      * 校验验证码
      * 校验验证码
      *
      *
-     * @param   int    $email 邮箱
-     * @param   int    $code  验证码
-     * @param   string $event 事件
+     * @param int    $email 邮箱
+     * @param int    $code  验证码
+     * @param string $event 事件
      * @return  boolean
      * @return  boolean
      */
      */
     public static function check($email, $code, $event = 'default')
     public static function check($email, $code, $event = 'default')
@@ -119,8 +144,8 @@ class Ems
     /**
     /**
      * 清空指定邮箱验证码
      * 清空指定邮箱验证码
      *
      *
-     * @param   int    $email 邮箱
-     * @param   string $event 事件
+     * @param int    $email 邮箱
+     * @param string $event 事件
      * @return  boolean
      * @return  boolean
      */
      */
     public static function flush($email, $event = 'default')
     public static function flush($email, $event = 'default')