实现后台及前台通过API访问UV埋点,所有代码全部保存
This commit is contained in:
@@ -30,34 +30,53 @@ class StatShop extends BaseModel
|
||||
*/
|
||||
public function addShopStat($data)
|
||||
{
|
||||
$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']);
|
||||
$stat_extend->handleData($data);//写入文件
|
||||
log_write('店铺按天新统计数据开始添加', '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']);
|
||||
$stat_extend->handleData($data);//写入文件
|
||||
|
||||
//增加当天时统计
|
||||
$this->addShopHourStat($data, $carbon);
|
||||
//增加当天时统计
|
||||
$this->addShopHourStat($data, $carbon);
|
||||
}catch (\Exception $e){
|
||||
log_write('店铺按天新统计数据添加失败:' . $e->getMessage(), 'error');
|
||||
return $this->error('店铺按天新统计数据添加失败');
|
||||
}
|
||||
|
||||
log_write('店铺按天新统计数据已添加', 'debug');
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从stat_shop目录下读取所有文件,将数据处理后,写入数据表中
|
||||
* 处理完每个文件后,删除文件
|
||||
*/
|
||||
public function cronShopStat()
|
||||
{
|
||||
log_write('店铺按天统计数据开始处理', 'debug');
|
||||
$path = __UPLOAD__.'/stat/stat_shop';
|
||||
if(!is_dir($path)) return;
|
||||
if(!is_dir($path)) {
|
||||
log_write('店铺按天统计数据处理失败:目录不存在', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
$result = $this->scanFile($path);
|
||||
if(empty($result)) return;
|
||||
if(empty($result)) {
|
||||
log_write('店铺按天统计数据处理失败:目录下无文件', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$json_array = [];
|
||||
foreach ($result as $key => $val){
|
||||
$stat_extend = new Stat($path.'/'.$val, 'stat_shop');
|
||||
$json_array[] = $stat_extend->load();
|
||||
unlink($path.'/'.$val);
|
||||
unlink($path.'/'.$val); // 处理完文件后,删除文件
|
||||
}
|
||||
|
||||
|
||||
@@ -83,8 +102,10 @@ class StatShop extends BaseModel
|
||||
foreach ($data_array as $json_k => $json_v){
|
||||
$system_stat->addStatShopModel($json_v);
|
||||
}
|
||||
log_write('店铺按天统计数据处理成功', 'debug');
|
||||
} catch (\Exception $e) {
|
||||
|
||||
log_write('店铺按天统计数据处理失败:' . $e->getMessage(), 'error');
|
||||
return $this->error('店铺按天统计数据处理失败');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -97,23 +118,37 @@ class StatShop extends BaseModel
|
||||
*/
|
||||
public function addShopHourStat($data, $carbon)
|
||||
{
|
||||
$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']);
|
||||
$stat_extend->handleData($data);//写入文件
|
||||
log_write('店铺按小时新统计数据开始添加', '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']);
|
||||
$stat_extend->handleData($data);//写入文件
|
||||
log_write('店铺按小时新统计数据已添加', 'debug');
|
||||
}catch (\Exception $e){
|
||||
log_write('店铺按小时新统计数据添加失败:' . $e->getMessage(), 'error');
|
||||
return $this->error('店铺按小时新统计数据添加失败');
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
public function cronShopStatHour()
|
||||
{
|
||||
log_write('系统计划任务开始执行店铺按小时统计');
|
||||
$path = __UPLOAD__.'/stat/stat_shop_hour';
|
||||
if(!is_dir($path)) return;
|
||||
if(!is_dir($path)) {
|
||||
log_write('系统计划任务执行店铺按小时统计异常:目录不存在' . $path . ',请检查目录是否存在');
|
||||
return;
|
||||
}
|
||||
|
||||
$result = $this->scanFile($path);
|
||||
if(empty($result)) return;
|
||||
if(empty($result)) {
|
||||
log_write('系统计划任务执行店铺按小时统计异常:目录下无文件' . $path . ',请检查是否有文件存在');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$json_array = [];
|
||||
@@ -145,8 +180,9 @@ class StatShop extends BaseModel
|
||||
foreach ($json_array as $json_k => $json_v){
|
||||
$system_stat->addStatShopHourModel($json_v);
|
||||
}
|
||||
log_write('系统计划任务执行店铺按小时统计完成');
|
||||
} catch (\Exception $e) {
|
||||
|
||||
log_write('系统计划任务执行店铺按小时统计异常:'.$e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user