AdminController.php 7.0 KB

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