Files
cunkebao_v3/Server/application/chukebao/controller/WechatChatroomController.php

144 lines
5.0 KiB
PHP
Raw Normal View History

2025-09-13 17:20:34 +08:00
<?php
namespace app\chukebao\controller;
use app\ai\controller\DouBaoAI;
use app\chukebao\controller\TokensRecordController as tokensRecord;
2025-09-13 17:20:34 +08:00
use library\ResponseHelper;
use think\Db;
class WechatChatroomController extends BaseController
{
public function getList(){
$page = $this->request->param('page', 1);
$limit = $this->request->param('limit', 10);
$accountId = $this->getUserInfo('s2_accountId');
if (empty($accountId)){
return ResponseHelper::error('请先登录');
}
2025-09-16 09:57:06 +08:00
$query = Db::table('s2_wechat_chatroom')
->where(['accountId' => $accountId,'isDeleted' => 0])
->order('id desc');
$list = $query->page($page, $limit)->select();
$total = $query->count();
2025-09-17 16:51:11 +08:00
// 提取所有聊天室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;
2025-09-16 09:57:06 +08:00
}
unset($v);
return ResponseHelper::success(['list'=>$list,'total'=>$total]);
2025-09-13 17:20:34 +08:00
}
public function aiAnnouncement()
{
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
$wechatAccountId = $this->request->param('wechatAccountId', '');
$groupId = $this->request->param('groupId', '');
$content = $this->request->param('content', '');
if (empty($groupId) || empty($content)|| empty($wechatAccountId)){
return ResponseHelper::error('参数缺失');
}
$tokens = Db::name('users')
->where('id', $userId)
->where('companyId', $companyId)
->value('tokens');
if ($tokens <= 0){
return ResponseHelper::error('用户Tokens余额不足');
}
$params = [
'model' => 'doubao-1-5-pro-32k-250115',
'messages' => [
['role' => 'system', 'content' => '你现在是存客宝的AI助理你精通中国大陆的法律'],
['role' => 'user', 'content' => $content],
],
];
//AI处理
$ai = new DouBaoAI();
$res = $ai->text($params);
$res = json_decode($res,true);
if ($res['code'] == 200) {
//扣除Tokens
$tokensRecord = new tokensRecord();
$nickname = Db::table('s2_wechat_chatroom')->where(['id' => $groupId])->value('nickname');
$remarks = !empty($nickname) ? '生成【'.$nickname.'】群公告' : '生成群公告';
$data = [
'tokens' => $res['data']['token'],
'type' => 0,
'form' => 3,
'wechatAccountId' => $wechatAccountId,
'friendIdOrGroupId' => $groupId,
'remarks' => $remarks,
];
$tokensRecord->consumeTokens($data);
return ResponseHelper::success($res['data']['content']);
}else{
return ResponseHelper::error($res['msg']);
}
exit_data($res);
}
2025-09-13 17:20:34 +08:00
}