123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\Country;
- use App\Models\GoodsCategory;
- use App\Models\Invite;
- use App\Models\Label;
- use App\Models\Level;
- use App\Models\Node;
- use App\Models\NodeDailyDataFlow;
- use App\Models\NodeHourlyDataFlow;
- use App\Models\Order;
- use App\Models\ReferralApply;
- use App\Models\ReferralLog;
- use App\Models\SsConfig;
- use App\Models\User;
- use App\Models\UserHourlyDataFlow;
- use Cache;
- use DB;
- use Illuminate\Http\JsonResponse;
- use Log;
- use PhpOffice\PhpSpreadsheet\Exception;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
- use Response;
- use Str;
- class AdminController extends Controller
- {
- public function index()
- {
- $past = strtotime('-'.sysConfig('expire_days').' days');
- $today = today();
- $stats = Cache::remember('user_stats', now()->addMinutes(5), function () use ($today, $past) {
- $dailyTrafficUsage = NodeHourlyDataFlow::whereDate('created_at', $today)->sum(DB::raw('u + d'));
- return [
- 'activeUserCount' => User::where('t', '>=', $past)->count(), // 活跃用户数
- 'inactiveUserCount' => User::whereEnable(1)->where('t', '<', $past)->count(), // 不活跃用户数
- 'expireWarningUserCount' => User::whereBetween('expired_at', [$today, today()->addDays(sysConfig('expire_days'))])->count(), // 临近过期用户数
- 'largeTrafficUserCount' => User::whereRaw('(u + d)/transfer_enable >= 0.9')->where('status', '<>', -1)->count(), // 流量使用超过90%的用户
- 'flowAbnormalUserCount' => count((new UserHourlyDataFlow)->trafficAbnormal()), // 1小时内流量异常用户
- 'monthlyTrafficUsage' => formatBytes(NodeDailyDataFlow::whereNull('node_id')->whereMonth('created_at', now()->month)->sum(DB::raw('u + d'))),
- 'dailyTrafficUsage' => $dailyTrafficUsage ? formatBytes($dailyTrafficUsage) : 0,
- 'totalTrafficUsage' => formatBytes(NodeDailyDataFlow::whereNull('node_id')->where('created_at', '>=', now()->subDays(30))->sum(DB::raw('u + d'))),
- ];
- });
- return view('admin.index', [
- 'totalUserCount' => User::count(), // 总用户数
- 'todayRegister' => User::whereDate('created_at', $today)->count(), // 今日注册用户
- 'enableUserCount' => User::whereEnable(1)->count(), // 有效用户数
- 'activeUserCount' => $stats['activeUserCount'],
- 'payingUserCount' => User::has('paidOrders')->count(), // 付费用户数
- 'payingNewUserCount' => User::whereDate('created_at', $today)->has('paidOrders')->count(), // 不活跃用户数
- 'inactiveUserCount' => $stats['inactiveUserCount'],
- 'onlineUserCount' => User::where('t', '>=', strtotime('-10 minutes'))->count(), // 10分钟内在线用户数,
- 'expireWarningUserCount' => $stats['expireWarningUserCount'],
- 'largeTrafficUserCount' => $stats['largeTrafficUserCount'],
- 'flowAbnormalUserCount' => $stats['flowAbnormalUserCount'],
- 'nodeCount' => Node::count(),
- 'abnormalNodeCount' => Node::whereStatus(0)->count(),
- 'monthlyTrafficUsage' => $stats['monthlyTrafficUsage'],
- 'dailyTrafficUsage' => $stats['dailyTrafficUsage'],
- 'totalTrafficUsage' => $stats['totalTrafficUsage'],
- 'totalCredit' => User::where('credit', '<>', 0)->sum('credit') / 100,
- 'totalWaitRefAmount' => ReferralLog::whereIn('status', [0, 1])->sum('commission') / 100,
- 'todayWaitRefAmount' => ReferralLog::whereIn('status', [0, 1])->whereDate('created_at', $today)->sum('commission') / 100,
- 'totalRefAmount' => ReferralApply::whereStatus(2)->sum('amount') / 100,
- 'totalOrder' => Order::count(),
- 'todayOrder' => Order::whereDate('created_at', $today)->count(),
- 'totalOnlinePayOrder' => Order::where('pay_type', '<>', 0)->count(),
- 'todayOnlinePayOrder' => Order::where('pay_type', '<>', 0)->whereDate('created_at', $today)->count(),
- 'totalSuccessOrder' => Order::whereIn('status', [2, 3])->count(),
- 'todaySuccessOrder' => Order::whereIn('status', [2, 3])->whereDate('created_at', $today)->count(),
- ]);
- }
- // 邀请码列表
- public function inviteList()
- {
- return view('admin.aff.invite', [
- 'inviteList' => Invite::with(['invitee:id,username', 'inviter:id,username'])->orderBy('status')->orderByDesc('id')->paginate(15)->appends(request('page')),
- ]);
- }
- // 生成邀请码
- public function makeInvite(): JsonResponse
- {
- for ($i = 0; $i < 10; $i++) {
- $obj = new Invite;
- $obj->code = strtoupper(substr(md5(microtime().Str::random(6)), 8, 12));
- $obj->dateline = date('Y-m-d H:i:s', strtotime(sysConfig('admin_invite_days').' days'));
- $obj->save();
- }
- return Response::json(['status' => 'success', 'message' => trans('common.success_item', ['attribute' => trans('common.generate')])]);
- }
- // 导出邀请码
- public function exportInvite(): void
- {
- $inviteList = Invite::whereStatus(0)->orderBy('id')->get();
- $filename = trans('user.invite.attribute').'_'.date('Ymd').'.xlsx';
- $spreadsheet = new Spreadsheet;
- $spreadsheet->getProperties()->setCreator('ProxyPanel')->setLastModifiedBy('ProxyPanel')->setTitle(trans('user.invite.attribute'))->setSubject(trans('user.invite.attribute'));
- $spreadsheet->setActiveSheetIndex(0);
- $sheet = $spreadsheet->getActiveSheet();
- $sheet->setTitle(trans('user.invite.attribute'));
- $sheet->fromArray([trans('user.invite.attribute'), trans('common.available_date')]);
- foreach ($inviteList as $k => $vo) {
- $sheet->fromArray([$vo->code, $vo->dateline], null, 'A'.($k + 2));
- }
- header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // 输出07Excel文件
- //header('Content-Type:application/vnd.ms-excel'); // 输出Excel03版本文件
- header('Content-Disposition: attachment;filename="'.$filename.'"');
- header('Cache-Control: max-age=0');
- try {
- $writer = new Xlsx($spreadsheet);
- $writer->save('php://output');
- } catch (Exception $e) {
- Log::error(trans('common.error_action_item', ['action' => trans('common.export'), 'attribute' => trans('user.invite.attribute')]).': '.$e->getMessage());
- }
- }
- public function config()
- {
- return view('admin.config.common', [
- 'methods' => SsConfig::type(1)->get(),
- 'protocols' => SsConfig::type(2)->get(),
- 'categories' => GoodsCategory::all(),
- 'obfsList' => SsConfig::type(3)->get(),
- 'countries' => Country::all(),
- 'levels' => Level::all(),
- 'labels' => Label::with('nodes')->get(),
- ]);
- }
- }
|