AI功能提交

This commit is contained in:
wong
2025-09-17 16:51:11 +08:00
parent 706be3efd1
commit 02c94a12d5
15 changed files with 1060 additions and 10 deletions

View File

@@ -0,0 +1,153 @@
<?php
namespace app\chukebao\controller;
use app\chukebao\model\AiFriendSettings;
use app\chukebao\model\Questions;
use library\ResponseHelper;
use think\Db;
class AiSettingsController extends BaseController
{
const SETTING_DEFAULT = [
'audioSetting' => false,
'round' => 10,
'aiStopSetting' => [
'status' => true,
'key' => ['好', '不错', '好的', '下次', '可以']
],
'fileSetting' => [
'type' => 1,
'content' => ''
],
'modelSetting' => [
'model' => 'GPT-4',
'role' => '你是一名销售的AI助理同时也是一个工智能技术专家你的名字叫小灵你是单身女性出生于2003年10月10日喜欢听音乐和看电影有着丰富的人生阅历前成熟大方分享用幽默风趣的语言和客户交流顾客问起你的感情回复内容中不要使用号特别注意不要跟客户问题不要更多选择发送的信息。',
'businessBackground' => '灵销智能公司开发了多款AI营销智能技术产品以提升销售GPT AI大模型为核心接入打造的销售/营销/客服等AI智能应用为企业AI办公AI助理AI销售AI营销AI直播等大AI应用产品。',
'dialogueStyle' => '客户你们的AI解决方案具体是怎么收费的销售朋友我们的AI解决方案是根据项目需求来定的这样吧你能跟我说说你们的具体情况吗不过这样一分钱您看怎么样我们可以给您做个详细的方案对比。',
]
];
const TYPE_DATA = ['audioSetting', 'round', 'aiStopSetting', 'fileSetting', 'modelSetting'];
/**
* 获取配置信息
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getSetting()
{
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
$data = Db::name('ai_settings')->where(['userId' => $userId, 'companyId' => $companyId])->find();
if (empty($data)) {
$setting = self::SETTING_DEFAULT;
$data = [
'companyId' => $companyId,
'userId' => $userId,
'config' => json_encode($setting, 256),
'createTime' => time(),
'updateTime' => time()
];
Db::name('ai_settings')->insert($data);
} else {
$setting = json_decode($data['config'], true);
}
return ResponseHelper::success($setting, '获取成功');
}
/**
* 配置
* @return \think\response\Json
* @throws \Exception
*/
public function setSetting()
{
$key = $this->request->param('key', '');
$value = $this->request->param('value', '');
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
if (empty($key) || empty($value)) {
return ResponseHelper::error('参数缺失');
}
if (!in_array($key, self::TYPE_DATA)) {
return ResponseHelper::error('该类型不在配置项');
}
Db::startTrans();
try {
$data = Db::name('ai_settings')->where(['userId' => $userId, 'companyId' => $companyId])->find();
if (empty($data)) {
$setting = self::SETTING_DEFAULT;
} else {
$setting = json_decode($data['config'], true);
}
$setting[$key] = $value;
$setting = json_encode($setting, 256);
Db::name('ai_settings')->where(['id' => $data['id']])->update(['config' => $setting, 'updateTime' => time()]);
Db::commit();
return ResponseHelper::success(' ', '配置成功');
} catch (\Exception $e) {
Db::rollback();
return ResponseHelper::error('配置失败:' . $e->getMessage());
}
}
public function setFriend()
{
$friendId = $this->request->param('friendId', '');
$wechatAccountId = $this->request->param('wechatAccountId', '');
$type = $this->request->param('type', 0);
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
if (empty($friendId) || empty($wechatAccountId)) {
return ResponseHelper::error('参数缺失');
}
$friend = Db::table('s2_wechat_friend')->where(['id' => $friendId,'wechatAccountId' => $wechatAccountId])->find();
if (empty($friend)) {
return ResponseHelper::error('该好友不存在');
}
$aiFriendSettings = AiFriendSettings::where(['userId' => $userId, 'companyId' => $companyId,'friendId' => $friendId,'wechatAccountId' => $wechatAccountId])->find();
Db::startTrans();
try {
if (empty($aiFriendSettings)) {
$aiFriendSettings = new AiFriendSettings();
$aiFriendSettings->companyId = $companyId;
$aiFriendSettings->userId = $userId;
$aiFriendSettings->type = $type;
$aiFriendSettings->wechatAccountId = $wechatAccountId;
$aiFriendSettings->friendId = $friendId;
$aiFriendSettings->createTime = time();
$aiFriendSettings->updateTime = time();
}else{
$aiFriendSettings->type = $type;
$aiFriendSettings->updateTime = time();
}
$aiFriendSettings->save();
Db::commit();
return ResponseHelper::success(' ', '配置成功');
} catch (\Exception $e) {
Db::rollback();
return ResponseHelper::error('配置失败:' . $e->getMessage());
}
}
}

