love_php/app/Jobs/SendFriendQuestion.php

75 lines
2.4 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\ChatMessage;
use App\Services\IMService;
class SendFriendQuestion implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $tries = 3;
/**
* Create a new job instance.
*
* @return void
*/
protected $mine;
protected $user;
public function __construct($data)
{
$this->mine = $data['mine'];
$this->user = $data['user'];
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
sleep(1);
$this->sendFriendQuestion($this->mine, $this->user);
}
//用户在发送打招呼后收到的第一条信息
public function sendFriendQuestion($mine, $user)
{
try {
//第一条消息
$count = ChatMessage::where('user_id', $mine->id)->where('other_user_id', $user->id)->count();
if ($count == 1) {
//他的破冰问题
$content = $user->friendQuestion();
if (empty($content)) return true;
//网易聊天消息发送
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
$body = ['msg'=>$content];
$ext = ['type'=>'friend_question', 'is_used'=>$mine->friendQuestion()?1:0]; //friend_question破冰问题is_used 自己是否设置破冰问题
$result = $im_service->sendMsg($user->id,$ope=0,$mine->id,$type=0,$body,$option=array("push"=>false,"roam"=>true,"history"=>true,"sendersync"=>true, "route"=>false),$pushcontent='',$ext,1001);
if ($result['code'] != 200) {
throw new \Exception("网易发送单聊信息失败-".$result['desc'], 1);
}
//创建聊天记录
ChatMessage::create([
'user_id'=>$user->id,
'other_user_id'=>$mine->id,
'type'=>'CHAT',
'content'=>$content,
'status'=>1,
'system_type'=>'question',
]);
}
return true;
} catch (\Exception $e) {
// $this->getError($e);
return false;
}
}
}