253 lines
8.1 KiB
PHP
253 lines
8.1 KiB
PHP
<?php
|
||
|
||
namespace app\store\controller;
|
||
|
||
use think\Db;
|
||
use think\facade\Log;
|
||
|
||
/**
|
||
* Agent功能模块控制器 - V2版本
|
||
*/
|
||
class AgentController extends BaseController
|
||
{
|
||
/**
|
||
* 获取Agent模块列表
|
||
* @return \think\response\Json
|
||
*/
|
||
public function getModules()
|
||
{
|
||
try {
|
||
// 从BaseController获取设备ID(通过userInfo自动获取)
|
||
$deviceId = $this->device['id'] ?? 0;
|
||
|
||
if (!$deviceId) {
|
||
return json(['code' => 400, 'msg' => '设备不存在,请先绑定设备']);
|
||
}
|
||
|
||
// 获取所有可用的模块定义
|
||
$modules = $this->getModuleDefinitions();
|
||
|
||
// 从旧表获取配置
|
||
$taskConfig = Db::name('device_taskconf')
|
||
->where('deviceId', $deviceId)
|
||
->where('deleteTime', 0)
|
||
->find();
|
||
|
||
// 如果没有配置,返回默认关闭状态
|
||
if (!$taskConfig) {
|
||
$taskConfig = [
|
||
'autoLike' => 0,
|
||
'momentsSync' => 0,
|
||
'autoCustomerDev' => 0,
|
||
'groupMessageDeliver' => 0,
|
||
'autoGroup' => 0
|
||
];
|
||
}
|
||
|
||
// 字段映射关系
|
||
$fieldMap = [
|
||
'auto_like' => 'autoLike',
|
||
'moments_sync' => 'momentsSync',
|
||
'auto_customer_dev' => 'autoCustomerDev',
|
||
'group_message_deliver' => 'groupMessageDeliver',
|
||
'auto_group' => 'autoGroup'
|
||
];
|
||
|
||
// 组装返回数据
|
||
$result = [];
|
||
foreach ($modules as $module) {
|
||
$moduleCode = $module['code'];
|
||
$fieldName = $fieldMap[$moduleCode] ?? null;
|
||
|
||
$moduleData = array_merge($module, [
|
||
'userEnabled' => $fieldName && isset($taskConfig[$fieldName]) ? (bool)$taskConfig[$fieldName] : false
|
||
]);
|
||
|
||
$result[] = $moduleData;
|
||
}
|
||
|
||
|
||
return json([
|
||
'code' => 200,
|
||
'msg' => 'success',
|
||
'data' => $result
|
||
]);
|
||
|
||
} catch (\Exception $e) {
|
||
Log::error('获取Agent模块列表失败:' . $e->getMessage());
|
||
return json(['code' => 500, 'msg' => '获取失败:' . $e->getMessage()]);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 更新模块状态
|
||
* @param string $moduleCode 模块代码
|
||
* @return \think\response\Json
|
||
*/
|
||
public function updateModuleStatus($moduleCode)
|
||
{
|
||
try {
|
||
// 从BaseController获取设备ID(通过userInfo自动获取)
|
||
$deviceId = $this->device['id'] ?? 0;
|
||
$isEnabled = (bool)$this->request->param('isEnabled', false);
|
||
|
||
if (!$deviceId) {
|
||
return json(['code' => 400, 'msg' => '设备不存在,请先绑定设备']);
|
||
}
|
||
|
||
// 验证模块代码是否有效
|
||
$validModules = array_column($this->getModuleDefinitions(), 'code');
|
||
if (!in_array($moduleCode, $validModules)) {
|
||
return json(['code' => 400, 'msg' => '无效的模块代码']);
|
||
}
|
||
|
||
// 字段映射关系
|
||
$fieldMap = [
|
||
'auto_like' => 'autoLike',
|
||
'moments_sync' => 'momentsSync',
|
||
'auto_customer_dev' => 'autoCustomerDev',
|
||
'group_message_deliver' => 'groupMessageDeliver',
|
||
'auto_group' => 'autoGroup'
|
||
];
|
||
|
||
$fieldName = $fieldMap[$moduleCode] ?? null;
|
||
if (!$fieldName) {
|
||
return json(['code' => 400, 'msg' => '不支持的模块']);
|
||
}
|
||
|
||
// 查询现有配置
|
||
$taskConfig = Db::name('device_taskconf')
|
||
->where('deviceId', $deviceId)
|
||
->where('deleteTime', 0)
|
||
->find();
|
||
|
||
$now = time();
|
||
|
||
if ($taskConfig) {
|
||
// 更新现有配置
|
||
Db::name('device_taskconf')
|
||
->where('id', $taskConfig['id'])
|
||
->update([
|
||
$fieldName => $isEnabled ? 1 : 0,
|
||
'updateTime' => $now
|
||
]);
|
||
|
||
// 清除设备缓存
|
||
$this->clearDeviceCache();
|
||
} else {
|
||
// 创建新配置
|
||
$insertData = [
|
||
'deviceId' => $deviceId,
|
||
'autoLike' => 0,
|
||
'momentsSync' => 0,
|
||
'autoCustomerDev' => 0,
|
||
'groupMessageDeliver' => 0,
|
||
'autoGroup' => 0,
|
||
'companyId' => $this->device['companyId'] ?? $this->userInfo['companyId'] ?? 0,
|
||
'createTime' => $now,
|
||
'updateTime' => $now
|
||
];
|
||
$insertData[$fieldName] = $isEnabled ? 1 : 0;
|
||
|
||
Db::name('device_taskconf')->insert($insertData);
|
||
|
||
// 清除设备缓存
|
||
$this->clearDeviceCache();
|
||
}
|
||
|
||
return json([
|
||
'code' => 200,
|
||
'msg' => '操作成功',
|
||
'data' => [
|
||
'moduleCode' => $moduleCode,
|
||
'isEnabled' => $isEnabled
|
||
]
|
||
]);
|
||
|
||
} catch (\Exception $e) {
|
||
Log::error('更新模块状态失败:' . $e->getMessage());
|
||
return json(['code' => 500, 'msg' => '更新失败:' . $e->getMessage()]);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 获取模块定义
|
||
* @return array
|
||
*/
|
||
protected function getModuleDefinitions()
|
||
{
|
||
return [
|
||
[
|
||
'code' => 'auto_like',
|
||
'name' => '自动点赞',
|
||
'icon' => 'icon-dianzan',
|
||
'color' => '#ff6699',
|
||
'description' => '自动为好友朋友圈点赞',
|
||
'category' => 'social',
|
||
'sort' => 1,
|
||
'isEnabled' => true,
|
||
'needAuth' => true
|
||
],
|
||
[
|
||
'code' => 'moments_sync',
|
||
'name' => '朋友圈同步',
|
||
'icon' => 'icon-tupian',
|
||
'color' => '#9966ff',
|
||
'description' => '同步好友朋友圈内容',
|
||
'category' => 'social',
|
||
'sort' => 2,
|
||
'isEnabled' => true,
|
||
'needAuth' => true
|
||
],
|
||
[
|
||
'code' => 'auto_customer_dev',
|
||
'name' => '自动开发客户',
|
||
'icon' => 'icon-yonghu',
|
||
'color' => '#33cc99',
|
||
'description' => '自动化客户开发流程',
|
||
'category' => 'customer',
|
||
'sort' => 3,
|
||
'isEnabled' => true,
|
||
'needAuth' => true
|
||
],
|
||
[
|
||
'code' => 'group_message_deliver',
|
||
'name' => '群消息群发',
|
||
'icon' => 'icon-xiaoxi',
|
||
'color' => '#ff9966',
|
||
'description' => '批量推送消息到微信群',
|
||
'category' => 'message',
|
||
'sort' => 4,
|
||
'isEnabled' => true,
|
||
'needAuth' => false
|
||
],
|
||
[
|
||
'code' => 'auto_group',
|
||
'name' => '自动建群',
|
||
'icon' => 'icon-yonghuqun',
|
||
'color' => '#6699ff',
|
||
'description' => '自动创建和管理微信群',
|
||
'category' => 'group',
|
||
'sort' => 5,
|
||
'isEnabled' => true,
|
||
'needAuth' => true
|
||
],
|
||
[
|
||
'code' => 'video_distribute',
|
||
'name' => '视频分发',
|
||
'icon' => 'icon-video',
|
||
'color' => '#ff66cc',
|
||
'description' => '自动分发视频内容',
|
||
'category' => 'content',
|
||
'sort' => 6,
|
||
'isEnabled' => false,
|
||
'needAuth' => false
|
||
]
|
||
];
|
||
}
|
||
|
||
}
|
||
|