diff --git a/Server/application/common/model/Attachment.php b/Server/application/common/model/Attachment.php index 53b85f33..c668217c 100644 --- a/Server/application/common/model/Attachment.php +++ b/Server/application/common/model/Attachment.php @@ -8,4 +8,9 @@ class Attachment extends Model { // 设置表名 protected $name = 'attachments'; + + // 自动写入时间戳 + protected $autoWriteTimestamp = true; + protected $createTime = 'createTime'; + protected $updateTime = 'updateTime'; } \ No newline at end of file diff --git a/Server/application/common/model/Device.php b/Server/application/common/model/Device.php index 5938ffcf..a186b65e 100644 --- a/Server/application/common/model/Device.php +++ b/Server/application/common/model/Device.php @@ -11,4 +11,9 @@ class Device extends Model { // 设置表名 protected $name = 'device'; + + // 自动写入时间戳 + protected $autoWriteTimestamp = true; + protected $createTime = 'createTime'; + protected $updateTime = 'updateTime'; } \ No newline at end of file diff --git a/Server/application/common/model/DeviceHandleLog.php b/Server/application/common/model/DeviceHandleLog.php new file mode 100644 index 00000000..bbf1a598 --- /dev/null +++ b/Server/application/common/model/DeviceHandleLog.php @@ -0,0 +1,34 @@ +allowField(true)->save($data); + + return $log->id; + } +} \ No newline at end of file diff --git a/Server/application/common/model/DeviceTaskconf.php b/Server/application/common/model/DeviceTaskconf.php index 064ec9bb..2905b023 100644 --- a/Server/application/common/model/DeviceTaskconf.php +++ b/Server/application/common/model/DeviceTaskconf.php @@ -11,4 +11,9 @@ class DeviceTaskconf extends Model { // 设置表名 protected $name = 'device_taskconf'; + + // 自动写入时间戳 + protected $autoWriteTimestamp = true; + protected $createTime = 'createTime'; + protected $updateTime = 'updateTime'; } \ No newline at end of file diff --git a/Server/application/common/model/DeviceUser.php b/Server/application/common/model/DeviceUser.php index 525c527d..fcbebaba 100644 --- a/Server/application/common/model/DeviceUser.php +++ b/Server/application/common/model/DeviceUser.php @@ -1,4 +1,5 @@ 'integer', - 'userId' => 'integer', - 'deviceId' => 'integer' - ]; - - /** - * 获取指定用户的所有设备ID - * @param int $userId 用户ID - * @param int $companyId 公司ID - * @return array 设备ID数组 - */ - public static function getUserDeviceIds($userId, $companyId = null) - { - $where = ['userId' => $userId]; - - if (!is_null($companyId)) { - $where['companyId'] = $companyId; - } - - return self::where($where) - ->column('deviceId'); - } - - /** - * 获取指定设备的所有用户ID - * @param int $deviceId 设备ID - * @param int $companyId 公司ID - * @return array 用户ID数组 - */ - public static function getDeviceUserIds($deviceId, $companyId = null) - { - $where = ['deviceId' => $deviceId]; - - if (!is_null($companyId)) { - $where['companyId'] = $companyId; - } - - return self::where($where) - ->column('userId'); - } - - /** - * 添加设备与用户的关联 - * @param int $companyId 公司ID - * @param int $userId 用户ID - * @param int $deviceId 设备ID - * @return bool 是否添加成功 - */ - public static function addRelation($companyId, $userId, $deviceId) - { - // 检查关联是否已存在 - $exists = self::where([ - 'companyId' => $companyId, - 'userId' => $userId, - 'deviceId' => $deviceId - ])->find(); - - if ($exists) { - return true; // 已存在,视为添加成功 - } - - // 添加新关联 - return self::create([ - 'companyId' => $companyId, - 'userId' => $userId, - 'deviceId' => $deviceId - ]) ? true : false; - } - - /** - * 批量添加设备与用户的关联 - * @param int $companyId 公司ID - * @param int $userId 用户ID - * @param array $deviceIds 设备ID数组 - * @return int 成功添加的记录数 - */ - public static function batchAddRelations($companyId, $userId, array $deviceIds) - { - if (empty($deviceIds)) { - return 0; - } - - $data = []; - foreach ($deviceIds as $deviceId) { - $data[] = [ - 'companyId' => $companyId, - 'userId' => $userId, - 'deviceId' => $deviceId - ]; - } - - // 批量添加前先删除已存在的关联,避免主键冲突 - self::where('userId', $userId) - ->where('companyId', $companyId) - ->whereIn('deviceId', $deviceIds) - ->delete(); - - return self::insertAll($data); - } - - /** - * 删除设备与用户的关联 - * @param int $companyId 公司ID - * @param int $userId 用户ID - * @param int $deviceId 设备ID - * @return bool 是否删除成功 - */ - public static function removeDeviceUserRelation($companyId, $userId, $deviceId) - { - return self::where([ - 'companyId' => $companyId, - 'userId' => $userId, - 'deviceId' => $deviceId - ])->delete() !== false; - } - - + // 自动写入时间戳 + protected $autoWriteTimestamp = true; + protected $createTime = 'createTime'; + protected $updateTime = 'updateTime'; + /** * 关联用户模型 * @return \think\model\relation\BelongsTo @@ -160,22 +29,22 @@ class DeviceUser extends Model { return $this->belongsTo('User', 'userId', 'id'); } - + /** * 关联设备模型 * @return \think\model\relation\BelongsTo */ public function device() { - return $this->belongsTo('app\devices\model\Device', 'deviceId', 'id'); + return $this->belongsTo('app\common\model\Device', 'deviceId', 'id'); } - + /** * 关联公司模型 * @return \think\model\relation\BelongsTo */ public function company() { - return $this->belongsTo('Company', 'companyId', 'id'); + return $this->belongsTo('app\common\model\Company', 'companyId', 'id'); } } \ No newline at end of file diff --git a/Server/application/common/model/DeviceWechatLogin.php b/Server/application/common/model/DeviceWechatLogin.php index 5a9545eb..88efba35 100644 --- a/Server/application/common/model/DeviceWechatLogin.php +++ b/Server/application/common/model/DeviceWechatLogin.php @@ -11,4 +11,10 @@ class DeviceWechatLogin extends Model { // 登录日志最新登录 alive = 1,旧数据全部设置0 protected $name = 'device_wechat_login'; + + // 自动写入时间戳 + protected $autoWriteTimestamp = true; + protected $createTime = 'createTime'; + protected $updateTime = 'updateTime'; + } \ No newline at end of file diff --git a/Server/application/common/model/User.php b/Server/application/common/model/User.php index da989461..1e4681d8 100644 --- a/Server/application/common/model/User.php +++ b/Server/application/common/model/User.php @@ -14,6 +14,11 @@ class User extends Model */ protected $table = 'ck_users'; + // 自动写入时间戳 + protected $autoWriteTimestamp = true; + protected $createTime = 'createTime'; + protected $updateTime = 'updateTime'; + /** * 主键 * @var string diff --git a/Server/application/common/model/WechatAccount.php b/Server/application/common/model/WechatAccount.php index afe19d8e..6357dc26 100644 --- a/Server/application/common/model/WechatAccount.php +++ b/Server/application/common/model/WechatAccount.php @@ -11,4 +11,10 @@ class WechatAccount extends Model { // 设置表名 protected $name = 'wechat_account'; + + // 自动写入时间戳 + protected $autoWriteTimestamp = true; + protected $createTime = 'createTime'; + protected $updateTime = 'updateTime'; + } \ No newline at end of file diff --git a/Server/application/common/model/WechatFriend.php b/Server/application/common/model/WechatFriend.php index 0f90278b..54319f3a 100644 --- a/Server/application/common/model/WechatFriend.php +++ b/Server/application/common/model/WechatFriend.php @@ -11,4 +11,9 @@ class WechatFriend extends Model { // 设置表名 protected $name = 'wechat_friend'; + + // 自动写入时间戳 + protected $autoWriteTimestamp = true; + protected $createTime = 'createTime'; + protected $updateTime = 'updateTime'; } \ No newline at end of file diff --git a/Server/application/cunkebao/config/route.php b/Server/application/cunkebao/config/route.php index 6f519ff9..24a47dec 100644 --- a/Server/application/cunkebao/config/route.php +++ b/Server/application/cunkebao/config/route.php @@ -18,7 +18,7 @@ Route::group('v1/', function () { Route::post('', 'app\\cunkebao\\controller\\Device@save'); // 添加设备 Route::put('refresh', 'app\\cunkebao\\controller\\Device@refresh'); // 刷新设备状态 Route::delete(':id', 'app\\cunkebao\\controller\\Device@delete'); // 删除设备 - Route::post('task-config', 'app\\cunkebao\\controller\\Device@updateTaskConfig'); // 更新设备任务配置 + Route::post('task-config', 'app\\cunkebao\\controller\\device\\UpdateDeviceTaskConfigV1Controller@index'); // 更新设备任务配置 }); // 设备微信相关 diff --git a/Server/application/cunkebao/controller/BaseController.php b/Server/application/cunkebao/controller/BaseController.php index 2b9328af..59aa8f58 100644 --- a/Server/application/cunkebao/controller/BaseController.php +++ b/Server/application/cunkebao/controller/BaseController.php @@ -24,7 +24,7 @@ class BaseController extends Controller date_default_timezone_set('Asia/Shanghai'); - $this->user = request()->userInfo;; + $this->user = request()->userInfo; } /** diff --git a/Server/application/cunkebao/controller/Device.php b/Server/application/cunkebao/controller/Device.php index 5d7231fe..7d99aed4 100644 --- a/Server/application/cunkebao/controller/Device.php +++ b/Server/application/cunkebao/controller/Device.php @@ -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() - ]); - } - } /** * 刷新设备 diff --git a/Server/application/cunkebao/controller/device/GetDeviceDetailV1Controller.php b/Server/application/cunkebao/controller/device/GetDeviceDetailV1Controller.php index 2e7cae10..f3104157 100644 --- a/Server/application/cunkebao/controller/device/GetDeviceDetailV1Controller.php +++ b/Server/application/cunkebao/controller/device/GetDeviceDetailV1Controller.php @@ -17,7 +17,8 @@ class GetDeviceDetailV1Controller extends BaseController { /** * 检查用户是否有权限操作指定设备 - * @param int $userId 用户ID + * + * @param int $deviceId * @return void */ protected function checkUserDevicePermission(int $deviceId): void diff --git a/Server/application/cunkebao/controller/device/UpdateDeviceTaskConfigV1Controller.php b/Server/application/cunkebao/controller/device/UpdateDeviceTaskConfigV1Controller.php new file mode 100644 index 00000000..ecf5db60 --- /dev/null +++ b/Server/application/cunkebao/controller/device/UpdateDeviceTaskConfigV1Controller.php @@ -0,0 +1,141 @@ + $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() + ]); + } + } +} \ No newline at end of file diff --git a/Server/application/cunkebao/model/DeviceHandleLog.php b/Server/application/cunkebao/model/DeviceHandleLog.php index d601183d..4afe46cf 100644 --- a/Server/application/cunkebao/model/DeviceHandleLog.php +++ b/Server/application/cunkebao/model/DeviceHandleLog.php @@ -11,17 +11,7 @@ class DeviceHandleLog extends Model // 设置表名 protected $name = 'device_handle_log'; - /** - * 添加设备操作日志 - * @param array $data 日志数据 - * @return int 新增日志ID - */ - public static function addLog($data) - { - $log = new self(); - $log->allowField(true)->save($data); - return $log->id; - } + /** * 获取设备操作日志列表