love_php/app/Jobs/SendSubTemplateMsg.php
2026-04-02 09:20:51 +08:00

441 lines
16 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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\Http\Response\ResponseJson;
use App\Repositories\Eloquent\SmsRepository as Sms;
use Illuminate\Container\Container;
use App\Models\Message;
use App\Models\UserPreviewHistory;
use App\Models\SignLog;
use App\Models\UserSign;
use App\Models\UserMessageSetting;
use Illuminate\Support\Facades\Log;
class SendSubTemplateMsg implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use ResponseJson;
protected $type, $user, $targe_user;
protected $rule;//数字代表福币字符表示会员 签到赠送福币规则
protected $sms; //短信
public $tries = 1;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($type, $user, $targe_user=null)
{
$this->type = $type;
$this->user = $user;
$this->targe_user = $targe_user;
$this->rule = [2,3,5,7,10,15,'rank'];
$container = new Container();
$this->sms = new Sms($container);
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// if (config('app.env') != 'production') {
// return true;
// }
try {
switch ($this->type) {
case 'follow':
$param = $this->getFollowParam($this->user, $this->targe_user);
if (empty($param)) return false;
\WechatService::followedNotice($param);
break;
case "friend_request":
$param = $this->getFriendRequestParam($this->user, $this->targe_user);
if (empty($param)) return false;
$result = \WechatService::friendRequestNotice($param);
//订阅消息失败发送短信
// if ($result && $result['errcode'] != 0) {
$this->sendUserFriendsMessage($this->type);
// }
break;
case 'deal_friend_request':
$param = $this->getDealFriendRequestParam($this->user, $this->targe_user);
if (empty($param)) return false;
$result = \WechatService::dealFriendRequestNotice($param);
// 订阅消息失败发送短信
if ($result && $result['errcode'] != 0) {
$this->sendUserFriendsMessage($this->type);
}
break;
case "sign_in":
$param = $this->getSignInParam($this->user);
if (empty($param)) return false;
\WechatService::signInSubNotice($param);
break;
case "sign_in_v2":
$param = $this->getSignInV2Param($this->user);
if (empty($param)) return false;
\WechatService::signInSubNotice($param);
break;
case "match":
$param = $this->getMatchParam($this->user,$this->targe_user);
if (empty($param)) return false;
\WechatService::matchNotice($param);
break;
case "visit_log":
$param = $this->getVisitLogParam($this->user);
if (empty($param)) return false;
\WechatService::vistitLogNotice($param);
break;
default:
# code...
break;
}
return true;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function getVisitLogParam($user)
{
try {
$param['openid'] = $user->wechat?$user->wechat->openid:null;
if (empty($param['openid'])) return false;
$param['nickname_str'] = $this->getVisitLogName($user);
$param['time'] = date('Y-m-d', strtotime('-1 day'));
return $param;
} catch (\Exception $e) {
$this->getError($e);
}
}
public function getVisitLogName($user)
{
try {
$start_time = date('Y-m-d', strtotime('-1 day'));
$end_time = date('Y-m-d 23:59::59', strtotime('-1 day'));
$logs = UserPreviewHistory::with('previewUser')->whereHas('previewUser', function($sql){
$sql->whereNotNull('nickname')->where('nickname', '<>', '')->where('type', 'single');
})->where('user_id', $user->id)->whereBetween('created_at', [$start_time, $end_time])->distinct('preview_user_id')->limit(3)->get();
$visit_count = UserPreviewHistory::whereHas('previewUser', function($sql){
$sql->whereNotNull('nickname')->where('nickname', '<>', '')->where('type', 'single');
})->where('user_id', $user->id)->whereBetween('created_at', [$start_time, $end_time])->distinct('preview_user_id')->count();
$nickname_arr = [];
foreach ($logs as $log) {
$nickname_arr[] = $log->previewUser->nickname;
}
$nickname_str = implode('、',$nickname_arr);
switch ($visit_count) {
case 1:
case 2:
$nickname_str .= '昨天访问了你';
break;
default:
$nickname_str .= '等'.$visit_count.'位用户昨天访问了你';
break;
}
return $nickname_str;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function sendUserDetailMessage($type)
{
try {
$url = $type == 'friend_request' ? env('APP_URL')."/h5/#/appUserDetails/".$this->user->id : env('APP_URL').'/h5/#/jump?type=request';
$result = CommonUtilsService::shortUrl($url);
if (is_array($result) && $result['code'] == 0) {
$url = $result['url'];
}
$sms = '';
switch ($type) {
case 'friend_request':
$sms = "Hi-".$this->targe_user->nickname."!你有一条来自".$this->user->nickname."的好友验证消息,点击".$url." 查看Ta的信息哦";
break;
case 'deal_friend_request':
$sms = "Hi-".$this->targe_user->nickname."!".$this->user->nickname."通过了你的好友请求,点击".$url." 即可与Ta聊天哦";
break;
default:
# code...
break;
}
if ($sms) {
//是否设置短信接收
$log = UserMessageSetting::where(['user_id'=>$this->targe_user->id, 'type'=>$type])->first();
if ($log) return true;
$this->sms->sentMessage($this->targe_user->mobile, $sms);
}
return true;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
/**发送好友消息 请求 或者 同意 */
public function sendUserFriendsMessage($type){
try {
//是否设置短信接收
$log = UserMessageSetting::where(['user_id'=>$this->targe_user->id, 'type'=>$type])->first();
if ($log) return true;
$mobile = $this->targe_user->mobile;
$nickname = $this->user->nickname;
$m = Message::create([
'phone' => $mobile,
'message' => '',
'confirmed' => 1,
'code' => '',
'ip' => request() ? request()->ip() : '127.0.0.1',
]);
$wecharLink = \CommonUtilsService::getWecharLink('pages/users/friendRequest');
$url = \CommonUtilsService::shortUrl(env('APP_URL').'/h5/#/jump_url?url='.$wecharLink);
$message = '';
switch ($type) {
case 'friend_request':
$message = "Hi- ".$this->targe_user->nickname." !你有一条来自".$this->user->nickname."的好友验证消息,点击".$url['url']." 查看Ta的信息哦";
break;
case 'deal_friend_request':
$message = "Hi- ".$this->targe_user->nickname." !".$this->user->nickname."通过了你的好友请求,点击".$url['url']." 即可与Ta聊天哦";
break;
default:
# code...
break;
}
$m->message = $message;
$m->save();
if($mobile == '13681071004'){
return true;
}
if ($message) {
$this->sms->sendMessages($mobile, $message);
}
return true;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
/**
* 处理好友请求信息
* @param [type] $user [description]
* @param [type] $targe_user [description]
* @return [type] [description]
*/
public function getDealFriendRequestParam($user, $targe_user)
{
try {
$param['openid'] = $targe_user->wechat?$targe_user->wechat->openid:null;
if (empty($param['openid'])) return false;
$param['nickname'] = $user->nickname?:$user->name;
$param['time'] = date('Y-m-d H:i:s');
$param['prompt'] = "对方已同意你的好友请求";
$param['user_id'] = $user->id;
return $param;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function getSignInV2Param($user)
{
try {
$param['openid'] = $user->wechat?$user->wechat->openid:null;
if (empty($param['openid'])) return false;
$sign_num = $user->signLogs()->where('sign_date', '>=', '2021-01-19 16:45:49')->count();
if (empty($sign_num)) {
$sign_num_text = '新签到规则,暂无签到记录';
}else{
$sign_num_text = '已签到'.$sign_num.'天,继续加油哦~';
}
$param['sign_num'] = $sign_num_text;
$result = $this->getAwardInfoV2($user);
if (empty($result)) throw new \Exception("没有连续签到次数", 1);
$param['award'] = $result['text'];
$param['prompt'] = $result['prompt_text'];
return $param;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function getSignInParam($user)
{
try {
$param['openid'] = $user->wechat?$user->wechat->openid:null;
if (empty($param['openid'])) return false;
$sign_num = $user->signLogs()->count();
$param['sign_num'] = $sign_num == 7?'已签到'.$sign_num.'天':'已签到'.$sign_num.'天,继续加油哦~';
$param['award'] = $this->getAwardInfo($sign_num);
$param['prompt'] = $this->getPromptInfo($sign_num);
return $param;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
/**
* 根据连续签到次数 计算奖励
* @return [type] [description]
*/
public function getAwardInfoV2($user)
{
//连续签到天数
$sign_days = UserSign::where('user_id', $user->id)->value('sign_days')?:0;
$logs = $user->signLogs()->signType(SignLog::NEWYEARTYPE)->order('sign_date')->pluck('sign_date')->toArray();
//是否新用户
$is_new = 0;
if(count($logs) < 4 && $user->created_at > '2020-12-31 23:59:59' && $sign_days == count($logs)){
$is_new = 1;
}
if ($is_new) {//新用户
$coin_arr = config('sign.new');
}else {//老用户
$coin_arr = config('sign.old');
}
if (empty($sign_days)) {
$coin = $coin_arr[1];
$text = "再连续签到1天可获得".$coin."个福币";
$prompt_text = "新签到规则,保持连续签到可以获得更多福币";
return ['text'=>$text, 'prompt_text'=>$prompt_text];
}
//下一次的福币数
$coin = $coin_arr[$sign_days+1]??2;
$text = "再连续签到1天可获得".$coin."个福币";
$rank = 0;
$rank_arr = config('sign.rank');
if (in_array($sign_days + 1, array_keys($rank_arr))) {
$rank = $rank_arr[$sign_days+1];
$text .= "".$rank."天超级会员";
}
//上一次的福币数
$last_coin = $coin_arr[$sign_days];
$prompt_text = "已连续签到".$sign_days."天,上次签到已获得".$last_coin."个福币";
$last_rank = 0;
if (in_array($sign_days, array_keys($rank_arr))) {
$last_rank = $rank_arr[$sign_days];
$prompt_text .= "".$last_rank."天超级会员";
}
return ['text'=>$text, 'prompt_text'=>$prompt_text];
}
public function getAwardInfo($count)
{
switch ($count) {
case 1:
case 2:
case 3:
case 4:
case 5:
$text = "再签到1天可获得".($this->rule)[$count]."个福币";
break;
case 6:
$text = "再签到1天可获得15天超级会员";
break;
default:
$text = "再签到1天可获得2个福币";
break;
}
return $text;
}
public function getPromptInfo($count)
{
switch ($count) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
$text = "上次签到已获得".($this->rule)[$count-1]."个福币";
break;
case 7:
$text = "上次签到已获得获得15天超级会员";
break;
default:
$text = "上次签到已获得2个福币";
break;
}
return $text;
}
public function getMatchParam($user,$target_user)
{
try {
if(!$target_user || !$user || empty($target_user)){
return false;
}
$param['openid'] = $user->wechat?$user->wechat->openid:null;
if(!$param['openid']){
return false;
}
// if (empty($param['openid'])) return [];
$param['target_name'] = $target_user;
$param['match_type'] = '每日匹配更新';
$param['tips'] = "为你推荐了多位异性,点击查看";
return $param;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function getFollowParam($user, $targe_user)
{
try {
$param['user_id'] = $user->id;
$param['openid'] = $targe_user->wechat?$targe_user->wechat->openid:null;
if (empty($param['openid'])) return false;
$param['nickname'] = $user->nickname;
$param['time'] = date('Y-m-d H:i:s');
return $param;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function getFriendRequestParam($user, $targe_user)
{
try {
$param['openid'] = $targe_user->wechat?$targe_user->wechat->openid:null;
if (empty($param['openid'])) return false;
$param['nickname'] = $user->nickname;
$param['time'] = date('Y-m-d H:i:s');
$param['prompt'] = "收到一条好友请求,请点击查看";
return $param;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
}