paid torrent

This commit is contained in:
xiaomlove
2023-02-11 16:08:48 +08:00
parent 43b35b4e77
commit 3c64e76011
50 changed files with 556 additions and 87 deletions
+53
View File
@@ -8,6 +8,8 @@ use App\Models\Invite;
use App\Models\Medal;
use App\Models\Message;
use App\Models\Setting;
use App\Models\Torrent;
use App\Models\TorrentBuyLog;
use App\Models\User;
use App\Models\UserMedal;
use App\Models\UserMeta;
@@ -250,6 +252,57 @@ class BonusRepository extends BaseRepository
}
public function consumeToBuyTorrent($uid, $torrentId, $channel = 'Web'): bool
{
$user = User::query()->findOrFail($uid);
$torrent = Torrent::query()->findOrFail($torrentId, Torrent::$commentFields);
$requireBonus = $torrent->price;
NexusDB::transaction(function () use ($user, $requireBonus, $torrent, $channel) {
$comment = nexus_trans('bonus.comment_buy_torrent', [
'bonus' => $requireBonus,
'torrent_id' => $torrent->id,
], $user->locale);
do_log("comment: $comment");
$this->consumeUserBonus($user, $requireBonus, BonusLogs::BUSINESS_TYPE_BUY_TORRENT, $comment);
TorrentBuyLog::query()->create([
'uid' => $user->id,
'torrent_id' => $torrent->id,
'price' => $requireBonus,
'channel' => $channel,
]);
//increment owner bonus
$taxFactor = Setting::get('torrent.tax_factor');
if (!is_numeric($taxFactor) || $taxFactor < 0 || $taxFactor > 1) {
throw new \RuntimeException("Invalid tax_factor: $taxFactor");
}
$increaseBonus = $requireBonus * (1 - $taxFactor);
$owner = $torrent->user;
if ($owner->id) {
$nowStr = now()->toDateTimeString();
$businessType = BonusLogs::BUSINESS_TYPE_TORRENT_BE_DOWNLOADED;
$owner->increment('seedbonus', $increaseBonus);
$comment = nexus_trans('bonus.comment_torrent_be_downloaded', [
'username' => $user->username,
'uid' => $user->id,
], $owner->locale);
$bonusLog = [
'business_type' => $businessType,
'uid' => $owner->id,
'old_total_value' => $owner->seedbonus,
'value' => $increaseBonus,
'new_total_value' => bcadd($owner->seedbonus, $increaseBonus),
'comment' => sprintf('[%s] %s', BonusLogs::$businessTypes[$businessType]['text'], $comment),
'created_at' => $nowStr,
'updated_at' => $nowStr,
];
BonusLogs::query()->insert($bonusLog);
}
});
return true;
}
public function consumeUserBonus($user, $requireBonus, $logBusinessType, $logComment = '', array $userUpdates = [])
{
if (!isset(BonusLogs::$businessTypes[$logBusinessType])) {
+8
View File
@@ -711,4 +711,12 @@ HTML;
}
}
public function getPaidIcon(array $torrentInfo, $size = 16, $verticalAlign = 'sub')
{
if (!isset($torrentInfo['price']) || $torrentInfo['price'] <= 0) {
return '';
}
return sprintf('<span title="%s" style="vertical-align: %s"><svg t="1676058062789" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3406" width="%s" height="%s"><path d="M554.666667 810.666667v42.666666h-85.333334v-42.666666c-93.866667 0-170.666667-76.8-170.666666-170.666667h85.333333c0 46.933333 38.4 85.333333 85.333333 85.333333v-170.666666c-93.866667 0-170.666667-76.8-170.666666-170.666667s76.8-170.666667 170.666666-170.666667V170.666667h85.333334v42.666666c93.866667 0 170.666667 76.8 170.666666 170.666667h-85.333333c0-46.933333-38.4-85.333333-85.333333-85.333333v170.666666h17.066666c29.866667 0 68.266667 17.066667 98.133334 42.666667 34.133333 29.866667 59.733333 76.8 59.733333 128-4.266667 93.866667-81.066667 170.666667-174.933333 170.666667z m0-85.333334c46.933333 0 85.333333-38.4 85.333333-85.333333s-38.4-85.333333-85.333333-85.333333v170.666666zM469.333333 298.666667c-46.933333 0-85.333333 38.4-85.333333 85.333333s38.4 85.333333 85.333333 85.333333V298.666667z" fill="#CD7F32" p-id="3407"></path></svg></span>', nexus_trans('torrent.paid_torrent'), $verticalAlign, $size, $size);
}
}