操盘手端 - 更新设备任务配置返工
This commit is contained in:
@@ -24,7 +24,7 @@ class BaseController extends Controller
|
||||
|
||||
date_default_timezone_set('Asia/Shanghai');
|
||||
|
||||
$this->user = request()->userInfo;;
|
||||
$this->user = request()->userInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -99,139 +99,6 @@ class Device extends Controller
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备列表
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
// 获取登录用户信息
|
||||
$userInfo = request()->userInfo;
|
||||
// 获取查询条件
|
||||
$where = [];
|
||||
|
||||
// 关键词搜索(同时搜索IMEI和备注)
|
||||
$keyword = Request::param('keyword');
|
||||
if (!empty($keyword)) {
|
||||
// 使用复杂条件实现OR查询
|
||||
$where[] = ['exp', "d.imei LIKE '%{$keyword}%' OR d.memo LIKE '%{$keyword}%'"];
|
||||
}
|
||||
|
||||
// 设备在线状态
|
||||
$alive = Request::param('alive');
|
||||
if (is_numeric($alive)) {
|
||||
$where['d.alive'] = $alive;
|
||||
}
|
||||
|
||||
// 获取分页参数
|
||||
$page = (int)Request::param('page', 1);
|
||||
$limit = (int)Request::param('limit', 10);
|
||||
|
||||
// 获取排序参数
|
||||
$sort = Request::param('sort', 'd.id');
|
||||
$order = Request::param('order', 'desc');
|
||||
|
||||
// 添加公司ID过滤条件
|
||||
$where['d.companyId'] = $userInfo['companyId'];
|
||||
|
||||
// 根据用户管理员状态调整查询条件
|
||||
if ($userInfo['isAdmin'] == 1) {
|
||||
// 管理员直接查询所有设备
|
||||
$list = DeviceModel::getDeviceList($where, "{$sort} {$order}", $page, $limit);
|
||||
} else {
|
||||
// 非管理员需要查询关联表
|
||||
$deviceIds = DeviceUserModel::getUserDeviceIds(
|
||||
$userInfo['id'],
|
||||
$userInfo['companyId']
|
||||
);
|
||||
|
||||
if (empty($deviceIds)) {
|
||||
return json([
|
||||
'code' => 403,
|
||||
'msg' => '请联系管理员绑定设备'
|
||||
]);
|
||||
}
|
||||
|
||||
// 添加设备ID过滤条件
|
||||
$where['d.id'] = ['in', $deviceIds];
|
||||
$list = DeviceModel::getDeviceList($where, "{$sort} {$order}", $page, $limit);
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => [
|
||||
'total' => $list->total(),
|
||||
'list' => $list->items()
|
||||
]
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '获取失败:' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备详情
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function read()
|
||||
{
|
||||
try {
|
||||
// 获取登录用户信息
|
||||
$userInfo = request()->userInfo;
|
||||
|
||||
// 获取设备ID
|
||||
$id = Request::param('id/d');
|
||||
if (empty($id)) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '参数错误'
|
||||
]);
|
||||
}
|
||||
|
||||
// 检查用户权限
|
||||
if ($userInfo['isAdmin'] != 1) {
|
||||
// 非管理员需要检查是否有权限访问该设备
|
||||
$hasPermission = \app\common\model\DeviceUser::checkUserDevicePermission(
|
||||
$userInfo['id'],
|
||||
$id,
|
||||
$userInfo['companyId']
|
||||
);
|
||||
|
||||
if (!$hasPermission) {
|
||||
return json([
|
||||
'code' => 403,
|
||||
'msg' => '您没有权限查看该设备'
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取设备详情
|
||||
$info = DeviceModel::getDeviceInfo($id);
|
||||
if (empty($info)) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '设备不存在'
|
||||
]);
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => $info
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '获取失败:' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新设备
|
||||
|
||||
@@ -17,7 +17,8 @@ class GetDeviceDetailV1Controller extends BaseController
|
||||
{
|
||||
/**
|
||||
* 检查用户是否有权限操作指定设备
|
||||
* @param int $userId 用户ID
|
||||
*
|
||||
* @param int $deviceId
|
||||
* @return void
|
||||
*/
|
||||
protected function checkUserDevicePermission(int $deviceId): void
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
namespace app\cunkebao\controller\device;
|
||||
|
||||
use app\common\model\DeviceTaskconf;
|
||||
use app\common\model\DeviceUser as DeviceUserModel;
|
||||
use app\cunkebao\controller\BaseController;
|
||||
use app\common\model\Device as DeviceModel;
|
||||
use app\common\model\DeviceHandleLog as DeviceHandleLogModel;
|
||||
use think\Db;
|
||||
use think\facade\Request;
|
||||
|
||||
/**
|
||||
* 设备管理控制器
|
||||
*/
|
||||
class UpdateDeviceTaskConfigV1Controller extends BaseController
|
||||
{
|
||||
/**
|
||||
* 先获取设备信息,确认设备存在且未删除
|
||||
*
|
||||
* @param int $deviceId
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function checkDeviceExists(int $deviceId)
|
||||
{
|
||||
$where = [
|
||||
'deviceId' => $deviceId,
|
||||
'companyId' => $this->getUserInfo('companyId'),
|
||||
'deleteTime' => 0
|
||||
];
|
||||
|
||||
$device = DeviceModel::find($where);
|
||||
|
||||
if (!$device) {
|
||||
throw new \Exception('设备不存在或已删除', '404');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查用户是否有权限操作指定设备
|
||||
*
|
||||
* @param int $deviceId
|
||||
* @return void
|
||||
*/
|
||||
protected function checkUserDevicePermission(int $deviceId): void
|
||||
{
|
||||
$where = [
|
||||
'deviceId' => $deviceId,
|
||||
'userId' => $this->getUserInfo('id'),
|
||||
'companyId' => $this->getUserInfo('companyId')
|
||||
];
|
||||
|
||||
$hasPermission = DeviceUserModel::where($where)->count() > 0;
|
||||
|
||||
if (!$hasPermission) {
|
||||
throw new \Exception('您没有权限操作该设备', '403');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加设备操作日志
|
||||
*
|
||||
* @param int $deviceId
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function addHandleLog(int $deviceId): void
|
||||
{
|
||||
$data = $this->request->post();
|
||||
|
||||
if (isset($data['autoAddFriend']))/**/$content = $data['autoAddFriend'] ? '开启自动添加好友' : '关闭自动添加好友';
|
||||
if (isset($data['autoReply']))/* */$content = $data['autoReply'] ? '开启自动回复' : '关闭自动回复';
|
||||
if (isset($data['momentsSync']))/* */$content = $data['momentsSync'] ? '开启朋友圈同步' : '关闭朋友圈同步';
|
||||
if (isset($data['aiChat']))/* */$content = $data['aiChat'] ? '开启AI会话' : '关闭AI会话';
|
||||
|
||||
DeviceHandleLogModel::addLog(
|
||||
[
|
||||
'deviceId' => $deviceId,
|
||||
'content' => $content,
|
||||
'userId' => $this->getUserInfo('id'),
|
||||
'companyId' => $this->getUserInfo('companyId'),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新设备taskConfig字段
|
||||
*
|
||||
* @param int $deviceId
|
||||
* @return void
|
||||
*/
|
||||
protected function setTaskconf(int $deviceId): void
|
||||
{
|
||||
$data = $this->request->post();
|
||||
$conf = DeviceTaskconf::where('deviceId', $deviceId)->find();
|
||||
|
||||
if ($conf) {
|
||||
DeviceTaskconf::where('deviceId', $deviceId)->update($data);
|
||||
} else {
|
||||
DeviceTaskconf::create(array_merge($data, [
|
||||
'companyId' => $this->getUserInfo('companyId'),
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新设备任务配置
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$id = Request::param('deviceId/d');
|
||||
|
||||
$this->checkDeviceExists($id);
|
||||
|
||||
if ($this->getUserInfo('isAdmin') != 1) {
|
||||
$this->checkUserDevicePermission($id);
|
||||
}
|
||||
|
||||
try {
|
||||
Db::startTrans();
|
||||
|
||||
$this->setTaskconf($id);
|
||||
$this->addHandleLog($id);
|
||||
|
||||
Db::commit();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '更新任务配置成功'
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
|
||||
return json([
|
||||
'code' => $e->getCode(),
|
||||
'msg' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user