登录"); } // 获取用户ID if (isset($_SESSION['user_id']) && is_numeric($_SESSION['user_id'])) { $userId = (int)$_SESSION['user_id']; } else { throw new Exception("无法获取用户ID,请重新登录"); } // 加载配置文件 $config = []; $configFile = 'pmconfig.php'; if (file_exists($configFile)) { $config = include $configFile; if (!is_array($config)) { throw new Exception("配置文件必须返回一个数组"); } } else { throw new Exception("配置文件 pmconfig.php 不存在"); } // 检查OSS配置 $ossParams = ['oss_access_key', 'oss_secret_key', 'oss_endpoint', 'oss_bucket']; $missingOssParams = []; foreach ($ossParams as $param) { if (!isset($config[$param]) || empty($config[$param])) { $missingOssParams[] = $param; } } $ossConfigured = empty($missingOssParams); // 引入OSS SDK if ($ossConfigured) { $ossSdkPath = __DIR__ . '/oss-sdk/autoload.php'; if (file_exists($ossSdkPath) && is_readable($ossSdkPath)) { require_once $ossSdkPath; $ossLoaded = class_exists('OSS\OssClient') && class_exists('OSS\Core\OssException'); } } // 生成头像URL if ($ossConfigured && $ossLoaded) { $bucket = $config['oss_bucket']; $endpoint = preg_replace('/^https?:\/\//', '', $config['oss_endpoint']); $object = 'sunmusic/profile/' . $userId . '头像.png'; $avatarUrl = "https://{$bucket}.{$endpoint}/{$object}?t=" . time(); } // 数据库连接和用户信息获取 $dbRequired = ['db_host', 'db_user', 'db_name']; $dbMissing = []; foreach ($dbRequired as $param) { if (!isset($config[$param]) || empty($config[$param])) { $dbMissing[] = $param; } } if (empty($dbMissing)) { $conn = new mysqli( $config['db_host'], $config['db_user'], $config['db_pass'] ?? '', $config['db_name'] ); if ($conn->connect_error) { throw new Exception("数据库连接失败: " . $conn->connect_error); } $conn->set_charset("utf8mb4"); // 获取用户信息 $stmt = $conn->prepare("SELECT nickname, email FROM users WHERE id = ?"); $stmt->bind_param("i", $userId); $stmt->execute(); $result = $stmt->get_result(); if ($userData = $result->fetch_assoc()) { $currentNickname = $userData['nickname'] ?? '未设置'; $currentEmail = $userData['email'] ?? '未设置'; } else { throw new Exception("未找到用户信息"); } $stmt->close(); // 处理基础信息更新 if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_basic'])) { $newNickname = trim($_POST['nickname'] ?? ''); $newEmail = trim($_POST['email'] ?? ''); $updates = []; $params = []; $types = ''; if (!empty($newNickname) && $newNickname !== $currentNickname) { if (strlen($newNickname) < 2 || strlen($newNickname) > 20) { throw new Exception("用户名长度必须在2-20个字符之间"); } $updates[] = "nickname = ?"; $params[] = $newNickname; $types .= "s"; } if (!empty($newEmail) && $newEmail !== $currentEmail) { if (!filter_var($newEmail, FILTER_VALIDATE_EMAIL)) { throw new Exception("请输入有效的邮箱地址"); } // 检查邮箱是否已被使用 $checkStmt = $conn->prepare("SELECT id FROM users WHERE email = ? AND id != ?"); $checkStmt->bind_param("si", $newEmail, $userId); $checkStmt->execute(); $checkResult = $checkStmt->get_result(); if ($checkResult->num_rows > 0) { throw new Exception("该邮箱已被其他用户使用"); } $checkStmt->close(); $updates[] = "email = ?"; $params[] = $newEmail; $types .= "s"; } if (!empty($updates)) { $sql = "UPDATE users SET " . implode(", ", $updates) . " WHERE id = ?"; $types .= "i"; $params[] = $userId; $stmt = $conn->prepare($sql); $bindParams = array_merge([$types], $params); $tmp = []; foreach ($bindParams as $key => $value) { $tmp[$key] = &$bindParams[$key]; } call_user_func_array([$stmt, 'bind_param'], $tmp); $stmt->execute(); $stmt->close(); $currentNickname = $newNickname; $currentEmail = $newEmail; $message = "信息更新成功"; $messageType = 'success'; } else { $message = "没有需要更新的信息"; $messageType = 'success'; } } $conn->close(); } else { throw new Exception("数据库配置不完整(缺少: " . implode(', ', $dbMissing) . ")"); } // 处理头像上传 if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['upload_avatar']) && $ossConfigured && $ossLoaded) { if (!isset($_FILES['avatar'])) { throw new Exception("未收到上传文件"); } // 检查上传错误 switch ($_FILES['avatar']['error']) { case UPLOAD_ERR_OK: break; case UPLOAD_ERR_NO_FILE: throw new Exception("请选择要上传的图片"); case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: throw new Exception("文件过大,超过上传限制"); default: throw new Exception("上传错误,代码: " . $_FILES['avatar']['error']); } $avatarFile = $_FILES['avatar']; // 验证文件 if (!is_uploaded_file($avatarFile['tmp_name'])) { throw new Exception("文件上传异常"); } $fileInfo = getimagesize($avatarFile['tmp_name']); if (!$fileInfo) { throw new Exception("不是有效的图片文件"); } $allowedMime = ['image/jpeg', 'image/png', 'image/gif']; if (!in_array($fileInfo['mime'], $allowedMime)) { throw new Exception("不支持的图片类型"); } if ($avatarFile['size'] > 10 * 1024 * 1024) { throw new Exception("文件过大,最大支持10MB"); } // OSS上传 $object = 'sunmusic/profile/' . $userId . '头像.png'; try { $ossClient = new OSS\OssClient( $config['oss_access_key'], $config['oss_secret_key'], $config['oss_endpoint'] ); $options = [ OSS\OssClient::OSS_HEADERS => [ 'x-oss-object-acl' => 'public-read', ], ]; $ossClient->uploadFile( $config['oss_bucket'], $object, $avatarFile['tmp_name'], $options ); $endpoint = preg_replace('/^https?:\/\//', '', $config['oss_endpoint']); $avatarUrl = "https://{$config['oss_bucket']}.{$endpoint}/{$object}?t=" . time(); $message = "头像上传成功!"; $messageType = 'success'; } catch (OSS\Core\OssException $e) { throw new Exception("OSS错误: " . $e->getMessage()); } } } catch (Exception $e) { $message = $e->getMessage(); $messageType = 'error'; } // 检测设备类型 $isMobile = preg_match('/(android|iphone|ipad|ipod|blackberry|windows phone)/i', $_SERVER['HTTP_USER_AGENT']); ?> 个人信息

个人信息管理

个人头像

用户头像

返回