love_php/app/Jobs/SendMpRed.php

156 lines
6.5 KiB
PHP
Raw Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Jobs;
use App\Models\BlackIp;
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\RedPacketOrder;
use App\Models\Wechat;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Utils\Messenger;
class SendMpRed implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $user_id;
protected $type;
protected $trade_no;
protected $amount;
protected $send_name;
protected $packet_type;
protected $openid;
protected $from_openid;
protected $id;
protected $ip;
protected $source;
public $tries = 5;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($data)
{
$this->type = $data['type'];
$this->trade_no = $data['trade_no'];
$this->amount = $data['amount'];
$this->send_name = $data['send_name'];
$this->packet_type = $data['packet_type'];
$this->openid = $data['openid'];
$this->from_openid = $data['from_openid'];
$this->id = $data['id'];
$this->ip = $data['ip'];
$this->source = $data['source'];
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// 更新IP黑名单以及请求次数
$blackIp = BlackIp::where('ip', $this->ip)->first();
if(empty($blackIp)){
$receiveCount = RedPacketOrder::where('ip', $this->ip)->count();
if($receiveCount >= 5){
$blackIps = new BlackIp();
$blackIps->type = 1;
$blackIps->ip = $this->ip;
$blackIps->count = $receiveCount;
$blackIps->save();
}
}else{
BlackIp::where('ip', $this->ip)->increment('count');
}
$total_amount = $this->amount * 100;
$order = RedPacketOrder::find($this->id);
//如果已经is_hooked就不再处理
if ($order && $order->is_hooked == 0 && $this->packet_type == 'normal') {
$orderCount = RedPacketOrder::where('official_openid', $this->openid)->where('type', 'MP')->where('trigged', '0')->count();
$oldOrderCount = RedPacketOrder::where('official_openid', $this->openid)->where('type', 'MP')->where('trigged', '1')->count();
$order->trigged = '1';
if ($orderCount > 0 && $oldOrderCount == 0) {
//订单处理上锁
$order_lock = Redis::get('rp_order_lock_'.$order->id);
if($order_lock){
//已锁订单
return false;
}else{
//锁定去处理
Redis::setex('rp_order_lock_'.$order->id,10,1);
}
$result = \WechatService::officialUserTransfer($this->trade_no, $this->openid, $total_amount, $this->send_name);
if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
$order->is_hooked = 1;
$order->err_code = $result['result_code'];
$order->return_msg = $order->send_name;
}
if($order->from_official_openid && $result && strlen($order->from_official_openid) == 28){
//推荐人发送红包
$app = \WechatService::officialApp();
$wechatUser = $app->user->get($this->openid);
$result = !empty($wechatUser)&&isset($wechatUser['subscribe']);
$is_subscribe = $result?$wechatUser['subscribe']:0;
$nickname = isset($wechatUser['nickname']) ? $wechatUser['nickname'] : "已取消关注";
$name = mb_substr($nickname, 0, 10);
$sendName = $name ? "福恋助脱单分享(".$name."" : "福恋助脱单分享";
$trade_no = \CommonUtilsService::getTradeNO();
$array = [3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4];
$key = array_rand($array);
$amount = $array[$key] / 10 + mt_rand(0, 9)/100;
$shareOrder = new RedPacketOrder();
$shareOrder->user_id = Wechat::where('official_openid', $this->from_openid)->value('user_id') ?: 0;
$shareOrder->type = 'SHAREMP';
$shareOrder->trigged = 1;
$shareOrder->official_openid = $this->from_openid;
$shareOrder->from_official_openid = $this->openid;
$shareOrder->trade_no = $trade_no;
$shareOrder->amount = $amount;
$shareOrder->send_name = $sendName;
$shareOrder->ip = $this->ip;
$shareOrder->source = $this->source;
$amount = $amount * 100;
if($is_subscribe){
//如果关注了才发分享红包。
$shareResult = \WechatService::officialUserTransfer($trade_no, $this->from_openid, $amount, $sendName);
if($shareResult['return_code'] == 'SUCCESS' && $shareResult['result_code'] == 'SUCCESS'){
$shareOrder->is_hooked = 1;
$shareOrder->err_code = $shareResult['result_code'];
$shareOrder->return_msg = $shareOrder->send_name;
}
}else{
$shareOrder->is_hooked = 2;
$shareOrder->err_code = 'NOSUB';
}
$shareOrder->save();
//领取红包超过40个发送通知给管理员
$redCount = RedPacketOrder::where('official_openid', $order->from_official_openid)->where('type', 'SHAREMP')->count();
if($redCount > 39 && $redCount < 42){
$content = 'openid为'.$order->from_official_openid.'的用户获取分享红包超过40个';
\CronService::adminNotice($content);
}
}
}else{
$order->err_code = 'REPEATED';
$order->return_msg = '已经领取过红包';
}
$order->save();
}
}
}