love_php/app/Services/ImCallback/Message/Person.php

147 lines
3.9 KiB
PHP
Raw Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Services\ImCallback\Message;
use App\Jobs\LoveGPTChat;
use App\Jobs\SendTemplateMsg;
use App\Models\ChatMessage;
use App\Models\User;
use App\Models\Wechat;
class Person
{
public function handle()
{
$this->saveMessage();
$this->loveReply();
$this->cherubBeOutReply();
$this->miniNotice();
}
/**
* 保存消息记录
* @return void
*/
private function saveMessage()
{
$from_account = request()->input('fromAccount');
$msg_type = request()->input('msgType');
$to = request()->input('to');
$body = request()->input('body');
$msgid_server = request()->input('msgidServer');
$attach = request()->input('attach');
if (!in_array($msg_type, ['TEXT', 'PICTURE', 'AUDIO', 'VIDEO'])) {
return;
}
ChatMessage::create([
'user_id' => $from_account,
'other_user_id' => $to,
'content' => $body,
'content_type' => $msg_type,
'msgid' => $msgid_server,
'attach' => $attach,
'system_type' => 'notice'
]);
}
/**
* 小恋回复
* @return void
*/
private function loveReply()
{
$from_account = request()->input('fromAccount');
$msg_type = request()->input('msgType');
$to = request()->input('to');
$body = request()->input('body');
//不是发给小恋
if ($to != User::LOVE_GPT_USER_ID) {
return;
}
//不是文本
if ($msg_type != 'TEXT') {
return;
}
LoveGPTChat::dispatch('chat', $from_account, null, User::LOVE_GPT_USER_ID, $body, 'love')->onQueue('love_gpt');
}
/**
* 小天使非工作时间自动回复
* @return void
*/
private function cherubBeOutReply()
{
$from_account = request()->input('fromAccount');
$msg_type = request()->input('msgType');
$to = request()->input('to');
$body = request()->input('body');
//不是发给小天使
if ($to != User::CHERUB_ID) {
return;
}
//不是文本
if ($msg_type != 'TEXT') {
return;
}
date_default_timezone_set('PRC');
$time = time();
$start_time = strtotime(date('Y-m-d 09:30:00'));
$end_time = strtotime(date('Y-m-d 18:30:00'));
//工作时间
$week_num = date('w');
$check1 = $week_num >= 1 && $week_num <= 5;
$check2 = $time > $start_time && $time < $end_time;
if ($check1 && $check2) {
return;
}
LoveGPTChat::dispatch('chat', $from_account, null, User::CHERUB_ID, $body, 'cherub')->onQueue('love_gpt');
}
/**
* 小程序会话开启通知
*/
private function miniNotice()
{
$from_account = request()->input('fromAccount');
$to = request()->input('to');
// //test
// if ($to != 130163){
// return;
// }
$wechat = Wechat::query()->where('user_id',$to)->first();
if (!$wechat) {
return;
}
if (!$wechat->official_openid) {
return;
}
$data['touser'] = $wechat->official_openid;
$data['template_id'] = config('wechat.tpls.start_message_notice');
$data['url'] = '';
$data['miniprogram'] = [
'appid'=> config('wechat.mini_program.app_id'),
'pagepath'=>'/pages/tabBar/news'
];
$nickname = User::query()->where('id',$from_account)->value('nickname') ?: '好友';
$data['data'] = [
'first' => 'Hi, Ta在找你',
'keyword1' => date('Y-m-d H:i:s'),
'keyword2' => $nickname,
'remark' => '本通知可在福恋小程序 我的-设置 中关闭。',
];
SendTemplateMsg::dispatch($data)->onQueue('start_message');
}
}