144 lines
5.2 KiB
PHP
144 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace app\chukebao\controller;
|
|
|
|
use app\chukebao\model\FriendSettings;
|
|
use library\ResponseHelper;
|
|
use think\Db;
|
|
|
|
class WechatFriendController 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('请先登录');
|
|
}
|
|
$query = Db::table('s2_wechat_friend')
|
|
->where(['accountId' => $accountId, 'isDeleted' => 0])
|
|
->order('id desc');
|
|
$total = $query->count();
|
|
$list = $query->page($page, $limit)->select();
|
|
|
|
|
|
// 提取所有好友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();
|
|
if (!empty($unreadResults)) {
|
|
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();
|
|
|
|
if (!empty($subQuery)) {
|
|
// 查询最新消息的详细信息
|
|
$messageResults = Db::table('s2_wechat_message')
|
|
->alias('m')
|
|
->join([$subQuery => 'sub'], 'm.id = sub.max_id')
|
|
->field('m.*, sub.wechatFriendId')
|
|
->select();
|
|
|
|
if (!empty($messageResults)) {
|
|
foreach ($messageResults as $message) {
|
|
$latestMessages[$message['wechatFriendId']] = $message;
|
|
}
|
|
}
|
|
}
|
|
}*/
|
|
|
|
|
|
$aiTypeData = [];
|
|
if (!empty($friendIds)) {
|
|
$aiTypeData = FriendSettings::where('friendId', 'in', $friendIds)->column('friendId,type');
|
|
}
|
|
|
|
|
|
// 处理每个好友的数据
|
|
foreach ($list as $k => &$v) {
|
|
$v['labels'] = json_decode($v['labels'], true);
|
|
$v['siteLabels'] = json_decode($v['siteLabels'], true);
|
|
$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;*/
|
|
|
|
|
|
$v['aiType'] = isset($aiTypeData[$v['id']]) ? $aiTypeData[$v['id']] : 0;
|
|
}
|
|
unset($v);
|
|
|
|
return ResponseHelper::success(['list' => $list, 'total' => $total]);
|
|
}
|
|
|
|
/**
|
|
* 获取单个好友详情
|
|
* @return \think\response\Json
|
|
*/
|
|
public function getDetail()
|
|
{
|
|
$friendId = $this->request->param('id');
|
|
$accountId = $this->getUserInfo('s2_accountId');
|
|
|
|
if (empty($accountId)) {
|
|
return ResponseHelper::error('请先登录');
|
|
}
|
|
|
|
if (empty($friendId)) {
|
|
return ResponseHelper::error('好友ID不能为空');
|
|
}
|
|
|
|
// 查询好友详情
|
|
$friend = Db::table('s2_wechat_friend')
|
|
->where(['id' => $friendId, 'isDeleted' => 0])
|
|
->find();
|
|
|
|
if (empty($friend)) {
|
|
return ResponseHelper::error('好友不存在');
|
|
}
|
|
|
|
// 处理好友数据
|
|
$friend['labels'] = json_decode($friend['labels'], true);
|
|
$friend['siteLabels'] = json_decode($friend['siteLabels'], true);
|
|
$friend['createTime'] = !empty($friend['createTime']) ? date('Y-m-d H:i:s', $friend['createTime']) : '';
|
|
$friend['updateTime'] = !empty($friend['updateTime']) ? date('Y-m-d H:i:s', $friend['updateTime']) : '';
|
|
$friend['passTime'] = !empty($friend['passTime']) ? date('Y-m-d H:i:s', $friend['passTime']) : '';
|
|
|
|
// 获取AI类型设置
|
|
$aiTypeSetting = FriendSettings::where('friendId', $friendId)->find();
|
|
$friend['aiType'] = $aiTypeSetting ? $aiTypeSetting['type'] : 0;
|
|
|
|
return ResponseHelper::success(['detail' => $friend]);
|
|
}
|
|
} |