AdminController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Components\Helpers;
  4. use App\Models\Country;
  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\Order;
  11. use App\Models\ReferralApply;
  12. use App\Models\ReferralLog;
  13. use App\Models\SsConfig;
  14. use App\Models\User;
  15. use App\Models\UserHourlyDataFlow;
  16. use Auth;
  17. use Hash;
  18. use Illuminate\Http\JsonResponse;
  19. use Illuminate\Http\Request;
  20. use Log;
  21. use PhpOffice\PhpSpreadsheet\Exception;
  22. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  23. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  24. use Redirect;
  25. use Response;
  26. use Str;
  27. /**
  28. * 管理员控制器
  29. *
  30. * Class AdminController
  31. *
  32. * @package App\Http\Controllers
  33. */
  34. class AdminController extends Controller
  35. {
  36. public function index()
  37. {
  38. $past = strtotime("-".sysConfig('expire_days')." days");
  39. $view['expireDays'] = sysConfig('expire_days');
  40. $view['totalUserCount'] = User::count(); // 总用户数
  41. $view['enableUserCount'] = User::whereEnable(1)->count(); // 有效用户数
  42. $view['activeUserCount'] = User::where('t', '>=', $past)->count(); // 活跃用户数
  43. $view['unActiveUserCount'] = User::whereEnable(1)->whereBetween('t', [1, $past,])->count(); // 不活跃用户数
  44. $view['onlineUserCount'] = User::where('t', '>=', strtotime("-10 minutes"))->count(); // 10分钟内在线用户数
  45. $view['expireWarningUserCount'] = User::whereBetween('expired_at',
  46. [date('Y-m-d'), strtotime("+".sysConfig('expire_days')." days")])->count(); // 临近过期用户数
  47. $view['largeTrafficUserCount'] = User::whereRaw('(u + d) >= 107374182400')->where('status', '<>', -1)->count(); // 流量超过100G的用户
  48. $view['flowAbnormalUserCount'] = count($this->trafficAbnormal());// 1小时内流量异常用户
  49. $view['nodeCount'] = Node::count();
  50. $view['unnormalNodeCount'] = Node::whereStatus(0)->count();
  51. $view['flowCount'] = flowAutoShow(NodeDailyDataFlow::where('created_at', '>=', date('Y-m-d', strtotime("-30 days")))->sum('total'));
  52. $view['totalFlowCount'] = flowAutoShow(NodeDailyDataFlow::sum('total'));
  53. $view['totalCredit'] = User::where('credit', '<>', 0)->sum('credit') / 100;
  54. $view['totalWaitRefAmount'] = ReferralLog::whereIn('status', [0, 1])->sum('commission') / 100;
  55. $view['totalRefAmount'] = ReferralApply::whereStatus(2)->sum('amount') / 100;
  56. $view['totalOrder'] = Order::count();
  57. $view['totalOnlinePayOrder'] = Order::where('pay_type', '<>', 0)->count();
  58. $view['totalSuccessOrder'] = Order::whereStatus(2)->count();
  59. $view['todaySuccessOrder'] = Order::whereStatus(2)->whereDate('created_at', date('Y-m-d'))->count();
  60. // 今日
  61. $view['todayRegister'] = User::whereDate('created_at', date('Y-m-d'))->count();
  62. return view('admin.index', $view);
  63. }
  64. // 1小时内流量异常用户
  65. private function trafficAbnormal(): array
  66. {
  67. $userTotalTrafficList = UserHourlyDataFlow::whereNodeId(0)
  68. ->where('total', '>', MB * 50)
  69. ->where('created_at', '>=', date('Y-m-d H:i:s', time() - 3900))
  70. ->groupBy('user_id')
  71. ->selectRaw("user_id, sum(total) as totalTraffic")
  72. ->pluck('totalTraffic', 'user_id')
  73. ->toArray(); // 只统计50M以上的记录,加快速度
  74. foreach ($userTotalTrafficList as $user) {
  75. if ($user->totalTraffic > sysConfig('traffic_ban_value') * GB) {
  76. $result[] = $user->user_id;
  77. }
  78. }
  79. return $result ?? [];
  80. }
  81. // 修改个人资料
  82. public function profile(Request $request)
  83. {
  84. if ($request->isMethod('POST')) {
  85. $new_password = $request->input('new_password');
  86. if (!Hash::check($request->input('old_password'), Auth::getUser()->password)) {
  87. return Redirect::back()->withErrors('旧密码错误,请重新输入');
  88. }
  89. if (Hash::check($new_password, Auth::getUser()->password)) {
  90. return Redirect::back()->withErrors('新密码不可与旧密码一样,请重新输入');
  91. }
  92. $ret = Auth::getUser()->update(['password' => $new_password]);
  93. if (!$ret) {
  94. return Redirect::back()->withErrors('修改失败');
  95. }
  96. return Redirect::back()->with('successMsg', '修改成功');
  97. }
  98. return view('admin.config.profile');
  99. }
  100. // 邀请码列表
  101. public function inviteList(Request $request)
  102. {
  103. $view['inviteList'] = Invite::with(['invitee:id,email', 'inviter:id,email'])
  104. ->orderBy('status')
  105. ->orderByDesc('id')
  106. ->paginate(15)
  107. ->appends($request->except('page'));
  108. return view('admin.inviteList', $view);
  109. }
  110. // 生成邀请码
  111. public function makeInvite(): JsonResponse
  112. {
  113. for ($i = 0; $i < 10; $i++) {
  114. $obj = new Invite();
  115. $obj->inviter_id = 0;
  116. $obj->invitee_id = 0;
  117. $obj->code = strtoupper(substr(md5(microtime().Str::random(6)), 8, 12));
  118. $obj->status = 0;
  119. $obj->dateline = date('Y-m-d H:i:s', strtotime("+".sysConfig('admin_invite_days')." days"));
  120. $obj->save();
  121. }
  122. return Response::json(['status' => 'success', 'message' => '生成成功']);
  123. }
  124. // 导出邀请码
  125. public function exportInvite()
  126. {
  127. $inviteList = Invite::whereStatus(0)->orderBy('id')->get();
  128. $filename = '邀请码'.date('Ymd').'.xlsx';
  129. $spreadsheet = new Spreadsheet();
  130. $spreadsheet->getProperties()
  131. ->setCreator('ProxyPanel')
  132. ->setLastModifiedBy('ProxyPanel')
  133. ->setTitle('邀请码')
  134. ->setSubject('邀请码');
  135. try {
  136. $spreadsheet->setActiveSheetIndex(0);
  137. $sheet = $spreadsheet->getActiveSheet();
  138. $sheet->setTitle('邀请码');
  139. $sheet->fromArray(['邀请码', '有效期'], null);
  140. foreach ($inviteList as $k => $vo) {
  141. $sheet->fromArray([$vo->code, $vo->dateline], null, 'A'.($k + 2));
  142. }
  143. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // 输出07Excel文件
  144. //header('Content-Type:application/vnd.ms-excel'); // 输出Excel03版本文件
  145. header('Content-Disposition: attachment;filename="'.$filename.'"');
  146. header('Cache-Control: max-age=0');
  147. $writer = new Xlsx($spreadsheet);
  148. $writer->save('php://output');
  149. } catch (Exception $e) {
  150. Log::error('导出邀请码时报错:'.$e->getMessage());
  151. }
  152. }
  153. public function config()
  154. {
  155. $view['methodList'] = SsConfig::type(1)->get();
  156. $view['protocolList'] = SsConfig::type(2)->get();
  157. $view['obfsList'] = SsConfig::type(3)->get();
  158. $view['countryList'] = Country::all();
  159. $view['levelList'] = Level::all();
  160. $view['labelList'] = Label::with('nodes')->get();
  161. return view('admin.config.config', $view);
  162. }
  163. public function getPort(): int { return Helpers::getPort(); }
  164. }