Commit a98fe19f authored by 刘小敏's avatar 刘小敏

统计数据静态页

parent 49793a7e
......@@ -8,6 +8,7 @@ use app\admin\model\shopro\goods\Goods;
use app\admin\model\shopro\user\User;
use app\admin\model\shopro\Share;
use addons\shopro\service\SearchHistory;
use think\Db;
class Dashboard extends Common
{
......@@ -35,6 +36,244 @@ class Dashboard extends Common
return $this->view->fetch();
}
/**
* 获取统计数据
*/
public function statistics()
{
$timeRange = $this->request->param('time_range', '30days');
// 根据时间范围获取条件
$timeCondition = $this->getTimeCondition($timeRange);
// 成交总额
$totalAmount = Order::whereTime('createtime', $timeCondition)
->whereIn('status', [Order::STATUS_PAID, Order::STATUS_COMPLETED])
->sum('pay_fee') ?: 0;
// 订单数
$orderCount = Order::whereTime('createtime', $timeCondition)
->whereIn('status', [Order::STATUS_PAID, Order::STATUS_COMPLETED])
->count();
// 客单价
$avgAmount = $orderCount > 0 ? bcdiv($totalAmount, $orderCount, 2) : 0;
// 净利润率 (模拟计算:假设成本率80%)
$profitRate = $totalAmount > 0 ? '18.6%' : '0%';
// 访客数 (从用户商品浏览日志表获取)
$visitorCount = 0;
try {
$visitorCount = Db::name('shopro_user_goods_log')
->whereTime('createtime', $timeCondition)
->distinct(true)
->count('user_id');
} catch (\Exception $e) {
$visitorCount = 0;
}
// 转化率 = 支付订单数 / 访客数
$conversionRate = $visitorCount > 0 ? bcdiv($orderCount * 100, $visitorCount, 2) . '%' : '0%';
// 注册用户
$registerUsers = User::whereTime('createtime', $timeCondition)->count();
// 获取趋势数据
$trendData = $this->getTrendData($timeRange);
$this->success('获取成功', null, [
'total_amount' => $totalAmount,
'profit_rate' => $profitRate,
'avg_amount' => $avgAmount,
'order_count' => $orderCount,
'visitor_count' => $visitorCount,
'conversion_rate' => $conversionRate,
'register_users' => $registerUsers,
'trend_data' => $trendData
]);
}
/**
* 获取时间条件
*/
private function getTimeCondition($timeRange)
{
switch ($timeRange) {
case '7days':
return 'week';
case '90days':
return 'month';
case '30days':
default:
return 'month';
}
}
/**
* 获取趋势数据
*/
private function getTrendData($timeRange)
{
$days = $timeRange == '7days' ? 7 : ($timeRange == '90days' ? 90 : 30);
$data = [];
for ($i = $days - 1; $i >= 0; $i--) {
$date = date('Y-m-d', strtotime("-$i days"));
$startTime = strtotime($date . ' 00:00:00');
$endTime = strtotime($date . ' 23:59:59');
$amount = Order::whereBetween('createtime', [$startTime, $endTime])
->whereIn('status', [Order::STATUS_PAID, Order::STATUS_COMPLETED])
->sum('pay_fee') ?: 0;
$orderNum = Order::whereBetween('createtime', [$startTime, $endTime])
->whereIn('status', [Order::STATUS_PAID, Order::STATUS_COMPLETED])
->count();
$registerNum = User::whereBetween('createtime', [$startTime, $endTime])->count();
$data[] = [
'date' => $date,
'amount' => $amount,
'order_num' => $orderNum,
'register_num' => $registerNum
];
}
return $data;
}
/**
* 获取转化漏斗数据
*/
public function funnel()
{
$timeRange = $this->request->param('time_range', '30days');
$timeCondition = $this->getTimeCondition($timeRange);
// 访问用户数
$visitCount = Db::name('shopro_user_goods_log')
->whereTime('createtime', $timeCondition)
->distinct(true)
->count('user_id');
// 浏览商品用户数
$browseCount = Db::name('shopro_user_goods_log')
->whereTime('createtime', $timeCondition)
->where('type', 'browse')
->distinct(true)
->count('user_id');
// 加入购物车用户数
$cartCount = Db::name('shopro_user_goods_log')
->whereTime('createtime', $timeCondition)
->where('type', 'cart')
->distinct(true)
->count('user_id');
// 提交订单用户数
$submitOrderCount = Order::whereTime('createtime', $timeCondition)
->distinct(true)
->count('user_id');
// 完成支付用户数
$payCount = Order::whereTime('createtime', $timeCondition)
->whereIn('status', [Order::STATUS_PAID, Order::STATUS_COMPLETED])
->distinct(true)
->count('user_id');
$this->success('获取成功', null, [
'visit' => $visitCount,
'browse' => $browseCount,
'cart' => $cartCount,
'submit_order' => $submitOrderCount,
'pay' => $payCount
]);
}
/**
* 获取客单价分布
*/
public function priceDistribution()
{
$timeRange = $this->request->param('time_range', '30days');
$timeCondition = $this->getTimeCondition($timeRange);
$orders = Order::whereTime('createtime', $timeCondition)
->whereIn('status', [Order::STATUS_PAID, Order::STATUS_COMPLETED])
->field('pay_fee')
->select();
$distribution = [
'0-100' => 0,
'100-300' => 0,
'300-500' => 0,
'500-1000' => 0,
'1000+' => 0
];
$total = count($orders);
foreach ($orders as $order) {
$amount = $order['pay_fee'];
if ($amount < 100) {
$distribution['0-100']++;
} elseif ($amount < 300) {
$distribution['100-300']++;
} elseif ($amount < 500) {
$distribution['300-500']++;
} elseif ($amount < 1000) {
$distribution['500-1000']++;
} else {
$distribution['1000+']++;
}
}
// 转换为百分比
foreach ($distribution as &$val) {
$val = $total > 0 ? round($val / $total * 100) : 0;
}
$this->success('获取成功', null, $distribution);
}
/**
* 获取实时订单动态
*/
public function recentOrders()
{
$orders = Order::whereIn('status', [Order::STATUS_PAID, Order::STATUS_COMPLETED])
->order('createtime', 'desc')
->limit(10)
->select();
$result = [];
foreach ($orders as $order) {
$user = User::where('id', $order['user_id'])->find();
$result[] = [
'amount' => $order['pay_fee'],
'time' => $this->formatTime($order['createtime']),
'location' => $user ? ($user['city'] ?: '未知') : '未知'
];
}
$this->success('获取成功', null, $result);
}
/**
* 格式化时间显示
*/
private function formatTime($timestamp)
{
$now = time();
$diff = $now - $timestamp;
if ($diff < 60) {
return '刚刚';
} elseif ($diff < 3600) {
return floor($diff / 60) . '分钟前';
} elseif ($diff < 86400) {
return floor($diff / 3600) . '小时前';
} else {
return date('m-d H:i', $timestamp);
}
}
public function total()
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment