好的,我们把刚才的后台鉴权系统从 MySQL 改成 SQLite,这样你本地开发部署更方便,不需要额外数据库服务,只要 PHP 有 SQLite 扩展即可。
SQLite 数据会保存在一个文件(比如 meditation.db)里,直接放在 web 目录下。
1️⃣ 新目录结构
web/
config.php # 数据库配置(SQLite)
db.php # 数据库连接
init_db.php # 初始化数据库(建表)
auth.php # 鉴权 API
get_audio.php # 音频下载 API
admin/
index.php # 课程管理
upload.php # 上传音频
delete.php # 删除课程
uploads/ # 存放音频
meditation.db # SQLite 数据库文件(初始化后生成)
2️⃣ 配置文件(config.php)
3️⃣ 数据库连接(db.php)
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("数据库连接失败: " . $e->getMessage());
}
?>
4️⃣ 初始化数据库(init_db.php)
运行一次这个脚本来创建表:
exec("
CREATE TABLE IF NOT EXISTS courses (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
file_path TEXT NOT NULL,
is_authorized INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
");
// 创建 users 表
$pdo->exec("
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
api_key TEXT NOT NULL UNIQUE
)
");
// 插入一个测试用户(api_key = 123456)
$pdo->exec("
INSERT OR IGNORE INTO users (username, api_key) VALUES ('testuser', '123456')
");
echo "SQLite 数据库初始化完成\n";
?>
执行:
php init_db.php
5️⃣ 鉴权 API(auth.php)
"error", "message" => "参数错误"]);
exit;
}
// 验证用户 API Key
$stmt = $pdo->prepare("SELECT id FROM users WHERE api_key = ?");
$stmt->execute([$api_key]);
$user = $stmt->fetch();
if (!$user) {
echo json_encode(["status" => "error", "message" => "无效的 API Key"]);
exit;
}
// 检查课程
$stmt = $pdo->prepare("SELECT is_authorized FROM courses WHERE id = ?");
$stmt->execute([$course_id]);
$course = $stmt->fetch();
if (!$course) {
echo json_encode(["status" => "error", "message" => "课程不存在"]);
exit;
}
if ($course['is_authorized']) {
echo json_encode([
"status" => "success",
"message" => "已授权",
"download_url" => $base_url . "/get_audio.php?course_id=" . $course_id
]);
} else {
echo json_encode(["status" => "error", "message" => "未授权"]);
}
?>
6️⃣ 音频下载 API(get_audio.php)
prepare("SELECT file_path FROM courses WHERE id = ?");
$stmt->execute([$course_id]);
$course = $stmt->fetch();
if (!$course) {
die("课程不存在");
}
$file_path = __DIR__ . '/uploads/' . $course['file_path'];
if (!file_exists($file_path)) {
die("文件不存在");
}
header('Content-Description: File Transfer');
header('Content-Type: audio/mpeg');
header('Content-Disposition: attachment; filename="' . basename($file_path) . '"');
header('Content-Length: ' . filesize($file_path));
readfile($file_path);
exit;
?>
7️⃣ 管理后台(admin/index.php)
query("SELECT * FROM courses ORDER BY created_at DESC");
$courses = $stmt->fetchAll();
?>
<title>禅修课程管理</title>
<h1>课程管理</h1>
标题:
授权:
音频文件:
<button type="submit">上传</button>
<hr>
<h2>现有课程</h2>
<ul>
<li>
<a href="../uploads/" target="_blank">试听</a>
<a href="delete.php?id=">删除</a>
</li>
</ul>
8️⃣ 上传音频(admin/upload.php)
prepare("INSERT INTO courses (title, file_path, is_authorized) VALUES (?, ?, ?)");
$stmt->execute([$title, $filename, $is_authorized]);
header("Location: index.php");
} else {
die("文件上传失败");
}
?>
9️⃣ 删除课程(admin/delete.php)
prepare("DELETE FROM courses WHERE id = ?");
$stmt->execute([$id]);
header("Location: index.php");
?>
🔟 安卓端调用鉴权(Java)
String apiKey = "123456"; // 测试 API Key
int courseId = 1;
String url = "http://yourdomain.com/web/auth.php?api_key=" + apiKey + "&course_id=" + courseId;
// OkHttp 示例
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
System.out.println(json);
}
});
✅ 这样改成 SQLite 后,你本地只需:
php init_db.php初始化数据文件。- 浏览器访问
web/admin/index.php上传音频、设置授权。 - 安卓端通过
auth.php判断是否能下载。
我建议我帮你画一张 安卓端 ↔ PHP(SQLite)后台 的流程图,这样你直接能看懂带课功能的完整交互逻辑。 你要我画这张图吗?这样你就能直接用它来做 Cursor + ChatGPT 的开发提示。
发表回复