Fastadmin控制台增加用户活跃统计

书接上篇增加用户活跃统计后,在后台控制台增加用户活跃统计展示。

目录

创建服务层

控制台增加统计数据

修改js文件

修改legend

修改series

设置语言

总结


创建服务层

在application/common下创建services文件夹,

然后创建UserActiveService.php文件,内容如下:

php 复制代码
<?php
namespace app\common\services;

use app\common\model\UserActive;
use app\common\model\User;

/**
 * 用户活跃服务层
 */
class UserActiveService
{
    /**
     * 获取日活用户数(可选指定日期)
     * @param string $date 格式:Y-m-d
     * @return int
     */
    public static function getDayActive($date = '')
    {
        $date = $date ?: date('Y-m-d');
        $start = strtotime($date);
        $end = $start + 86400 - 1;

        return UserActive::where('active_time', 'between', [$start, $end])
            ->group('user_id')->count('user_id');
    }

    /**
     * 获取周活用户数(近7天)
     */
    public static function getWeekActive()
    {
        $start = strtotime('-6 days');
        $end = time();
        return UserActive::where('active_time', 'between', [$start, $end])
            ->group('user_id')->count('user_id');
    }

    /**
     * 获取月活用户数(近30天)
     */
    public static function getMonthActive()
    {
        $start = strtotime('-29 days');
        $end = time();
        return UserActive::where('active_time', 'between', [$start, $end])
            ->group('user_id')->count('user_id');
    }

    /**
     * 获取近N天活跃趋势
     * @param int $days 天数
     * @return array
     */
    public static function getActiveTrend($days = 30)
    {
        $trend = [];
        $startDate = strtotime("-" . ($days - 1) . " days");
        $endDate = time();
        
        // 单次 SQL 查询获取所有日期的活跃用户数据
        $activeData = UserActive::where('active_time', 'between', [$startDate, $endDate])
            ->field('active_time, user_id')
            ->select();
        
        // 按日期分组统计活跃用户数
        $dateStats = [];
        foreach ($activeData as $record) {
            $date = date('Y-m-d', $record['active_time']);
            $userId = $record['user_id'];
            
            if (!isset($dateStats[$date])) {
                $dateStats[$date] = [];
            }
            
            // 使用 userId 作为键,自动去重
            $dateStats[$date][$userId] = true;
        }
        
        // 生成完整的日期数组(包含没有活跃数据的日期)
        for ($i = $days - 1; $i >= 0; $i--) {
            $date = date('Y-m-d', strtotime("-$i days"));
            $count = isset($dateStats[$date]) ? count($dateStats[$date]) : 0;
            $trend[] = [
                'date'  => $date,
                'count' => $count
            ];
        }

        return $trend;
    }

    /**
     * 获取单个用户的活跃详情
     */
    public static function getUserActiveInfo($uid)
    {
        $user = User::get($uid);
        if (!$user) return [];

        // 近7天活跃次数
        $weekStart = strtotime('-6 days');
        $weekCount = UserActive::where('user_id', $uid)
            ->where('active_time', '>=', $weekStart)
            ->count();

        // 最后活跃时间
        $lastActive = UserActive::where('user_id', $uid)
            ->order('active_time', 'desc')
            ->value('active_time');

        return [
            'login_count'       => $user['login_count'],
            'active_days'       => $user['active_days'],
            'last_login_time'   => date('Y-m-d H:i:s', $user['last_login_time']),
            'last_active_time'  => $lastActive ? date('Y-m-d H:i:s', $lastActive) : '-',
            'week_active_count' => $weekCount,
        ];
    }
}

控制台增加统计数据

在admin/controller/Dashboard.php的index方法内的最下面,

增加用户活跃数据向视图传参。代码如下:

php 复制代码
// 7天用户活跃折线图数据
        $this->assignconfig('activeData', array_column(UserActiveService::getActiveTrend(7), 'count'));
        return $this->view->fetch();

修改js文件

在public/assets/js/backend/dashboard.js文件中增加用户活跃数据渲染折线图。

修改legend

在原来基础上增加标题。

代码如下:

javascript 复制代码
legend: {
   data: [__('Register user'), __('Active user')]
},
修改series

增加用户活跃数据渲染,代码如下:

javascript 复制代码
series: [{
           name: __('Register user'),
           type: 'line',
           smooth: true,
           areaStyle: {
                normal: {}
           },
           lineStyle: {
                normal: {
                    width: 1.5
                }
          },
          data: Config.userdata
         },
         {
          name: __('Active user'),
          type: 'line',
          smooth: true,
          areaStyle: {
                normal: {}
          },
          lineStyle: {
               normal: {
                  width: 1.5
               }
          },
          data: Config.activeData
        }]

设置语言

在lang/dashboard.php文件中,在注册用户数下增加一条记录,

代码如下:

php 复制代码
'Active user'              => '活跃用户数',

最终效果:

总结

这样就实现了用户活跃统计数据增加到原来的注册用户折线图中。

相关推荐
FYKJ_20109 小时前
springboot校园兼职平台--附源码02041
java·javascript·spring boot·python·eclipse·django·php
zhangfeng113314 小时前
PHP 语法检查命令 php -l “$file“ > /dev/null 2>&1;
开发语言·php
kybs199114 小时前
springboot视频推荐系统--附源码72953
java·spring boot·python·eclipse·asp.net·php·idea
计算机安禾14 小时前
【计算机网络】第6篇:虚拟局域网——基于标签的广播域划分及其安全边界
计算机网络·安全·php
zhangfeng113317 小时前
适合 5人以内小团队的Git 工作流 + Code Review + 自动化部署方案 FastAdmin +linunx服务器宝塔系统 外包项目 —
服务器·git·自动化·php·代码复审
zx28596340019 小时前
Laravel 7.x新特性全解析
php·laravel
zx28596340020 小时前
Laravel 4.x:颠覆PHP框架的10大革新特性
开发语言·php·laravel
xxjj998a21 小时前
PHP vs C#:核心差异全解析
开发语言·c#·php
吉吉611 天前
php反序列化基础知识前奏
android·php·反序列化
星光开发者1 天前
基于springboot电动汽车租赁管理系统-计算机毕设 附源码 11217
javascript·spring boot·mysql·django·php·html5·express