love_php/app/Jobs/ReferreAward.php

214 lines
7.1 KiB
PHP
Raw 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\Wechat;
use App\Models\Referre;
use App\Models\ReferreAwardHistory;
use App\Services\WechatService;
use App\Models\Message;
use App\Models\Score;
use App\Models\ScoreHistory;
use App\Models\User;
use App\Repositories\Eloquent\SmsRepository as Sms;
use App\Jobs\SyncScore;
use App\Utils\Messenger;
class ReferreAward implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 1;
protected $other_user_id;
protected $from_openid;
protected $type;
protected $num;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($array)
{
$this->other_user_id = $array['user_id'];
$this->from_openid = $array['from_openid'];
$this->type = $array['type'];
$this->num = isset($array['num']) ? $array['num'] : 1;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//是否是推荐人
$user_id = Wechat::where('openid', $this->from_openid)->value('user_id');
$referre = Referre::where("user_id", $user_id)->first();
//获取当前渠道对应的类型
$scoreStatusArr = config('score.status');
$scoreStatus = '';
foreach ($scoreStatusArr as $k => $item) {
if(in_array($this->type, $item)){
$scoreStatus = array_search($this->type, $item);
continue;
}
}
if (empty($referre)) {//不是推荐人
//短信通知、
$user = User::find($user_id);
$other_user = User::find($this->other_user_id);
if (empty($user) || empty($other_user)) {
return ;
}
switch ($this->type){
case 'register':
$ms = $user->name.'您好,您的好友'.$other_user->name.'应邀加入福恋,您成功获得'.$this->num.'个福分的奖励,感谢您的参与!';
break;
case 'approve':
$ms = $user->name.'您好,您的好友'.$other_user->name.'已完成实名认证,您成功获得'.$this->num.'个福分的奖励,感谢您的参与!';
break;
case 'rank':
$ms = $user->name.'您好,您的好友'.$other_user->name.'已购买vip您成功获得'.$this->num.'个福分的奖励,感谢您的参与!';
break;
case 'love':
$ms = $user->name.'您好,您的好友'.$other_user->name.'已购买脱单服务,您成功获得'.$this->num.'个福分的奖励,感谢您的参与!';
break;
}
$message = Message::where('phone', $user->mobile)->where('message', $ms)->first();
if (empty($message)) {
if ($user->mobile != '18898888797') {
$this->sentMessage($user->mobile, $ms);
}
}
//赠送一福分
$this->scoreRecharge($user_id, $this->num, $scoreStatus);
return ;
}
//创建奖励记录
$history = new ReferreAwardHistory();
$history->user_id = $user_id;
$history->other_user_id = $this->other_user_id;
$history->type = $this->type;
if ($this->type === 'register') {//注册
$history->amount = $referre->register_amount;
$amount = $referre->register_amount * 100;
$desc = '推荐注册用户奖励(福恋小程序)';
$referre->increment('num', $this->num);
//转账
$trade_no = $this->getTradeNO();
$history->trade_no = $trade_no;
$history->save();
$result = WechatService::userTransfer($trade_no, $this->from_openid, $amount, $desc);
}elseif ($this->type === 'approve') {
$history->amount = $referre->approve_amount;
$amount = $referre->approve_amount * 100;
$desc = '推荐认证用户奖励(福恋小程序)';
// return;
//转账
$trade_no = $this->getTradeNO();
$history->trade_no = $trade_no;
$history->save();
$result = WechatService::userTransfer($trade_no, $this->from_openid, $amount, $desc);
}
if (empty($result)) {
$history->is_hooked = -1;
$history->save();
}
}
public function getTradeNO()
{
$dateline = time();
$mix_1 = rand(100, 999);
$mix_2 = rand(100, 999);
return $dateline . $mix_1 . $mix_2;
}
/**
* 添加积分
* @param Request $request [description]
* @param int $user_id 用户id
* @return [type] [description]
*/
public function scoreRecharge($user_id, $score, $scoreStatus = '')
{
$scoreObj = Score::where('user_id', $user_id)->first();
if (empty($scoreObj)) {
$scoreObj = Score::create([
'user_id'=>$user_id,
]);
}
if (empty($score)) {
return ;
}
$status = 0;
if(is_numeric($scoreStatus)){
$scoreObj->cash_amount = $scoreObj->cash_amount + $score;
$status = 1;
}
//2020/11/12 11:10开始不送福分
// $scoreObj->remain_amount = $scoreObj->remain_amount + $score;
// $scoreObj->save();
switch ($this->type){
case 'register':
$msgHeader = '注册';
break;
case 'approve':
$msgHeader = '推荐人认证';
break;
case 'rank':
$msgHeader = '推荐人购买VIP';
break;
case 'love':
$msgHeader = '推荐人购买脱单服务';
break;
}
ScoreHistory::create([
'user_id'=>$user_id,
'other_user_id' => $this->other_user_id,
'type'=>'gained',
'amount'=>$score,
'value'=>$scoreObj->remain_amount,
'message'=>$msgHeader.'赠送福分',
'status'=>$status,
'source'=>$scoreStatus,
]);
$array['message'] = $msgHeader.'赠送福分';
$array['user_id'] = $user_id;
$array['type'] = 'gained';
$array['score'] = $score;
$array['other_user_id'] = $this->other_user_id;
$array['status'] = 1;
SyncScore::dispatch($array)->onQueue('love');
return;
}
//发送普通消息
function sentMessage($mobile, $message){
if (empty($mobile)) {
return true;
}
Message::create([
'phone'=>$mobile,
'message'=>$message,
'confirmed' => 1,
'ip' => request()?request()->ip():'127.0.0.1',
]);
Messenger::sendSMS($mobile, $message);
return true;
}
}