AdminController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Country;
  4. use App\Models\GoodsCategory;
  5. use App\Models\Invite;
  6. use App\Models\Label;
  7. use App\Models\Level;
  8. use App\Models\Node;
  9. use App\Models\NodeDailyDataFlow;
  10. use App\Models\NodeHourlyDataFlow;
  11. use App\Models\Order;
  12. use App\Models\ReferralApply;
  13. use App\Models\ReferralLog;
  14. use App\Models\SsConfig;
  15. use App\Models\User;
  16. use App\Models\UserHourlyDataFlow;
  17. use Cache;
  18. use DB;
  19. use Illuminate\Http\JsonResponse;
  20. use Log;
  21. use PhpOffice\PhpSpreadsheet\Exception;
  22. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  23. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  24. use Response;
  25. use Str;
  26. class AdminController extends Controller
  27. {
  28. public function index()
  29. {
  30. $past = strtotime('-'.sysConfig('expire_days').' days');
  31. $today = today();
  32. $stats = Cache::remember('user_stats', now()->addMinutes(5), function () use ($today, $past) {
  33. $dailyTrafficUsage = NodeHourlyDataFlow::whereDate('created_at', $today)->sum(DB::raw('u + d'));
  34. return [
  35. 'activeUserCount' => User::where('t', '>=', $past)->count(), // 活跃用户数
  36. 'inactiveUserCount' => User::whereEnable(1)->where('t', '<', $past)->count(), // 不活跃用户数
  37. 'expireWarningUserCount' => User::whereBetween('expired_at', [$today, today()->addDays(sysConfig('expire_days'))])->count(), // 临近过期用户数
  38. 'largeTrafficUserCount' => User::whereRaw('(u + d)/transfer_enable >= 0.9')->where('status', '<>', -1)->count(), // 流量使用超过90%的用户
  39. 'flowAbnormalUserCount' => count((new UserHourlyDataFlow)->trafficAbnormal()), // 1小时内流量异常用户
  40. 'monthlyTrafficUsage' => formatBytes(NodeDailyDataFlow::whereNull('node_id')->whereMonth('created_at', now()->month)->sum(DB::raw('u + d'))),
  41. 'dailyTrafficUsage' => $dailyTrafficUsage ? formatBytes($dailyTrafficUsage) : 0,
  42. 'totalTrafficUsage' => formatBytes(NodeDailyDataFlow::whereNull('node_id')->where('created_at', '>=', now()->subDays(30))->sum(DB::raw('u + d'))),
  43. ];
  44. });
  45. return view('admin.index', [
  46. 'totalUserCount' => User::count(), // 总用户数
  47. 'todayRegister' => User::whereDate('created_at', $today)->count(), // 今日注册用户
  48. 'enableUserCount' => User::whereEnable(1)->count(), // 有效用户数
  49. 'activeUserCount' => $stats['activeUserCount'],
  50. 'payingUserCount' => User::has('paidOrders')->count(), // 付费用户数
  51. 'payingNewUserCount' => User::whereDate('created_at', $today)->has('paidOrders')->count(), // 不活跃用户数
  52. 'inactiveUserCount' => $stats['inactiveUserCount'],
  53. 'onlineUserCount' => User::where('t', '>=', strtotime('-10 minutes'))->count(), // 10分钟内在线用户数,
  54. 'expireWarningUserCount' => $stats['expireWarningUserCount'],
  55. 'largeTrafficUserCount' => $stats['largeTrafficUserCount'],
  56. 'flowAbnormalUserCount' => $stats['flowAbnormalUserCount'],
  57. 'nodeCount' => Node::count(),
  58. 'abnormalNodeCount' => Node::whereStatus(0)->count(),
  59. 'monthlyTrafficUsage' => $stats['monthlyTrafficUsage'],
  60. 'dailyTrafficUsage' => $stats['dailyTrafficUsage'],
  61. 'totalTrafficUsage' => $stats['totalTrafficUsage'],
  62. 'totalCredit' => User::where('credit', '<>', 0)->sum('credit') / 100,
  63. 'totalWaitRefAmount' => ReferralLog::whereIn('status', [0, 1])->sum('commission') / 100,
  64. 'todayWaitRefAmount' => ReferralLog::whereIn('status', [0, 1])->whereDate('created_at', $today)->sum('commission') / 100,
  65. 'totalRefAmount' => ReferralApply::whereStatus(2)->sum('amount') / 100,
  66. 'totalOrder' => Order::count(),
  67. 'todayOrder' => Order::whereDate('created_at', $today)->count(),
  68. 'totalOnlinePayOrder' => Order::where('pay_type', '<>', 0)->count(),
  69. 'todayOnlinePayOrder' => Order::where('pay_type', '<>', 0)->whereDate('created_at', $today)->count(),
  70. 'totalSuccessOrder' => Order::whereIn('status', [2, 3])->count(),
  71. 'todaySuccessOrder' => Order::whereIn('status', [2, 3])->whereDate('created_at', $today)->count(),
  72. ]);
  73. }
  74. // 邀请码列表
  75. public function inviteList()
  76. {
  77. return view('admin.aff.invite', [
  78. 'inviteList' => Invite::with(['invitee:id,username', 'inviter:id,username'])->orderBy('status')->orderByDesc('id')->paginate(15)->appends(request('page')),
  79. ]);
  80. }
  81. // 生成邀请码
  82. public function makeInvite(): JsonResponse
  83. {
  84. for ($i = 0; $i < 10; $i++) {
  85. $obj = new Invite;
  86. $obj->code = strtoupper(substr(md5(microtime().Str::random(6)), 8, 12));
  87. $obj->dateline = date('Y-m-d H:i:s', strtotime(sysConfig('admin_invite_days').' days'));
  88. $obj->save();
  89. }
  90. return Response::json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.generate')])]);
  91. }
  92. // 导出邀请码
  93. public function exportInvite(): void
  94. {
  95. $inviteList = Invite::whereStatus(0)->orderBy('id')->get();
  96. $filename = trans('user.invite.attribute').'_'.date('Ymd').'.xlsx';
  97. $spreadsheet = new Spreadsheet;
  98. $spreadsheet->getProperties()->setCreator('ProxyPanel')->setLastModifiedBy('ProxyPanel')->setTitle(trans('user.invite.attribute'))->setSubject(trans('user.invite.attribute'));
  99. $spreadsheet->setActiveSheetIndex(0);
  100. $sheet = $spreadsheet->getActiveSheet();
  101. $sheet->setTitle(trans('user.invite.attribute'));
  102. $sheet->fromArray([trans('user.invite.attribute'), trans('common.available_date')]);
  103. foreach ($inviteList as $k => $vo) {
  104. $sheet->fromArray([$vo->code, $vo->dateline], null, 'A'.($k + 2));
  105. }
  106. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // 输出07Excel文件
  107. //header('Content-Type:application/vnd.ms-excel'); // 输出Excel03版本文件
  108. header('Content-Disposition: attachment;filename="'.$filename.'"');
  109. header('Cache-Control: max-age=0');
  110. try {
  111. $writer = new Xlsx($spreadsheet);
  112. $writer->save('php://output');
  113. } catch (Exception $e) {
  114. Log::error(trans('common.error_action_item', ['action' => trans('common.export'), 'attribute' => trans('user.invite.attribute')]).': '.$e->getMessage());
  115. }
  116. }
  117. public function config()
  118. {
  119. return view('admin.config.common', [
  120. 'methods' => SsConfig::type(1)->get(),
  121. 'protocols' => SsConfig::type(2)->get(),
  122. 'categories' => GoodsCategory::all(),
  123. 'obfsList' => SsConfig::type(3)->get(),
  124. 'countries' => Country::all(),
  125. 'levels' => Level::all(),
  126. 'labels' => Label::with('nodes')->get(),
  127. ]);
  128. }
  129. }