改造部分参数

This commit is contained in:
1099438829 2021-05-09 23:45:40 +08:00
parent fde1e60552
commit a2ee4ea12e
5 changed files with 148 additions and 68 deletions

View File

@ -3,6 +3,9 @@ declare (strict_types = 1);
namespace app\index\controller;
use app\BaseController;
use app\index\model\PvLog;
use app\index\model\UrlLog;
use app\index\model\UvLog;
use think\facade\Config;
use think\facade\Db;
use think\facade\View;
@ -37,43 +40,10 @@ class Base extends BaseController
}
//判断后台统计配置是否开启 1 开启
if ($systemConfig["web_statistics"] == 1) {
//pv表 zz_pv_log 栏目存在 点击进入页面后
//判断 时间 0-1点 为time=0 H 24小时制
$date_data = date("Y-m-d");
$hour = date('H');
$pvInfo = Db::name('pv_log')
->where('date',$date_data)
->where('time','=',$hour)
->value('id');
if ($pvInfo) {
Db::name('pv_log')
->where('id', $pvInfo)
->inc('view')
->update();
} else {
$pvData['view'] = 1;
$pvData['date'] = $date_data;
$pvData['time'] = $hour;
$pvData['create_time'] = time();
Db::name('pv_log')->insert($pvData);
}
//uv表
//获取ip
$ipData = request()->ip();
//查询该ip今天是否存在过
$uvInfo = Db::name('uv_log')
->where('date',$date_data)
->where('ip',$ipData)
->field('id')
->find();
//不存在 添加数据
if (!$uvInfo) {
$uvData['ip'] = $ipData;
$uvData['time'] = $hour;
$uvData['date'] = $date_data;
$uvData['create_time'] = time();
Db::name('uv_log')->insert($uvData);
}
//增加pv信息
(new PvLog())->addLog();
//增加uv信息
(new UvLog())->addLog();
}
//判断是否开启了伪静态
if ($systemConfig['web_rewrite']) {
@ -83,27 +53,14 @@ class Base extends BaseController
}
}
//统计url
/**
* url 统计
* @param $title
* @author 李玉坤
* @date 2021-05-09 23:44
*/
protected function urlrecord($title)
{
$date_data = date("Y-m-d");
//获取url
$urlInfo = request()->url(true);
//根据url和date字段判断数据库中是否存在该页面的记录
$url_data = Db::name('url_log')
->where('date',$date_data)
->where('url',$urlInfo)
->field('id')
->find();
if ($url_data) {
Db::name('url_log')->where('id',$url_data['id'])->inc('pv')->update();
} else {
$dataUrl['url'] = $urlInfo;
$dataUrl['pv'] = 1;
$dataUrl['title'] = $title;
$dataUrl['date'] = $date_data;
$dataUrl['create_time'] = time();
Db::name('url_log')->insert($dataUrl);
}
(new UrlLog())->addLog($title);
}
}

View File

@ -9,8 +9,6 @@
namespace app\index\controller;
use think\facade\Db;
/**
* 应用入口
* Class Index
@ -35,13 +33,4 @@ class Index extends Base
$this->assign('cid',false);
return $this->fetch();
}
public function test($cid){
if(!$cid){
throw new Exception('请指定要获取的栏目分类id');
}
dd(get_document_category_children($cid));
}
}

50
app/index/model/PvLog.php Normal file
View File

@ -0,0 +1,50 @@
<?php
namespace app\index\model;
use think\Model;
/**
* Class PvLog
* @package app\index\model
* @author 李玉坤
* @date 2021-05-09 23:28
*/
class PvLog extends Model
{
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'datetime';
// 定义时间戳字段名
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
/**
* 增加pv信息
* @author 李玉坤
* @date 2021-05-09 23:31
*/
public function addLog(){
//pv表 栏目存在 点击进入页面后
//判断 时间 0-1点 为time=0 H 24小时制
$model = new self();
$date_data = date("Y-m-d");
$hour = date('H');
$pvInfo = $model->where('date',$date_data)
->where('time','=',$hour)
->value('id');
if ($pvInfo) {
$model->where('id', $pvInfo)
->inc('view')
->update();
} else {
$pvData = [
'view' => 1,
'time' => $hour,
'date' => $date_data
];
$model->save($pvData);
}
}
}

View File

@ -14,4 +14,37 @@ use think\Model;
*/
class UrlLog extends Model
{
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'datetime';
// 定义时间戳字段名
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
/**
* url访问记录
* @author 李玉坤
* @date 2021-05-09 23:40
*/
public function addLog($title){
$model = new self();
$date_data = date("Y-m-d");
//获取url
$urlInfo = request()->url(true);
//根据url和date字段判断数据库中是否存在该页面的记录
$url_data = $model->where('date',$date_data)
->where('url',$urlInfo)
->field('id')
->find();
if ($url_data) {
$model->where('id',$url_data['id'])->inc('pv')->update();
} else {
$dataUrl = [
'url' => $urlInfo,
'pv' => 1,
'title' => $title,
'date' => $date_data
];
$model->save($dataUrl);
}
}
}

51
app/index/model/UvLog.php Normal file
View File

@ -0,0 +1,51 @@
<?php
namespace app\index\model;
use think\facade\Db;
use think\Model;
/**
* Class UvLog
* @package app\index\model
* @author 李玉坤
* @date 2021-05-09 23:27
*/
class UvLog extends Model
{
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'datetime';
// 定义时间戳字段名
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
/**
* 增加pv信息
* @author 李玉坤
* @date 2021-05-09 23:31
*/
public function addLog(){
$model = new self();
//uv表
//获取ip
$date_data = date("Y-m-d");
$hour = date('H');
$ipData = request()->ip();
//查询该ip今天是否存在过
$uvInfo = $model->where('date',$date_data)
->where('ip',$ipData)
->field('id')
->find();
//不存在 添加数据
if (!$uvInfo) {
$uvData = [
'ip' => $ipData,
'time' => $hour,
'date' => $date_data
];
$model->save($uvData);
}
}
}