Files
fast_response/admin/application/api/controller/Feedback.php
2025-03-30 14:12:37 +08:00

101 lines
3.0 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
/*
* description
* authorwh
* email
* createTime{2025/3/28} {17:01}
*/
namespace app\api\controller;
use think\Db;
use wanghua\general_utility_tools_php\gpt\chat\ChatGPT;
use wanghua\general_utility_tools_php\Mmodel;
use wanghua\general_utility_tools_php\tool\Tools;
class Feedback extends BaseHttpApi
{
/**
* desc用户反馈
*
* 参数:
* content 反馈内容
* chatroom_id 客户所在微信群id
*
* 接口:
* /api/Feedback/setUserFeedback
*
* authorwh
*/
function setUserFeedback(){
return Mmodel::catchJson(function (){
$content = input('content');
$wx_groupid = input('chatroom_id');//客户所在微信群id
if(empty($content)){
return Tools::set_fail('反馈内容不能为空');
}
if(empty($wx_groupid)){
return Tools::set_fail('群id不能为空');
}
//得到3条反馈消息AI去总结
$str = $this->aiResult($content);
if(empty($str)){
$str = $content;
}
//查询是否有回访记录
$ret = Db::table('fa_firmcustomerfollowuprecord')
->where('rel_group',$wx_groupid)
->order('id desc')
->find();
if(empty($ret)){
return Tools::set_fail('该客户无回访记录');
}
Db::table('fa_firmcustomerfeedback')
->data([
'firmcustomer_id'=>$ret['firmcustomer_id'],
'rel_wx'=>$ret['rel_wx'],
'rel_group'=>$ret['rel_group'],
'msg'=>$str,
'firmcustomerfollowuprecord_id'=>$ret['id'],
])
->insert();
return Tools::set_ok();
});
}
//ai总结反馈消息
private function aiResult(string $msg){
$ai_config = config('ai_config.follow_result');
//反馈消息AI去总结
$chat_obj = new ChatGPT();
$chat_obj->url = $ai_config['base_url'];
$chat_obj->apiKey = $ai_config['api_key'];
$question = <<<EOF
【用户反馈】
$msg
EOF;
//dump($question);
//{"id":"","model":"","usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":1},"choices":[{"message":{"role":"assistant","content":"无正式反馈内容"},"finish_reason":"stop","index":0}]}
Tools::log_to_write_txt(['ai总结反馈消息开始'=>$question]);
$res = $chat_obj->getchatgptresponse($question);
Tools::log_to_write_txt(['ai总结反馈消息结束'=>$res]);
//dump(input());
//dump($res);die;
if($res['code'] != 200){
return '';
}
$json_arr = json_decode($res['data'],true);
Tools::log_to_write_txt(['$json_arr'=>$json_arr]);
if(empty($json_arr['choices'][0]['message']['content'])){
return '';
}
return $json_arr['choices'][0]['message']['content'];
}
}