私域操盘手 - 设备关联微信列表逻辑调整

This commit is contained in:
柳清爽
2025-05-16 11:14:48 +08:00
parent 4934460997
commit 69024fa3f8
6 changed files with 78 additions and 68 deletions

View File

@@ -35,7 +35,7 @@ export const fetchDeviceDetail = async (id: string | number): Promise<ApiRespons
// 获取设备关联的微信账号
export const fetchDeviceRelatedAccounts = async (id: string | number): Promise<ApiResponse<any>> => {
return api.get<ApiResponse<any>>(`/v1/devices/${id}/related-accounts`);
return api.get<ApiResponse<any>>(`/v1/wechats/related-device/${id}`);
};
// 获取设备操作记录

View File

@@ -9,19 +9,19 @@ use think\facade\Route;
Route::group('v1/', function () {
// 设备管理相关
Route::group('devices', function () {
Route::get('add-results', 'app\cunkebao\controller\device\GetAddResultedV1Controller@index');
Route::get(':id/related-accounts', 'app\cunkebao\controller\device\GetRelatedAccountsV1Controller@index');
Route::get(':id/handle-logs', 'app\cunkebao\controller\device\GetDeviceHandleLogsV1Controller@index');
Route::get('', 'app\cunkebao\controller\device\GetDeviceListV1Controller@index');
Route::get(':id', 'app\cunkebao\controller\device\GetDeviceDetailV1Controller@index');
Route::post('', 'app\cunkebao\controller\device\PostAddDeviceV1Controller@index');
Route::put('refresh', 'app\cunkebao\controller\device\RefreshDeviceDetailV1Controller@index');
Route::delete(':id', 'app\cunkebao\controller\device\DeleteDeviceV1Controller@index');
Route::get('add-results', 'app\cunkebao\controller\device\GetAddResultedV1Controller@index');
Route::post('task-config', 'app\cunkebao\controller\device\UpdateDeviceTaskConfigV1Controller@index');
Route::get(':id/handle-logs', 'app\cunkebao\controller\device\GetDeviceHandleLogsV1Controller@index');
Route::get(':id', 'app\cunkebao\controller\device\GetDeviceDetailV1Controller@index');
Route::delete(':id', 'app\cunkebao\controller\device\DeleteDeviceV1Controller@index');
Route::get('', 'app\cunkebao\controller\device\GetDeviceListV1Controller@index');
Route::post('', 'app\cunkebao\controller\device\PostAddDeviceV1Controller@index');
});
// 设备微信相关
Route::group('wechats', function () {
Route::get('related-device/:id', 'app\cunkebao\controller\wechat\GetWechatsRelatedDeviceV1Controller@index');
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');

View File

@@ -26,13 +26,14 @@ class GetDeviceDetailV1Controller extends BaseController
*/
protected function checkUserDevicePermission(int $deviceId): void
{
$where = [
'deviceId' => $deviceId,
'userId' => $this->getUserInfo('id'),
'companyId' => $this->getUserInfo('companyId')
];
$hasPermission = DeviceUserModel::where($where)->count() > 0;
$hasPermission = DeviceUserModel::where(
[
'deviceId' => $deviceId,
'userId' => $this->getUserInfo('id'),
'companyId' => $this->getUserInfo('companyId')
]
)
->count() > 0;
if (!$hasPermission) {
throw new \Exception('您没有权限查看该设备', 403);
@@ -67,21 +68,19 @@ class GetDeviceDetailV1Controller extends BaseController
*/
protected function getTaskConfig(int $deviceId): array
{
$companyId = $this->getUserInfo('companyId');
$conf = DeviceTaskconfModel::alias('c')
->field([
'c.autoAddFriend', 'c.autoReply', 'c.momentsSync', 'c.aiChat'
])
->where(compact('companyId', 'deviceId'))
$conf = DeviceTaskconfModel::alias('c')->field([
'c.autoAddFriend', 'c.autoReply', 'c.momentsSync', 'c.aiChat'
])
->where(
[
'companyId' => $this->getUserInfo('companyId'),
'deviceId' => $deviceId
]
)
->find();
if (!is_null($conf)) {
return $conf->toArray();
}
// 未配置时赋予默认关闭的状态
return ArrHelper::getValue('autoAddFriend,autoReply,momentsSync,aiChat', [], 0);
return !is_null($conf) ? $conf->toArray() : ArrHelper::getValue('autoAddFriend,autoReply,momentsSync,aiChat', [], 0);
}
/**

View File

@@ -1,19 +1,20 @@
<?php
namespace app\cunkebao\controller\device;
namespace app\cunkebao\controller\wechat;
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\WechatFriendShip;
use app\common\model\WechatFriendShip as WechatFriendShipModel;
use app\cunkebao\controller\BaseController;
use library\ResponseHelper;
use think\model\Collection as ResultCollection;
/**
* 设备管理控制器
*/
class GetRelatedAccountsV1Controller extends BaseController
class GetWechatsRelatedDeviceV1Controller extends BaseController
{
/**
* 检查用户是否有权限操作指定设备
@@ -23,13 +24,14 @@ class GetRelatedAccountsV1Controller extends BaseController
*/
protected function checkUserDevicePermission(int $deviceId): void
{
$where = [
'deviceId' => $deviceId,
'userId' => $this->getUserInfo('id'),
'companyId' => $this->getUserInfo('companyId')
];
$hasPermission = DeviceUserModel::where($where)->count() > 0;
$hasPermission = DeviceUserModel::where(
[
'deviceId' => $deviceId,
'userId' => $this->getUserInfo('id'),
'companyId' => $this->getUserInfo('companyId')
]
)
->count() > 0;
if (!$hasPermission) {
throw new \Exception('您没有权限查看该设备', 403);
@@ -39,34 +41,35 @@ class GetRelatedAccountsV1Controller extends BaseController
/**
* 查询设备关联的微信ID列表
*
* @param int $deviceId 设备ID
* @return array 微信ID列表
* @param int $deviceId
* @return array
*/
protected function getDeviceWechatIds(int $deviceId): array
{
$where = [
'deviceId' => $deviceId,
'companyId' => $this->getUserInfo('companyId')
];
return DeviceWechatLoginModel::where($where)->group('wechatId')->column('wechatId');
return DeviceWechatLoginModel::where(
[
'deviceId' => $deviceId,
'companyId' => $this->getUserInfo('companyId')
]
)
->group('wechatId')->column('wechatId');
}
/**
* 通过设备关联的微信号列表获取微信账号
*
* @param array $wechatIds
* @return array
* @return ResultCollection
*/
protected function getWechatAccountsByIds(array $wechatIds): array
protected function getWechatAccountsByIds(array $wechatIds): ResultCollection
{
return (array)WechatAccountModel::alias('a')
return WechatAccountModel::alias('w')
->field([
'a.wechatId', 'a.nickname', 'a.avatar', 'a.gender', 'a.createTime'
'w.wechatId', 'w.nickname', 'w.avatar', 'w.gender', 'w.createTime',
'CASE WHEN w.alias IS NULL OR w.alias = "" THEN w.wechatId ELSE w.alias END AS wechatAccount',
])
->whereIn('a.wechatId', $wechatIds)
->select()
->toArray();
->whereIn('w.wechatId', $wechatIds)
->select();
}
/**
@@ -75,9 +78,9 @@ class GetRelatedAccountsV1Controller extends BaseController
* @param int $time
* @return string
*/
protected function getWechatLastActiveTime(int $time): string
protected function getWechatLastActiveTime(string $wechatId): string
{
return date('Y-m-d H:i:s', $time ?: time());
return date('Y-m-d H:i:s', time());
}
/**
@@ -105,36 +108,42 @@ class GetRelatedAccountsV1Controller extends BaseController
/**
* 统计微信好友
*
* @param string $wechatId
* @param string $ownerWechatId
* @return int
*/
protected function countFriend(string $wechatId): int
protected function getCountFriend(string $ownerWechatId): int
{
return WechatFriendShip::where(['ownerWechatId' => $wechatId])->count();
return WechatFriendShipModel::where(
[
'ownerWechatId' => $ownerWechatId,
'companyId' => $this->getUserInfo('companyId')
]
)
->count();
}
/**
* 获取设备关联的微信账号信息
*
* @param int $deviceId 设备ID
* @return array 微信账号信息列表
* @param int $deviceId
* @return array
*/
protected function getDeviceRelatedAccounts(int $deviceId)
protected function getDeviceRelatedAccounts(int $deviceId): array
{
// 获取设备关联的微信ID列表
$wechatIds = $this->getDeviceWechatIds($deviceId);
if (!empty($wechatIds)) {
$results = $this->getWechatAccountsByIds($wechatIds);
$collection = $this->getWechatAccountsByIds($wechatIds);
foreach ($results as &$account) {
$account['lastActive'] = $this->getWechatLastActiveTime($account['createTime']);
$account['statusText'] = $this->getWechatStatusText($account['wechatId']);
$account['totalFriend'] = $this->countFriend($account['wechatId']);
$account['wechatAliveText'] = $this->getWechatAliveText($account['wechatId']);
foreach ($collection as $account) {
$account->lastActive = $this->getWechatLastActiveTime($account->wechatId);
$account->statusText = $this->getWechatStatusText($account->wechatId);
$account->totalFriend = $this->getCountFriend($account->wechatId);
$account->wechatAliveText = $this->getWechatAliveText($account->wechatId);
}
return $results;
return $collection->toArray();
}
return [];
@@ -142,6 +151,7 @@ class GetRelatedAccountsV1Controller extends BaseController
/**
* 获取设备关联的微信账号
*
* @return \think\response\Json
*/
public function index()

View File

@@ -101,7 +101,7 @@ return [
// 合并路由规则
'route_rule_merge' => false,
// 路由是否完全匹配
'route_complete_match' => false,
'route_complete_match' => true,
// 使用注解路由
'route_annotation' => false,
// 域名根如thinkphp.cn

View File

@@ -81,6 +81,7 @@
<meta charset="UTF-8">
<title>系统发生错误</title>
<meta name="robots" content="noindex,nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<style>
/* Base */
body {