View File

@@ -25,8 +25,8 @@ class CustomerServiceController extends BaseController
->group('id')
->select();
foreach ($list as $k=>&$v){
$v['createTime'] = !empty($v['createTime']) ? date('Y-m-d H:i:s',$v['createTime']) : '';
$v['updateTime'] = !empty($v['updateTime']) ? date('Y-m-d H:i:s',$v['updateTime']) : '';
$v['createTime'] = !empty($v['createTime']) && is_numeric($v['createTime']) ? date('Y-m-d H:i:s',$v['createTime']) : '';
$v['updateTime'] = !empty($v['updateTime']) && is_numeric($v['updateTime']) ? date('Y-m-d H:i:s',$v['updateTime']) : '';
$v['labels'] = json_decode($v['labels'],true);
unset(
$v['accountUserName'],

View File

@@ -53,6 +53,9 @@ class MessageController extends BaseController
->field('id,nickname,avatar')
->find();
$v['msgInfo'] = $friend;
$v['unreadCount'] = Db::table('s2_wechat_message')
->where(['wechatFriendId' => $v['wechatFriendId'],'isRead' => 0])
->count();
}
if (!empty($v['wechatChatroomId'])){
@@ -61,12 +64,45 @@ class MessageController extends BaseController
->field('id,nickname,chatroomAvatar as avatar')
->find();
$v['msgInfo'] = $chatroom;
$v['unreadCount'] = Db::table('s2_wechat_message')
->where(['wechatChatroomId' => $v['wechatChatroomId'],'isRead' => 0])
->count();
}
}
unset($v);
return ResponseHelper::success($list);
}
public function readMessage(){
$wechatFriendId = $this->request->param('wechatFriendId', '');
$wechatChatroomId = $this->request->param('wechatChatroomId', '');
$accountId = $this->getUserInfo('s2_accountId');
if (empty($accountId)){
return ResponseHelper::error('请先登录');
}
if (empty($wechatChatroomId) && empty($wechatFriendId)){
return ResponseHelper::error('参数缺失');
}
$where = [];
if (!empty($wechatChatroomId)){
$where[] = ['wechatChatroomId','=',$wechatChatroomId];
}
if (!empty($wechatFriendId)){
$where[] = ['wechatFriendId','=',$wechatFriendId];
}
Db::table('s2_wechat_message')->where($where)->update(['isRead' => 1]);
return ResponseHelper::success([]);
}
}

View File

