健康分功能提交 + 微信客服页面改版

This commit is contained in:
wong
2025-11-26 11:17:23 +08:00
parent cd41190663
commit 7e2dd2914d
13 changed files with 2945 additions and 481 deletions

View File

@@ -36,13 +36,13 @@ Route::group('v1/', function () {
Route::get(':id/summary', 'app\cunkebao\controller\wechat\GetWechatOnDeviceSummarizeV1Controller@index');
Route::get(':id/friends', 'app\cunkebao\controller\wechat\GetWechatOnDeviceFriendsV1Controller@index');
Route::get('getWechatInfo', 'app\cunkebao\controller\wechat\GetWechatController@getWechatInfo');
Route::get(':wechatId', 'app\cunkebao\controller\wechat\GetWechatProfileV1Controller@index');
Route::post('transfer-friends', 'app\cunkebao\controller\wechat\PostTransferFriends@index'); // 微信好友转移
Route::get('overview', 'app\cunkebao\controller\wechat\GetWechatOverviewV1Controller@index'); // 获取微信账号概览数据
Route::get('moments', 'app\cunkebao\controller\wechat\GetWechatMomentsV1Controller@index'); // 获取微信朋友圈
Route::get('count', 'app\cunkebao\controller\DeviceWechat@count');
Route::get('device-count', 'app\cunkebao\controller\DeviceWechat@deviceCount'); // 获取有登录微信的设备数量
Route::put('refresh', 'app\cunkebao\controller\DeviceWechat@refresh'); // 刷新设备微信状态
Route::post('transfer-friends', 'app\cunkebao\controller\wechat\PostTransferFriends@index'); // 微信好友转移
Route::get(':wechatId', 'app\cunkebao\controller\wechat\GetWechatProfileV1Controller@index');
});
// 获客场景相关

View File

