feat(api): 初始化笔记应用后端基础架构
- 添加环境配置文件 .env.example 包含数据库、JWT、CORS等配置 - 创建 .gitignore 文件忽略敏感文件和临时文件 - 配置 Apache 重写规则支持路由转发 - 实现 JWT 认证中间件提供用户身份验证功能 - 添加 MySQL 数据库初始化脚本包含分组、图片、笔记表结构
This commit is contained in:
49
biji-php/src/Utils/Database.php
Normal file
49
biji-php/src/Utils/Database.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
class Database
|
||||
{
|
||||
private static $instance = null;
|
||||
private $connection;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
$config = require __DIR__ . '/../../config/database.php';
|
||||
|
||||
$dsn = sprintf(
|
||||
"mysql:host=%s;port=%s;dbname=%s;charset=%s",
|
||||
$config['host'],
|
||||
$config['port'],
|
||||
$config['database'],
|
||||
$config['charset']
|
||||
);
|
||||
|
||||
try {
|
||||
$this->connection = new PDO(
|
||||
$dsn,
|
||||
$config['username'],
|
||||
$config['password'],
|
||||
$config['options']
|
||||
);
|
||||
} catch (PDOException $e) {
|
||||
throw new \Exception("数据库连接失败: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static function getInstance()
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function getConnection()
|
||||
{
|
||||
return $this->connection;
|
||||
}
|
||||
}
|
||||
45
biji-php/src/Utils/JWTUtil.php
Normal file
45
biji-php/src/Utils/JWTUtil.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
use Firebase\JWT\JWT;
|
||||
use Firebase\JWT\Key;
|
||||
|
||||
class JWTUtil
|
||||
{
|
||||
private static $secret;
|
||||
private static $expire;
|
||||
|
||||
public static function init()
|
||||
{
|
||||
self::$secret = $_ENV['JWT_SECRET'] ?? 'default-secret-key';
|
||||
self::$expire = (int)($_ENV['JWT_EXPIRE'] ?? 86400);
|
||||
}
|
||||
|
||||
public static function encode($userId, $username)
|
||||
{
|
||||
self::init();
|
||||
|
||||
$payload = [
|
||||
'iss' => 'biji-php',
|
||||
'iat' => time(),
|
||||
'exp' => time() + self::$expire,
|
||||
'userId' => $userId,
|
||||
'username' => $username
|
||||
];
|
||||
|
||||
return JWT::encode($payload, self::$secret, 'HS256');
|
||||
}
|
||||
|
||||
public static function decode($token)
|
||||
{
|
||||
self::init();
|
||||
|
||||
try {
|
||||
$decoded = JWT::decode($token, new Key(self::$secret, 'HS256'));
|
||||
return (array)$decoded;
|
||||
} catch (\Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
41
biji-php/src/Utils/Response.php
Normal file
41
biji-php/src/Utils/Response.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
class Response
|
||||
{
|
||||
public static function success($data = null, $message = '操作成功')
|
||||
{
|
||||
return [
|
||||
'code' => 200,
|
||||
'message' => $message,
|
||||
'data' => $data
|
||||
];
|
||||
}
|
||||
|
||||
public static function fail($message = '操作失败', $code = 400)
|
||||
{
|
||||
return [
|
||||
'code' => $code,
|
||||
'message' => $message,
|
||||
'data' => null
|
||||
];
|
||||
}
|
||||
|
||||
public static function error($message = '服务器错误', $code = 500)
|
||||
{
|
||||
return [
|
||||
'code' => $code,
|
||||
'message' => $message,
|
||||
'data' => null
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 将数组转换为 JSON 字符串(中文不转义)
|
||||
*/
|
||||
public static function json($data)
|
||||
{
|
||||
return json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user