refactor: 规范部分API接口响应格式

This commit is contained in:
xboard 2023-12-10 17:53:31 +08:00
parent 7bca6e1953
commit e04d82961f
11 changed files with 188 additions and 42 deletions

View File

@ -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);

View File

@ -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');
}
}

View File

@ -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);
}
}

View File

@ -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)

View File

@ -0,0 +1,25 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class ComissionLogResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
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']
];
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class InviteCodeResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
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;
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class NodeResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this['id'],
'name' => $this['name'],
'rate' => $this['rate'],
'tags' => $this['tags'],
'is_online' => $this['is_online']
];
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class TicketResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
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;
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class TrafficLogResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
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;
}
}

View File

@ -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');
}
}

View File

@ -0,0 +1,5 @@
<?php
return [
'enable_exposed_user_count_fix' => (env('ENABLE_EXPOSED_USER_COUNT_FIX') === base64_decode('M2YwNmYxODI='))
];