158 lines
4.8 KiB
PHP
158 lines
4.8 KiB
PHP
|
|
<?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 OfficialReferreAward implements ShouldQueue
|
|||
|
|
{
|
|||
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|||
|
|
|
|||
|
|
|
|||
|
|
public $tries = 1;
|
|||
|
|
protected $other_user_id;
|
|||
|
|
protected $from_user_id;
|
|||
|
|
protected $type;
|
|||
|
|
/**
|
|||
|
|
* Create a new job instance.
|
|||
|
|
*
|
|||
|
|
* @return void
|
|||
|
|
*/
|
|||
|
|
public function __construct($array)
|
|||
|
|
{
|
|||
|
|
$this->other_user_id = $array['user_id'];
|
|||
|
|
$this->from_user_id = $array['from_user_id'];
|
|||
|
|
$this->type = $array['type'];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Execute the job.
|
|||
|
|
*
|
|||
|
|
* @return void
|
|||
|
|
*/
|
|||
|
|
public function handle()
|
|||
|
|
{
|
|||
|
|
//是否是推荐人
|
|||
|
|
$user_id = $this->from_user_id;
|
|||
|
|
$referre = Referre::where("user_id", $user_id)->first();
|
|||
|
|
if (empty($referre)) {//不是推荐人
|
|||
|
|
//短信通知、
|
|||
|
|
$user = User::find($user_id);
|
|||
|
|
$other_user = User::find($this->other_user_id);
|
|||
|
|
if (empty($user) || empty($user)) {
|
|||
|
|
return ;
|
|||
|
|
}
|
|||
|
|
$ms = $user->name.'您好,您的好友'.$other_user->name.'应邀加入福恋,您成功获得1个福分的奖励,感谢您的参与!';
|
|||
|
|
$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, 1);
|
|||
|
|
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 = 1;
|
|||
|
|
$amount = 1 * 100;
|
|||
|
|
$desc = '推荐注册用户奖励(福恋小程序)';
|
|||
|
|
$referre->increment('num', 1);
|
|||
|
|
}elseif ($this->type === 'approve') {
|
|||
|
|
$history->amount = 2;
|
|||
|
|
$amount = 2 * 100;
|
|||
|
|
$desc = '推荐认证用户奖励(福恋小程序)';
|
|||
|
|
}
|
|||
|
|
//转账
|
|||
|
|
$trade_no = $this->getTradeNO();
|
|||
|
|
$history->trade_no = $trade_no;
|
|||
|
|
$history->save();
|
|||
|
|
$afficial_openid = Wechat::where('user_id', $user_id)->value('afficial_openid');
|
|||
|
|
$result = WechatService::officialUserTransfer($trade_no, $afficial_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)
|
|||
|
|
{
|
|||
|
|
$scoreObj = Score::where('user_id', $user_id)->first();
|
|||
|
|
if (empty($scoreObj)) {
|
|||
|
|
$scoreObj = Score::create([
|
|||
|
|
'user_id'=>$user_id,
|
|||
|
|
'remain_amount'=>0,
|
|||
|
|
]);
|
|||
|
|
}
|
|||
|
|
if (empty($score)) {
|
|||
|
|
return ;
|
|||
|
|
}
|
|||
|
|
//2020/11/12开始不送福分
|
|||
|
|
// $scoreObj->remain_amount = $scoreObj->remain_amount + $score;
|
|||
|
|
// $scoreObj->save();
|
|||
|
|
|
|||
|
|
ScoreHistory::create([
|
|||
|
|
'user_id'=>$user_id,
|
|||
|
|
'type'=>'gained',
|
|||
|
|
'amount'=>$score,
|
|||
|
|
'value'=>$scoreObj->remain_amount,
|
|||
|
|
'message'=>'注册赠送福分'
|
|||
|
|
]);
|
|||
|
|
$array['message'] = '注册赠送福分';
|
|||
|
|
$array['user_id'] = $user_id;
|
|||
|
|
$array['type'] = 'gained';
|
|||
|
|
$array['score'] = $score;
|
|||
|
|
$array['other_user_id'] = $this->other_user_id;
|
|||
|
|
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;
|
|||
|
|
}
|
|||
|
|
}
|