私域操盘手 - 设备配置信息提供最小访问单元,与设备详情解耦

This commit is contained in:
柳清爽
2025-05-16 18:32:21 +08:00
parent 7111d48886
commit bff2d5eb8b
6 changed files with 385 additions and 209 deletions

View File

@@ -12,7 +12,7 @@ Route::group('v1/', function () {
Route::put('refresh', 'app\cunkebao\controller\device\RefreshDeviceDetailV1Controller@index');
Route::get('add-results', 'app\cunkebao\controller\device\GetAddResultedV1Controller@index');
Route::post('task-config', 'app\cunkebao\controller\device\UpdateDeviceTaskConfigV1Controller@index');
Route::get(':id/task-config', 'app\cunkebao\controller\device\UpdateDeviceTaskConfigV1Controller@index');
Route::get(':id/task-config', 'app\cunkebao\controller\device\GetDeviceTaskConfigV1Controller@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');

View File

@@ -59,29 +59,6 @@ class GetDeviceDetailV1Controller extends BaseController
return 0;
}
/**
* 解析taskConfig字段获取功能开关
*
* @param int $deviceId
* @return int[]
* @throws \Exception
*/
protected function getTaskConfig(int $deviceId): array
{
$conf = DeviceTaskconfModel::alias('c')->field([
'c.autoAddFriend', 'c.autoReply', 'c.momentsSync', 'c.aiChat'
])
->where(
[
'companyId' => $this->getUserInfo('companyId'),
'deviceId' => $deviceId
]
)
->find();
// 未配置时赋予默认关闭的状态
return !is_null($conf) ? $conf->toArray() : ArrHelper::getValue('autoAddFriend,autoReply,momentsSync,aiChat', [], 0);
}
/**
* 获取设备最新登录微信的 wechatId
@@ -102,55 +79,6 @@ class GetDeviceDetailV1Controller extends BaseController
->value('wechatId');
}
/**
* 统计设备登录微信的好友
*
* @param int $deviceId
* @return int
* @throws \Exception
*/
protected function getTotalFriend(int $deviceId): int
{
$ownerWechatId = $this->getDeviceLatestWechatLogin($deviceId);
if ($ownerWechatId) {
return WechatFriendShipModel::where(
[
'companyId' => $this->getUserInfo('companyId'),
'ownerWechatId' => $ownerWechatId
]
)
->count();
}
return 0;
}
/**
* 获取设备绑定微信的消息总数
*
* @param int $deviceId
* @return int
*/
protected function getThirtyDayMsgCount(int $deviceId): int
{
$ownerWechatId = $this->getDeviceLatestWechatLogin($deviceId);
if ($ownerWechatId) {
$activity = (string)WechatCustomerModel::where(
[
'wechatId' => $ownerWechatId,
'companyId' => $this->getUserInfo('companyId')
]
)
->value('activity');
return json_decode($activity)->totalMsgCount ?? 0;
}
return 0;
}
/**
* 获取设备详情
* @param int $id 设备ID
@@ -170,11 +98,6 @@ class GetDeviceDetailV1Controller extends BaseController
}
$device['battery'] = $this->parseExtraForBattery($device['extra']);
$device['features'] = $this->getTaskConfig($id);
$device['totalFriend'] = $this->getTotalFriend($id);
$device['thirtyDayMsgCount'] = $this->getThirtyDayMsgCount($id);
// 设备最后活跃时间为设备状态更新时间
$device['lastUpdateTime'] = date('Y-m-d H:i:s', $device['lastUpdateTime']);
// 删除冗余字段

View File

@@ -0,0 +1,85 @@
<?php
namespace app\cunkebao\controller\device;
use app\common\model\DeviceTaskconf as DeviceTaskconfModel;
use app\common\model\DeviceUser as DeviceUserModel;
use app\common\model\User as UserModel;
use app\cunkebao\controller\BaseController;
use Eison\Utils\Helper\ArrHelper;
use library\ResponseHelper;
/**
* 设备管理控制器
*/
class GetDeviceTaskConfigV1Controller extends BaseController
{
/**
* 检查用户是否有权限操作指定设备
*
* @param int $deviceId
* @return void
*/
protected function checkUserDevicePermission(int $deviceId): void
{
$hasPermission = DeviceUserModel::where(
[
'deviceId' => $deviceId,
'userId' => $this->getUserInfo('id'),
'companyId' => $this->getUserInfo('companyId')
]
)
->count() > 0;
if (!$hasPermission) {
throw new \Exception('您没有权限查看该设备', 403);
}
}
/**
* 解析taskConfig字段获取功能开关
*
* @param int $deviceId
* @return int[]
* @throws \Exception
*/
protected function getTaskConfig(int $deviceId): array
{
$conf = DeviceTaskconfModel::alias('c')
->field([
'c.autoAddFriend', 'c.autoReply', 'c.momentsSync', 'c.aiChat'
])
->where(
[
'companyId' => $this->getUserInfo('companyId'),
'deviceId' => $deviceId
]
)
->find();
// 未配置时赋予默认关闭的状态
return !is_null($conf) ? $conf->toArray() : ArrHelper::getValue('autoAddFriend,autoReply,momentsSync,aiChat', [], 0);
}
/**
* 获取设备详情
*
* @return \think\response\Json
*/
public function index()
{
try {
$id = $this->request->param('id/d');
if ($this->getUserInfo('isAdmin') != UserModel::ADMIN_STP) {
$this->checkUserDevicePermission($id);
}
return ResponseHelper::success(
$this->getTaskConfig($id)
);
} catch (\Exception $e) {
return ResponseHelper::error($e->getMessage(), $e->getCode());
}
}
}