Controller.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Components\Client\Text;
  4. use App\Components\Client\URLSchemes;
  5. use App\Models\NodeDailyDataFlow;
  6. use App\Models\NodeHourlyDataFlow;
  7. use App\Models\UserDailyDataFlow;
  8. use App\Models\UserDataFlowLog;
  9. use App\Models\UserHourlyDataFlow;
  10. use DB;
  11. use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  12. use Illuminate\Foundation\Bus\DispatchesJobs;
  13. use Illuminate\Foundation\Validation\ValidatesRequests;
  14. use Illuminate\Routing\Controller as BaseController;
  15. class Controller extends BaseController
  16. {
  17. use AuthorizesRequests;
  18. use DispatchesJobs;
  19. use ValidatesRequests;
  20. // 节点信息
  21. public function getUserNodeInfo(array $server, bool $is_url): ?string
  22. {
  23. $type = $is_url ? new URLSchemes() : new Text();
  24. switch ($server['type']) {
  25. case'shadowsocks':
  26. $data = $type->buildShadowsocks($server);
  27. break;
  28. case 'shadowsocksr':
  29. $data = $type->buildShadowsocksr($server);
  30. break;
  31. case 'v2ray':
  32. $data = $type->buildVmess($server);
  33. break;
  34. case 'trojan':
  35. $data = $type->buildTrojan($server);
  36. break;
  37. default:
  38. }
  39. return $data ?? null;
  40. }
  41. // 流量使用图表
  42. public function dataFlowChart($id, $is_node = false): array
  43. {
  44. if ($is_node) {
  45. $currentFlow = UserDataFlowLog::whereNodeId($id);
  46. $hourlyFlow = NodeHourlyDataFlow::whereNodeId($id)->whereDate('created_at', date('Y-m-d'))->selectRaw('(DATE_FORMAT(node_hourly_data_flow.created_at, "%k")) as date, total')->pluck('total', 'date');
  47. $dailyFlow = NodeDailyDataFlow::whereNodeId($id)->whereMonth('created_at', date('n'))->selectRaw('(DATE_FORMAT(node_daily_data_flow.created_at, "%e")) as date, total')->pluck('total', 'date');
  48. } else {
  49. $currentFlow = UserDataFlowLog::whereUserId($id);
  50. $hourlyFlow = UserHourlyDataFlow::userHourly($id)->whereDate('created_at', date('Y-m-d'))->selectRaw('(DATE_FORMAT(user_hourly_data_flow.created_at, "%k")) as date, total')->pluck('total', 'date');
  51. $dailyFlow = UserDailyDataFlow::userDaily($id)->whereMonth('created_at', date('n'))->selectRaw('(DATE_FORMAT(user_daily_data_flow.created_at, "%e")) as date, total')->pluck('total', 'date');
  52. }
  53. $currentFlow = $currentFlow->where('log_time', '>=', strtotime(date('Y-m-d H:0')))->sum(DB::raw('u + d'));
  54. // 节点一天内的流量
  55. $hourlyData = array_fill(0, date('G') + 1, 0);
  56. foreach ($hourlyFlow as $date => $dataFlow) {
  57. $hourlyData[$date] = round($dataFlow / GB, 3);
  58. }
  59. $hourlyData[date('G') + 1] = round($currentFlow / GB, 3);
  60. // 节点一个月内的流量
  61. $dailyData = array_fill(0, date('j') - 1, 0);
  62. foreach ($dailyFlow as $date => $dataFlow) {
  63. $dailyData[$date - 1] = round($dataFlow / GB, 3);
  64. }
  65. $dailyData[date('j', strtotime(now())) - 1] = round(array_sum($hourlyData) + $currentFlow / GB, 3);
  66. return [
  67. 'trafficDaily' => $dailyData,
  68. 'trafficHourly' => $hourlyData,
  69. 'monthDays' => range(1, date('j')), // 本月天数
  70. 'dayHours' => range(0, date('G') + 1), // 本日小时
  71. ];
  72. }
  73. /*
  74. // 将Base64图片转换为本地图片并保存
  75. public function base64ImageSaver($base64_image_content): ?string
  76. {
  77. // 匹配出图片的格式
  78. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  79. $type = $result[2];
  80. $directory = date('Ymd');
  81. $path = '/assets/images/qrcode/'.$directory.'/';
  82. // 检查是否有该文件夹,如果没有就创建,并给予最高权限
  83. if (! file_exists(public_path($path))
  84. && ! mkdir($concurrentDirectory = public_path($path), 0755, true)
  85. && ! is_dir($concurrentDirectory)) {
  86. throw new RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
  87. }
  88. $fileName = Str::random(18).".{$type}";
  89. if (file_put_contents(public_path($path.$fileName), base64_decode(str_replace($result[1], '', $base64_image_content)))) {
  90. chmod(public_path($path.$fileName), 0744);
  91. return $path.$fileName;
  92. }
  93. }
  94. return '';
  95. }
  96. */
  97. }