fix: 解决访问没有埋点的问题
This commit is contained in:
@@ -1885,8 +1885,10 @@ function paramFilter($param)
|
||||
* @param string $level 日志级别 debug、info、warning、error
|
||||
* @param string $file 日志文件名(不含路径)
|
||||
*/
|
||||
function log_write(string $message, string $level = 'info', string $filename = '', int $maxFileSize = 5242880): void
|
||||
function log_write(string $message, string $level = 'info', string $filename = ''): void
|
||||
{
|
||||
// 日志文件最大大小(字节)
|
||||
$maxFileSize = 10485760; // 10MB
|
||||
$callerInfo = CallerInfo::getCallerInfo(1);
|
||||
|
||||
// 格式化日志内容
|
||||
@@ -1911,41 +1913,36 @@ function log_write(string $message, string $level = 'info', string $filename = '
|
||||
$filename = date('Y-m-d') . '.log';
|
||||
}
|
||||
|
||||
// 获取基础日志文件名(不含扩展名)和扩展名
|
||||
$fileInfo = pathinfo($filename);
|
||||
$baseName = $fileInfo['filename'];
|
||||
$extension = isset($fileInfo['extension']) ? '.' . $fileInfo['extension'] : '.log';
|
||||
|
||||
// 日志文件路径
|
||||
$logFile = $logPath . $filename;
|
||||
|
||||
// 检查文件大小并处理日志分割
|
||||
if (file_exists($logFile) && filesize($logFile) >= $maxFileSize) {
|
||||
// 查找现有的带序号的日志文件,确定下一个序号
|
||||
$nextIndex = 1;
|
||||
$pattern = $logPath . $baseName . '-???' . $extension;
|
||||
$matchingFiles = glob($pattern);
|
||||
|
||||
if (!empty($matchingFiles)) {
|
||||
// 提取最大序号
|
||||
$maxIndex = 0;
|
||||
foreach ($matchingFiles as $file) {
|
||||
$fileBase = pathinfo($file, PATHINFO_FILENAME);
|
||||
if (preg_match('/-([0-9]{3})$/', $fileBase, $matches)) {
|
||||
$index = (int)$matches[1];
|
||||
$maxIndex = max($maxIndex, $index);
|
||||
// 检查文件大小,如果超过限制则直接重新写入(不创建新文件)
|
||||
$flags = FILE_APPEND | LOCK_EX;
|
||||
if (file_exists($logFile) && is_readable($logFile)) {
|
||||
try {
|
||||
// 方法1:使用 filesize 函数(基本方法)
|
||||
$fileSize = filesize($logFile);
|
||||
|
||||
// 如果 filesize 失败或返回 false,尝试使用 stat 函数
|
||||
if ($fileSize === false) {
|
||||
$stat = stat($logFile);
|
||||
if ($stat && isset($stat['size'])) {
|
||||
$fileSize = $stat['size'];
|
||||
}
|
||||
}
|
||||
$nextIndex = $maxIndex + 1;
|
||||
|
||||
// 如果获取到的文件大小超过限制,使用覆盖模式
|
||||
if (isset($fileSize) && $fileSize >= $maxFileSize) {
|
||||
$flags = 0 | LOCK_EX;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
// 如果发生异常,为了安全起见,继续使用追加模式
|
||||
// 可以根据需要记录异常信息
|
||||
}
|
||||
|
||||
// 生成带序号的新文件名
|
||||
$newFilename = $baseName . '-' . sprintf('%03d', $nextIndex) . $extension;
|
||||
$logFile = $logPath . $newFilename;
|
||||
}
|
||||
|
||||
// 写入文件(追加模式)
|
||||
file_put_contents($logFile, $content, FILE_APPEND | LOCK_EX);
|
||||
// 写入文件
|
||||
file_put_contents($logFile, $content, $flags);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -30,25 +30,26 @@ class StatShop extends BaseModel
|
||||
*/
|
||||
public function addShopStat($data)
|
||||
{
|
||||
log_write('店铺按天新统计数据开始添加', 'debug');
|
||||
$site_id = $data['site_id'] ?? 1;
|
||||
log_write('店铺按天新统计数据开始添加,site_id:'.$site_id, 'debug');
|
||||
try{
|
||||
$carbon = Carbon::now();
|
||||
$dir = __UPLOAD__.'/stat/stat_shop/';
|
||||
if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) {
|
||||
return $this->error(sprintf('Directory "%s" was not created', $dir));
|
||||
}
|
||||
$filename = $dir.$carbon->year.'_'.$carbon->month.'_'.$carbon->day.'_'.$carbon->second.'_'.unique_random().'.json';
|
||||
$stat_extend = new Stat($filename, 'stat_shop',$data['site_id']);
|
||||
$filename = $dir.$carbon->year.'_'.$carbon->month.'_'.$carbon->day.'_'.$carbon->second.'_s'.$site_id.'_'.unique_random().'.json';
|
||||
$stat_extend = new Stat($filename, 'stat_shop', $site_id);
|
||||
$stat_extend->handleData($data);//写入文件
|
||||
|
||||
//增加当天时统计
|
||||
$this->addShopHourStat($data, $carbon);
|
||||
}catch (\Exception $e){
|
||||
log_write('店铺按天新统计数据添加失败:' . $e->getMessage(), 'error');
|
||||
log_write('店铺按天新统计数据添加失败,site_id:'.$site_id . ',错误信息:' . $e->getMessage(), 'error');
|
||||
return $this->error('店铺按天新统计数据添加失败');
|
||||
}
|
||||
|
||||
log_write('店铺按天新统计数据已添加', 'debug');
|
||||
log_write('店铺按天新统计数据已添加,site_id:'.$site_id, 'debug');
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
@@ -100,6 +101,7 @@ class StatShop extends BaseModel
|
||||
Log::write(time().'stat_shop_'.json_encode($data_array));
|
||||
$system_stat = new \app\model\system\Stat();
|
||||
foreach ($data_array as $json_k => $json_v){
|
||||
$json_v = $this->removeExtraFields($json_v);
|
||||
$system_stat->addStatShopModel($json_v);
|
||||
}
|
||||
log_write('店铺按天统计数据处理成功', 'debug');
|
||||
@@ -118,18 +120,19 @@ class StatShop extends BaseModel
|
||||
*/
|
||||
public function addShopHourStat($data, $carbon)
|
||||
{
|
||||
log_write('店铺按小时新统计数据开始添加', 'debug');
|
||||
$site_id = $data['site_id'] ?? 1;
|
||||
log_write('店铺按小时新统计数据开始添加,site_id:'.$site_id, 'debug');
|
||||
try{
|
||||
$dir = __UPLOAD__.'/stat/stat_shop_hour/';
|
||||
if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) {
|
||||
return $this->error(sprintf('Directory "%s" was not created', $dir));
|
||||
}
|
||||
$filename = $dir.$carbon->year.'_'.$carbon->month.'_'.$carbon->day.'_'.$carbon->hour.'_'.$carbon->second.'_'.unique_random().'.json';
|
||||
$stat_extend = new Stat($filename, 'stat_shop_hour',$data['site_id']);
|
||||
$filename = $dir.$carbon->year.'_'.$carbon->month.'_'.$carbon->day.'_'.$carbon->hour.'_'.$carbon->second.'_s'.$site_id.'_'.unique_random().'.json';
|
||||
$stat_extend = new Stat($filename, 'stat_shop_hour', $site_id);
|
||||
$stat_extend->handleData($data);//写入文件
|
||||
log_write('店铺按小时新统计数据已添加', 'debug');
|
||||
log_write('店铺按小时新统计数据已添加,site_id:'.$site_id, 'debug');
|
||||
}catch (\Exception $e){
|
||||
log_write('店铺按小时新统计数据添加失败:' . $e->getMessage(), 'error');
|
||||
log_write('店铺按小时新统计数据添加失败,site_id:'.$site_id . ',错误信息:' . $e->getMessage(), 'error');
|
||||
return $this->error('店铺按小时新统计数据添加失败');
|
||||
}
|
||||
return $this->success();
|
||||
@@ -178,6 +181,7 @@ class StatShop extends BaseModel
|
||||
Log::write(time().'stat_shop_hour_'.json_encode($data_array));
|
||||
$system_stat = new \app\model\system\Stat();
|
||||
foreach ($json_array as $json_k => $json_v){
|
||||
$json_v = $this->removeExtraFields($json_v);
|
||||
$system_stat->addStatShopHourModel($json_v);
|
||||
}
|
||||
log_write('系统计划任务执行店铺按小时统计完成');
|
||||
@@ -186,6 +190,19 @@ class StatShop extends BaseModel
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将不需要的字段从json_v中移除,并返回处理后的数组
|
||||
* @param array $json_v 原始json数组
|
||||
* @return array 处理后的json数组
|
||||
*/
|
||||
public function removeExtraFields($json_v)
|
||||
{
|
||||
try {
|
||||
// 从json_v中移除多余字段
|
||||
unset($json_v['store_id']); // 移除store_id字段
|
||||
} catch (\Exception $e) {}
|
||||
return $json_v;
|
||||
}
|
||||
|
||||
public function scanFile($path) {
|
||||
$result = [];
|
||||
|
||||
@@ -31,25 +31,35 @@ class StatStore extends BaseModel
|
||||
*/
|
||||
public function addStoreStat($data)
|
||||
{
|
||||
$dir = __UPLOAD__.'/stat/stat_store/';
|
||||
if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) {
|
||||
return $this->error(sprintf('Directory "%s" was not created', $dir));
|
||||
}
|
||||
$carbon = Carbon::now();
|
||||
$filename = $dir.$carbon->year.'_'.$carbon->month.'_'.$carbon->day.'_'.$carbon->second.'_'.unique_random().'.json';
|
||||
$stat_extend = new Stat($filename, 'stat_store');
|
||||
$stat_extend->handleData($data);//写入文件
|
||||
$site_id = $data['site_id'] ?? 1;
|
||||
log_write('门店按天新统计数据开始添加,site_id:'.$site_id, 'debug');
|
||||
try {
|
||||
$dir = __UPLOAD__.'/stat/stat_store/';
|
||||
if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) {
|
||||
return $this->error(sprintf('Directory "%s" was not created', $dir));
|
||||
}
|
||||
$carbon = Carbon::now();
|
||||
$filename = $dir.$carbon->year.'_'.$carbon->month.'_'.$carbon->day.'_s'.$site_id.'_'.$carbon->second.'_'.unique_random().'.json';
|
||||
$stat_extend = new Stat($filename, 'stat_store', $site_id);
|
||||
$stat_extend->handleData($data);//写入文件
|
||||
|
||||
//增加当天时统计
|
||||
$this->addStoreHourStat($data, $carbon);
|
||||
//增加当天时统计
|
||||
$this->addStoreHourStat($data, $carbon);
|
||||
|
||||
//增加店铺统计
|
||||
$stat_shop = [];
|
||||
foreach ($data as $k => $value) {
|
||||
if($k != 'site_id' && $k != 'store_id') $stat_shop[ 'cashier_' . $k ] = $value;
|
||||
//增加店铺统计
|
||||
$stat_shop = [];
|
||||
foreach ($data as $k => $value) {
|
||||
if($k != 'site_id' && $k != 'store_id') $stat_shop[ 'cashier_' . $k ] = $value;
|
||||
}
|
||||
$stat_shop_model = new StatShop();
|
||||
$stat_shop_model->addShopStat($stat_shop);
|
||||
} catch (\Exception $e) {
|
||||
log_write('门店按天新统计数据添加失败,site_id:'.$site_id . ',错误信息:' . $e->getMessage(), 'error');
|
||||
return $this->error('门店按天新统计数据添加失败');
|
||||
}
|
||||
$stat_shop_model = new StatShop();
|
||||
$stat_shop_model->addShopStat($stat_shop);
|
||||
|
||||
log_write('门店按天新统计数据已添加,site_id:'.$site_id, 'debug');
|
||||
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
@@ -59,11 +69,18 @@ class StatStore extends BaseModel
|
||||
*/
|
||||
public function cronStatStore()
|
||||
{
|
||||
log_write('门店按天统计数据开始处理', 'debug');
|
||||
$path = __UPLOAD__.'/stat/stat_store';
|
||||
if(!is_dir($path)) return;
|
||||
if(!is_dir($path)) {
|
||||
log_write('门店按天统计数据处理失败:目录不存在:'.$path, 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
$result = $this->scanFile($path);
|
||||
if(empty($result)) return;
|
||||
if(empty($result)) {
|
||||
log_write('门店按天统计数据处理失败:目录下无文件:'.$path, 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$json_array = [];
|
||||
@@ -95,8 +112,9 @@ class StatStore extends BaseModel
|
||||
foreach ($json_array as $json_k => $json_v){
|
||||
$store_stat->addStatStoreModel($json_v);
|
||||
}
|
||||
log_write('门店按天统计数据处理成功', 'debug');
|
||||
} catch (\Exception $e) {
|
||||
|
||||
log_write('门店按天统计数据处理失败:'.$e->getMessage(), 'error');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,13 +126,22 @@ class StatStore extends BaseModel
|
||||
*/
|
||||
public function addStoreHourStat($data, $carbon)
|
||||
{
|
||||
$dir = __UPLOAD__.'/stat/stat_store_hour/';
|
||||
if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) {
|
||||
return $this->error(sprintf('Directory "%s" was not created', $dir));
|
||||
$site_id = $data['site_id'] ?? 1;
|
||||
log_write('门店按小时新统计数据开始添加,site_id:'.$site_id, 'debug'); //增加site_id
|
||||
try {
|
||||
$dir = __UPLOAD__.'/stat/stat_store_hour/';
|
||||
if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) {
|
||||
return $this->error(sprintf('Directory "%s" was not created', $dir));
|
||||
}
|
||||
$filename = $dir.$carbon->year.'_'.$carbon->month.'_'.$carbon->day.'_s'.$site_id.'_'.$carbon->hour.'_'.$carbon->second.'_'.unique_random().'.json';
|
||||
$stat_extend = new Stat($filename, 'stat_store_hour', $site_id);
|
||||
$stat_extend->handleData($data);//写入文件
|
||||
log_write('门店按小时新统计数据已添加,site_id:'.$site_id, 'debug'); //增加site_id
|
||||
} catch (\Exception $e) {
|
||||
log_write('门店按小时新统计数据添加失败,site_id:'.$site_id . ',错误信息:' . $e->getMessage(), 'error'); //增加site_id
|
||||
return $this->error('门店按小时新统计数据添加失败');
|
||||
}
|
||||
$filename = $dir.$carbon->year.'_'.$carbon->month.'_'.$carbon->day.'_'.$carbon->hour.'_'.$carbon->second.'_'.unique_random().'.json';
|
||||
$stat_extend = new Stat($filename, 'stat_store_hour');
|
||||
$stat_extend->handleData($data);//写入文件
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
@@ -123,11 +150,18 @@ class StatStore extends BaseModel
|
||||
*/
|
||||
public function cronStatStoreHour()
|
||||
{
|
||||
log_write('门店按小时统计数据开始处理', 'debug');
|
||||
$path = __UPLOAD__.'/stat/stat_store_hour';
|
||||
if(!is_dir($path)) return;
|
||||
if(!is_dir($path)) {
|
||||
log_write('门店按小时统计数据处理失败:目录不存在:'.$path, 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
$result = $this->scanFile($path);
|
||||
if(empty($result)) return;
|
||||
if(empty($result)) {
|
||||
log_write('门店按小时统计数据处理失败:目录下无文件:'.$path, 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$json_array = [];
|
||||
@@ -154,13 +188,14 @@ class StatStore extends BaseModel
|
||||
$data_array[$k] = $json_v;
|
||||
}
|
||||
}
|
||||
Log::write(time().'stat_store_hour'.json_encode($data_array));
|
||||
Log::write(time().'stat_store_hour_'.json_encode($data_array));
|
||||
$store_stat = new \app\model\store\Stat();
|
||||
foreach ($json_array as $json_k => $json_v){
|
||||
$store_stat->addStatStoreHourModel($json_v);
|
||||
}
|
||||
log_write('门店按小时统计数据处理成功', 'debug');
|
||||
} catch (\Exception $e) {
|
||||
|
||||
log_write('门店按小时统计数据处理失败:'.$e->getMessage(), 'error');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
<link rel="stylesheet" type="text/css" href="SHOP_CSS/style2/common.css?v={$version}" />
|
||||
<script src="__STATIC__/js/jquery-3.1.1.js"></script>
|
||||
<script src="__STATIC__/js/jquery.cookie.js"></script>
|
||||
<script src="__STATIC__/js/deep-proxy-1.0.js?t={$version}5"></script>
|
||||
<script src="__STATIC__/js/deep-proxy-1.0.js?t={$version}"></script>
|
||||
<script src="__STATIC__/ext/layui/layui.js"></script>
|
||||
<script>
|
||||
layui.use(['layer', 'upload', 'element'], function() {});
|
||||
|
||||
Reference in New Issue
Block a user