love_php/app/Jobs/StoreUserWorthShare.php

83 lines
2.6 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\Wechat;
use App\Models\WorthShare;
class StoreUserWorthShare implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $target_user;
protected $openid;
protected $from_user_id;
protected $from_openid;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($user=null,$openid=null, $from_user_id =null, $from_openid=null)
{
$this->target_user = $user;
$this->openid = $openid;
$this->from_user_id = $from_user_id;
$this->from_openid = $from_openid;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//未注册
if($this->openid && ($this->from_user_id || $this->from_openid)){
$user_id = $this->from_user_id;
if (empty($user_id) && $this->from_openid){
$user_id = Wechat::where('openid', $this->from_openid)->value("user_id");
}
if (empty($user_id)) return true;
$openid = $this->openid;
$data = [
'user_id'=>$user_id,
'openid'=>$openid,
'channel'=>'register',
'sub_channel'=>'indirect',
];
$share = WorthShare::where($data)->first();
if(empty($share)){
WorthShare::addWorthShare($data);
}
return true;
}
//注册 带分享人信息
if($this->target_user && (!empty($this->target_user->from_user_id) || !empty($this->target_user->from_openid))){
$target_user_id = $this->target_user->id;
if($this->target_user->from_user_id){
$user_id = $this->target_user->from_user_id;
}else{
$user_id = Wechat::where('openid', $this->target_user->from_openid)->whereNotNull("user_id")->value('user_id');
}
if (empty($user_id)) return true;
$data = [
'user_id'=>$user_id,
'target_user_id'=>$target_user_id,
'channel'=>'register',
'sub_channel'=>'direct',
];
$share = WorthShare::where($data)->first();
if(empty($share)){
WorthShare::addWorthShare($data);
}
}
}
}