AdminController.php 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Node;
  4. use App\Models\NodeDailyDataFlow;
  5. use App\Models\NodeHourlyDataFlow;
  6. use App\Models\Order;
  7. use App\Models\ReferralApply;
  8. use App\Models\ReferralLog;
  9. use App\Models\User;
  10. use App\Models\UserHourlyDataFlow;
  11. use Cache;
  12. use DB;
  13. use Response;
  14. class AdminController extends Controller
  15. {
  16. public function index()
  17. {
  18. $past = strtotime('-'.sysConfig('expire_days').' days');
  19. $today = today();
  20. $stats = Cache::remember('user_stats', now()->addMinutes(5), function () use ($today, $past) {
  21. $dailyTrafficUsage = NodeHourlyDataFlow::whereDate('created_at', $today)->sum(DB::raw('u + d'));
  22. return [
  23. 'activeUserCount' => User::where('t', '>=', $past)->count(), // 活跃用户数
  24. 'inactiveUserCount' => User::whereEnable(1)->where('t', '<', $past)->count(), // 不活跃用户数
  25. 'expireWarningUserCount' => User::whereBetween('expired_at', [$today, today()->addDays(sysConfig('expire_days'))])->count(), // 临近过期用户数
  26. 'largeTrafficUserCount' => User::whereRaw('(u + d)/transfer_enable >= 0.9')->where('status', '<>', -1)->count(), // 流量使用超过90%的用户
  27. 'flowAbnormalUserCount' => count((new UserHourlyDataFlow)->trafficAbnormal()), // 1小时内流量异常用户
  28. 'monthlyTrafficUsage' => formatBytes(NodeDailyDataFlow::whereNull('node_id')->whereMonth('created_at', now()->month)->sum(DB::raw('u + d'))),
  29. 'dailyTrafficUsage' => $dailyTrafficUsage ? formatBytes($dailyTrafficUsage) : 0,
  30. 'totalTrafficUsage' => formatBytes(NodeDailyDataFlow::whereNull('node_id')->where('created_at', '>=', now()->subDays(30))->sum(DB::raw('u + d'))),
  31. ];
  32. });
  33. return view('admin.index', [
  34. 'totalUserCount' => User::count(), // 总用户数
  35. 'todayRegister' => User::whereDate('created_at', $today)->count(), // 今日注册用户
  36. 'enableUserCount' => User::whereEnable(1)->count(), // 有效用户数
  37. 'activeUserCount' => $stats['activeUserCount'],
  38. 'payingUserCount' => User::has('paidOrders')->count(), // 付费用户数
  39. 'payingNewUserCount' => User::whereDate('created_at', $today)->has('paidOrders')->count(), // 不活跃用户数
  40. 'inactiveUserCount' => $stats['inactiveUserCount'],
  41. 'onlineUserCount' => User::where('t', '>=', strtotime('-10 minutes'))->count(), // 10分钟内在线用户数,
  42. 'expireWarningUserCount' => $stats['expireWarningUserCount'],
  43. 'largeTrafficUserCount' => $stats['largeTrafficUserCount'],
  44. 'flowAbnormalUserCount' => $stats['flowAbnormalUserCount'],
  45. 'nodeCount' => Node::count(),
  46. 'abnormalNodeCount' => Node::whereStatus(0)->count(),
  47. 'monthlyTrafficUsage' => $stats['monthlyTrafficUsage'],
  48. 'dailyTrafficUsage' => $stats['dailyTrafficUsage'],
  49. 'totalTrafficUsage' => $stats['totalTrafficUsage'],
  50. 'totalCredit' => User::where('credit', '<>', 0)->sum('credit') / 100,
  51. 'totalWaitRefAmount' => ReferralLog::whereIn('status', [0, 1])->sum('commission') / 100,
  52. 'todayWaitRefAmount' => ReferralLog::whereIn('status', [0, 1])->whereDate('created_at', $today)->sum('commission') / 100,
  53. 'totalRefAmount' => ReferralApply::whereStatus(2)->sum('amount') / 100,
  54. 'totalOrder' => Order::count(),
  55. 'todayOrder' => Order::whereDate('created_at', $today)->count(),
  56. 'totalOnlinePayOrder' => Order::where('pay_type', '<>', 0)->count(),
  57. 'todayOnlinePayOrder' => Order::where('pay_type', '<>', 0)->whereDate('created_at', $today)->count(),
  58. 'totalSuccessOrder' => Order::whereIn('status', [2, 3])->count(),
  59. 'todaySuccessOrder' => Order::whereIn('status', [2, 3])->whereDate('created_at', $today)->count(),
  60. ]);
  61. }
  62. }