Xboard/app/Http/Controllers/V1/User/StatController.php
2023-11-17 14:44:01 +08:00

45 lines
1.3 KiB
PHP

<?php
namespace App\Http\Controllers\V1\User;
use App\Http\Controllers\Controller;
use App\Models\StatUser;
use App\Services\StatisticalService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class StatController extends Controller
{
public function getTrafficLog(Request $request)
{
$records = StatUser::select([
'u',
'd',
'record_at',
'user_id',
'server_rate'
])
->where('user_id', $request->user['id'])
->where('record_at', '>=', strtotime(date('Y-m-1')))
->orderBy('record_at', 'DESC')
->get();
// 追加当天流量
$recordAt = strtotime(date('Y-m-d'));
$statService = new StatisticalService();
$statService->setStartAt($recordAt);
$statService->setUserStats();
$todayTraffics = $statService->getStatUserByUserID($request->user['id']);
if (count($todayTraffics) > 0) {
foreach ($todayTraffics as $todayTraffic){
$todayTraffic['server_rate'] = number_format($todayTraffic['server_rate'], 2);
$records->prepend($todayTraffic);
}
};
return response([
'data' => $records
]);
}
}