diff --git a/Server/application/common/model/Attachment.php b/Server/application/common/model/Attachment.php index cf3a8a57..53b85f33 100644 --- a/Server/application/common/model/Attachment.php +++ b/Server/application/common/model/Attachment.php @@ -1,4 +1,5 @@ 'integer', - 'dl_count' => 'integer', - 'size' => 'integer', - 'scene' => 'integer', - 'create_at' => 'datetime', - 'update_at' => 'datetime', - 'delete_at' => 'datetime' - ]; - - /** - * 添加附件记录 - * @param array $data 附件数据 - * @return int|bool - */ - public static function addAttachment($data) - { - $attachment = new self(); - return $attachment->allowField(true)->save($data); - } - - /** - * 根据hash_key获取附件 - * @param string $hashKey - * @return array|null - */ - public static function getByHashKey($hashKey) - { - return self::where('hash_key', $hashKey) - ->where('delete_at', null) - ->find(); - } } \ No newline at end of file diff --git a/Server/application/common/model/Device.php b/Server/application/common/model/Device.php new file mode 100644 index 00000000..5938ffcf --- /dev/null +++ b/Server/application/common/model/Device.php @@ -0,0 +1,14 @@ +delete() !== false; } - /** - * 检查用户是否有权限操作指定设备 - * @param int $userId 用户ID - * @param int $deviceId 设备ID - * @param int $companyId 公司ID - * @return bool 是否有权限 - */ - public static function checkUserDevicePermission($userId, $deviceId, $companyId = null) - { - $where = [ - 'userId' => $userId, - 'deviceId' => $deviceId - ]; - - if (!is_null($companyId)) { - $where['companyId'] = $companyId; - } - - return self::where($where)->count() > 0; - } + /** * 关联用户模型 diff --git a/Server/application/common/model/DeviceWechatLogin.php b/Server/application/common/model/DeviceWechatLogin.php new file mode 100644 index 00000000..5a9545eb --- /dev/null +++ b/Server/application/common/model/DeviceWechatLogin.php @@ -0,0 +1,14 @@ +user = request()->userInfo;; + } + + /** + * 获取用户信息 + * + * @param string $column + * @return mixed + * @throws \Exception + */ + protected function getUserInfo(string $column = '') + { + if (!$this->user) { + throw new \Exception('未授权访问,缺少有效的身份凭证', 401); + } + + return $column ? $this->user[$column] : $this->user; + } +} \ No newline at end of file diff --git a/Server/application/cunkebao/controller/device/GetDeviceDetailV1Controller.php b/Server/application/cunkebao/controller/device/GetDeviceDetailV1Controller.php new file mode 100644 index 00000000..2e7cae10 --- /dev/null +++ b/Server/application/cunkebao/controller/device/GetDeviceDetailV1Controller.php @@ -0,0 +1,176 @@ + $deviceId, + 'userId' => $this->getUserInfo('id'), + 'companyId' => $this->getUserInfo('companyId') + ]; + + $hasPermission = DeviceUserModel::where($where)->count() > 0; + + if (!$hasPermission) { + throw new \Exception('您没有权限查看该设备', '403'); + } + } + + /** + * 解析设备额外信息 + * + * @param string $extra + * @return int + */ + protected function parseExtraForBattery(string $extra): int + { + if (!empty($extra)) { + $extra = json_decode($extra, true); + + if (is_array($extra) && isset($extra['battery'])) { + return intval($extra['battery']); + } + } + + return 0; + } + + /** + * 解析taskConfig字段获取功能开关 + * + * @param int $deviceId + * @return int[] + * @throws \Exception + */ + protected function getTaskConfig(int $deviceId): array + { + $where = [ + 'deviceId' => $deviceId, + 'companyId' => $this->getUserInfo('companyId'), + 'deleteTime' => 0 + ]; + + $conf = DeviceTaskconfModel::where($where)->field('autoAddFriend,autoReply,contentSync,aiChat')->find(); + + return $conf ? $conf->toArray() : [ + 'autoAddFriend' => 0, + 'autoReply' => 0, + 'contentSync' => 0, + 'aiChat' => 0 + ]; + } + + /** + * 统计设备登录微信的好友 + * + * @param int $deviceId + * @return int + * @throws \Exception + */ + protected function getTotalFriend(int $deviceId): int + { + $where = [ + 'deviceId' => $deviceId, + 'companyId' => $this->getUserInfo('companyId'), + ]; + + $ownerWechatId = DeviceWechatLogin::where($where)->order('createTime desc')->value('wechatId'); + + if ($ownerWechatId) { + return WechatFriend::where(['ownerWechatId' => $ownerWechatId])->count(); + } + + return 0; + } + + /** + * 获取设备绑定微信你的消息总数 + * + * @return int + */ + protected function getThirtyDayMsgCount(): int + { + return 0; + } + + /** + * 获取设备详情 + * @param int $id 设备ID + * @return array|null 设备信息 + */ + protected function getDeviceInfo(int $id) + { + // 查询设备基础信息与关联的微信账号信息 + $device = DeviceModel::alias('d') + ->field([ + 'd.id', 'd.imei', 'd.memo', 'd.alive', 'd.updateTime as lastUpdateTime', 'd.extra' + ]) + ->where('d.deleteTime', 0) + ->find($id); + + if (empty($device)) { + throw new \Exception('设备不存在', '404'); + } + + $device['battery'] = $this->parseExtraForBattery($device['extra']); + $device['features'] = $this->getTaskConfig($id); + $device['totalFriend'] = $this->getTotalFriend($id); + $device['thirtyDayMsgCount'] = $this->getThirtyDayMsgCount(); + + // 设备最后活跃时间为设备状态更新时间 + $device['lastUpdateTime'] = date('Y-m-d H:i:s', $device['lastUpdateTime']); + + // 删除冗余字段 + unset($device['extra']); + + return $device; + } + + /** + * 获取设备详情 + * @return \think\response\Json + */ + public function index() + { + try { + // 获取设备ID + $id = Request::param('id/d'); + + if ($this->getUserInfo('isAdmin') != 1) { + $this->checkUserDevicePermission($id); + } + + $info = $this->getDeviceInfo($id); + + return json([ + 'code' => 200, + 'msg' => '获取成功', + 'data' => $info + ]); + } catch (\Exception $e) { + return json([ + 'code' => $e->getMessage(), + 'msg' => $e->getMessage() + ]); + } + } +} \ No newline at end of file diff --git a/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php b/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php index 857015af..dd87b040 100644 --- a/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php +++ b/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php @@ -1,35 +1,17 @@ user = request()->userInfo;; - } - /** * 构建查询条件 * @@ -50,7 +32,7 @@ class GetDeviceListV1Controller extends Controller $where['d.alive'] = $alive; } - $where['d.companyId'] = $this->user['companyId']; + $where['d.companyId'] = $this->getUserInfo('companyId'); $where['d.deleteTime'] = 0; return array_merge($params, $where); @@ -64,8 +46,8 @@ class GetDeviceListV1Controller extends Controller protected function makeDeviceIdsWhere(): array { $deviceIds = DeviceUserModel::where( - $this->user['id'], - $this->user['companyId'] + $this->getUserInfo('id'), + $this->getUserInfo('companyId') ) ->column('deviceId'); @@ -85,11 +67,12 @@ class GetDeviceListV1Controller extends Controller * @param int $limit 每页数量 * @return \think\Paginator 分页对象 */ - protected function getDeviceList($where, $page = 1, $limit = 10) + protected function getDeviceList(array $where, int $page = 1, int $limit = 10) { $query = DeviceModel::alias('d') ->field(['d.id', 'd.imei', 'd.memo', 'l.wechatId', 'd.alive', '0 totalFriend']) - ->leftJoin('device_wechat_login l', 'd.id = l.deviceId'); + ->leftJoin('device_wechat_login l', 'd.id = l.deviceId') + ->order('d.alive desc'); foreach ($where as $key => $value) { if (is_numeric($key) && is_array($value) && isset($value[0]) && $value[0] === 'exp') { @@ -136,7 +119,7 @@ class GetDeviceListV1Controller extends Controller $page = (int)Request::param('page', 1); $limit = (int)Request::param('limit', 10); - if ($this->user['isAdmin'] == 1) { + if ($this->getUserInfo('isAdmin') == 1) { $where = $this->makeWhere(); $result = $this->getDeviceList($where, $page, $limit); } else { @@ -148,14 +131,14 @@ class GetDeviceListV1Controller extends Controller 'code' => 200, 'msg' => '获取成功', 'data' => [ - 'list' => $this->countFriend($result), + 'list' => $this->countFriend($result), 'total' => $result->total(), ] ]); } catch (\Exception $e) { return json([ 'code' => $e->getCode(), - 'msg' => $e->getMessage() + 'msg' => $e->getMessage() ]); } } diff --git a/Server/application/cunkebao/model/Device.php b/Server/application/cunkebao/model/Device.php index 0fd236da..19b1da19 100644 --- a/Server/application/cunkebao/model/Device.php +++ b/Server/application/cunkebao/model/Device.php @@ -40,72 +40,7 @@ class Device extends Model return self::where($where)->count(); } } - - /** - * 获取设备详情 - * @param int $id 设备ID - * @return array|null 设备信息 - */ - public static function getDeviceInfo($id) - { - // 查询设备基础信息与关联的微信账号信息 - $device = self::alias('d') - ->field([ - 'd.id', 'd.imei', 'd.memo', 'd.alive', 'd.taskConfig', 'd.lastUpdateTime', - 'w.id as wechatId', 'w.thirtyDayMsgCount', 'w.totalFriend', 'd.extra' - ]) - ->leftJoin('ck_wechat_account w', 'd.imei = w.imei') - ->where('d.id', $id) - ->where('d.isDeleted', 0) - ->find(); - - // 如果设备存在,处理额外信息 - if ($device) { - // 解析电量信息 - $battery = 0; - if (!empty($device['extra'])) { - $extra = json_decode($device['extra'], true); - if (is_array($extra) && isset($extra['battery'])) { - $battery = intval($extra['battery']); - } - } - $device['battery'] = $battery; - - // 解析taskConfig字段获取功能开关 - $features = [ - 'autoAddFriend' => false, - 'autoReply' => false, - 'contentSync' => false, - 'aiChat' => false - ]; - - if (!empty($device['taskConfig'])) { - $taskConfig = json_decode($device['taskConfig'], true); - if (is_array($taskConfig)) { - // 映射taskConfig中的字段到前端需要的features - $features['autoAddFriend'] = isset($taskConfig['autoAddFriend']) ? (bool)$taskConfig['autoAddFriend'] : false; - $features['autoReply'] = isset($taskConfig['autoReply']) ? (bool)$taskConfig['autoReply'] : false; - $features['contentSync'] = isset($taskConfig['momentsSync']) ? (bool)$taskConfig['momentsSync'] : false; - $features['aiChat'] = isset($taskConfig['aiChat']) ? (bool)$taskConfig['aiChat'] : false; - } - } - - $device['features'] = $features; - unset($device['extra']); - unset($device['taskConfig']); - - // 格式化最后活跃时间 - $device['lastUpdateTime'] = !empty($device['lastUpdateTime']) ? date('Y-m-d H:i:s', strtotime($device['lastUpdateTime'])) : date('Y-m-d H:i:s'); - - // 确保totalFriend和thirtyDayMsgCount有值,防止NULL - $device['totalFriend'] = intval($device['totalFriend'] ?? 0); - $device['thirtyDayMsgCount'] = intval($device['thirtyDayMsgCount'] ?? 0); - } - - return $device; - } - /** * 添加设备 * @param array $data 设备数据