65 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			65 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace addons\cardocr\model;
 | 
						|
 | 
						|
use app\common\library\Auth;
 | 
						|
use think\Cookie;
 | 
						|
use think\Model;
 | 
						|
 | 
						|
class CardocrLog extends Model
 | 
						|
{
 | 
						|
 | 
						|
 | 
						|
    // 开启自动写入时间戳字段
 | 
						|
 | 
						|
    // 定义时间戳字段名
 | 
						|
    protected $name = 'cardocr_log';
 | 
						|
    protected $autoWriteTimestamp = 'int';
 | 
						|
    //自定义身份证api日志标题
 | 
						|
    protected $createTime = 'createtime';
 | 
						|
    //自定义身份证api日志内容
 | 
						|
    protected $updateTime = '';
 | 
						|
 | 
						|
 | 
						|
    /*
 | 
						|
     * 身份证api日志记录
 | 
						|
     */
 | 
						|
    public static function record($title = '', $type='', $content ='')
 | 
						|
    {
 | 
						|
        $auth = Auth::instance();
 | 
						|
        $token = request()->server('HTTP_TOKEN', request()->request('token', Cookie::get('token')));
 | 
						|
        $auth->init($token);
 | 
						|
        $user_id = $auth->isLogin() ? $auth->id : 0;
 | 
						|
        $username = $auth->isLogin() ? $auth->username : __('Unknown');
 | 
						|
        if (!$content) {
 | 
						|
            $content = request()->param();
 | 
						|
            foreach ($content as $k => $v) {
 | 
						|
                if (is_string($v) && strlen($v) > 200 || stripos($k, 'password') !== false) {
 | 
						|
                    unset($content[$k]);
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        if (!$title) {
 | 
						|
            $title = [];
 | 
						|
            $breadcrumb = Auth::instance()->getBreadCrumb();
 | 
						|
            if ($breadcrumb) {
 | 
						|
                foreach ($breadcrumb as $k => $v) {
 | 
						|
                    $title[] = $v['title'];
 | 
						|
                }
 | 
						|
            }
 | 
						|
            $title = implode(' ', $title);
 | 
						|
        }
 | 
						|
        self::create([
 | 
						|
            'title' => $title,
 | 
						|
            'content' => !is_scalar($content) ? json_encode($content) : $content,
 | 
						|
            'url' => request()->url(),
 | 
						|
            'type' => intval($type),
 | 
						|
            'user_id' => $user_id,
 | 
						|
            'username' => $username,
 | 
						|
            'useragent' => request()->server('HTTP_USER_AGENT'),
 | 
						|
            'ip' => request()->ip()
 | 
						|
        ]);
 | 
						|
    }
 | 
						|
}
 |