diff --git a/app/Http/Controllers/Server/DeepbworkController.php b/app/Http/Controllers/Server/DeepbworkController.php index 1754150d..629baa85 100644 --- a/app/Http/Controllers/Server/DeepbworkController.php +++ b/app/Http/Controllers/Server/DeepbworkController.php @@ -82,9 +82,9 @@ class DeepbworkController extends Controller Cache::put(CacheKey::get('SERVER_V2RAY_LAST_PUSH_AT', $server->id), time(), 3600); $userService = new UserService(); foreach ($data as $item) { - $u = $item['u'] * $server->rate; - $d = $item['d'] * $server->rate; - $userService->trafficFetch($u, $d, $item['user_id'], $server, 'vmess'); + $u = $item['u']; + $d = $item['d']; + $userService->trafficFetch($u, $d, $item['user_id'], $server->toArray(), 'vmess'); } return response([ diff --git a/app/Http/Controllers/Server/ShadowsocksTidalabController.php b/app/Http/Controllers/Server/ShadowsocksTidalabController.php index 1183be8b..01b6cfac 100644 --- a/app/Http/Controllers/Server/ShadowsocksTidalabController.php +++ b/app/Http/Controllers/Server/ShadowsocksTidalabController.php @@ -74,9 +74,9 @@ class ShadowsocksTidalabController extends Controller Cache::put(CacheKey::get('SERVER_SHADOWSOCKS_LAST_PUSH_AT', $server->id), time(), 3600); $userService = new UserService(); foreach ($data as $item) { - $u = $item['u'] * $server->rate; - $d = $item['d'] * $server->rate; - $userService->trafficFetch($u, $d, $item['user_id'], $server, 'shadowsocks'); + $u = $item['u']; + $d = $item['d']; + $userService->trafficFetch($u, $d, $item['user_id'], $server->toArray(), 'shadowsocks'); } return response([ diff --git a/app/Http/Controllers/Server/TrojanTidalabController.php b/app/Http/Controllers/Server/TrojanTidalabController.php index 8209b7fd..b4357eb8 100644 --- a/app/Http/Controllers/Server/TrojanTidalabController.php +++ b/app/Http/Controllers/Server/TrojanTidalabController.php @@ -79,9 +79,9 @@ class TrojanTidalabController extends Controller Cache::put(CacheKey::get('SERVER_TROJAN_LAST_PUSH_AT', $server->id), time(), 3600); $userService = new UserService(); foreach ($data as $item) { - $u = $item['u'] * $server->rate; - $d = $item['d'] * $server->rate; - $userService->trafficFetch($u, $d, $item['user_id'], $server, 'trojan'); + $u = $item['u']; + $d = $item['d']; + $userService->trafficFetch($u, $d, $item['user_id'], $server->toArray(), 'trojan'); } return response([ diff --git a/app/Http/Controllers/Server/VProxyController.php b/app/Http/Controllers/Server/VProxyController.php index 73efb0ad..dd458789 100644 --- a/app/Http/Controllers/Server/VProxyController.php +++ b/app/Http/Controllers/Server/VProxyController.php @@ -86,9 +86,9 @@ class VProxyController extends Controller Cache::put(CacheKey::get('SERVER_' . strtoupper($this->nodeType) . '_LAST_PUSH_AT', $this->nodeInfo->id), time(), 3600); $userService = new UserService(); foreach ($data as $item) { - $u = $item['u'] * $this->nodeInfo->rate; - $d = $item['d'] * $this->nodeInfo->rate; - $userService->trafficFetch($u, $d, $item['user_id'], $this->nodeInfo, $this->nodeType); + $u = $item['u']; + $d = $item['d']; + $userService->trafficFetch($u, $d, $item['user_id'], $this->nodeInfo->toArray(), $this->nodeType); } return response([ diff --git a/app/Jobs/StatUserJob.php b/app/Jobs/StatUserJob.php index 03a9050b..0d1dd5b0 100644 --- a/app/Jobs/StatUserJob.php +++ b/app/Jobs/StatUserJob.php @@ -28,7 +28,7 @@ class StatUserJob implements ShouldQueue * * @return void */ - public function __construct($u, $d, $userId, $server, $protocol, $recordType = 'd') + public function __construct($u, $d, $userId, array $server, $protocol, $recordType = 'd') { $this->onQueue('stat'); $this->u = $u; @@ -52,14 +52,14 @@ class StatUserJob implements ShouldQueue } $data = StatUser::where('record_at', $recordAt) - ->where('server_rate', $this->server->rate) + ->where('server_rate', $this->server['rate']) ->where('user_id', $this->userId) ->first(); if ($data) { try { $data->update([ - 'u' => $data['u'] + $this->u, - 'd' => $data['d'] + $this->d + 'u' => $data['u'] + ($this->u * $this->server['rate']), + 'd' => $data['d'] + ($this->d * $this->server['rate']) ]); } catch (\Exception $e) { abort(500, '用户统计数据更新失败'); @@ -67,7 +67,7 @@ class StatUserJob implements ShouldQueue } else { if (!StatUser::create([ 'user_id' => $this->userId, - 'server_rate' => $this->server->rate, + 'server_rate' => $this->server['rate'], 'u' => $this->u, 'd' => $this->d, 'record_type' => $this->recordType, diff --git a/app/Jobs/TrafficFetchJob.php b/app/Jobs/TrafficFetchJob.php index a82cdc59..71b500a6 100644 --- a/app/Jobs/TrafficFetchJob.php +++ b/app/Jobs/TrafficFetchJob.php @@ -27,7 +27,7 @@ class TrafficFetchJob implements ShouldQueue * * @return void */ - public function __construct($u, $d, $userId, $server, $protocol) + public function __construct($u, $d, $userId, array $server, $protocol) { $this->onQueue('traffic_fetch'); $this->u = $u; @@ -46,10 +46,10 @@ class TrafficFetchJob implements ShouldQueue { $user = User::lockForUpdate()->find($this->userId); if (!$user) return; - + $user->t = time(); - $user->u = $user->u + $this->u; - $user->d = $user->d + $this->d; + $user->u = $user->u + ($this->u * $this->server['rate']); + $user->d = $user->d + ($this->d * $this->server['rate']); if (!$user->save()) throw new \Exception('流量更新失败'); $mailService = new MailService(); $mailService->remindTraffic($user); diff --git a/app/Services/CouponService.php b/app/Services/CouponService.php index 2284cfb5..4d4e9dfd 100644 --- a/app/Services/CouponService.php +++ b/app/Services/CouponService.php @@ -15,7 +15,9 @@ class CouponService public function __construct($code) { - $this->coupon = Coupon::where('code', $code)->first(); + $this->coupon = Coupon::where('code', $code) + ->lockForUpdate() + ->first(); } public function use(Order $order):bool @@ -37,6 +39,7 @@ class CouponService } if ($this->coupon->limit_use !== NULL) { $this->coupon->limit_use = $this->coupon->limit_use - 1; + if ($this->coupon->limit_use <= 0) return false; if (!$this->coupon->save()) { return false; } diff --git a/app/Services/UserService.php b/app/Services/UserService.php index a6c402a9..b6008594 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -130,7 +130,7 @@ class UserService return true; } - public function trafficFetch(int $u, int $d, int $userId, object $server, string $protocol) + public function trafficFetch(int $u, int $d, int $userId, array $server, string $protocol) { TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol); StatServerJob::dispatch($u, $d, $server, $protocol, 'd');