@@ -0,0 +1,232 @@
<?php
namespace app\chukebao\controller;
use app\chukebao\model\Questions;
use library\ResponseHelper;
use think\Db;
class QuestionsController extends BaseController
{
/**
* 列表
* @return \think\response\Json
* @throws \Exception
*/
public function getList(){
$page = $this->request->param('page', 1);
$limit = $this->request->param('limit', 10);
$keyword = $this->request->param('keyword', '');
$accountId = $this->getUserInfo('s2_accountId');
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
if (empty($accountId)){
return ResponseHelper::error('请先登录');
}
$query = Questions::where(['userId' => $userId,'companyId' => $companyId,'isDel' => 0])
->order('id desc');
if (!empty($keyword)){
$query->where('questions|answers', 'like', '%'.$keyword.'%');
}
$list = $query->page($page, $limit)->select()->toArray();
$total = $query->count();
foreach ($list as $k => &$v){
$user = Db::name('users')->where(['id' => $v['userId']])->field('username,account')->find();
if (!empty($user)){
$v['userName'] = !empty($user['username']) ? $user['username'] : $user['account'];
}else{
$v['userName'] = '';
}
$v['answers'] = json_decode($v['answers'],true);
}
unset($v);
return ResponseHelper::success(['list'=>$list,'total'=>$total]);
}
/**
* 新增
* @return \think\response\Json
* @throws \Exception
*/
public function create(){
$type = $this->request->param('type', 0);
$questions = $this->request->param('questions', '');
$answers = $this->request->param('answers', []);
$status = $this->request->param('status', 0);
$accountId = $this->getUserInfo('s2_accountId');
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
if (empty($accountId)){
return ResponseHelper::error('请先登录');
}
if (empty($questions) || empty($answers)){
return ResponseHelper::error('问题和答案不能为空');
}
Db::startTrans();
try {
$questionsModel = new Questions();
$questionsModel->type = $type;
$questionsModel->questions = $questions;
$questionsModel->answers = !empty($answers) ? json_encode($answers,256) : json_encode([],256);
$questionsModel->status = $status;
$questionsModel->accountId = $accountId;
$questionsModel->userId = $userId;
$questionsModel->companyId = $companyId;
$questionsModel->createTime = time();
$questionsModel->updateTime = time();
$questionsModel->save();
Db::commit();
return ResponseHelper::success(' ','创建成功');
} catch (\Exception $e) {
Db::rollback();
return ResponseHelper::error('创建失败:'.$e->getMessage());
}
}
/**
* 更新
* @return \think\response\Json
* @throws \Exception
*/
public function update(){
$id = $this->request->param('id', 0);
$accountId = $this->getUserInfo('s2_accountId');
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
$type = $this->request->param('type', 0);
$questions = $this->request->param('questions', '');
$answers = $this->request->param('answers', []);
$status = $this->request->param('status', 0);
$accountId = $this->getUserInfo('s2_accountId');
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
if (empty($accountId)){
return ResponseHelper::error('请先登录');
}
if (empty($id)){
return ResponseHelper::error('参数缺失');
}
if (empty($questions) || empty($answers)){
return ResponseHelper::error('问题和答案不能为空');
}
Db::startTrans();
try {
$questionsData = Questions::where(['id' => $id,'userId' => $userId,'companyId' => $companyId,'isDel' => 0])->find();
$questionsData->type = $type;
$questionsData->questions = $questions;
$questionsData->answers = !empty($answers) ? json_encode($answers,256) : json_encode([],256);
$questionsData->status = $status;
$questionsData->accountId = $accountId;
$questionsData->userId = $userId;
$questionsData->companyId = $companyId;
$questionsData->updateTime = time();
$questionsData->save();
Db::commit();
return ResponseHelper::success(' ','更新成功');
} catch (\Exception $e) {
Db::rollback();
return ResponseHelper::error('更新失败:'.$e->getMessage());
}
}
/**
* 删除
* @return \think\response\Json
* @throws \Exception
*/
public function delete(){
$id = $this->request->param('id', 0);
$accountId = $this->getUserInfo('s2_accountId');
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
if (empty($accountId)){
return ResponseHelper::error('请先登录');
}
if (empty($id)){
return ResponseHelper::error('参数缺失');
}
$questions = Questions::where(['id' => $id,'userId' => $userId,'companyId' => $companyId,'isDel' => 0])->find();
if (empty($questions)){
return ResponseHelper::error('该问题不存在或者已删除');
}
$res = Questions::where(['id' => $id])->update(['isDel' => 1,'deleteTime' => time()]);
if (!empty($res)){
return ResponseHelper::success('','已删除');
}else{
return ResponseHelper::error('删除失败');
}
}
/**
* 详情
* @return \think\response\Json
* @throws \Exception
*/
public function detail(){
$id = $this->request->param('id', 0);
$accountId = $this->getUserInfo('s2_accountId');
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
if (empty($accountId)){
return ResponseHelper::error('请先登录');
}
if (empty($id)){
return ResponseHelper::error('参数缺失');
}
$questions = Questions::where(['id' => $id,'userId' => $userId,'companyId' => $companyId,'isDel' => 0])->find();
if (empty($questions)){
return ResponseHelper::error('该问题不存在或者已删除');
}
$questions['answers'] = json_decode($questions['answers'],true);
$user = Db::name('users')->where(['id' => $questions['userId']])->field('username,account')->find();
if (!empty($user)){
$questions['userName'] = !empty($user['username']) ? $user['username'] : $user['account'];
}else{
$questions['userName'] = '';
}
unset(
$questions['isDel'],
$questions['deleteTime'],
$questions['createTime'],
$questions['updateTime']
);
return ResponseHelper::success($questions,'获取成功');
}
}

View File

