love_php/app/Jobs/InviteMerchant.php

145 lines
6.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\MerchantAccount;
use App\Models\Live\Anchor;
use App\Models\Live\Viewer;
use App\Models\CoinLog;
use App\Models\Message;
use App\Models\MerchantUsers;
use App\Models\MEarningAccount;
use App\Models\MEarning;
use App\Models\User;
use App\Utils\Messenger as Messengers;
class InviteMerchant implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $merchant_id;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($merchant_id)
{
$this->merchant_id = $merchant_id;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//如果商家用户达到指定目标,发送推送通知
$user_num = MerchantAccount::PromotionIncome($this->merchant_id);
if ($user_num < 80) return;
$MerchantAccount = MerchantAccount::where('id', $this->merchant_id)->first();
$anchor = Anchor::where('m_id', $MerchantAccount->id)->first();
$coin = 0;
$remark = '';
if ($user_num == 80) {
$message = '你推荐入驻的【' . $anchor->name . '】平台新用户已经达到80人离奖励40元更进一步了就差20人赶快来助力分享吧';
} elseif ($user_num == 100) {
$message = '恭喜你,你推荐入驻的【' . $anchor->name . '】平台新用户已经达到100人已获得40元分享越多还有更多奖励等着你哦';
$remark = '平台新用户已经达到100人';
$coin = 40;
} elseif ($user_num == 800) {
$message = '你推荐入驻的【' . $anchor->name . '】平台新用户已经达到800人再成功推荐200人即可获得50元哦前往助力分享';
} elseif ($user_num == 1000) {
$message = '恭喜你,你推荐入驻的【' . $anchor->name . '】平台新用户已经达到1000人额外获得50元感谢你对福恋的一直支持';
$remark = '平台新用户已经达到1000人';
$coin = 50;
}
if ($anchor->channel == 1) {
$Viewer = Viewer::select('user_id', 'nickname', 'mobile')->where('openid', $anchor->from_openid)->first();
if (!$Viewer) return;
$user_id = $Viewer->user_id ?? 0;
if ($user_id != 0 && $coin > 0) {
$CoinLog = new CoinLog();
$CoinLog->user_id = $Viewer->user_id;
$CoinLog->coin = $coin;
$CoinLog->is_hooked = 1;
$CoinLog->type_id = $anchor->id;
$CoinLog->type = 'INVITE';
$CoinLog->remark = $remark;
$CoinLog->save();
$from_openid = User::where('id', $user_id)->value('from_openid');
$url = urlencode(env('APP_URL') . '/h5/#/recommendedSaasV2');
$data['url'] = env('APP_URL') . '/api/official/live/wechat/oauth?from_openid=' . $from_openid . '&url=' . $url;
$data['data'] = [
'first' => $message,
'keyword1' => $Viewer->nickname,
'keyword2' => $MerchantAccount->anchor->name,
'reamrk' => '',
];
}
} else {
// 推荐商家入驻奖励
// $merchant_user = MerchantUsers::where('openid', $anchor->from_openid)->first();
// if ($merchant_user && $coin > 0) {
// $erning = new MEarning();
// $erning->m_id = $this->merchant_id;
// $erning->m_user_id = $merchant_user->id;
// $erning->m_order_id = $this->merchant_id;
// $erning->type = 'enter';
// $erning->sharer = 'first_sharer';
// $erning->ratio = 1;
// $erning->total = 1;
// $erning->value = $coin;
// $erning->status = 'finished';
// $erning->save();
// $Account = MEarningAccount::where('m_id', $this->merchant_id)->where('m_user_id', $merchant_user->id)->first();
// if ($Account) {
// $Account->increment('total_value', $coin);
// $Account->increment('balance', $coin);
// } else {
// $Account = new MEarningAccount();
// $Account->m_id = $this->merchant_id;
// $Account->m_user_id = $merchant_user->id;
// $Account->total_value = $coin;
// $Account->balance = $coin;
// $Account->save();
// }
// }
// $url = urlencode(env('APP_URL') . '/pu/#/myRecommend');
// $data['url'] = env('APP_URL') . '/api/official/live/wechat/FamilyAuth?merchant_id=' . $this->merchant_id . '&url=' . $url;
// $data['data'] = [
// 'first' => $message,
// 'keyword1' => $merchant_user->nickname,
// 'keyword2' => $anchor->name,
// 'reamrk' => '',
// ];
}
// 是否关注公众号
$app = \WechatService::officialApp();
$wechat_user = $app->user->get($anchor->from_openid);
if ($wechat_user && $wechat_user['subscribe'] == 1) {
$data['touser'] = $anchor->from_openid;
$data['template_id'] = 'BFrJ8oohsB15DRyQz5uw9Xfmn4SSIYF9bhTZHo6TRPA';
SendTemplateMsg::dispatch($data)->onQueue('template_message');
} else {
// 短信推送
if ($Viewer && $Viewer->mobile) {
Message::create([
'phone' => $Viewer->mobile,
'message' => $message,
'confirmed' => 1,
'ip' => request() ? request()->ip() : '127.0.0.1',
]);
Messengers::sendSMS($Viewer->mobile, $message);
}
}
}
}