love_php/app/Jobs/SendSignCoinV2.php

91 lines
2.7 KiB
PHP
Raw Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Jobs;
use App\Models\User;
use App\Models\SignLog;
use App\Models\Coin;
use App\Models\CoinLog;
use App\Models\RankHistory;
use App\Http\Response\ResponseJson;
use App\Models\UserSign;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use function GuzzleHttp\default_ca_bundle;
class SendSignCoinV2 implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use ResponseJson;
public $tries = 1;
protected $user_id;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($user_id)
{
$this->user_id = $user_id;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
try {
$user = User::find($this->user_id);
//连续签到天数
$sign_days = UserSign::where('user_id', $user->id)->value('sign_days') ?: 0;
if($sign_days == 0) return;
//是否新用户
$logs = $user->signLogs()->signType(SignLog::NEWYEARTYPE)->order('sign_date')->pluck('sign_date')->toArray();
$is_new = 0;
if (count($logs) < 4 && $user->created_at > '2020-12-31 23:59:59' && $sign_days == count($logs)) {
$is_new = 1;
}
//\Log::info('签到天数---'.$sign_days);
//新用户
if ($is_new) {
$new_coin = config('sign.new');
$this->sendCoin($user, $new_coin[$sign_days], $sign_days, 1);
} else {
$old_coin = config('sign.old');
$this->sendCoin($user, $old_coin[$sign_days], $sign_days);
//赠送会员
$rank = config('sign.rank');
if (!empty($rank[$sign_days])) {
$user->addSuperRank($rank[$sign_days], $month = 0, $type = RankHistory::SIGNNEWYEAR);
}
}
}catch (\Exception $e) {
$this->getError($e);
}
}
public function sendCoin($user, $coin, $count,$is_new=0)
{
try {
$remark=$is_new ? '新用户连续'.$count.'天签到' : '连续'.$count.'天签到';
\DB::beginTransaction();
//增加福币记录
$user->addCoinLog(CoinLog::RECSYSTEM, 0, $coin, $remark);
//修改福币钱包
$user->updateCoinInfo('add', $coin, 'other');
\DB::commit();
return true;
} catch (\Exception $e) {
\DB::rollback();
$this->getError($e);
return false;
}
}
}