@@ -22,9 +22,57 @@ class WechatChatroomController extends BaseController
$total = $query->count();
foreach ($list as $k=>&$v){
$v['createTime'] = !empty($v['createTime']) ? date('Y-m-d H:i:s',$v['createTime']) : '';
$v['updateTime'] = !empty($v['updateTime']) ? date('Y-m-d H:i:s',$v['updateTime']) : '';
// 提取所有聊天室ID用于批量查询
$chatroomIds = array_column($list, 'id');
// 一次性查询所有聊天室的未读消息数量
$unreadCounts = [];
if (!empty($chatroomIds)) {
$unreadResults = Db::table('s2_wechat_message')
->field('wechatChatroomId, COUNT(*) as count')
->where('wechatChatroomId', 'in', $chatroomIds)
->where('isRead', 0)
->group('wechatChatroomId')
->select();
foreach ($unreadResults as $result) {
$unreadCounts[$result['wechatChatroomId']] = $result['count'];
}
}
// 一次性查询所有聊天室的最新消息
$latestMessages = [];
if (!empty($chatroomIds)) {
// 使用子查询获取每个聊天室的最新消息ID
$subQuery = Db::table('s2_wechat_message')
->field('MAX(id) as max_id, wechatChatroomId')
->where('wechatChatroomId', 'in', $chatroomIds)
->group('wechatChatroomId')
->buildSql();
// 查询最新消息的详细信息
$messageResults = Db::table('s2_wechat_message')
->alias('m')
->join([$subQuery => 'sub'], 'm.id = sub.max_id')
->field('m.*, sub.wechatChatroomId')
->select();
foreach ($messageResults as $message) {
$latestMessages[$message['wechatChatroomId']] = $message;
}
}
// 处理每个聊天室的数据
foreach ($list as $k => &$v) {
$v['createTime'] = !empty($v['createTime']) ? date('Y-m-d H:i:s', $v['createTime']) : '';
$v['updateTime'] = !empty($v['updateTime']) ? date('Y-m-d H:i:s', $v['updateTime']) : '';
$config = [
'unreadCount' => isset($unreadCounts[$v['id']]) ? $unreadCounts[$v['id']] : 0,
'chat' => isset($latestMessages[$v['id']]),
'msgTime' => isset($latestMessages[$v['id']]) ? $latestMessages[$v['id']]['wechatTime'] : 0
];
$v['config'] = $config;
}
unset($v);

View File

@@ -22,10 +22,60 @@ class WechatFriendController extends BaseController
$total = $query->count();
foreach ($list as $k=>&$v){
$v['createTime'] = !empty($v['createTime']) ? date('Y-m-d H:i:s',$v['createTime']) : '';
$v['updateTime'] = !empty($v['updateTime']) ? date('Y-m-d H:i:s',$v['updateTime']) : '';
$v['passTime'] = !empty($v['passTime']) ? date('Y-m-d H:i:s',$v['passTime']) : '';
// 提取所有好友ID
$friendIds = array_column($list, 'id');
// 一次性查询所有好友的未读消息数量
$unreadCounts = [];
if (!empty($friendIds)) {
$unreadResults = Db::table('s2_wechat_message')
->field('wechatFriendId, COUNT(*) as count')
->where('wechatFriendId', 'in', $friendIds)
->where('isRead', 0)
->group('wechatFriendId')
->select();
foreach ($unreadResults as $result) {
$unreadCounts[$result['wechatFriendId']] = $result['count'];
}
}
// 一次性查询所有好友的最新消息
$latestMessages = [];
if (!empty($friendIds)) {
// 使用子查询获取每个好友的最新消息ID
$subQuery = Db::table('s2_wechat_message')
->field('MAX(id) as max_id, wechatFriendId')
->where('wechatFriendId', 'in', $friendIds)
->group('wechatFriendId')
->buildSql();
// 查询最新消息的详细信息
$messageResults = Db::table('s2_wechat_message')
->alias('m')
->join([$subQuery => 'sub'], 'm.id = sub.max_id')
->field('m.*, sub.wechatFriendId')
->select();
foreach ($messageResults as $message) {
$latestMessages[$message['wechatFriendId']] = $message;
}
}
// 处理每个好友的数据
foreach ($list as $k => &$v) {
$v['createTime'] = !empty($v['createTime']) ? date('Y-m-d H:i:s', $v['createTime']) : '';
$v['updateTime'] = !empty($v['updateTime']) ? date('Y-m-d H:i:s', $v['updateTime']) : '';
$v['passTime'] = !empty($v['passTime']) ? date('Y-m-d H:i:s', $v['passTime']) : '';
$config = [
'unreadCount' => isset($unreadCounts[$v['id']]) ? $unreadCounts[$v['id']] : 0,
'chat' => isset($latestMessages[$v['id']]),
'msgTime' => isset($latestMessages[$v['id']]) ? $latestMessages[$v['id']]['wechatTime'] : 0
];
// 将消息配置添加到好友数据中
$v['config'] = $config;
}
unset($v);