首页数据统计

This commit is contained in:
wong
2025-07-17 16:22:01 +08:00
parent 48cb1a40f9
commit 7079f558c5
2 changed files with 152 additions and 0 deletions

View File

@@ -100,6 +100,15 @@ Route::group('v1/', function () {
Route::get('getMemberList', 'app\cunkebao\controller\chatroom\GetChatroomListV1Controller@getMemberList'); // 获取群详情
});
Route::group('dashboard',function (){
Route::get('', 'app\cunkebao\controller\StatsController@baseInfoStats');
Route::get('plan-stats', 'app\cunkebao\controller\StatsController@planStats');
Route::get('sevenDay-stats', 'app\cunkebao\controller\StatsController@customerAcquisitionStats7Days');
});
})->middleware(['jwt']);

View File

@@ -0,0 +1,143 @@
<?php
namespace app\cunkebao\controller;
use think\Db;
use think\Controller;
class StatsController extends Controller
{
const WEEK = [
0 => '周日',
1 => '周一',
2 => '周二',
3 => '周三',
4 => '周四',
5 => '周五',
6 => '周六',
];
/**
* 基础信息
* @return \think\response\Json
*/
public function baseInfoStats()
{
$deviceNum = Db::name('device')->where(['companyId' => $this->request->userInfo['companyId'], 'deleteTime' => 0])->count();
$wechatNum = Db::name('wechat_customer')->where(['companyId' => $this->request->userInfo['companyId']])->count();
$aliveWechatNum = Db::name('wechat_customer')->alias('wc')
->join('device_wechat_login dwl', 'wc.wechatId = dwl.wechatId')
->where(['wc.companyId' => $this->request->userInfo['companyId'], 'dwl.alive' => 1])
->group('wc.wechatId')
->count();
$data = [
'deviceNum' => $deviceNum,
'wechatNum' => $wechatNum,
'aliveWechatNum' => $aliveWechatNum,
];
return successJson($data, '获取成功');
}
/**
* 场景获客统计
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function planStats()
{
$num = $this->request->param('num', 4);
$planScene = Db::name('plan_scene')
->field('id,name,image')
->where(['status' => 1])
->order('sort DESC')
->page(1, $num)
->select();
foreach ($planScene as &$v) {
$allNum = Db::name('customer_acquisition_task')->alias('ac')
->join('task_customer tc', 'tc.task_id = ac.id')
->where(['ac.sceneId' => $v['id'], 'ac.companyId' => $this->request->userInfo['companyId'], 'ac.deleteTime' => 0])
->count();
$addNum = Db::name('customer_acquisition_task')->alias('ac')
->join('task_customer tc', 'tc.task_id = ac.id')
->where(['ac.sceneId' => $v['id'], 'ac.companyId' => $this->request->userInfo['companyId'], 'ac.deleteTime' => 0])
->whereIn('tc.status', [1, 2, 3, 4])
->count();
$passNum = Db::name('customer_acquisition_task')->alias('ac')
->join('task_customer tc', 'tc.task_id = ac.id')
->where(['ac.sceneId' => $v['id'], 'ac.companyId' => $this->request->userInfo['companyId'], 'ac.deleteTime' => 0])
->whereIn('tc.status', [4])
->count();
$v['allNum'] = $allNum;
$v['addNum'] = $addNum;
$v['passNum'] = $passNum;
}
unset($v);
return successJson($planScene, '获取成功');
}
/**
* 近7天获客统计
* @return \think\response\Json
*/
public function customerAcquisitionStats7Days()
{
$companyId = $this->request->userInfo['companyId'];
$days = 7;
$dates = [];
$allNum = [];
$addNum = [];
$passNum = [];
for ($i = $days - 1; $i >= 0; $i--) {
$date = date('Y-m-d', strtotime("-$i day"));
$start = strtotime($date . ' 00:00:00');
$end = strtotime($date . ' 23:59:59');
// 获客总量
$allNum[] = Db::name('customer_acquisition_task')->alias('ac')
->join('task_customer tc', 'tc.task_id = ac.id')
->where(['ac.companyId' => $companyId, 'ac.deleteTime' => 0])
->where('tc.createTime', 'between', [$start, $end])
->count();
// 添加量
$addNum[] = Db::name('customer_acquisition_task')->alias('ac')
->join('task_customer tc', 'tc.task_id = ac.id')
->where(['ac.companyId' => $companyId, 'ac.deleteTime' => 0])
->where('tc.updateTime', 'between', [$start, $end])
->whereIn('tc.status', [1, 2, 3, 4])
->count();
// 通过量
$passNum[] = Db::name('customer_acquisition_task')->alias('ac')
->join('task_customer tc', 'tc.task_id = ac.id')
->where(['ac.companyId' => $companyId, 'ac.deleteTime' => 0])
->where('tc.updateTime', 'between', [$start, $end])
->whereIn('tc.status', [4])
->count();
$week = date("w", strtotime($date));
$dates[] = self::WEEK[$week];
}
$data = [
'date' => $dates,
'allNum' => $allNum,
'addNum' => $addNum,
'passNum' => $passNum,
];
return successJson($data, '获取成功');
}
}