2025-09-13 17:20:34 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace app\chukebao\controller;
|
|
|
|
|
|
|
2025-09-19 16:47:41 +08:00
|
|
|
|
use app\ai\controller\DouBaoAI;
|
|
|
|
|
|
use app\chukebao\controller\TokensRecordController as tokensRecord;
|
2025-09-30 16:10:43 +08:00
|
|
|
|
use app\chukebao\model\TokensCompany;
|
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);
|
2025-12-05 10:44:43 +08:00
|
|
|
|
$keyword = $this->request->param('keyword', '');
|
|
|
|
|
|
$groupIds = $this->request->param('groupIds', '');
|
2025-09-13 17:20:34 +08:00
|
|
|
|
$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')
|
2025-12-05 10:44:43 +08:00
|
|
|
|
->where(['accountId' => $accountId,'isDeleted' => 0]);
|
|
|
|
|
|
|
|
|
|
|
|
// 关键字搜索:群昵称、微信号(这里使用chatroomId作为群标识)
|
|
|
|
|
|
if ($keyword !== '' && $keyword !== null) {
|
|
|
|
|
|
$query->where(function ($q) use ($keyword) {
|
|
|
|
|
|
$like = '%' . $keyword . '%';
|
|
|
|
|
|
$q->whereLike('nickname', $like)
|
|
|
|
|
|
->whereOr('conRemark', 'like', $like);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 分组筛选:groupIds(单个分组ID)
|
|
|
|
|
|
if ($groupIds !== '' && $groupIds !== null) {
|
|
|
|
|
|
$query->where('groupIds', $groupIds);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$query->order('id desc');
|
2025-09-16 09:57:06 +08:00
|
|
|
|
$total = $query->count();
|
2025-09-25 17:52:56 +08:00
|
|
|
|
$list = $query->page($page, $limit)->select();
|
|
|
|
|
|
|
2025-09-16 09:57:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2025-09-19 16:47:41 +08:00
|
|
|
|
|
2025-10-28 09:25:39 +08:00
|
|
|
|
public function getDetail(){
|
|
|
|
|
|
$id = input('id', 0);
|
|
|
|
|
|
|
|
|
|
|
|
if (!$id) {
|
|
|
|
|
|
return ResponseHelper::error('聊天室ID不能为空');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$accountId = $this->getUserInfo('s2_accountId');
|
|
|
|
|
|
if (empty($accountId)){
|
|
|
|
|
|
return ResponseHelper::error('请先登录');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$detail = Db::table('s2_wechat_chatroom')
|
|
|
|
|
|
->where(['accountId' => $accountId, 'id' => $id, 'isDeleted' => 0])
|
|
|
|
|
|
->find();
|
2025-09-19 16:47:41 +08:00
|
|
|
|
|
2025-10-28 09:25:39 +08:00
|
|
|
|
if (!$detail) {
|
|
|
|
|
|
return ResponseHelper::error('聊天室不存在或无权限访问');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理时间格式
|
|
|
|
|
|
$detail['createTime'] = !empty($detail['createTime']) ? date('Y-m-d H:i:s', $detail['createTime']) : '';
|
|
|
|
|
|
$detail['updateTime'] = !empty($detail['updateTime']) ? date('Y-m-d H:i:s', $detail['updateTime']) : '';
|
|
|
|
|
|
|
|
|
|
|
|
// 查询未读消息数量
|
|
|
|
|
|
$unreadCount = Db::table('s2_wechat_message')
|
|
|
|
|
|
->where('wechatChatroomId', $id)
|
|
|
|
|
|
->where('isRead', 0)
|
|
|
|
|
|
->count();
|
|
|
|
|
|
|
|
|
|
|
|
// 查询最新消息
|
|
|
|
|
|
$latestMessage = Db::table('s2_wechat_message')
|
|
|
|
|
|
->where('wechatChatroomId', $id)
|
|
|
|
|
|
->order('id desc')
|
|
|
|
|
|
->find();
|
|
|
|
|
|
|
|
|
|
|
|
$config = [
|
|
|
|
|
|
'unreadCount' => $unreadCount,
|
|
|
|
|
|
'chat' => !empty($latestMessage),
|
|
|
|
|
|
'msgTime' => isset($latestMessage['wechatTime']) ? $latestMessage['wechatTime'] : 0
|
|
|
|
|
|
];
|
|
|
|
|
|
$detail['config'] = $config;
|
|
|
|
|
|
|
|
|
|
|
|
return ResponseHelper::success($detail);
|
|
|
|
|
|
}
|
2025-09-19 16:47:41 +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('参数缺失');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-30 16:10:43 +08:00
|
|
|
|
$tokens = TokensCompany::where(['companyId' => $companyId])->value('tokens');
|
|
|
|
|
|
if (empty($tokens) || $tokens <= 0){
|
2025-09-19 16:47:41 +08:00
|
|
|
|
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,
|
2025-12-09 15:01:38 +08:00
|
|
|
|
'form' => 14,
|
2025-09-19 16:47:41 +08:00
|
|
|
|
'wechatAccountId' => $wechatAccountId,
|
|
|
|
|
|
'friendIdOrGroupId' => $groupId,
|
|
|
|
|
|
'remarks' => $remarks,
|
|
|
|
|
|
];
|
|
|
|
|
|
$tokensRecord->consumeTokens($data);
|
|
|
|
|
|
return ResponseHelper::success($res['data']['content']);
|
|
|
|
|
|
}else{
|
|
|
|
|
|
return ResponseHelper::error($res['msg']);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-13 17:20:34 +08:00
|
|
|
|
}
|