Files

325 lines
9.4 KiB
PHP

<?php
namespace app\admin\controller\data;
use app\common\controller\Backend;
use think\Db;
/**
* 流量数据
*
* @icon fa fa-circle-o
*/
class Traffic extends Backend
{
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\data\Traffic();
$this->timeModel = new \app\admin\model\data\Time();
}
/**
* 每日的数据汇总
*/
public function day()
{
//当前是否为关联查询
$this->relationSearch = false;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = $this->model
->where($where)
->where('uniacid='.UNIACID)
->order($sort, $order)
->setUniacid(false)
->field([
"FROM_UNIXTIME(createtime, '%Y-%m-%d') as date", // 转换创建时间为日期
"COUNT(*) as store_pv", // 店铺浏览量,包括所有商品浏览
"COUNT(DISTINCT ip) as store_uv", // 基于IP的店铺访客数
"COUNT(IF(page_type<>'other', 1, NULL)) as product_pv", // 商品浏览量
"COUNT(DISTINCT IF(page_type<>'other', ip, NULL)) as product_uv" // 商品访客数,基于IP
])
->group("FROM_UNIXTIME(createtime, '%Y-%m-%d')") // 按日期分组
->paginate($limit);
foreach ($list as $row) {
}
$result = array("total" => $list->total(), "rows" => $list->items());
return $this->success("获取成功",$result);
}
/**
* 访问趋势统计
* @return void
*/
public function trend(){
$time = $this->request->post('time/a',0);
if(!$time){
$this->success('获取成功', []);
}
if(count($time) == 1){
$time = $time[0];
}
$unit = $this->request->post('unit',0);
$this->timeModel = new \app\admin\model\data\Time();
$emptyData = [
'store_pv'=>0,
'store_uv'=>0,
'product_pv'=>0,
'product_uv'=>0
];
$time = $this->timeModel->getFilterStartEndTime($time,$unit);
if($unit == 'date'){
$interval = 3600;
$timeFormat = 'H:i';
$timeFormatSql = '%H';
}else{
$interval = 86400;
$timeFormat = 'Y-m-d';
$timeFormatSql = '%Y-%m-%d';
}
$result = $this->model
->where([
'createtime'=>['between',[$time['start'],$time['end']]]
])
->order("createtime", "desc")
->field([
"FROM_UNIXTIME(createtime, '{$timeFormatSql}') as date", // 转换创建时间为日期
"COUNT(*) as store_pv", // 店铺浏览量,包括所有商品浏览
"COUNT(DISTINCT ip) as store_uv", // 基于IP的店铺访客数
"COUNT(IF(page_type<>'other', 1, NULL)) as product_pv", // 商品浏览量
"COUNT(DISTINCT IF(page_type<>'other', ip, NULL)) as product_uv" // 商品访客数,基于IP
])
->group("FROM_UNIXTIME(createtime, '{$timeFormatSql}')") // 按日期分组
->select();
$result = collection($result)->toArray();
$list = [];
if($result){
foreach ($result as $item){
$timeUnit = $item['date'];
if($unit == 'date'){
$timeUnit = $timeUnit.":00";
}
$list[$timeUnit] = $item;
}
}
$data = [];
for($i=$time['start'];$i<$time['end'];$i+=$interval){
$timeUnit = date($timeFormat,$i);
if(!isset($data[$timeUnit])){
$data[$timeUnit] = $emptyData;
}
if(isset($list[$timeUnit])){
$data[$timeUnit] = $list[$timeUnit];
}
}
$this->success('获取成功', $data);
}
/**
* 流量趋势统计
* @return void
*/
public function total(){
$time = $this->request->post('time/a',0);
$unit = $this->request->post('unit','');
if(!$time){
$this->success('获取成功', []);
}
if(count($time) == 1){
$time = $time[0];
}
$time = $this->timeModel->getFilterStartEndTime($time,$unit);
$data = $this->model->getTotal($time['start'], $time['end']);
$this->success('获取成功', $data);
}
protected $deviceConditions = ['IOS'=>['is_ios'=>1],'Android'=>['is_android'=>1],'Other'=>['is_ios'=>0,'is_android'=>0]];
/**
* 终端统计
* 总计数量
* @return void
*/
public function deviceTotal(){
$data = [
'IOS'=>0,
'Android'=>0,
'Other'=>0
];
$time = $this->request->post('time/a',0);
$unit = $this->request->post('unit','');
if(!$time){
$this->success('获取成功', []);
}
if(count($time) == 1){
$time = $time[0];
}
$time = $this->timeModel->getFilterStartEndTime($time,$unit);
foreach ($this->deviceConditions as $device => $condition){
$data[$device] = $this->model->where($condition)->where([
'createtime'=>['between',[$time['start'],$time['end']]]
])->group("ip")->count();
}
$this->success('获取成功', $data);
}
/**
* 终端统计
* 终端对应的 pv/uv
* @return void
*/
public function deviceList(){
$time = $this->request->post('time/a',0);
$unit = $this->request->post('unit','');
if(!$time){
$this->success('获取成功', []);
}
if(count($time) == 1){
$time = $time[0];
}
$time = $this->timeModel->getFilterStartEndTime($time,$unit);
$orderModel = new \app\admin\model\data\Order;
$data = [];
foreach ($this->deviceConditions as $device => $condition){
$pv = $this->model->where($condition)->where([
'createtime'=>['between',[$time['start'],$time['end']]]
])->count();
$uv = $this->model->where($condition)->where([
'createtime'=>['between',[$time['start'],$time['end']]]
])->group("ip")->count();
$temp = [
'device'=>$device,
'pv'=>$pv,
'uv'=>$uv,
'payer_count'=>$orderModel->getTimeRangePayUserCount($time['start'],$time['end'])
];
$temp['pay_rate'] = $temp['payer_count'] && $temp['uv'] ? number_format($temp['payer_count'] / $temp['uv'] * 100,2) ."%" : '0%';
$data[] = $temp;
}
$this->success('获取成功', $data);
}
protected $platformConditions = ['dyMiniProgram','wxOfficialAccount','wxMiniProgram','H5','Pc'];
/**
* 平台统计
* 总计数量
* @return void
*/
public function platformTotal(){
$data = [];
$time = $this->request->post('time/a',0);
$unit = $this->request->post('unit','');
if(!$time){
$this->success('获取成功', []);
}
if(count($time) == 1){
$time = $time[0];
}
$time = $this->timeModel->getFilterStartEndTime($time,$unit);
foreach ($this->platformConditions as $platform){
$data[__($platform)] = $this->model->where([
'platform'=>$platform
])->where([
'createtime'=>['between',[$time['start'],$time['end']]]
])->group("ip")->count();
}
$this->success('获取成功', $data);
}
/**
* 平台统计
* 终端对应的 pv/uv
* @return void
*/
public function platformList(){
$time = $this->request->post('time/a',0);
$unit = $this->request->post('unit','');
if(!$time){
$this->success('获取成功', []);
}
if(count($time) == 1){
$time = $time[0];
}
$time = $this->timeModel->getFilterStartEndTime($time,$unit);
$orderModel = new \app\admin\model\data\Order;
$data = [];
foreach ($this->platformConditions as $platform){
$pv = $this->model->where([
'platform'=>$platform
])->where([
'createtime'=>['between',[$time['start'],$time['end']]]
])->count();
$uv = $this->model->where([
'platform'=>$platform
])->where([
'createtime'=>['between',[$time['start'],$time['end']]]
])->group("ip")->count();
$temp = [
'platform'=>__($platform),
'pv'=>$pv,
'uv'=>$uv,
'payer_count'=>$orderModel->getTimeRangePayUserCount($time['start'],$time['end'])
];
$temp['pay_rate'] = $temp['payer_count'] && $temp['uv'] ? intval($temp['payer_count'] / $temp['uv'] * 100) ."%" : '0%';
$data[] = $temp;
}
$this->success('获取成功', $data);
}
}