记录设备操作状态
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace app\devices\controller;
|
namespace app\devices\controller;
|
||||||
|
|
||||||
|
use app\devices\model\DeviceHandleLog;
|
||||||
use think\Controller;
|
use think\Controller;
|
||||||
use app\devices\model\Device as DeviceModel;
|
use app\devices\model\Device as DeviceModel;
|
||||||
|
use think\Db;
|
||||||
use think\facade\Request;
|
use think\facade\Request;
|
||||||
use app\common\util\JwtUtil;
|
use app\common\util\JwtUtil;
|
||||||
|
|
||||||
@@ -305,8 +307,30 @@ class Device extends Controller
|
|||||||
$data['companyId'] = $userInfo['companyId'];
|
$data['companyId'] = $userInfo['companyId'];
|
||||||
$data['id'] = time();
|
$data['id'] = time();
|
||||||
|
|
||||||
// 添加设备
|
try {
|
||||||
$id = DeviceModel::addDevice($data);
|
Db::startTrans();
|
||||||
|
|
||||||
|
// 添加设备
|
||||||
|
$id = DeviceModel::addDevice($data);
|
||||||
|
|
||||||
|
// 添加设备操作记录
|
||||||
|
DeviceHandleLog::addLog(
|
||||||
|
[
|
||||||
|
'imei' => $data['imei'],
|
||||||
|
'userId' => $userInfo['id'],
|
||||||
|
'content' => '添加设备',
|
||||||
|
'companyId' => $userInfo['companyId'],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
Db::commit();
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Db::rollback();
|
||||||
|
|
||||||
|
return json([
|
||||||
|
'code' => 500,
|
||||||
|
'msg' => '添加失败:' . $e->getMessage()
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
// 此处调用底层API
|
// 此处调用底层API
|
||||||
return json([
|
return json([
|
||||||
@@ -333,12 +357,6 @@ class Device extends Controller
|
|||||||
try {
|
try {
|
||||||
// 获取登录用户信息
|
// 获取登录用户信息
|
||||||
$userInfo = request()->userInfo;
|
$userInfo = request()->userInfo;
|
||||||
if (empty($userInfo)) {
|
|
||||||
return json([
|
|
||||||
'code' => 401,
|
|
||||||
'msg' => '未登录或登录已过期'
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查用户权限,只有管理员可以删除设备
|
// 检查用户权限,只有管理员可以删除设备
|
||||||
if ($userInfo['isAdmin'] != 1) {
|
if ($userInfo['isAdmin'] != 1) {
|
||||||
@@ -396,6 +414,9 @@ class Device extends Controller
|
|||||||
{
|
{
|
||||||
// 获取请求参数
|
// 获取请求参数
|
||||||
$data = $this->request->post();
|
$data = $this->request->post();
|
||||||
|
|
||||||
|
// 获取登录用户信息
|
||||||
|
$userInfo = request()->userInfo;
|
||||||
|
|
||||||
// 验证参数
|
// 验证参数
|
||||||
if (empty($data['id'])) {
|
if (empty($data['id'])) {
|
||||||
@@ -436,14 +457,52 @@ class Device extends Controller
|
|||||||
if (!$hasUpdate) {
|
if (!$hasUpdate) {
|
||||||
return json(['code' => 200, 'msg' => '更新成功', 'data' => ['taskConfig' => $taskConfig]]);
|
return json(['code' => 200, 'msg' => '更新成功', 'data' => ['taskConfig' => $taskConfig]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新设备taskConfig字段
|
try {
|
||||||
$result = \app\devices\model\Device::where('id', $deviceId)
|
Db::startTrans();
|
||||||
->update([
|
|
||||||
'taskConfig' => json_encode($taskConfig),
|
// 更新设备taskConfig字段
|
||||||
'updateTime' => time()
|
$result = \app\devices\model\Device::where('id', $deviceId)
|
||||||
]);
|
->update([
|
||||||
|
'taskConfig' => json_encode($taskConfig),
|
||||||
|
'updateTime' => time()
|
||||||
|
]);
|
||||||
|
|
||||||
|
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会话';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加设备操作记录
|
||||||
|
DeviceHandleLog::addLog(
|
||||||
|
[
|
||||||
|
'imei' => $device['imei'],
|
||||||
|
'userId' => $userInfo['id'],
|
||||||
|
'content' => $content,
|
||||||
|
'companyId' => $userInfo['companyId'],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
Db::commit();
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Db::rollback();
|
||||||
|
|
||||||
|
return json([
|
||||||
|
'code' => 500,
|
||||||
|
'msg' => '更新任务配置失败'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
if ($result) {
|
if ($result) {
|
||||||
return json([
|
return json([
|
||||||
'code' => 200,
|
'code' => 200,
|
||||||
|
|||||||
120
Server/application/devices/model/DeviceHandleLog.php
Normal file
120
Server/application/devices/model/DeviceHandleLog.php
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
<?php
|
||||||
|
namespace app\devices\model;
|
||||||
|
|
||||||
|
use think\Model;
|
||||||
|
use think\Db;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备操作日志模型类
|
||||||
|
*/
|
||||||
|
class DeviceHandleLog extends Model
|
||||||
|
{
|
||||||
|
// 设置表名
|
||||||
|
protected $name = 'device_handle_log';
|
||||||
|
protected $prefix = 'tk_';
|
||||||
|
|
||||||
|
// 设置主键
|
||||||
|
protected $pk = 'id';
|
||||||
|
|
||||||
|
// 自动写入时间戳
|
||||||
|
protected $autoWriteTimestamp = 'datetime';
|
||||||
|
|
||||||
|
// 定义时间戳字段名
|
||||||
|
protected $createTime = 'createTime';
|
||||||
|
protected $updateTime = false;
|
||||||
|
|
||||||
|
// 定义字段类型
|
||||||
|
protected $type = [
|
||||||
|
'id' => 'integer',
|
||||||
|
'userId' => 'integer',
|
||||||
|
'companyId' => 'integer',
|
||||||
|
'createTime' => 'datetime'
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加设备操作日志
|
||||||
|
* @param array $data 日志数据
|
||||||
|
* @return int 新增日志ID
|
||||||
|
*/
|
||||||
|
public static function addLog($data)
|
||||||
|
{
|
||||||
|
$log = new self();
|
||||||
|
$log->allowField(true)->save($data);
|
||||||
|
return $log->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取设备操作日志列表
|
||||||
|
* @param array $where 查询条件
|
||||||
|
* @param string $order 排序方式
|
||||||
|
* @param int $page 页码
|
||||||
|
* @param int $limit 每页数量
|
||||||
|
* @return \think\Paginator 分页对象
|
||||||
|
*/
|
||||||
|
public static function getLogList($where = [], $order = 'createTime desc', $page = 1, $limit = 10)
|
||||||
|
{
|
||||||
|
return self::where($where)
|
||||||
|
->order($order)
|
||||||
|
->paginate($limit, false, ['page' => $page]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据IMEI获取设备操作日志
|
||||||
|
* @param string $imei 设备IMEI
|
||||||
|
* @param int $companyId 租户ID
|
||||||
|
* @param int $limit 获取条数
|
||||||
|
* @return array 日志记录
|
||||||
|
*/
|
||||||
|
public static function getLogsByImei($imei, $companyId = null, $limit = 10)
|
||||||
|
{
|
||||||
|
$query = self::where('imei', $imei);
|
||||||
|
|
||||||
|
if ($companyId !== null) {
|
||||||
|
$query->where('companyId', $companyId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query->order('createTime', 'desc')
|
||||||
|
->limit($limit)
|
||||||
|
->select();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据用户ID获取操作日志
|
||||||
|
* @param int $userId 用户ID
|
||||||
|
* @param int $companyId 租户ID
|
||||||
|
* @param int $page 页码
|
||||||
|
* @param int $limit 每页数量
|
||||||
|
* @return \think\Paginator 分页对象
|
||||||
|
*/
|
||||||
|
public static function getLogsByUser($userId, $companyId = null, $page = 1, $limit = 10)
|
||||||
|
{
|
||||||
|
$query = self::where('userId', $userId);
|
||||||
|
|
||||||
|
if ($companyId !== null) {
|
||||||
|
$query->where('companyId', $companyId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query->order('createTime', 'desc')
|
||||||
|
->paginate($limit, false, ['page' => $page]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 记录设备操作日志的便捷方法
|
||||||
|
* @param string $imei 设备IMEI
|
||||||
|
* @param int $userId 操作用户ID
|
||||||
|
* @param string $content 操作内容
|
||||||
|
* @param int $companyId 租户ID
|
||||||
|
* @return int 日志ID
|
||||||
|
*/
|
||||||
|
public static function recordLog($imei, $userId, $content, $companyId = null)
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'imei' => $imei,
|
||||||
|
'userId' => $userId,
|
||||||
|
'content' => $content,
|
||||||
|
'companyId' => $companyId
|
||||||
|
];
|
||||||
|
|
||||||
|
return self::addLog($data);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user