panda 1 年之前
父节点
当前提交
ea36cd220e
共有 2 个文件被更改,包括 183 次插入46 次删除
  1. 14 46
      application/api/controller/Passport.php
  2. 169 0
      application/utils/LogUtil.php

+ 14 - 46
application/api/controller/Passport.php

@@ -4,14 +4,18 @@ namespace app\api\controller;
 
 
 use app\common\controller\Api;
 use app\common\controller\Api;
 use app\utils\Easywechat\MiniAppService;
 use app\utils\Easywechat\MiniAppService;
+use app\utils\LogUtil;
 use app\utils\PayUtil;
 use app\utils\PayUtil;
 use think\Cache;
 use think\Cache;
+use think\Request;
 
 
 /**
 /**
  * 通行证
  * 通行证
  */
  */
 class Passport extends Api
 class Passport extends Api
 {
 {
+    // 日志模块名称
+    const LOG_MODULE = 'Passport';
     protected $noNeedLogin = ['loginWxMini', 'loginWxMiniPhone', 'testPay', 'pay_notify'];
     protected $noNeedLogin = ['loginWxMini', 'loginWxMiniPhone', 'testPay', 'pay_notify'];
     protected $noNeedRight = '*';
     protected $noNeedRight = '*';
     protected $model       = null;
     protected $model       = null;
@@ -21,6 +25,14 @@ class Passport extends Api
         parent::_initialize();
         parent::_initialize();
     }
     }
 
 
+    public function __construct(Request $request = null)
+    {
+        parent::__construct($request);
+        //日志统一写入
+        register_shutdown_function([new LogUtil, 'close']);
+        LogUtil::getInstance('Api/'); //设置日志存入通道
+    }
+
     /**
     /**
      * 微信小程序手机号授权
      * 微信小程序手机号授权
      * @return void
      * @return void
@@ -138,51 +150,7 @@ class Passport extends Api
      */
      */
     public function pay_notify()
     public function pay_notify()
     {
     {
-//        $params = \request()->post();
-//
-//        $params = json_encode($params,JSON_UNESCAPED_UNICODE);
-
-        $this->notify_log_start('hf_pay');
-    }
-
-    //异步日志
-    private function notify_log_start($paytype = 'wechat')
-    {
-        //记录支付回调数据
-        ignore_user_abort(); // run script in background
-        set_time_limit(30);
-        // 日志文件 start
-        $log_base_dir = '../paylog/' . $paytype . '/';
-        if (!is_dir($log_base_dir)) {
-            mkdir($log_base_dir, 0770, true);
-            @chmod($log_base_dir, 0770);
-        }
-        $notify_file = $log_base_dir . 'notify.txt';
-        if (!file_exists($notify_file)) {
-            @touch($notify_file);
-            @chmod($notify_file, 0770);
-        }
-        if (filesize($notify_file) > 5242880)//大于5M自动切换
-        {
-            rename($notify_file, $log_base_dir . 'notify_' . date('Y_m_d_H_i_s') . '.txt');
-        }
-        if (!file_exists($notify_file)) {
-            @touch($notify_file);
-            @chmod($notify_file, 0770);
-        }
-        // 日志文件 end
-        //开始写入
-        $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
-        if ($_REQUEST && $paytype == 'alipay') {
-            file_put_contents($notify_file, "\r\n\r\n" . date('Y-m-d H:i:s') . " [notify][入口接收request]" . json_encode($_REQUEST), FILE_APPEND);
-        } else {
-            $xml = file_get_contents("php://input");
-            file_put_contents($notify_file, "\r\n\r\n" . date('Y-m-d H:i:s') . " [notify][入口接收php://input流原始数据] \n" . $xml, FILE_APPEND);
-            $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
-            file_put_contents($notify_file, "\r\n\r\n" . date('Y-m-d H:i:s') . " [notify][入口接收php://input流] " . json_encode($xmlObj), FILE_APPEND);
-        }
-
-        ini_set('display_errors', 'On');
-        return $notify_file;
+        $params = $this->request->param();
+        LogUtil::info('支付回调请求参数', self::LOG_MODULE, __FUNCTION__,$params);
     }
     }
 }
 }

+ 169 - 0
application/utils/LogUtil.php