@@ -1061,6 +1061,7 @@ class ContentLibraryController extends Controller
$where = [
['isDel', '=', 0], // 未删除
['status', '=', 1], // 已开启
['id', '=', 99], // 已开启
];
// 查询符合条件的内容库
@@ -1225,7 +1226,7 @@ class ContentLibraryController extends Controller
foreach ($friends as $friend) {
$processedFriends++;
// 如果配置了API并且需要主动获取朋友圈
if ($needFetch) {
try {
@@ -1264,9 +1265,9 @@ class ContentLibraryController extends Controller
}
// 如果指定了采集类型,进行过滤
if (!empty($catchTypes)) {
/*if (!empty($catchTypes)) {
$query->whereIn('type', $catchTypes);
}
}*/
// 获取最近20条朋友圈
$moments = $query->page(1, 20)->select();
@@ -1289,7 +1290,7 @@ class ContentLibraryController extends Controller
continue;
}
// 如果启用了AI处理
/* // 如果启用了AI处理
if (!empty($library['aiEnabled']) && !empty($content)) {
try {
$contentAi = $this->aiRewrite($library, $content);
@@ -1300,7 +1301,7 @@ class ContentLibraryController extends Controller
\think\facade\Log::error('AI处理失败: ' . $e->getMessage() . ' [朋友圈ID: ' . ($moment['id'] ?? 'unknown') . ']');
$moment['contentAi'] = '';
}
}
}*/
// 保存到内容库的content_item表
if ($this->saveMomentToContentItem($moment, $library['id'], $friend, $nickname)) {

View File

@@ -0,0 +1,204 @@
<?php
namespace app\cunkebao\controller\wechat;
use app\common\model\Device as DeviceModel;
use app\common\model\DeviceUser as DeviceUserModel;
use app\common\model\DeviceWechatLogin as DeviceWechatLoginModel;
use app\common\model\User as UserModel;
use app\cunkebao\controller\BaseController;
use library\ResponseHelper;
use think\Db;
/**
* 查看微信朋友圈列表(仅限当前操盘手可访问的微信)
*/
class GetWechatMomentsV1Controller extends BaseController
{
/**
* 主操盘手获取项目下所有设备ID
*
* @return array
*/
protected function getCompanyDevicesId(): array
{
return DeviceModel::where('companyId', $this->getUserInfo('companyId'))
->column('id');
}
/**
* 非主操盘手仅可查看分配到的设备
*
* @return array
*/
protected function getUserDevicesId(): array
{
return DeviceUserModel::where([
'userId' => $this->getUserInfo('id'),
'companyId' => $this->getUserInfo('companyId'),
])->column('deviceId');
}
/**
* 获取当前用户可访问的设备ID
*
* @return array
*/
protected function getDevicesId(): array
{
return ($this->getUserInfo('isAdmin') == UserModel::ADMIN_STP)
? $this->getCompanyDevicesId()
: $this->getUserDevicesId();
}
/**
* 获取用户可访问的微信ID集合
*
* @return array
* @throws \Exception
*/
protected function getAccessibleWechatIds(): array
{
$deviceIds = $this->getDevicesId();
if (empty($deviceIds)) {
throw new \Exception('暂无可用设备', 200);
}
return DeviceWechatLoginModel::distinct(true)
->where('companyId', $this->getUserInfo('companyId'))
->whereIn('deviceId', $deviceIds)
->column('wechatId');
}
/**
* 查看朋友圈列表
*
* @return \think\response\Json
*/
public function index()
{
try {
$wechatId = $this->request->param('wechatId/s', '');
if (empty($wechatId)) {
return ResponseHelper::error('wechatId不能为空');
}
// 权限校验:只能查看当前账号可访问的微信
$accessibleWechatIds = $this->getAccessibleWechatIds();
if (!in_array($wechatId, $accessibleWechatIds, true)) {
return ResponseHelper::error('无权查看该微信的朋友圈', 403);
}
// 获取对应的微信账号ID
$accountId = Db::table('s2_wechat_account')
->where('wechatId', $wechatId)
->value('id');
if (empty($accountId)) {
return ResponseHelper::error('微信账号不存在或尚未同步', 404);
}
$query = Db::table('s2_wechat_moments')
->where('wechatAccountId', $accountId);
// 关键词搜索
if ($keyword = trim((string)$this->request->param('keyword', ''))) {
$query->whereLike('content', '%' . $keyword . '%');
}
// 类型筛选
$type = $this->request->param('type', '');
if ($type !== '' && $type !== null) {
$query->where('type', (int)$type);
}
// 时间筛选
$startTime = $this->request->param('startTime', '');
$endTime = $this->request->param('endTime', '');
if ($startTime || $endTime) {
$start = $startTime ? strtotime($startTime) : 0;
$end = $endTime ? strtotime($endTime) : time();
if ($start && $end && $end < $start) {
return ResponseHelper::error('结束时间不能早于开始时间');
}
$query->whereBetween('createTime', [$start ?: 0, $end ?: time()]);
}
$page = (int)$this->request->param('page', 1);
$limit = (int)$this->request->param('limit', 10);
$paginator = $query->order('createTime', 'desc')
->paginate($limit, false, ['page' => $page]);
$list = array_map(function ($item) {
return $this->formatMomentRow($item);
}, $paginator->items());
return ResponseHelper::success([
'list' => $list,
'total' => $paginator->total(),
'page' => $page,
'limit' => $limit,
]);
} catch (\Exception $e) {
return ResponseHelper::error($e->getMessage(), $e->getCode() ?: 500);
}
}
/**
* 格式化朋友圈数据
*
* @param array $row
* @return array
*/
protected function formatMomentRow(array $row): array
{
$formatTime = function ($timestamp) {
if (empty($timestamp)) {
return '';
}
return is_numeric($timestamp)
? date('Y-m-d H:i:s', $timestamp)
: date('Y-m-d H:i:s', strtotime($timestamp));
};
return [
'id' => (int)$row['id'],
'snsId' => $row['snsId'] ?? '',
'type' => (int)($row['type'] ?? 0),
'content' => $row['content'] ?? '',
'commentList' => $this->decodeJson($row['commentList'] ?? null),
'likeList' => $this->decodeJson($row['likeList'] ?? null),
'resUrls' => $this->decodeJson($row['resUrls'] ?? null),
'createTime' => $formatTime($row['createTime'] ?? null),
'momentEntity' => [
'lat' => $row['lat'] ?? 0,
'lng' => $row['lng'] ?? 0,
'location' => $row['location'] ?? '',
'picSize' => $row['picSize'] ?? 0,
'userName' => $row['userName'] ?? '',
],
];
}
/**
* JSON字段解析
*
* @param mixed $value
* @return array
*/
protected function decodeJson($value): array
{
if (empty($value)) {
return [];
}
if (is_array($value)) {
return $value;
}
$decoded = json_decode($value, true);
return $decoded ?: [];
}
}

View File

@@ -0,0 +1,411 @@
<?php
namespace app\cunkebao\controller\wechat;
use app\common\service\WechatAccountHealthScoreService;
use app\cunkebao\controller\BaseController;
use library\ResponseHelper;
use think\Db;
/**
* 微信账号概览控制器
* 提供账号概览页面的所有数据接口
*/
class GetWechatOverviewV1Controller extends BaseController
{
/**
* 获取微信账号概览数据
*
* @return \think\response\Json
*/
public function index()
{
try {
$wechatId = $this->request->param('wechatId', '');
if (empty($wechatId)) {
return ResponseHelper::error('微信ID不能为空');
}
$companyId = $this->getUserInfo('companyId');
// 获取微信账号IDaccountId
$account = Db::table('s2_wechat_account')
->where('wechatId', $wechatId)
->find();
if (empty($account)) {
return ResponseHelper::error('微信账号不存在');
}
$accountId = $account['id'];
// 1. 健康分评估
$healthScoreData = $this->getHealthScoreAssessment($accountId, $wechatId);
// 2. 账号价值(模拟数据)
$accountValue = $this->getAccountValue($accountId);
// 3. 今日价值变化(模拟数据)
$todayValueChange = $this->getTodayValueChange($accountId);
// 4. 好友总数
$totalFriends = $this->getTotalFriends($wechatId, $companyId);
// 5. 今日新增好友
$todayNewFriends = $this->getTodayNewFriends($wechatId);
// 6. 高价群聊
$highValueChatrooms = $this->getHighValueChatrooms($wechatId, $companyId);
// 7. 今日新增群聊
$todayNewChatrooms = $this->getTodayNewChatrooms($wechatId, $companyId);
$result = [
'healthScoreAssessment' => $healthScoreData,
'accountValue' => $accountValue,
'todayValueChange' => $todayValueChange,
'totalFriends' => $totalFriends,
'todayNewFriends' => $todayNewFriends,
'highValueChatrooms' => $highValueChatrooms,
'todayNewChatrooms' => $todayNewChatrooms,
];
return ResponseHelper::success($result);
} catch (\Exception $e) {
return ResponseHelper::error($e->getMessage(), $e->getCode() ?: 500);
}
}
/**
* 获取健康分评估数据
*
* @param int $accountId 账号ID
* @param string $wechatId 微信ID
* @return array
*/
protected function getHealthScoreAssessment($accountId, $wechatId)
{
// 获取健康分信息
$healthScoreService = new WechatAccountHealthScoreService();
$healthScoreInfo = $healthScoreService->getHealthScore($accountId);
$healthScore = $healthScoreInfo['healthScore'] ?? 0;
$maxAddFriendPerDay = $healthScoreInfo['maxAddFriendPerDay'] ?? 0;
// 获取今日已加好友数
$todayAdded = $this->getTodayAddedCount($wechatId);
// 获取最后添加时间
$lastAddTime = $this->getLastAddTime($wechatId);
// 判断状态标签
$statusTag = $todayAdded > 0 ? '已添加加人' : '';
// 获取基础构成
$baseComposition = $this->getBaseComposition($healthScoreInfo);
// 获取动态记录
$dynamicRecords = $this->getDynamicRecords($healthScoreInfo);
return [
'score' => $healthScore,
'dailyLimit' => $maxAddFriendPerDay,
'todayAdded' => $todayAdded,
'lastAddTime' => $lastAddTime,
'statusTag' => $statusTag,
'baseComposition' => $baseComposition,
'dynamicRecords' => $dynamicRecords,
];
}
/**
* 获取基础构成数据
*
* @param array $healthScoreInfo 健康分信息
* @return array
*/
protected function getBaseComposition($healthScoreInfo)
{
$baseScore = $healthScoreInfo['baseScore'] ?? 0;
$baseInfoScore = $healthScoreInfo['baseInfoScore'] ?? 0;
$friendCountScore = $healthScoreInfo['friendCountScore'] ?? 0;
$friendCount = $healthScoreInfo['friendCount'] ?? 0;
// 账号基础分默认60分
$accountBaseScore = 60;
// 已修改微信号如果baseInfoScore > 0说明已修改
$isModifiedAlias = $baseInfoScore > 0;
$composition = [
[
'name' => '账号基础分',
'score' => $accountBaseScore,
'formatted' => '+' . $accountBaseScore,
]
];
// 如果已修改微信号,添加基础信息分
if ($isModifiedAlias) {
$composition[] = [
'name' => '已修改微信号',
'score' => $baseInfoScore,
'formatted' => '+' . $baseInfoScore,
];
}
// 好友数量加成
if ($friendCountScore > 0) {
$composition[] = [
'name' => '好友数量加成',
'score' => $friendCountScore,
'formatted' => '+' . $friendCountScore,
'friendCount' => $friendCount, // 显示好友总数
];
}
return $composition;
}
/**
* 获取动态记录数据
*
* @param array $healthScoreInfo 健康分信息
* @return array
*/
protected function getDynamicRecords($healthScoreInfo)
{
$records = [];
$frequentPenalty = $healthScoreInfo['frequentPenalty'] ?? 0;
$frequentCount = $healthScoreInfo['frequentCount'] ?? 0;
$banPenalty = $healthScoreInfo['banPenalty'] ?? 0;
$isBanned = $healthScoreInfo['isBanned'] ?? 0;
$noFrequentBonus = $healthScoreInfo['noFrequentBonus'] ?? 0;
$consecutiveNoFrequentDays = $healthScoreInfo['consecutiveNoFrequentDays'] ?? 0;
$lastFrequentTime = $healthScoreInfo['lastFrequentTime'] ?? null;
// 频繁扣分记录
// 根据frequentCount判断是首次还是再次
// frequentPenalty存储的是当前状态的扣分-15或-25不是累计值
if ($frequentCount > 0 && $frequentPenalty < 0) {
if ($frequentCount == 1) {
// 首次频繁:-15分
$records[] = [
'name' => '首次触发限额',
'score' => $frequentPenalty,
'formatted' => (string)$frequentPenalty,
'type' => 'penalty',
'time' => $lastFrequentTime ? date('Y-m-d H:i:s', $lastFrequentTime) : null,
];
} else {
// 再次频繁:-25分
$records[] = [
'name' => '再次触发限额',
'score' => $frequentPenalty,
'formatted' => (string)$frequentPenalty,
'type' => 'penalty',
'time' => $lastFrequentTime ? date('Y-m-d H:i:s', $lastFrequentTime) : null,
];
}
}
// 封号扣分记录
if ($isBanned && $banPenalty < 0) {
$lastBanTime = $healthScoreInfo['lastBanTime'] ?? null;
$records[] = [
'name' => '封号',
'score' => $banPenalty,
'formatted' => (string)$banPenalty,
'type' => 'penalty',
'time' => $lastBanTime ? date('Y-m-d H:i:s', $lastBanTime) : null,
];
}
// 不频繁加分记录
if ($noFrequentBonus > 0 && $consecutiveNoFrequentDays >= 3) {
$lastNoFrequentTime = $healthScoreInfo['lastNoFrequentTime'] ?? null;
$records[] = [
'name' => '连续' . $consecutiveNoFrequentDays . '天不触发频繁',
'score' => $noFrequentBonus,
'formatted' => '+' . $noFrequentBonus,
'type' => 'bonus',
'time' => $lastNoFrequentTime ? date('Y-m-d H:i:s', $lastNoFrequentTime) : null,
];
}
return $records;
}
/**
* 获取今日已加好友数
*
* @param string $wechatId 微信ID
* @return int
*/
protected function getTodayAddedCount($wechatId)
{
$start = strtotime(date('Y-m-d 00:00:00'));
$end = strtotime(date('Y-m-d 23:59:59'));
return Db::table('s2_friend_task')
->where('wechatId', $wechatId)
->whereBetween('createTime', [$start, $end])
->count();
}
/**
* 获取最后添加时间
*
* @param string $wechatId 微信ID
* @return string
*/
protected function getLastAddTime($wechatId)
{
$lastTask = Db::table('s2_friend_task')
->where('wechatId', $wechatId)
->order('createTime', 'desc')
->find();
if (empty($lastTask) || empty($lastTask['createTime'])) {
return '';
}
return date('H:i:s', $lastTask['createTime']);
}
/**
* 获取账号价值(模拟数据)
*
* @param int $accountId 账号ID
* @return array
*/
protected function getAccountValue($accountId)
{
// TODO: 后续替换为真实计算逻辑
// 模拟数据¥29,800
$value = 29800;
return [
'value' => $value,
'formatted' => '¥' . number_format($value, 0, '.', ','),
];
}
/**
* 获取今日价值变化(模拟数据)
*
* @param int $accountId 账号ID
* @return array
*/
protected function getTodayValueChange($accountId)
{
// TODO: 后续替换为真实计算逻辑
// 模拟数据:+500
$change = 500;
return [
'change' => $change,
'formatted' => $change > 0 ? '+' . $change : (string)$change,
'isPositive' => $change > 0,
];
}
/**
* 获取好友总数
*
* @param string $wechatId 微信ID
* @param int $companyId 公司ID
* @return int
*/
protected function getTotalFriends($wechatId, $companyId)
{
// 优先从 s2_wechat_account 表获取
$account = Db::table('s2_wechat_account')
->where('wechatId', $wechatId)
->field('totalFriend')
->find();
if (!empty($account) && isset($account['totalFriend'])) {
return (int)$account['totalFriend'];
}
// 如果 totalFriend 为空,则从 s2_wechat_friend 表统计
return Db::table('s2_wechat_friend')
->where('ownerWechatId', $wechatId)
->where('isDeleted', 0)
->count();
}
/**
* 获取今日新增好友数
*
* @param string $wechatId 微信ID
* @return int
*/
protected function getTodayNewFriends($wechatId)
{
$start = strtotime(date('Y-m-d 00:00:00'));
$end = strtotime(date('Y-m-d 23:59:59'));
// 从 s2_wechat_friend 表统计今日新增
return Db::table('s2_wechat_friend')
->where('ownerWechatId', $wechatId)
->whereBetween('createTime', [$start, $end])
->where('isDeleted', 0)
->count();
}
/**
* 获取高价群聊数量
* 高价群聊定义:群成员数 >= 50 的群聊
*
* @param string $wechatId 微信ID
* @param int $companyId 公司ID
* @return int
*/
protected function getHighValueChatrooms($wechatId, $companyId)
{
// 高价群聊定义:群成员数 >= 50
$minMemberCount = 50;
// 查询该微信账号下的高价群聊
// 使用子查询统计每个群的成员数
$result = Db::query("
SELECT COUNT(DISTINCT c.chatroomId) as count
FROM s2_wechat_chatroom c
INNER JOIN (
SELECT chatroomId, COUNT(*) as memberCount
FROM s2_wechat_chatroom_member
GROUP BY chatroomId
HAVING memberCount >= ?
) m ON c.chatroomId = m.chatroomId
WHERE c.wechatAccountWechatId = ?
AND c.isDeleted = 0
", [$minMemberCount, $wechatId]);
return !empty($result) ? (int)$result[0]['count'] : 0;
}
/**
* 获取今日新增群聊数
*
* @param string $wechatId 微信ID
* @param int $companyId 公司ID
* @return int
*/
protected function getTodayNewChatrooms($wechatId, $companyId)
{
$start = strtotime(date('Y-m-d 00:00:00'));
$end = strtotime(date('Y-m-d 23:59:59'));
return Db::table('s2_wechat_chatroom')
->where('wechatAccountWechatId', $wechatId)
->whereBetween('createTime', [$start, $end])
->where('isDeleted', 0)
->count();
}
}

View File

@@ -8,14 +8,25 @@ use app\common\model\DeviceUser as DeviceUserModel;
use app\common\model\DeviceWechatLogin as DeviceWechatLoginModel;
use app\common\model\User as UserModel;
use app\common\model\WechatAccount as WechatAccountModel;
use app\common\model\WechatCustomer as WechatCustomerModel;
use app\common\model\WechatFriendShip as WechatFriendShipModel;
// 不再使用WechatFriendShipModelWechatCustomerModel改为直接查询s2_wechat_friend和s2_wechat_account_score表
use app\cunkebao\controller\BaseController;
use library\ResponseHelper;
use think\Db;
/**
* 微信控制器
*
* 性能优化建议:
* 1. 为以下字段添加索引以提高查询性能:
* - device_wechat_login表: (companyId, wechatId), (deviceId)
* - wechat_account表: (wechatId)
* - wechat_customer表: (companyId, wechatId)
* - wechat_friend_ship表: (ownerWechatId), (createTime)
* - s2_wechat_message表: (wechatAccountId, wechatTime)
*
* 2. 考虑创建以下复合索引:
* - device_wechat_login表: (companyId, deviceId, wechatId)
* - wechat_friend_ship表: (ownerWechatId, createTime)
*/
class GetWechatsOnDevicesV1Controller extends BaseController
{
@@ -66,6 +77,7 @@ class GetWechatsOnDevicesV1Controller extends BaseController
/**
* 获取有登录设备的微信id
* 优化:使用索引字段,减少数据查询量
*
* @return array
*/
@@ -76,12 +88,12 @@ class GetWechatsOnDevicesV1Controller extends BaseController
throw new \Exception('暂无设备数据', 200);
}
return DeviceWechatLoginModel::where(
[
// 优化直接使用DISTINCT减少数据传输量
return DeviceWechatLoginModel::distinct(true)
->where([
'companyId' => $this->getUserInfo('companyId'),
// 'alive' => DeviceWechatLoginModel::ALIVE_WECHAT_ACTIVE,
]
)
])
->where('deviceId', 'in', $deviceIds)
->column('wechatId');
}
@@ -110,24 +122,50 @@ class GetWechatsOnDevicesV1Controller extends BaseController
/**
* 获取在线微信账号列表
* 优化减少查询字段使用索引优化JOIN条件
*
* @param array $where
* @return \think\Paginator 分页对象
*/
protected function getOnlineWechatList(array $where): \think\Paginator
{
// 获取微信在线状态筛选参数1=在线0=离线,不传=全部)
$wechatStatus = $this->request->param('wechatStatus');
// 优化只查询必要字段使用FORCE INDEX提示数据库使用索引
$query = WechatAccountModel::alias('w')
->field(
[
'w.id', 'w.nickname', 'w.avatar', 'w.wechatId',
'CASE WHEN w.alias IS NULL OR w.alias = "" THEN w.wechatId ELSE w.alias END AS wechatAccount',
'l.deviceId','l.alive'
'MAX(l.deviceId) as deviceId', 'MAX(l.alive) as alive' // 使用MAX确保GROUP BY时获取正确的在线状态
]
)
->join('device_wechat_login l', 'w.wechatId = l.wechatId AND l.companyId = '. $this->getUserInfo('companyId'))
->order('w.id desc')
->group('w.wechatId');
// 优化使用INNER JOIN代替LEFT JOIN并添加索引提示
->join('device_wechat_login l', 'w.wechatId = l.wechatId AND l.companyId = '. $this->getUserInfo('companyId'), 'INNER')
// 添加s2_wechat_account表的LEFT JOIN用于筛选微信在线状态
->join(['s2_wechat_account' => 'sa'], 'w.wechatId = sa.wechatId', 'LEFT')
->group('w.wechatId')
// 优化在线状态优先排序alive=1的排在前面然后按wechatId排序
// 注意ORDER BY使用SELECT中定义的别名alive而不是聚合函数
->order('alive desc, w.wechatId desc');
// 根据wechatStatus参数筛选1=在线0=离线,不传=全部)
if ($wechatStatus !== null && $wechatStatus !== '') {
$wechatStatus = (int)$wechatStatus;
if ($wechatStatus === 1) {
// 筛选在线wechatAlive = 1
$query->where('sa.wechatAlive', 1);
} elseif ($wechatStatus === 0) {
// 筛选离线wechatAlive = 0 或 NULL
$query->where(function($query) {
$query->where('sa.wechatAlive', 0)
->whereOr('sa.wechatAlive', 'exp', 'IS NULL');
});
}
}
// 应用查询条件
foreach ($where as $key => $value) {
if (is_numeric($key) && is_array($value) && isset($value[0]) && $value[0] === 'exp') {
$query->whereExp('', $value[1]);
@@ -142,7 +180,12 @@ class GetWechatsOnDevicesV1Controller extends BaseController
$query->where($key, $value);
}
return $query->paginate($this->request->param('limit/d', 10), false, ['page' => $this->request->param('page/d', 1)]);
// 优化:使用简单计数查询
return $query->paginate(
$this->request->param('limit/d', 10),
false,
['page' => $this->request->param('page/d', 1)]
);
}
/**
@@ -167,9 +210,15 @@ class GetWechatsOnDevicesV1Controller extends BaseController
$metrics = $this->collectWechatMetrics($wechatIds);
foreach ($items as $item) {
$addLimit = $metrics['addLimit'][$item->wechatId] ?? 0;
$todayAdded = $metrics['todayAdded'][$item->wechatId] ?? 0;
// 计算今日可添加数量 = 可添加额度 - 今日已添加
$todayCanAdd = max(0, $addLimit - $todayAdded);
$sections = $item->toArray() + [
'times' => $metrics['addLimit'][$item->wechatId] ?? 0,
'addedCount' => $metrics['todayAdded'][$item->wechatId] ?? 0,
'times' => $addLimit,
'addedCount' => $todayAdded,
'todayCanAdd' => $todayCanAdd, // 今日可添加数量
'wechatStatus' => $metrics['wechatStatus'][$item->wechatId] ?? 0,
'totalFriend' => $metrics['totalFriend'][$item->wechatId] ?? 0,
'deviceMemo' => $metrics['deviceMemo'][$item->wechatId] ?? '',
@@ -184,6 +233,8 @@ class GetWechatsOnDevicesV1Controller extends BaseController
/**
* 批量收集微信账号的统计信息
* 优化:合并查询,减少数据库访问次数,使用缓存
*
* @param array $wechatIds
* @return array
*/
@@ -203,106 +254,167 @@ class GetWechatsOnDevicesV1Controller extends BaseController
}
$companyId = $this->getUserInfo('companyId');
// 可添加好友额度
$weightRows = WechatCustomerModel::where('companyId', $companyId)
// 使用缓存键,避免短时间内重复查询
$cacheKey = 'wechat_metrics_' . md5(implode(',', $wechatIds) . '_' . $companyId);
// 尝试从缓存获取数据缓存5分钟
$cachedMetrics = cache($cacheKey);
if ($cachedMetrics) {
return $cachedMetrics;
}
// 优化1可添加好友额度 - 从s2_wechat_account_score表获取maxAddFriendPerDay
$scoreRows = Db::table('s2_wechat_account_score')
->whereIn('wechatId', $wechatIds)
->column('weight', 'wechatId');
foreach ($weightRows as $wechatId => $weight) {
$decoded = json_decode($weight, true);
$metrics['addLimit'][$wechatId] = $decoded['addLimit'] ?? 0;
->column('maxAddFriendPerDay', 'wechatId');
foreach ($scoreRows as $wechatId => $maxAddFriendPerDay) {
$metrics['addLimit'][$wechatId] = (int)($maxAddFriendPerDay ?? 0);
}
// 今日新增好友
// 优化2今日新增好友 - 使用索引字段和预计算
$start = strtotime(date('Y-m-d 00:00:00'));
$end = strtotime(date('Y-m-d 23:59:59'));
$todayRows = WechatFriendShipModel::whereIn('ownerWechatId', $wechatIds)
->whereBetween('createTime', [$start, $end])
->field('ownerWechatId, COUNT(*) as total')
->group('ownerWechatId')
->select();
foreach ($todayRows as $row) {
$wechatId = is_array($row) ? ($row['ownerWechatId'] ?? '') : ($row->ownerWechatId ?? '');
// 使用单次查询获取所有wechatIds的今日新增和总好友数
// 根据数据库结构使用s2_wechat_friend表而不是wechat_friend_ship
$friendshipStats = Db::query("
SELECT
ownerWechatId,
SUM(IF(createTime BETWEEN {$start} AND {$end}, 1, 0)) as today_added,
COUNT(*) as total_friend
FROM
s2_wechat_friend
WHERE
ownerWechatId IN ('" . implode("','", $wechatIds) . "')
AND isDeleted = 0
GROUP BY
ownerWechatId
");
// 处理结果
foreach ($friendshipStats as $row) {
$wechatId = $row['ownerWechatId'] ?? '';
if ($wechatId) {
$metrics['todayAdded'][$wechatId] = (int)(is_array($row) ? ($row['total'] ?? 0) : ($row->total ?? 0));
$metrics['todayAdded'][$wechatId] = (int)($row['today_added'] ?? 0);
$metrics['totalFriend'][$wechatId] = (int)($row['total_friend'] ?? 0);
}
}
// 总好友
$friendRows = WechatFriendShipModel::whereIn('ownerWechatId', $wechatIds)
->field('ownerWechatId, COUNT(*) as total')
->group('ownerWechatId')
// 优化3微信在线状态 - 从s2_wechat_account表获取wechatAlive
$wechatAccountRows = Db::table('s2_wechat_account')
->whereIn('wechatId', $wechatIds)
->field('wechatId, wechatAlive')
->select();
foreach ($friendRows as $row) {
$wechatId = is_array($row) ? ($row['ownerWechatId'] ?? '') : ($row->ownerWechatId ?? '');
if ($wechatId) {
$metrics['totalFriend'][$wechatId] = (int)(is_array($row) ? ($row['total'] ?? 0) : ($row->total ?? 0));
foreach ($wechatAccountRows as $row) {
$wechatId = $row['wechatId'] ?? '';
if (!empty($wechatId)) {
$metrics['wechatStatus'][$wechatId] = (int)($row['wechatAlive'] ?? 0);
}
}
// 设备状态与备注
// 优化4设备状态与备注 - 使用INNER JOIN和索引
$loginRows = Db::name('device_wechat_login')
->alias('l')
->leftJoin('device d', 'd.id = l.deviceId')
->field('l.wechatId,l.alive,d.memo')
->join('device d', 'd.id = l.deviceId', 'LEFT')
->field('l.wechatId, l.alive, d.memo')
->where('l.companyId', $companyId)
->whereIn('l.wechatId', $wechatIds)
->order('l.id', 'desc')
->select();
// 使用临时数组避免重复处理
$processedWechatIds = [];
foreach ($loginRows as $row) {
$wechatId = is_array($row) ? ($row['wechatId'] ?? '') : ($row->wechatId ?? '');
if (empty($wechatId) || isset($metrics['wechatStatus'][$wechatId])) {
continue;
$wechatId = $row['wechatId'] ?? '';
// 只处理每个wechatId的第一条记录最新的
if (!empty($wechatId) && !in_array($wechatId, $processedWechatIds)) {
// 如果s2_wechat_account表中没有wechatAlive则使用device_wechat_login的alive作为备用
if (!isset($metrics['wechatStatus'][$wechatId])) {
$metrics['wechatStatus'][$wechatId] = (int)($row['alive'] ?? 0);
}
$metrics['deviceMemo'][$wechatId] = $row['memo'] ?? '';
$processedWechatIds[] = $wechatId;
}
$metrics['wechatStatus'][$wechatId] = (int)(is_array($row) ? ($row['alive'] ?? 0) : ($row->alive ?? 0));
$metrics['deviceMemo'][$wechatId] = is_array($row) ? ($row['memo'] ?? '') : ($row->memo ?? '');
}
// 活跃时间
$accountMap = Db::table('s2_wechat_account')
->whereIn('wechatId', $wechatIds)
->column('id', 'wechatId');
if (!empty($accountMap)) {
$accountRows = Db::table('s2_wechat_message')
->whereIn('wechatAccountId', array_values($accountMap))
->field('wechatAccountId, MAX(wechatTime) as lastTime')
->group('wechatAccountId')
->select();
$accountLastTime = [];
foreach ($accountRows as $row) {
$accountId = is_array($row) ? ($row['wechatAccountId'] ?? 0) : ($row->wechatAccountId ?? 0);
if ($accountId) {
$accountLastTime[$accountId] = (int)(is_array($row) ? ($row['lastTime'] ?? 0) : ($row->lastTime ?? 0));
}
}
foreach ($accountMap as $wechatId => $accountId) {
if (isset($accountLastTime[$accountId]) && $accountLastTime[$accountId] > 0) {
$metrics['activeTime'][$wechatId] = date('Y-m-d H:i:s', $accountLastTime[$accountId]);
}
// 优化5活跃时间 - 使用JOIN减少查询次数
$activeTimeResults = Db::query("
SELECT
a.wechatId,
MAX(m.wechatTime) as lastTime
FROM
s2_wechat_account a
LEFT JOIN
s2_wechat_message m ON a.id = m.wechatAccountId
WHERE
a.wechatId IN ('" . implode("','", $wechatIds) . "')
GROUP BY
a.wechatId
");
foreach ($activeTimeResults as $row) {
$wechatId = $row['wechatId'] ?? '';
$lastTime = (int)($row['lastTime'] ?? 0);
if (!empty($wechatId) && $lastTime > 0) {
$metrics['activeTime'][$wechatId] = date('Y-m-d H:i:s', $lastTime);
} else {
$metrics['activeTime'][$wechatId] = '-';
}
}
// 确保所有wechatId都有wechatStatus值默认0
foreach ($wechatIds as $wechatId) {
if (!isset($metrics['wechatStatus'][$wechatId])) {
$metrics['wechatStatus'][$wechatId] = 0;
}
}
// 存入缓存有效期5分钟
cache($cacheKey, $metrics, 300);
return $metrics;
}
/**
* 获取在线微信账号列表
* 优化:添加缓存,优化分页逻辑
*
* @return \think\response\Json
*/
public function index()
{
try {
// 获取分页参数
$page = $this->request->param('page/d', 1);
$limit = $this->request->param('limit/d', 10);
$keyword = $this->request->param('keyword');
$wechatStatus = $this->request->param('wechatStatus');
// 创建缓存键(基于用户、分页、搜索条件和在线状态筛选)
$cacheKey = 'wechat_list_' . $this->getUserInfo('id') . '_' . $page . '_' . $limit . '_' . md5($keyword ?? '') . '_' . ($wechatStatus ?? 'all');
// 尝试从缓存获取数据缓存2分钟
$cachedData = cache($cacheKey);
if ($cachedData) {
return ResponseHelper::success($cachedData);
}
// 如果没有缓存,执行查询
$result = $this->getOnlineWechatList(
$this->makeWhere()
);
$responseData = [
'list' => $this->makeResultedSet($result),
'total' => $result->total(),
];
// 存入缓存有效期2分钟
cache($cacheKey, $responseData, 120);
return ResponseHelper::success(
[
'list' => $this->makeResultedSet($result),
'total' => $result->total(),
]
);
return ResponseHelper::success($responseData);
} catch (\Exception $e) {
return ResponseHelper::error($e->getMessage(), $e->getCode());
}