Controller.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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);
  47. $dailyFlow = NodeDailyDataFlow::whereNodeId($id);
  48. } else {
  49. $currentFlow = UserDataFlowLog::whereUserId($id);
  50. $hourlyFlow = UserHourlyDataFlow::userHourly($id);
  51. $dailyFlow = UserDailyDataFlow::userDaily($id);
  52. }
  53. $currentFlow = $currentFlow->where('log_time', '>=', strtotime(date('Y-m-d H:0')))->sum(DB::raw('u + d'));
  54. $hourlyFlow = $hourlyFlow->whereDate('created_at', date('Y-m-d'))->selectRaw('(DATE_FORMAT(user_hourly_data_flow.created_at, "%k")) as date, total')->pluck('total', 'date');
  55. $dailyFlow = $dailyFlow->whereMonth('created_at', date('n'))->selectRaw('(DATE_FORMAT(user_daily_data_flow.created_at, "%e")) as date, total')->pluck('total', 'date');
  56. // 节点一天内的流量
  57. $hourlyData = array_fill(0, date('G') + 1, 0);
  58. foreach ($hourlyFlow as $date => $dataFlow) {
  59. $hourlyData[$date] = round($dataFlow / GB, 3);
  60. }
  61. $hourlyData[date('G') + 1] = round($currentFlow / GB, 3);
  62. // 节点一个月内的流量
  63. $dailyData = array_fill(0, date('j') - 1, 0);
  64. foreach ($dailyFlow as $date => $dataFlow) {
  65. $dailyData[$date - 1] = round($dataFlow / GB, 3);
  66. }
  67. $dailyData[date('j', strtotime(now())) - 1] = round(array_sum($hourlyData) + $currentFlow / GB, 3);
  68. return [
  69. 'trafficDaily' => $dailyData,
  70. 'trafficHourly' => $hourlyData,
  71. 'monthDays' => range(1, date('j')), // 本月天数
  72. 'dayHours' => range(0, date('G') + 1), // 本日小时
  73. ];
  74. }
  75. /*
  76. // 将Base64图片转换为本地图片并保存
  77. public function base64ImageSaver($base64_image_content): ?string
  78. {
  79. // 匹配出图片的格式
  80. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  81. $type = $result[2];
  82. $directory = date('Ymd');
  83. $path = '/assets/images/qrcode/'.$directory.'/';
  84. // 检查是否有该文件夹,如果没有就创建,并给予最高权限
  85. if (! file_exists(public_path($path))
  86. && ! mkdir($concurrentDirectory = public_path($path), 0755, true)
  87. && ! is_dir($concurrentDirectory)) {
  88. throw new RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
  89. }
  90. $fileName = Str::random(18).".{$type}";
  91. if (file_put_contents(public_path($path.$fileName), base64_decode(str_replace($result[1], '', $base64_image_content)))) {
  92. chmod(public_path($path.$fileName), 0744);
  93. return $path.$fileName;
  94. }
  95. }
  96. return '';
  97. }
  98. */
  99. }