263 lines
9.4 KiB
PHP
Executable File
263 lines
9.4 KiB
PHP
Executable File
<?php
|
||
/*
|
||
* description:
|
||
* author:wh
|
||
* email:
|
||
* createTime:{2025/3/24} {17:23}
|
||
*/
|
||
|
||
namespace app\index\controller;
|
||
|
||
|
||
use app\api\logic\MessageWechatLogic;
|
||
use app\api\logic\PyappLofic;
|
||
use app\api\logic\TokenWechatLogic;
|
||
use app\api\logic\UpdateMessageWechatLogic;
|
||
use think\Controller;
|
||
use think\Db;
|
||
use wanghua\general_utility_tools_php\Mmodel;
|
||
use wanghua\general_utility_tools_php\tool\Tools;
|
||
|
||
//定时任务
|
||
class Tasktimer extends Controller
|
||
{
|
||
|
||
/**
|
||
* desc:按分钟执行
|
||
* author:wh
|
||
*
|
||
*/
|
||
function runMinutes(){
|
||
|
||
//客户回访计划
|
||
Mmodel::catch(function (){
|
||
$this->returnPlan();
|
||
});
|
||
//生日通知
|
||
if (date("H:i") == "10:00"){
|
||
Mmodel::catch(function (){
|
||
$this->returnBirthdayPlan();
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* desc:客户回访计划
|
||
* author:wh
|
||
* @return string
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
* @throws \think\exception\DbException
|
||
*/
|
||
function returnPlan(){
|
||
//回访记录
|
||
$return_record = Db::table('fa_firmcustomerfollowuprecord')
|
||
->where('status','no')
|
||
->select();
|
||
if(empty($return_record)){
|
||
Tools::log_to_write_txt(['无回访记录计划,无需回访']);
|
||
return '';
|
||
}
|
||
foreach ($return_record as $k=>$item){
|
||
|
||
Tools::log_to_write_txt(['客户回访计划,开始:$item'=>$item]);
|
||
$ret = $this->postText($item);
|
||
//dump($ret);
|
||
Tools::log_to_write_txt(['客户回访计划,结束:$res'=>$ret]);
|
||
}
|
||
|
||
//Tools::log_to_write_txt(['runMinutes,出参:$res'=>$ret]);
|
||
//dump($ret);
|
||
return '';
|
||
}
|
||
|
||
/**
|
||
* desc:发送文字消息
|
||
* author:wh
|
||
* @return array
|
||
*/
|
||
private function postText($return_record){
|
||
//$firmcustomerfollowuprecord_id = input('firmcustomerfollowuprecord_id',1);
|
||
//查询回访计划
|
||
$plan = Db::table('fa_firmcustomerfollowuptimelist')
|
||
->where('firmcustomerfollowuprecord_id',$return_record['id'])
|
||
->order('followup_time asc')
|
||
->find();
|
||
if(empty($plan)){
|
||
return Tools::set_fail('该客户没有回访计划');
|
||
}
|
||
//查询客户
|
||
$cust = Db::table('fa_firmcustomer')
|
||
->where('id',$return_record['firmcustomer_id'])
|
||
->find();
|
||
if(empty($cust)){
|
||
return Tools::set_fail('有回访计划,客户不存在');
|
||
}
|
||
if(empty($return_record['visit_msg'])){
|
||
return Tools::set_fail('有回访记录但无回访内容,请填写回访内容后再试');
|
||
}
|
||
//查询客户所绑定的ai客服
|
||
$ai_config = Db::table('fa_aicustomerservice')
|
||
->where('id',$cust['aicustomerservice_ids'])
|
||
->find();
|
||
if(empty($ai_config)){
|
||
return Tools::set_fail('有回访计划,无ai客服服务该客户');
|
||
}
|
||
$base_url = $ai_config['server_url'];
|
||
|
||
//记录创建时间
|
||
$create_time = $return_record['create_time'];
|
||
$day = (int)$plan['day'];
|
||
//在记录创建后的N天N点执行
|
||
$day_time = strtotime($create_time) + ($day * 86400);//N天后
|
||
if($day_time > time()){
|
||
return Tools::set_fail('未到回访时间');
|
||
}
|
||
|
||
$date = date('Y-m-d ',$day_time).$plan['followup_time'];
|
||
$exc_time = strtotime($date);//最终的执行时间
|
||
|
||
//dump(date('Y-m-d H:i:s',$exc_time));
|
||
//校验时间是否达到执行计划时间
|
||
if($exc_time > time()){
|
||
return Tools::set_fail('未到计划回访时间');
|
||
}
|
||
if(empty($cust['rel_group'])){
|
||
return Tools::set_fail('客户无绑定群');
|
||
}
|
||
if(empty($cust['wx_nickname'])){
|
||
return Tools::set_fail('客户无绑定微信昵称');
|
||
}
|
||
if(empty($cust['rel_wx'])){
|
||
return Tools::set_fail('客户无绑定微信');
|
||
}
|
||
|
||
$logic = new MessageWechatLogic();
|
||
$logic->setBaseUrl($base_url);
|
||
|
||
$tokenArr = (new TokenWechatLogic())->getToken($base_url);
|
||
$data = [
|
||
'appId'=>$tokenArr['appId'],
|
||
'toWxid'=>$cust['rel_group'],//是群则发送群消息,否则发个人私人消息
|
||
'content'=>"@".$cust['wx_nickname']." ".$return_record['visit_msg'],
|
||
//@的好友,多个英文逗号分隔。群主或管理员@全部的人,则填写'notify@all'
|
||
'ats'=>$cust['rel_wx'],
|
||
];
|
||
Tools::log_to_write_txt(['回访微信群客户,开始:$post_data'=>$data]);
|
||
$res = $logic->postGroupText($data);
|
||
Tools::log_to_write_txt(['回访微信群客户,结束:$res'=>$res]);
|
||
|
||
//修改回访记录回访状态
|
||
Db::table('fa_firmcustomerfollowuprecord')
|
||
->where('id',$return_record['id'])
|
||
->update([
|
||
'status'=>'yes',
|
||
'send_time'=>Tools::get_now_date(),
|
||
]);
|
||
|
||
//设置AI客服回访模式
|
||
$py_logic = new PyappLofic();
|
||
$py_logic->setFollowupModel($base_url,$cust['rel_group'],$return_record);
|
||
|
||
//设置AI客服更新信息
|
||
$updateMessage_logic = new UpdateMessageWechatLogic();
|
||
$updateMessage_logic->getUpdateWechatMessage($base_url,$return_record['visit_msg'],$cust['rel_group']);
|
||
|
||
return Tools::set_ok('发送成功');
|
||
}
|
||
|
||
/**
|
||
* @return string 生日祝福
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
* @throws \think\exception\DbException
|
||
*/
|
||
function returnBirthdayPlan(){
|
||
// 获取当前月和日
|
||
$customer_record = Db::table('fa_firmcustomer')
|
||
->where('birthday', date('m-d'))
|
||
->select();
|
||
|
||
if(empty($customer_record)){
|
||
Tools::log_to_write_txt(['无人生日,无需发送']);
|
||
return '';
|
||
}
|
||
foreach ($customer_record as $value){
|
||
//生日祝福模版
|
||
$birthday_record = Db::table('fa_mattertemplatecategorization')
|
||
->alias('m')
|
||
->join('fa_mattertemplatecategorizedcontent','fa_mattertemplatecategorizedcontent.mattertemplatecategorization_id = m.id')
|
||
->where('m.firm_id','in',[$value['firm_id'],1])
|
||
->where('m.mattertemplate_id',3)
|
||
->order(['m.firm_id'=>'desc','m.id'=>'desc'])
|
||
->field('fa_mattertemplatecategorizedcontent.content')
|
||
->find();
|
||
if(!empty($birthday_record)){
|
||
Tools::log_to_write_txt(['客户生日祝福计划,开始:$item'=>$value]);
|
||
$ret = $this->postMatterText($value,$birthday_record,'生日');
|
||
Tools::log_to_write_txt(['客户生日祝福计划,结束:$res'=>$ret]);
|
||
}
|
||
Tools::log_to_write_txt(['无生日祝福,无需发送']);
|
||
return '';
|
||
}
|
||
return '';
|
||
}
|
||
public function postMatterText($customer_record,$birthday_record,$msg=''){
|
||
|
||
//查询客户所绑定的ai客服
|
||
$ai_config = Db::table('fa_aicustomerservice')
|
||
->where('id',$customer_record['aicustomerservice_ids'])
|
||
->find();
|
||
|
||
$tokenArr = (new TokenWechatLogic())->getToken($ai_config['server_url']);
|
||
|
||
if(empty($ai_config)){
|
||
return Tools::set_fail('有'.$msg.'计划,无ai客服服务该客户');
|
||
}
|
||
$base_url = $ai_config['server_url'];
|
||
$type = 0;
|
||
if(!empty($customer_record['rel_group'])){
|
||
if(empty($customer_record['wx_nickname'])){
|
||
return Tools::set_fail('客户无绑定微信昵称');
|
||
}
|
||
$data = [
|
||
'appId'=>$tokenArr['appId'],
|
||
'toWxid'=>$customer_record['rel_group'],//是群则发送群消息,否则发个人私人消息
|
||
'content'=>"@".$customer_record['wx_nickname']." ".$birthday_record['content'],
|
||
//@的好友,多个英文逗号分隔。群主或管理员@全部的人,则填写'notify@all'
|
||
'ats'=>$customer_record['rel_wx'],
|
||
];
|
||
$wxid = $customer_record['rel_group'];
|
||
$type = 1;
|
||
}else{
|
||
if(!empty($customer_record['rel_wx'])){
|
||
$data = [
|
||
'appId'=>$tokenArr['appId'],
|
||
'toWxid'=>$customer_record['rel_wx'],//是群则发送群消息,否则发个人私人消息
|
||
'content'=>$birthday_record['content'],
|
||
];
|
||
$wxid = $customer_record['rel_wx'];
|
||
$type = 2;
|
||
}else{
|
||
return Tools::set_fail('客户未绑定微信和群');
|
||
}
|
||
}
|
||
|
||
$logic = new MessageWechatLogic();
|
||
$logic->setBaseUrl($base_url);
|
||
|
||
Tools::log_to_write_txt([$msg.'微信群客户,开始:$post_data'=>$data]);
|
||
if ($type == 1){
|
||
$res = $logic->postGroupText($data);
|
||
}else{
|
||
$res = $logic->postText($data);
|
||
}
|
||
Tools::log_to_write_txt([$msg.'微信群客户,结束:$res'=>$res]);
|
||
|
||
//设置AI客服更新信息
|
||
$updateMessage_logic = new UpdateMessageWechatLogic();
|
||
$updateMessage_logic->getUpdateWechatMessage($base_url,$birthday_record['content'],$wxid);
|
||
|
||
return Tools::set_ok('发送成功');
|
||
}
|
||
} |