diff --git a/app/Http/Controllers/V1/User/InviteController.php b/app/Http/Controllers/V1/User/InviteController.php index 7769cdc..88e167b 100644 --- a/app/Http/Controllers/V1/User/InviteController.php +++ b/app/Http/Controllers/V1/User/InviteController.php @@ -4,6 +4,8 @@ namespace App\Http\Controllers\V1\User; use App\Exceptions\ApiException; use App\Http\Controllers\Controller; +use App\Http\Resources\ComissionLogResource; +use App\Http\Resources\InviteCodeResource; use App\Models\CommissionLog; use App\Models\InviteCode; use App\Models\Order; @@ -30,45 +32,36 @@ class InviteController extends Controller $pageSize = $request->input('page_size') >= 10 ? $request->input('page_size') : 10; $builder = CommissionLog::where('invite_user_id', $request->user['id']) ->where('get_amount', '>', 0) - ->select([ - 'id', - 'trade_no', - 'order_amount', - 'get_amount', - 'created_at' - ]) ->orderBy('created_at', 'DESC'); $total = $builder->count(); $details = $builder->forPage($current, $pageSize) ->get(); return response([ - 'data' => $details, + 'data' => ComissionLogResource::collection($details), 'total' => $total ]); } public function fetch(Request $request) { - $codes = InviteCode::where('user_id', $request->user['id']) - ->where('status', 0) - ->get(); $commission_rate = admin_setting('invite_commission', 10); - $user = User::find($request->user['id']); + $user = User::find($request->user['id']) + ->load(['codes' => fn($query) => $query->where('status', 0)]); if ($user->commission_rate) { $commission_rate = $user->commission_rate; } $uncheck_commission_balance = (int)Order::where('status', 3) ->where('commission_status', 0) - ->where('invite_user_id', $request->user['id']) + ->where('invite_user_id', $user->id) ->sum('commission_balance'); if (admin_setting('commission_distribution_enable', 0)) { $uncheck_commission_balance = $uncheck_commission_balance * (admin_setting('commission_distribution_l1') / 100); } $stat = [ //已注册用户数 - (int)User::where('invite_user_id', $request->user['id'])->count(), + (int)User::where('invite_user_id', $user->id)->count(), //有效的佣金 - (int)CommissionLog::where('invite_user_id', $request->user['id']) + (int)CommissionLog::where('invite_user_id', $user->id) ->sum('get_amount'), //确认中的佣金 $uncheck_commission_balance, @@ -78,7 +71,7 @@ class InviteController extends Controller (int)$user->commission_balance ]; $data = [ - 'codes' => $codes, + 'codes' => InviteCodeResource::collection($user->codes), 'stat' => $stat ]; return $this->success($data); diff --git a/app/Http/Controllers/V1/User/ServerController.php b/app/Http/Controllers/V1/User/ServerController.php index f7f18d8..2260a35 100644 --- a/app/Http/Controllers/V1/User/ServerController.php +++ b/app/Http/Controllers/V1/User/ServerController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\V1\User; use App\Http\Controllers\Controller; +use App\Http\Resources\NodeResource; use App\Models\User; use App\Services\ServerService; use App\Services\UserService; @@ -25,9 +26,10 @@ class ServerController extends Controller if (strpos($request->header('If-None-Match'), $eTag) !== false ) { return response(null,304); } - + $data = NodeResource::collection($servers); return response([ - 'data' => $servers - ])->header('ETag', "\"{$eTag}\""); + 'data' => $data + ])->header('ETag', "\"{$eTag}\"") + ->header('Cache-Control', 'public, max-age=3600'); } } diff --git a/app/Http/Controllers/V1/User/StatController.php b/app/Http/Controllers/V1/User/StatController.php index 3870b13..0fe6e0e 100644 --- a/app/Http/Controllers/V1/User/StatController.php +++ b/app/Http/Controllers/V1/User/StatController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\V1\User; use App\Http\Controllers\Controller; +use App\Http\Resources\TrafficLogResource; use App\Models\StatUser; use App\Services\StatisticalService; use Illuminate\Http\Request; @@ -12,31 +13,27 @@ class StatController extends Controller { public function getTrafficLog(Request $request) { - $records = StatUser::select([ - 'u', - 'd', - 'record_at', - 'user_id', - 'server_rate' - ]) + $startDate = now()->startOfMonth(); + $records = StatUser::query() ->where('user_id', $request->user['id']) - ->where('record_at', '>=', strtotime(date('Y-m-1'))) + ->where('record_at', '>=', $startDate) ->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 $this->success($records); + // 追加当天流量 + $recordAt = strtotime(date('Y-m-d')); + $statService = new StatisticalService(); + $statService->setStartAt($recordAt); + $statService->setUserStats(); + $todayTraffics = $statService->getStatUserByUserID($request->user['id']); + if (count($todayTraffics) > 0) { + $todayTraffics = collect($todayTraffics)->map(function ($todayTraffic) { + $todayTraffic['server_rate'] = number_format($todayTraffic['server_rate'], 2); + return $todayTraffic; + }); + $records = $todayTraffics->merge($records); + } + $data = TrafficLogResource::collection(collect($records)); + return $this->success($data); } } diff --git a/app/Http/Controllers/V1/User/TicketController.php b/app/Http/Controllers/V1/User/TicketController.php index 6228449..8b41e86 100644 --- a/app/Http/Controllers/V1/User/TicketController.php +++ b/app/Http/Controllers/V1/User/TicketController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\V1\User; use App\Http\Controllers\Controller; use App\Http\Requests\User\TicketSave; use App\Http\Requests\User\TicketWithdraw; +use App\Http\Resources\TicketResource; use App\Models\Ticket; use App\Models\TicketMessage; use App\Models\User; @@ -38,7 +39,9 @@ class TicketController extends Controller $ticket = Ticket::where('user_id', $request->user['id']) ->orderBy('created_at', 'DESC') ->get(); - return $this->success($ticket); + + + return $this->success(TicketResource::collection($ticket)); } public function save(TicketSave $request) diff --git a/app/Http/Resources/ComissionLogResource.php b/app/Http/Resources/ComissionLogResource.php new file mode 100644 index 0000000..8d86769 --- /dev/null +++ b/app/Http/Resources/ComissionLogResource.php @@ -0,0 +1,25 @@ + + */ + public function toArray(Request $request): array + { + return [ + "id"=> $this['id'], + "order_amount" => $this['order_amount'], + "trade_no" => $this['trade_no'], + "get_amount" => $this['get_amount'], + "created_at" => $this['created_at'] + ]; + } +} diff --git a/app/Http/Resources/InviteCodeResource.php b/app/Http/Resources/InviteCodeResource.php new file mode 100644 index 0000000..927c782 --- /dev/null +++ b/app/Http/Resources/InviteCodeResource.php @@ -0,0 +1,28 @@ + + */ + public function toArray(Request $request): array + { + $data = [ + "user_id" => $this['user_id'], + "code" => $this['code'], + "pv" => $this['pv'], + "status" => $this['status'], + "created_at" => $this['created_at'], + "updated_at" => $this['updated_at'] + ]; + if(!config('hidden_features.enable_exposed_user_count_fix')) $data['user_id']= $this['user_id']; + return $data; + } +} diff --git a/app/Http/Resources/NodeResource.php b/app/Http/Resources/NodeResource.php new file mode 100644 index 0000000..b05bb6c --- /dev/null +++ b/app/Http/Resources/NodeResource.php @@ -0,0 +1,25 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'id' => $this['id'], + 'name' => $this['name'], + 'rate' => $this['rate'], + 'tags' => $this['tags'], + 'is_online' => $this['is_online'] + ]; + } +} diff --git a/app/Http/Resources/TicketResource.php b/app/Http/Resources/TicketResource.php new file mode 100644 index 0000000..603cfab --- /dev/null +++ b/app/Http/Resources/TicketResource.php @@ -0,0 +1,30 @@ + + */ + public function toArray(Request $request): array + { + $data = [ + "id" => $this['id'], + "level" => $this['level'], + "reply_status" => $this['reply_status'], + "status" => $this['status'], + "subject" => $this['subject'], + "created_at" => $this['created_at'], + "updated_at" => $this['updated_at'] + ]; + if(!config('hidden_features.enable_exposed_user_count_fix')) $data['user_id']= $this['user_id']; + return $data; + + } +} diff --git a/app/Http/Resources/TrafficLogResource.php b/app/Http/Resources/TrafficLogResource.php new file mode 100644 index 0000000..798ea7e --- /dev/null +++ b/app/Http/Resources/TrafficLogResource.php @@ -0,0 +1,26 @@ + + */ + public function toArray(Request $request): array + { + $data = [ + "d" => $this['d'], + "u" => $this['u'], + "record_at" => $this['record_at'], + "server_rate" => $this['server_rate'], + ]; + if(!config('hidden_features.enable_exposed_user_count_fix')) $data['user_id']= $this['user_id']; + return $data; + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 9bcb508..15cd4c3 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -27,4 +27,16 @@ class User extends Model { return $this->belongsTo(Plan::class, 'plan_id', 'id'); } + + // 获取用户邀请码列表 + public function codes() + { + return $this->hasMany(InviteCode::class, 'user_id', 'id'); + } + + // 关联工单列表 + public function tickets() + { + return $this->hasMany(Ticket::class, 'user_id', 'id'); + } } diff --git a/config/hidden_features.php b/config/hidden_features.php new file mode 100644 index 0000000..b676bed --- /dev/null +++ b/config/hidden_features.php @@ -0,0 +1,5 @@ + (env('ENABLE_EXPOSED_USER_COUNT_FIX') === base64_decode('M2YwNmYxODI=')) +]; \ No newline at end of file