From f08ffdb79bc0350ad40d17385c63865023529beb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E6=B8=85=E7=88=BD?= Date: Mon, 12 May 2025 17:59:34 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=81=E5=9F=9F=E6=93=8D=E7=9B=98=E6=89=8B?= =?UTF-8?q?=20-=20=E8=AE=BE=E5=A4=87=E5=BE=AE=E4=BF=A1=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cunkebao/api/wechat-accounts.ts | 12 +- Cunkebao/app/wechat-accounts/[id]/page.tsx | 2 +- Server/application/cunkebao/config/route.php | 3 +- .../cunkebao/controller/DeviceWechat.php | 581 ------------------ ...echatOnDeviceFriendProfileV1Controller.php | 33 + .../GetWechatOnDeviceFriendsV1Controller.php | 4 +- .../GetWechatsOnDevicesV1Controller.php | 1 + 7 files changed, 44 insertions(+), 592 deletions(-) delete mode 100644 Server/application/cunkebao/controller/DeviceWechat.php create mode 100644 Server/application/cunkebao/controller/wechat/GetWechatOnDeviceFriendProfileV1Controller.php diff --git a/Cunkebao/api/wechat-accounts.ts b/Cunkebao/api/wechat-accounts.ts index df4a654e..33ddf464 100644 --- a/Cunkebao/api/wechat-accounts.ts +++ b/Cunkebao/api/wechat-accounts.ts @@ -52,7 +52,7 @@ export const fetchWechatAccountList = async (params: QueryWechatAccountParams = if (params.order) queryParams.append('order', params.order); // 发起API请求 - return api.get(`/v1/device/wechats?${queryParams.toString()}`); + return api.get(`/v1/wechats?${queryParams.toString()}`); }; /** @@ -60,7 +60,7 @@ export const fetchWechatAccountList = async (params: QueryWechatAccountParams = * @returns 刷新结果 */ export const refreshWechatAccounts = async (): Promise<{ code: number; msg: string; data: any }> => { - return api.put<{ code: number; msg: string; data: any }>('/v1/device/wechats/refresh', {}); + return api.put<{ code: number; msg: string; data: any }>('/v1/wechats/refresh', {}); }; /** @@ -70,7 +70,7 @@ export const refreshWechatAccounts = async (): Promise<{ code: number; msg: stri * @returns 转移结果 */ export const transferWechatFriends = async (sourceId: string | number, targetId: string | number): Promise<{ code: number; msg: string; data: any }> => { - return api.post<{ code: number; msg: string; data: any }>('/v1/device/wechats/transfer-friends', { + return api.post<{ code: number; msg: string; data: any }>('/v1/wechats/transfer-friends', { source_id: sourceId, target_id: targetId }); @@ -147,9 +147,7 @@ export const transformWechatAccount = (serverAccount: any): import("@/types/wech */ export const fetchWechatFriends = async (wechatId: string, page: number = 1, pageSize: number = 20, searchQuery: string = '') => { try { - const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/v1/device/wechats/${wechatId}/friends?page=${page}&pageSize=${pageSize}&search=${searchQuery}`); - const data = await response.json(); - return data; + return api.get(`/v1/wechats/${wechatId}/friends?page=${page}&limit=${pageSize}${searchQuery ? `&search=${searchQuery}` : ''}`, true); } catch (error) { console.error("获取好友列表失败:", error); throw error; @@ -163,7 +161,7 @@ export const fetchWechatFriends = async (wechatId: string, page: number = 1, pag */ export const fetchWechatAccountSummary = async (id: string): Promise => { try { - return api.get(`/v1/device/wechats/${id}/summary`); + return api.get(`/v1/wechats/${id}/summary`); } catch (error) { console.error("获取账号概览失败:", error); throw error; diff --git a/Cunkebao/app/wechat-accounts/[id]/page.tsx b/Cunkebao/app/wechat-accounts/[id]/page.tsx index fab42f78..2849cbd5 100644 --- a/Cunkebao/app/wechat-accounts/[id]/page.tsx +++ b/Cunkebao/app/wechat-accounts/[id]/page.tsx @@ -396,7 +396,7 @@ export default function WechatAccountDetailPage() { setIsFetchingFriends(true); setHasFriendLoadError(false); - const data = await api.get>(`/v1/device/wechats/${id}/friends?page=${page}&limit=30`, true); + const data = await api.get>(`/v1/wechats/${id}/friends?page=${page}&limit=30`, true); if (data && data.code === 200) { // 更新总数计数 diff --git a/Server/application/cunkebao/config/route.php b/Server/application/cunkebao/config/route.php index 25c7f477..23dc2538 100644 --- a/Server/application/cunkebao/config/route.php +++ b/Server/application/cunkebao/config/route.php @@ -21,10 +21,11 @@ Route::group('v1/', function () { }); // 设备微信相关 - Route::group('device/wechats', function () { + Route::group('wechats', function () { Route::get('', 'app\cunkebao\controller\wechat\GetWechatsOnDevicesV1Controller@index'); // 获取在线微信账号列表 Route::get(':id/summary', 'app\cunkebao\controller\wechat\GetWechatOnDeviceSummarizeV1Controller@index'); // 获取微信号详情 Route::get(':id/friends', 'app\cunkebao\controller\wechat\GetWechatOnDeviceFriendsV1Controller@index'); // 获取微信好友列表 + Route::get(':id/friend/:id', 'app\cunkebao\controller\wechat\GetWechatOnDeviceFriendProfileV1Controller@index'); // 获取微信好友信息 Route::get('count', 'app\cunkebao\controller\DeviceWechat@count'); // 获取在线微信账号数量 Route::get('device-count', 'app\cunkebao\controller\DeviceWechat@deviceCount'); // 获取有登录微信的设备数量 diff --git a/Server/application/cunkebao/controller/DeviceWechat.php b/Server/application/cunkebao/controller/DeviceWechat.php deleted file mode 100644 index 7998b929..00000000 --- a/Server/application/cunkebao/controller/DeviceWechat.php +++ /dev/null @@ -1,581 +0,0 @@ - 200, - 'msg' => '获取成功', - 'data' => [ - 'count' => $count - ] - ]); - } catch (\Exception $e) { - return json([ - 'code' => 500, - 'msg' => '获取失败:' . $e->getMessage() - ]); - } - } - - /** - * 获取有登录微信的设备数量 - * @return \think\response\Json - */ - public function deviceCount() - { - try { - // 获取有登录微信的设备数量 - $count = WechatAccount::getDeviceWithWechatCount(); - - return json([ - 'code' => 200, - 'msg' => '获取成功', - 'data' => [ - 'count' => $count - ] - ]); - } catch (\Exception $e) { - return json([ - 'code' => 500, - 'msg' => '获取失败:' . $e->getMessage() - ]); - } - } - - /** - * 刷新设备微信状态 - * @return \think\response\Json - */ - public function refresh() - { - try { - return json([ - 'code' => 200, - 'msg' => '刷新成功', - 'data' => [] - ]); - } catch (\Exception $e) { - return json([ - 'code' => 500, - 'msg' => '获取失败:' . $e->getMessage() - ]); - } - } - - /** - * 获取在线微信账号列表 - * @return \think\response\Json - */ - public function index() - { - try { - // 获取登录用户信息 - $userInfo = request()->userInfo; - // 获取查询条件 - $where = []; - - // 微信ID - $wechatId = Request::param('wechat_id'); - if (!empty($wechatId)) { - $where['wechatId'] = ['like', "%{$wechatId}%"]; - } - - // 昵称 - $nickname = Request::param('nickname'); - if (!empty($nickname)) { - $where['nickname|accountNickname'] = ['like', "%{$nickname}%"]; - } - - // 获取分页参数 - $page = (int)Request::param('page', 1); - $limit = (int)Request::param('limit', 10); - - // 获取排序参数 - $sort = Request::param('sort', 'id'); - $order = Request::param('order', 'desc'); - - // 公司账户表没有 companyId,需要转换一下 - $acountInfo = CompanyAccount::getAccountByCompanyId($userInfo['companyId']); - - // 先用账号进行查询 - $where['accountUserName'] = $acountInfo['userName']; - - // 根据用户权限不同实现不同的查询逻辑 - if ($userInfo['isAdmin'] == 1) { - // 管理员直接查询tk_wechat_account表 - $list = WechatAccount::getOnlineWechatList($where, "{$sort} {$order}", $page, $limit); - } else { - // 非管理员先查询tk_device_user表 - $deviceIds = Db::table('tk_device_user') - ->where('companyId', $userInfo['companyId']) - ->where('userId', $userInfo['id']) - ->column('deviceId'); - - if (empty($deviceIds)) { - // 如果没有绑定设备,返回提示信息 - return json([ - 'code' => 403, - 'msg' => '请联系管理员绑定设备微信', - 'data' => [ - 'total' => 0, - 'list' => [] - ] - ]); - } - - // 获取这些设备关联的微信ID - $wechatIds = Db::table('tk_device_wechat_login') - ->where('companyId', $userInfo['companyId']) - ->whereIn('deviceId', $deviceIds) - ->column('wechatId'); - - if (empty($wechatIds)) { - return json([ - 'code' => 200, - 'msg' => '获取成功', - 'data' => [ - 'total' => 0, - 'list' => [] - ] - ]); - } - - // 将微信ID添加到查询条件中 - $where['id'] = ['in', $wechatIds]; - - // 查询微信账号 - $list = WechatAccount::getOnlineWechatList($where, "{$sort} {$order}", $page, $limit); - } - - // 处理返回数据 - $data = []; - foreach ($list->items() as $item) { - // 计算今日可添加好友数量(这里使用一个示例算法,你可以根据实际需求修改) - $canAddFriendCount = 20 - Db::table('tk_friend_task')->where('wechatId', $item['wechatId'])->count('*'); - if ($canAddFriendCount < 0) { - $canAddFriendCount = 0; - } - - // 计算今日新增好友数量(示例数据,实际需要从数据库获取或通过其他方式计算) - // 这里只是一个示例,你需要根据实际情况替换 - $todayNewFriendCount = Db::table('tk_friend_task')->where('wechatId', $item['wechatId']) - ->where('status', 3) - ->count('*'); - - $data[] = [ - 'id' => $item['id'], - 'wechatId' => $item['wechatId'], - 'nickname' => $item['nickname'] ?: $item['accountNickname'], - 'avatar' => $item['avatar'], - 'accountUserName' => $item['accountUserName'], - 'status' => $item['wechatAlive'] ? '在线' : '离线', - 'deviceStatus' => $item['deviceAlive'] ? '在线' : '离线', - 'totalFriend' => $item['totalFriend'], - 'canAddFriendCount' => $canAddFriendCount, - 'deviceInfo' => $item['imei'] . ($item['deviceMemo'] ? " ({$item['deviceMemo']})" : ''), - 'todayNewFriendCount' => $todayNewFriendCount - ]; - } - - return json([ - 'code' => 200, - 'msg' => '获取成功', - 'data' => [ - 'total' => $list->total(), - 'list' => $data - ] - ]); - } catch (\Exception $e) { - return json([ - 'code' => 500, - 'msg' => '获取失败:' . $e->getMessage() - ]); - } - } - - /** - * 获取微信号详情 - * @param int $id 微信号ID - * @return \think\response\Json - */ - public function detail($id) - { - try { - // 获取微信号基本信息 - $wechat = WechatAccount::where('id', $id) - ->where('isDeleted', 0) - ->find(); - - if (!$wechat) { - return json([ - 'code' => 404, - 'msg' => '微信号不存在' - ]); - } - - // 计算账号年龄(从创建时间到现在) - $accountAge = 0; - if ($wechat['createTime']) { - $createTime = strtotime($wechat['createTime']); - $now = time(); - $accountAge = floor(($now - $createTime) / (24 * 3600)); - } - - // 计算活跃程度(根据消息数) - $activityLevel = '低'; - if ($wechat['thirtyDayMsgCount'] > 1000) { - $activityLevel = '高'; - } elseif ($wechat['thirtyDayMsgCount'] > 500) { - $activityLevel = '中'; - } - - // 评估账号权重(示例算法) - $weight = 0; - // 基础权重 - $weight += 10; - // 好友数量权重 - $weight += min($wechat['totalFriend'] / 100, 20); - // 活跃度权重 - $weight += min($wechat['thirtyDayMsgCount'] / 100, 20); - // 账号年龄权重 - $weight += min($accountAge / 30, 10); - // 在线状态权重 - if ($wechat['wechatAlive']) { - $weight += 5; - } - - // 获取限制记录(示例数据,实际需要从数据库获取) - $restrictions = [ - [ - 'type' => '添加好友限制', - 'reason' => '频繁添加好友', - 'startTime' => date('Y-m-d H:i:s', strtotime('-1 day')), - 'endTime' => date('Y-m-d H:i:s', strtotime('+1 day')) - ] - ]; - - // 处理返回数据 - $data = [ - 'basicInfo' => [ - 'id' => $wechat['id'], - 'wechatId' => $wechat['wechatId'], - 'nickname' => $wechat['nickname'] ?: $wechat['accountNickname'], - 'avatar' => $wechat['avatar'], - 'status' => $wechat['wechatAlive'] ? '在线' : '离线', - 'deviceStatus' => $wechat['deviceAlive'] ? '在线' : '离线', - 'deviceInfo' => $wechat['imei'] . ($wechat['deviceMemo'] ? " ({$wechat['deviceMemo']})" : ''), - 'gender' => $wechat['gender'], - 'region' => $wechat['region'], - 'signature' => $wechat['signature'] - ], - 'statistics' => [ - 'totalFriend' => $wechat['totalFriend'], - 'maleFriend' => $wechat['maleFriend'], - 'femaleFriend' => $wechat['femaleFriend'], - 'canAddFriendCount' => 30 - (isset($wechat['yesterdayMsgCount']) ? intval($wechat['yesterdayMsgCount']) : 0), - 'yesterdayMsgCount' => $wechat['yesterdayMsgCount'], - 'sevenDayMsgCount' => $wechat['sevenDayMsgCount'], - 'thirtyDayMsgCount' => $wechat['thirtyDayMsgCount'] - ], - 'accountInfo' => [ - 'age' => $accountAge, - 'activityLevel' => $activityLevel, - 'weight' => round($weight, 2), - 'createTime' => $wechat['createTime'], - 'lastUpdateTime' => $wechat['updateTime'] - ], - 'restrictions' => $restrictions, - ]; - - return json([ - 'code' => 200, - 'msg' => '获取成功', - 'data' => $data - ]); - } catch (\Exception $e) { - return json([ - 'code' => 500, - 'msg' => '获取失败:' . $e->getMessage() - ]); - } - } - - /** - * 微信好友转移 - * 将一个微信号的好友转移至另一个在线微信号 - * - * @return \think\response\Json - */ - public function transferFriends() - { - try { - // 获取请求参数 - $sourceWechatId = Request::param('source_id'); // 源微信账号ID - $targetWechatId = Request::param('target_id'); // 目标微信账号ID - - // 参数验证 - if (empty($sourceWechatId) || empty($targetWechatId)) { - return json([ - 'code' => 400, - 'msg' => '参数错误:源微信账号ID和目标微信账号ID不能为空' - ]); - } - - // 检查源微信账号是否存在 - $sourceWechat = WechatAccount::where('id', $sourceWechatId) - ->where('isDeleted', 0) - ->find(); - - if (!$sourceWechat) { - return json([ - 'code' => 404, - 'msg' => '源微信账号不存在' - ]); - } - - // 检查目标微信账号是否存在且在线 - $targetWechat = WechatAccount::where('id', $targetWechatId) - ->where('isDeleted', 0) - ->where('wechatAlive', 1) - ->where('deviceAlive', 1) - ->find(); - - if (!$targetWechat) { - return json([ - 'code' => 404, - 'msg' => '目标微信账号不存在或不在线' - ]); - } - - // 获取源微信账号的好友列表 - $friends = Db::table('tk_wechat_friend') - ->where('wechatAccountId', $sourceWechatId) - ->where('isDeleted', 0) - ->select(); - - // 统计好友数量 - $totalFriends = count($friends); - - if ($totalFriends == 0) { - return json([ - 'code' => 400, - 'msg' => '源微信账号没有可转移的好友' - ]); - } - - // 开始事务 - Db::startTrans(); - - try { - $successCount = 0; - $failCount = 0; - $duplicateCount = 0; - $failList = []; - - foreach ($friends as $friend) { - // 检查目标微信账号是否已经有此好友 - $existFriend = Db::table('tk_wechat_friend') - ->where('wechatAccountId', $targetWechatId) - ->where('wechatId', $friend['wechatId']) - ->where('isDeleted', 0) - ->find(); - - if ($existFriend) { - // 已经存在此好友,跳过 - $duplicateCount++; - continue; - } - - // 准备插入数据 - $newFriend = [ - 'wechatAccountId' => $targetWechatId, - 'alias' => $friend['alias'], - 'wechatId' => $friend['wechatId'], - 'conRemark' => $friend['conRemark'], - 'nickname' => $friend['nickname'], - 'pyInitial' => $friend['pyInitial'], - 'quanPin' => $friend['quanPin'], - 'avatar' => $friend['avatar'], - 'gender' => $friend['gender'], - 'region' => $friend['region'], - 'addFrom' => $friend['addFrom'], - 'labels' => $friend['labels'], - 'signature' => $friend['signature'], - 'isDeleted' => 0, - 'isPassed' => $friend['isPassed'], - 'accountId' => $friend['accountId'], - 'extendFields' => $friend['extendFields'], - 'accountUserName' => $friend['accountUserName'], - 'accountRealName' => $friend['accountRealName'], - 'accountNickname' => $friend['accountNickname'], - 'ownerAlias' => $targetWechat['alias'], - 'ownerWechatId' => $targetWechat['wechatId'], - 'ownerNickname' => $targetWechat['nickname'] ?: $targetWechat['accountNickname'], - 'ownerAvatar' => $targetWechat['avatar'], - 'phone' => $friend['phone'], - 'thirdParty' => $friend['thirdParty'], - 'groupId' => $friend['groupId'], - 'passTime' => $friend['passTime'], - 'additionalPicture' => $friend['additionalPicture'], - 'desc' => $friend['desc'], - 'country' => $friend['country'], - 'province' => $friend['province'], - 'city' => $friend['city'], - 'createTime' => date('Y-m-d H:i:s'), - 'updateTime' => date('Y-m-d H:i:s') - ]; - - // 插入新好友记录 - $result = Db::table('tk_wechat_friend')->insert($newFriend); - - if ($result) { - $successCount++; - } else { - $failCount++; - $failList[] = [ - 'id' => $friend['id'], - 'wechatId' => $friend['wechatId'], - 'nickname' => $friend['nickname'] - ]; - } - } - - // 更新两个微信账号的好友数量 - $maleFriendsCount = Db::table('tk_wechat_friend') - ->where('wechatAccountId', $targetWechatId) - ->where('isDeleted', 0) - ->where('gender', 1) - ->count(); - - $femaleFriendsCount = Db::table('tk_wechat_friend') - ->where('wechatAccountId', $targetWechatId) - ->where('isDeleted', 0) - ->where('gender', 2) - ->count(); - - $totalFriendsCount = $maleFriendsCount + $femaleFriendsCount; - - // 更新目标微信账号的好友数量 - WechatAccount::where('id', $targetWechatId) - ->update([ - 'totalFriend' => $totalFriendsCount, - 'maleFriend' => $maleFriendsCount, - 'femaleFriend' => $femaleFriendsCount, - 'updateTime' => date('Y-m-d H:i:s') - ]); - - // 提交事务 - Db::commit(); - - return json([ - 'code' => 200, - 'msg' => '好友转移成功', - 'data' => [ - 'total' => $totalFriends, - 'success' => $successCount, - 'fail' => $failCount, - 'duplicate' => $duplicateCount, - 'failList' => $failList - ] - ]); - } catch (\Exception $e) { - // 回滚事务 - Db::rollback(); - throw $e; - } - } catch (\Exception $e) { - return json([ - 'code' => 500, - 'msg' => '好友转移失败:' . $e->getMessage() - ]); - } - } - - /** - * 获取微信好友列表 - * 根据wechatId查询微信好友,支持分页和关键词筛选 - * - * @return \think\response\Json - */ - public function getFriends() - { - try { - // 获取请求参数 - $wechatId = Request::param('wechatId'); - $page = (int)Request::param('page', 1); - $limit = (int)Request::param('limit', 20); - $keyword = Request::param('keyword', ''); - - // 参数验证 - if (empty($wechatId)) { - return json([ - 'code' => 400, - 'msg' => '参数错误:微信ID不能为空' - ]); - } - - // 查询参数 - $params = []; - if (!empty($keyword)) { - $params['keyword'] = $keyword; - } - - // 调用模型方法获取好友列表 - $result = WechatFriend::getFriendsByWechatId($wechatId, $params, $page, $limit); - - // 处理返回的数据 - $friendsList = []; - foreach ($result['list'] as $friend) { - $friendsList[] = [ - 'wechatId' => $friend['wechatId'], - 'avatar' => $friend['avatar'] ?: '/placeholder.svg', - 'labels' => $friend['labels'] ?: [], - 'accountNickname' => $friend['accountNickname'] ?: '', - 'accountRealName' => $friend['accountRealName'] ?: '', - 'nickname' => $friend['nickname'] ?: '', - 'remark' => $friend['conRemark'] ?: '', - 'alias' => $friend['alias'] ?: '', - 'gender' => $friend['gender'] ?: 0, - 'region' => $friend['region'] ?: '' - ]; - } - - return json([ - 'code' => 200, - 'msg' => '获取成功', - 'data' => [ - 'total' => $result['total'], - 'page' => $result['page'], - 'limit' => $result['limit'], - 'list' => $friendsList - ] - ]); - } catch (\Exception $e) { - return json([ - 'code' => 500, - 'msg' => '获取失败:' . $e->getMessage() - ]); - } - } -} \ No newline at end of file diff --git a/Server/application/cunkebao/controller/wechat/GetWechatOnDeviceFriendProfileV1Controller.php b/Server/application/cunkebao/controller/wechat/GetWechatOnDeviceFriendProfileV1Controller.php new file mode 100644 index 00000000..8f4c95fa --- /dev/null +++ b/Server/application/cunkebao/controller/wechat/GetWechatOnDeviceFriendProfileV1Controller.php @@ -0,0 +1,33 @@ +request->param('id/d'); + + } catch (\Exception $e) { + return ResponseHelper::error($e->getMessage(), $e->getCode()); + } + } +} \ No newline at end of file diff --git a/Server/application/cunkebao/controller/wechat/GetWechatOnDeviceFriendsV1Controller.php b/Server/application/cunkebao/controller/wechat/GetWechatOnDeviceFriendsV1Controller.php index 622e7899..5ad9a002 100644 --- a/Server/application/cunkebao/controller/wechat/GetWechatOnDeviceFriendsV1Controller.php +++ b/Server/application/cunkebao/controller/wechat/GetWechatOnDeviceFriendsV1Controller.php @@ -4,13 +4,13 @@ namespace app\cunkebao\controller\wechat; use app\common\model\WechatAccount as WechatAccountModel; use app\common\model\WechatFriendShip as WechatFriendShipModel; +use app\cunkebao\controller\BaseController; use library\ResponseHelper; -use think\Controller; /** * 设备微信控制器 */ -class GetWechatOnDeviceFriendsV1Controller extends Controller +class GetWechatOnDeviceFriendsV1Controller extends BaseController { /** * 构建返回数据 diff --git a/Server/application/cunkebao/controller/wechat/GetWechatsOnDevicesV1Controller.php b/Server/application/cunkebao/controller/wechat/GetWechatsOnDevicesV1Controller.php index db74b60c..68fde3ae 100644 --- a/Server/application/cunkebao/controller/wechat/GetWechatsOnDevicesV1Controller.php +++ b/Server/application/cunkebao/controller/wechat/GetWechatsOnDevicesV1Controller.php @@ -251,6 +251,7 @@ class GetWechatsOnDevicesV1Controller extends BaseController /** * 获取在线微信账号列表 + * * @return \think\response\Json */ public function index()