From c55cc5c64417fbcf69d9fa1bb68f749800bd3b08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E6=B8=85=E7=88=BD?= Date: Thu, 27 Mar 2025 18:08:28 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=99=BB=E5=BD=95=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/common/model/DeviceUser.php | 200 ++++++++++++++++++ Server/application/common/model/User.php | 4 +- .../common/service/AuthService.php | 2 +- Server/application/common/util/JwtUtil.php | 4 +- 4 files changed, 206 insertions(+), 4 deletions(-) create mode 100644 Server/application/common/model/DeviceUser.php diff --git a/Server/application/common/model/DeviceUser.php b/Server/application/common/model/DeviceUser.php new file mode 100644 index 00000000..74dd9771 --- /dev/null +++ b/Server/application/common/model/DeviceUser.php @@ -0,0 +1,200 @@ + '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 removeRelation($companyId, $userId, $deviceId) + { + return self::where([ + 'companyId' => $companyId, + 'userId' => $userId, + 'deviceId' => $deviceId + ])->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; + } + + /** + * 关联用户模型 + * @return \think\model\relation\BelongsTo + */ + public function user() + { + return $this->belongsTo('User', 'userId', 'id'); + } + + /** + * 关联设备模型 + * @return \think\model\relation\BelongsTo + */ + public function device() + { + return $this->belongsTo('app\devices\model\Device', 'deviceId', 'id'); + } + + /** + * 关联公司模型 + * @return \think\model\relation\BelongsTo + */ + public function company() + { + return $this->belongsTo('Company', 'companyId', 'id'); + } +} \ No newline at end of file diff --git a/Server/application/common/model/User.php b/Server/application/common/model/User.php index c9c7bad9..4c3e6a50 100644 --- a/Server/application/common/model/User.php +++ b/Server/application/common/model/User.php @@ -67,13 +67,13 @@ class User extends Model ]; /** - * 获取管理员用户信息 + * 获取用户信息 * @param string $account 账号(手机号) * @param string $password 密码(可能是加密后的) * @param int $typeId 身份信息 * @return array|null */ - public static function getAdminUser($account, $password, $typeId) + public static function getUser($account, $password, $typeId) { // 查询用户 $user = self::where('account', $account) diff --git a/Server/application/common/service/AuthService.php b/Server/application/common/service/AuthService.php index b18ba5c4..fcd52e57 100644 --- a/Server/application/common/service/AuthService.php +++ b/Server/application/common/service/AuthService.php @@ -40,7 +40,7 @@ class AuthService public function login($account, $password, $typeId, $ip) { // 获取用户信息 - $user = User::getAdminUser($account, $password, $typeId); + $user = User::getUser($account, $password, $typeId); if (empty($user)) { // 记录登录失败 diff --git a/Server/application/common/util/JwtUtil.php b/Server/application/common/util/JwtUtil.php index 2ba8fb5f..8285fd50 100644 --- a/Server/application/common/util/JwtUtil.php +++ b/Server/application/common/util/JwtUtil.php @@ -38,7 +38,9 @@ class JwtUtil // 附加过期时间 $payload['exp'] = time() + $expire; $payload['iat'] = time(); // 签发时间 - + + unset($payload['passwordMd5']); + $payload = self::base64UrlEncode(json_encode($payload, JSON_UNESCAPED_UNICODE)); $signature = self::signature($header . '.' . $payload, self::$secret);