@@ -0,0 +1,169 @@
+<?php
+namespace app\utils;
+
+
+/**
+ * Author:Panda
+ * Email:joeyoung0314@qq.com
+ * Class LogUtil
+ * @package App\Utils
+ */
+class LogUtil {
+    /**==============日志系统==============**/
+
+    static $logArr = array();
+    static $logRootPath = '';//日志根目录
+    static $logExtPath = '';//日志根目录02
+    static $logExtTypePath = 'api/';//默认通道Api
+
+    public static function getInstance($logExtTypePath){
+        self::$logExtTypePath = $logExtTypePath;
+    }
+
+    /**
+     * 记录各类日志
+     * 日志级别:普通
+     * @param string $controller 控制器
+     * @param string $action 方法
+     * @param ...$content //日志参数
+     */
+    public static function insert(string $controller,string $action, ...$content){
+        self::writeLog($content,'Api-Middleware-Log', 'request_log',"{$controller}/{$action}",'local.INFO');
+    }
+
+    /**
+     * debug 日志
+     * @param ...$content
+     */
+    public static function debug(...$content){
+        self::writeLog($content,'Api-Middleware-Log', 'request_log',"debug",'local.WARNING');
+    }
+
+    /**
+     * 日志级别:普通
+     * @param string $logTitle 日志名称
+     * @param string $logName 日志目录
+     * @param string $logFile 日志文件名
+     * @param array $content 日志参数
+     */
+    public static function info($logTitle = '', $logName = 'Common', $logFile = 'common',$content = [] ){
+        self::writeLog($content,$logName,$logFile,$logTitle,'local.INFO');
+    }
+
+    /**
+     * 日志级别:警告
+     * @param string $logTitle 日志名称
+     * @param string $logName 日志目录
+     * @param string $logFile 日志文件名
+     * @param array $content 日志参数
+     */
+    public static function warning($logTitle = '', $logName = 'Common', $logFile = 'common',$content = [] ){
+        self::writeLog($content,$logName,$logFile,$logTitle,'local.WARNING');
+    }
+
+    /**
+     * 日志级别:错误
+     * @param string $logTitle 日志名称
+     * @param string $logName 日志目录
+     * @param string $logFile 日志文件名
+     * @param array $content 日志参数
+     */
+    public static function error($logTitle = '', $logName = 'Common', $logFile = 'common',$content = [] ){
+        self::writeLog($content,$logName,$logFile,$logTitle,'local.ERROR');
+    }
+
+    /**
+     * 日志内容保存
+     * @param $content
+     * @param string $logName 日志模块名称
+     * @param string $logFile 日志内容
+     * @param string $logTitle 日志内容头部
+     * @param string $status
+     *
+     */
+    private static function writeLog($content, $logName = 'Common', $logFile = 'common', $logTitle = '',$status = 'INFO') {
+        $logPath = $logName.'_'.$logFile;
+        $logWhite = self::whiteLog();
+        if(in_array($logPath,$logWhite)) return;//验证日志黑名单
+        //获取日志文件目录
+        if (empty(self::$logRootPath)) self::$logRootPath = realpath(ROOT_PATH) . '/runtime/Log/'.self::$logExtTypePath;
+
+        if (empty(self::$logExtPath))  self::$logExtPath  =  realpath(ROOT_PATH) . '/runtime/Log02/'.self::$logExtTypePath;
+
+        if (empty(self::$logArr[$logName][$logFile])) {
+            self::$logArr[$logName][$logFile] = '================ Start ================'.PHP_EOL;
+        }
+
+        if (is_array($content)) $content = json_encode($content, JSON_UNESCAPED_UNICODE);
+        if (!empty($logTitle)) $logTitle .= ':';
+        self::$logArr[$logName][$logFile] .= '['.(date('Y-m-d H:i:s')."] {$status}: {$logTitle}{$content}".PHP_EOL.PHP_EOL);
+    }
+
+    public static function close() {
+        try{
+
+            if (empty(self::$logArr)) return;
+
+            //没有Log目录 创建Log02目录
+            if(!file_exists(self::$logRootPath)){
+                $tem_path = self::$logExtPath;
+                if(!file_exists($tem_path)){
+                    mkdir($tem_path, 0777 ,true);
+                }
+                $logPath = $tem_path.date('Y_m_d');
+            }else{
+                $logPath = self::$logRootPath.date('Y_m_d');
+            }
+
+            //创建日志文件目录
+            if (!file_exists($logPath)) mkdir($logPath);
+
+            //逐个日志文件写入
+            foreach (self::$logArr as $key => $value) {
+                $logDir = $logPath.'/'.$key;
+                if (!file_exists($logDir)) mkdir($logDir , 0777 , true);
+                self::logEnd($value, $logDir);
+            }
+            self::$logArr = null;
+        }catch (\Exception $e){
+            return;
+        }
+    }
+    public static function logEnd($data, $logDir) {
+        foreach ($data as $k => $v) {
+            //获取日志文件名 避免单个日志文件太大
+            $count = 1;
+            while(true) {
+                //生成日志文件名
+                $logFile = "{$logDir}".'/'."{$k}_{$count}.log";
+
+                //第一次写入日志
+                if (!is_file($logFile)) break;
+
+                //日志文件未大于1M
+                $file = filesize($logFile) / 1024;
+                if ($file < 1024 * 100) break;
+
+                $count++;
+            }
+            $v = rtrim($v);
+            $v .= PHP_EOL.'================ End ================'.PHP_EOL.PHP_EOL;
+
+            error_log($v, 3, $logFile);
+//            $ch = fopen($logFile, 'ab');
+//            fwrite($ch, $v);
+//            fclose($ch);
+        }
+    }
+
+    /**
+     * 日志黑名单
+     * @return array
+     */
+    public static function whiteLog()
+    {
+        return array(
+//            'Framework/PassportController_login',
+        );
+    }
+}