improve paid torrent + hit and run

This commit is contained in:
xiaomlove
2025-06-17 20:54:18 +07:00
parent ee4757cdc9
commit 3db4537153
21 changed files with 326 additions and 70 deletions
+8 -6
View File
@@ -34,6 +34,7 @@ class BuyTorrent implements ShouldQueue
* Execute the job.
*
* @return void
* @throws \Throwable
*/
public function handle()
{
@@ -42,27 +43,28 @@ class BuyTorrent implements ShouldQueue
$userId = $this->userId;
$torrentId = $this->torrentId;
$hasBuy = TorrentBuyLog::query()
$buyLog = TorrentBuyLog::query()
->where("uid", $userId)
->where("torrent_id", $torrentId)
->exists()
->first();
;
if ($hasBuy) {
if ($buyLog) {
//标记购买成功
do_log("$logPrefix, already bought");
$torrentRep->addBuySuccessCache($userId, $torrentId);
$torrentRep->addBuySuccessCache($userId, $torrentId, $buyLog->id);
return;
}
try {
$bonusRep = new BonusRepository();
$bonusRep->consumeToBuyTorrent($this->userId, $this->torrentId);
$buyLog = $bonusRep->consumeToBuyTorrent($this->userId, $this->torrentId);
//标记购买成功
do_log("$logPrefix, buy torrent success");
$torrentRep->addBuySuccessCache($userId, $torrentId);
$torrentRep->addBuySuccessCache($userId, $torrentId, $buyLog->id);
} catch (\Throwable $throwable) {
//标记购买失败,缓存 3600 秒,这个时间内不能再次购买
do_log("$logPrefix, buy torrent fail: " . $throwable->getMessage(), "error");
$torrentRep->addBuyFailCache($userId, $torrentId);
throw $throwable;
}
}
}
+30
View File
@@ -0,0 +1,30 @@
<?php
namespace App\Jobs;
use App\Repositories\UserRepository;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
class UpdateUserDownloadPrivilege implements ShouldQueue
{
use Queueable;
/**
* Create a new job instance.
*/
public function __construct(public int $userId, public string $status, public string $reasonKey)
{
//
}
/**
* Execute the job.
*/
public function handle(): void
{
$rep = new UserRepository();
$rep->updateDownloadPrivileges(null, $this->userId, $this->status, $this->reasonKey);
do_log("Updating user download privilege for user {$this->userId} to {$this->status} by reason {$this->reasonKey}");
}
}