love_php/app/Jobs/ParticipantPickUpRedPacket.php

106 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Models\Participant;
use App\Models\RedPacketOrder;
use Illuminate\Support\Facades\Cache;
use App\Repositories\Eloquent\SmsRepository as Sms;
use Illuminate\Container\Container;
class ParticipantPickUpRedPacket implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 1;
protected $participant;
protected $mobile;
protected $amount;
protected $sms;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($array)
{
$this->mobile = $array['mobile'];
$this->participant = $array['participant'];
$this->amount = $array['amount'];
$container = new Container();
$this->sms = new Sms($container);
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$amount = $this->amount;
$mobile = $this->mobile;
$participant = $this->participant;
$trade_no = \CommonUtilsService::getTradeNO();
$red_packet_data = [
'official_openid'=>$participant->official_openid,
'type'=>'FRUIT',
'trade_no'=>$trade_no,
'ip'=>$participant->ip,
'amount'=>$amount,
'trigged' => 0,
'created_at'=>date('Y-m-d H:i:s'),
'updated_at'=>date('Y-m-d H:i:s'),
];
try {
$this->pickUpPacket($mobile, $participant, $red_packet_data);
} catch (Exception $e) {
//TODO通知管理员
$message = $e->getMessage();
$this->sms->sentMessage($mobile, $message);
}
}
/**
* 领取红包
*/
public function pickUpPacket($mobile, $participant, $red_packet_data)
{
$key = Participant::PART_KEY;
$key = $key.$participant->official_openid;
$p = Participant::where('mobile', $mobile)->first();
if ($p) {
//通知用户领取红包失败
return ;
}
try {
// \DB::beginTransaction();
//添加一次果实并且减少一次机会
$participant->is_pick_up = 1;
$participant->mobile = $mobile;
$participant->save();
//更新緩存
Cache::forever($key, $participant);
//添加红包记录
$order = RedPacketOrder::where([
'official_openid'=>$red_packet_data['official_openid'],
'type'=>'FRUIT'
])->first();
// \DB::commit();
if ($order) {//已经过红包
return;
}
} catch (Exception $e) {
// \DB::rollBack();
}
// $result = \WechatService::officialUserTransfer($red_packet_data['trade_no'], $red_packet_data['official_openid'], $red_packet_data['amount']*100, "第三个果子的红包");
RedPacketOrder::insert($red_packet_data);
}
}