Procházet zdrojové kódy

token数据统计新增对普通用户角色

lh před 1 měsícem
rodič
revize
1cf4e469bc
1 změnil soubory, kde provedl 27 přidání a 14 odebrání
  1. 27 14
      app/Services/PointsStatsService.php

+ 27 - 14
app/Services/PointsStatsService.php

@@ -81,18 +81,21 @@ class PointsStatsService
     }
 
     /**
-     * 当前角色可查看的统计范围:superadmin 全部;admin 本组织;其他抛错。
+     * 当前角色可查看的统计范围:superadmin 全部;admin 本组织;user 仅自己;其他抛错。
      *
-     * @return int 限定的 cpid(0 表示不限定)
+     * @return array ['cpid' => int 组织限定(0 不限定), 'uid' => int 用户限定(0 不限定)]
      */
-    private function assertCanView(): int
+    private function assertCanView(): array
     {
         $role = (string)Site::getRole();
         if ($role === 'superadmin') {
-            return 0;
+            return ['cpid' => 0, 'uid' => 0];
         }
         if ($role === 'admin') {
-            return (int)Site::getCpid();
+            return ['cpid' => (int)Site::getCpid(), 'uid' => 0];
+        }
+        if ($role === 'user') {
+            return ['cpid' => (int)Site::getCpid(), 'uid' => (int)Site::getUid()];
         }
         Utils::throwError('1005:无权查看统计数据');
     }
@@ -106,13 +109,16 @@ class PointsStatsService
      */
     private function buildStatsQuery(array $params, array $groupBy)
     {
-        $cpid = $this->assertCanView();
+        $scope = $this->assertCanView();
 
         $query = DB::table('mp_points_daily_stats as s')
             ->leftJoin('mp_manage_users as u', 'u.id', '=', 's.uid');
 
-        if ($cpid > 0) {
-            $query->where('s.cpid', $cpid);
+        if ($scope['cpid'] > 0) {
+            $query->where('s.cpid', $scope['cpid']);
+        }
+        if ($scope['uid'] > 0) {
+            $query->where('s.uid', $scope['uid']);
         }
 
         $startDate = (string)getProp($params, 'start_date', date('Y-m-d', strtotime('-29 days')));
@@ -125,7 +131,8 @@ class PointsStatsService
         }
 
         $uid = (string)getProp($params, 'uid', '');
-        if ($uid !== '') {
+        // 普通用户仅能查看自己,忽略传入的 uid 筛选
+        if ($uid !== '' && $scope['uid'] <= 0) {
             $uids = array_values(array_filter(array_map('intval', explode(',', $uid))));
             if ($uids) {
                 $query->whereIn('s.uid', $uids);
@@ -251,11 +258,14 @@ class PointsStatsService
      */
     public function getFilters(array $params = []): array
     {
-        $cpid = $this->assertCanView();
+        $scope = $this->assertCanView();
 
         $users = DB::table('mp_manage_users')
-            ->when($cpid > 0, function ($q) use ($cpid) {
-                $q->where('cpid', $cpid);
+            ->when($scope['cpid'] > 0, function ($q) use ($scope) {
+                $q->where('cpid', $scope['cpid']);
+            })
+            ->when($scope['uid'] > 0, function ($q) use ($scope) {
+                $q->where('id', $scope['uid']);
             })
             ->where('is_enabled', 1)
             ->select('id', 'nickname', 'account')
@@ -273,8 +283,11 @@ class PointsStatsService
             })->all();
 
         $statsQuery = DB::table('mp_points_daily_stats as s');
-        if ($cpid > 0) {
-            $statsQuery->where('s.cpid', $cpid);
+        if ($scope['cpid'] > 0) {
+            $statsQuery->where('s.cpid', $scope['cpid']);
+        }
+        if ($scope['uid'] > 0) {
+            $statsQuery->where('s.uid', $scope['uid']);
         }
         $models = (clone $statsQuery)->distinct()->orderBy('s.model')->pluck('s.model')->values()->map(function ($model) {
             return ['name' => (string)$model, 'value' => (string)$model];