PHLI API 接口插件
PHLI API 接口插件为 PHLI(原 Prain 清雨)博客系统提供完整的 RESTful API,覆盖全部后台管理功能,可用于开发移动端 APP、小程序、第三方客户端等。
| 插件版本 | 1.0.0 |
| 适用系统 | PHLI ≥ 1.3.0 |
| 插件ID | api |
| 配套插件 | api-auth(API 权限管理,可选) |
快速开始
安装
- 将
api 文件夹放入 ext/ 目录
- 在 PHLI 后台 → 扩展管理 中安装「API接口」插件
- 安装后系统自动生成 32 位随机 API Key
- 在后台 → API设置 中查看和管理 API Key
URL 格式
API 地址取决于是否开启伪静态:
| 伪静态 | URL 格式 | 示例 |
| 开启 | /api/{module}/{action} | https://blog.com/api/article/list/1 |
| 关闭 | /?api/{module}/{action} | https://blog.com/?api/article/list/1 |
可通过 api/check 接口返回的 rewrite 字段判断伪静态状态。
认证方式
所有 API 请求(api/check 除外)均需携带 API Key,支持两种方式:
方式一:请求头(推荐)
X-API-Key: your_api_key_here
方式二:Bearer Token
Authorization: Bearer your_api_key_here
响应格式
所有接口统一返回 JSON 格式:
成功响应
{
"error": false,
"code": 0,
"message": "操作成功",
"data": { ... }
}
错误响应
{
"error": true,
"code": 401,
"message": "API Key无效",
"data": []
}
常见错误码
| 错误码 | 含义 |
| 400 | 请求参数错误或请求方式不正确 |
| 401 | API Key 无效或未提供 |
| 403 | 权限不足(api-auth 限制) |
| 404 | 接口不存在或资源不存在 |
| 500 | 服务器内部错误 |
接口总览
| 模块 | 接口 | 方法 | 说明 |
| 系统 | api/check | GET 无需认证 | 连接检测 |
api/system | GET | 获取系统信息 |
api/system/checkUpdate | GET | 检查系统更新 |
api/system/update | POST | 执行系统更新 |
api/system/dbUpdate | POST | 数据库版本更新 |
| 文章 | api/article/list/{page} | GET | 文章列表 |
api/article/detail/{id} | GET | 文章详情 |
api/article/create | POST | 创建文章 |
api/article/update | POST | 更新文章 |
api/article/delete | POST | 删除文章 |
api/article/move | POST | 移动文章分类/标签 |
| 分类 | api/category/list | GET | 分类列表 |
api/category/add | POST | 添加分类 |
api/category/edit | POST | 编辑分类 |
api/category/save | POST | 批量保存分类 |
api/category/delete | POST | 删除分类 |
| 标签 | api/tag/list | GET | 标签列表 |
| 评论 | api/comment/list/{articleId} | GET | 文章评论列表 |
api/comment/listall | POST | 全部评论列表 |
api/comment/delete | POST | 删除评论 |
| 链接 | api/link/list | GET | 友情链接列表 |
api/link/add | POST | 添加链接 |
api/link/edit | POST | 编辑链接 |
api/link/save | POST | 批量保存链接 |
api/link/delete | POST | 删除链接 |
| 导航 | api/navbar/list | GET | 导航菜单列表 |
api/navbar/add | POST | 添加导航 |
api/navbar/edit | POST | 编辑导航 |
api/navbar/save | POST | 批量保存导航 |
api/navbar/delete | POST | 删除导航 |
| 设置 | api/setting/get | GET | 获取博客设置 |
| 设置 | api/setting/save | POST | 保存博客设置 |
| 上传 | api/upload/upload | POST | 文件上传 |
| 主题 | api/theme/list | GET | 主题列表 |
api/theme/install | POST | 启用主题 |
api/theme/delete | POST | 删除主题 |
api/theme/setting | POST | 获取主题设置页 |
api/theme/saveSetting | POST | 保存主题设置 |
| 扩展 | api/ext/list | GET | 扩展列表 |
api/ext/install | POST | 安装扩展 |
api/ext/uninstall | POST | 卸载扩展 |
api/ext/delete | POST | 删除扩展 |
api/ext/setting | POST | 获取扩展设置页 |
| 日志 | api/log/stats | GET | 日志统计 |
api/log/setting | GET | 日志开关设置 |
api/log/list | POST | 日志列表 |
api/log/clear | POST | 清空日志 |
api/log/setting | POST | 修改日志开关 |
1. 系统模块
1.1 连接检测 无需认证
用于检测 API 是否可用及获取站点基本信息。
GET/api/check
{
"error": false,
"code": 0,
"message": "操作成功",
"data": {
"title": "我的博客",
"name": "PHLI",
"intro": "一个简单的博客",
"avatar": "https://blog.com/db/upload/202601/01_120000_123.jpg",
"version": "1.3.2",
"url": "https://blog.com",
"rewrite": true
}
}
提示:rewrite 字段用于判断 URL 格式,true 表示伪静态开启,false 表示关闭。
1.2 获取系统信息
GET/api/system
响应 data 字段
| 字段 | 类型 | 说明 |
| version | string | PHLI 版本号 |
| dbVersion | string | 数据库版本 |
| articleCount | int | 文章总数 |
| commentCount | int | 评论总数 |
| categoryCount | int | 分类总数 |
| tagCount | int | 标签总数 |
| linkCount | int | 链接总数 |
| navbarCount | int | 导航总数 |
| extCount | int | 扩展总数 |
| themeCount | int | 主题总数 |
| views | int | 总浏览量 |
| phpVersion | string | PHP 版本 |
1.3 检查系统更新
GET/api/system/checkUpdate
| 字段 | 类型 | 说明 |
| currentVersion | string | 当前版本 |
| latestVersion | string | 最新版本 |
| hasUpdate | bool | 是否有更新 |
1.4 执行系统更新
POST/api/system/update
无参数,直接调用即执行更新。
1.5 数据库版本更新
POST/api/system/dbUpdate
无参数,同步数据库结构到最新版本。
2. 文章模块
2.1 文章列表
GET/api/article/list/{page}
查询参数
| 参数 | 类型 | 必填 | 说明 |
| pageSize | int | 否 | 每页条数,默认使用系统设置 |
| cid | string | 否 | 按分类ID筛选 |
| tag | string | 否 | 按标签筛选 |
| keyword | string | 否 | 按标题关键词搜索 |
响应 data 字段
| 字段 | 类型 | 说明 |
| list | array | 文章列表 |
| list[].id | string | 文章URL名称(唯一标识) |
| list[].cid | string | 所属分类ID |
| list[].category | string | 所属分类名称 |
| list[].title | string | 文章标题 |
| list[].intro | string | 文章摘要 |
| list[].img | string | 缩略图完整URL |
| list[].isTop | int | 是否置顶(0/1) |
| list[].isPrivate | int | 是否私密(0/1) |
| list[].isComment | int | 是否允许评论(0/1) |
| list[].isFk | int | 是否FK格式(0/1) |
| list[].comments | int | 评论数 |
| list[].views | int | 浏览量 |
| list[].tag | array | 标签数组 |
| list[].time | string | 创建时间 |
| list[].updateTime | string | 更新时间 |
| list[].url | string | 文章访问URL |
| count | int | 文章总数 |
| currentPage | int | 当前页码 |
| countPage | int | 总页数 |
| pageSize | int | 每页条数 |
2.2 文章详情
GET/api/article/detail/{id}
路径参数
| 参数 | 类型 | 必填 | 说明 |
| id | string | 是 | 文章URL名称 |
响应包含文章列表所有字段,额外增加:
| 字段 | 类型 | 说明 |
| content | string | 解析后的HTML内容(图片URL已补全) |
| rawContent | string | 原始内容(图片URL已补全) |
重要:content 是经过 FK 解析的 HTML,rawContent 是原始保存的内容。编辑文章时应使用 rawContent。
2.3 创建文章
POST/api/article/create
请求参数(application/x-www-form-urlencoded)
| 参数 | 类型 | 必填 | 说明 |
| title | string | 是 | 文章标题 |
| content | string | 是 | 文章内容(HTML) |
| id | string | 否 | URL名称,留空自动生成(T+时间戳) |
| cid | string | 否 | 分类ID |
| tag | string | 否 | 标签,空格分隔 |
| intro | string | 否 | 摘要,留空自动截取 |
| isTop | int | 否 | 是否置顶,默认0 |
| isPrivate | int | 否 | 是否私密,默认0 |
| isComment | int | 否 | 允许评论,默认0 |
| isFk | int | 否 | FK格式,默认0 |
{
"error": false,
"code": 0,
"message": "创建成功",
"data": { "id": "my-first-post" }
}
注意:content 中的图片完整URL会自动转换为相对路径存储。
2.4 更新文章
POST/api/article/update
| 参数 | 类型 | 必填 | 说明 |
| id | string | 是 | 原文章URL名称 |
| title | string | 是 | 文章标题 |
| content | string | 是 | 文章内容 |
| newId | string | 否 | 新URL名称,留空保持不变 |
| cid | string | 否 | 分类ID |
| tag | string | 否 | 标签,空格分隔 |
| intro | string | 否 | 摘要 |
| isTop | int | 否 | 是否置顶 |
| isPrivate | int | 否 | 是否私密 |
| isComment | int | 否 | 允许评论 |
| isFk | int | 否 | FK格式 |
注意:如果 newId 与 id 不同,系统会自动迁移文章数据和评论数据。
2.5 删除文章
POST/api/article/delete
| 参数 | 类型 | 必填 | 说明 |
| id | string/array | 是 | 文章URL名称,支持批量(传数组) |
批量删除示例:id[0]=first-post&id[1]=second-post
2.6 移动文章
POST/api/article/move
| 参数 | 类型 | 必填 | 说明 |
| id | string/array | 是 | 文章URL名称,支持批量 |
| cid | string | 是 | 目标分类ID或标签名 |
| type | string | 否 | 移动类型:category(默认)或 tag |
3. 分类模块
3.1 分类列表
GET/api/category/list
| 字段 | 类型 | 说明 |
| id | string | 分类别称(唯一标识) |
| name | string | 分类名称 |
| intro | string | 分类描述 |
| count | int | 文章数量 |
| url | string | 分类页URL |
重要:PHLI 的分类使用关联数组存储,id(别称)是字符串键名,不是自增数字。
3.2 添加分类
POST/api/category/add
| 参数 | 类型 | 必填 | 说明 |
| id | string | 是 | 分类别称(如 php、tech),唯一标识 |
| name | string | 是 | 分类名称 |
| intro | string | 否 | 分类描述 |
3.3 编辑分类
POST/api/category/edit
| 参数 | 类型 | 必填 | 说明 |
| id | string | 是 | 分类别称 |
| name | string | 是 | 分类名称 |
| intro | string | 否 | 分类描述 |
3.4 批量保存分类
POST/api/category/save
用于后台管理页面的全量保存模式,参数为数组格式。
| 参数 | 类型 | 必填 | 说明 |
| id | array | 是 | 原分类别称数组 |
| newId | array | 是 | 新分类别称数组 |
| name | array | 是 | 分类名称数组 |
| intro | array | 是 | 分类描述数组 |
| delId | array | 否 | 要删除的分类别称数组 |
3.5 删除分类
POST/api/category/delete
| 参数 | 类型 | 必填 | 说明 |
| id | string/array | 是 | 分类别称,支持批量 |
删除分类后,该分类下的文章 cid 会被置空。
4. 标签模块
4.1 标签列表
GET/api/tag/list
| 字段 | 类型 | 说明 |
| name | string | 标签名称 |
| count | int | 文章数量 |
| url | string | 标签页URL |
标签为只读,由系统根据文章标签自动生成,无增删改接口。
5.1 文章评论列表
GET/api/comment/list/{articleId}?page=1&pageSize=10
| 参数 | 类型 | 必填 | 说明 |
| articleId | string | 是 | 文章URL名称(路径参数) |
| pageSize | int | 否 | 每页条数(查询参数) |
| 字段 | 类型 | 说明 |
| id | int | 评论ID |
| pid | int | 父评论ID |
| admin | bool | 是否管理员评论 |
| name | string | 评论者名称 |
| contact | string | 联系方式 |
| content | string | 评论内容 |
| ip | string | IP地址 |
| time | int | 时间戳 |
| timeText | string | 人性化时间文本 |
| child | array | 子评论 |
5.2 全部评论列表
POST/api/comment/listall
聚合所有文章的评论,按时间倒序排列。
| 参数 | 类型 | 必填 | 说明 |
| page | int | 否 | 页码,默认1 |
| pageSize | int | 否 | 每页条数,默认20 |
额外字段:
| 字段 | 类型 | 说明 |
| articleId | string | 所属文章ID |
| articleTitle | string | 所属文章标题 |
5.3 删除评论
POST/api/comment/delete
| 参数 | 类型 | 必填 | 说明 |
| articleId | string | 是 | 文章URL名称 |
| commentId | int | 是 | 评论ID |
6. 友情链接模块
6.1 链接列表
GET/api/link/list
| 字段 | 类型 | 说明 |
| name | string | 链接名称 |
| url | string | 链接地址 |
| target | int | 是否新窗口打开(0/1) |
6.2 添加链接
POST/api/link/add
| 参数 | 类型 | 必填 | 说明 |
| name | string | 是 | 链接名称 |
| url | string | 是 | 链接地址 |
| target | int | 否 | 新窗口打开,默认0 |
6.3 编辑链接
POST/api/link/edit
| 参数 | 类型 | 必填 | 说明 |
| index | int | 是 | 链接索引(从列表中获取) |
| name | string | 是 | 链接名称 |
| url | string | 是 | 链接地址 |
| target | int | 否 | 新窗口打开 |
6.4 批量保存链接
POST/api/link/save
参数为数组格式,用于后台全量保存。
| 参数 | 类型 | 必填 | 说明 |
| name | array | 是 | 名称数组 |
| url | array | 是 | 地址数组 |
| target | array | 否 | 新窗口打开的索引数组 |
6.5 删除链接
POST/api/link/delete
7. 导航菜单模块
7.1 导航列表
GET/api/navbar/list
| 字段 | 类型 | 说明 |
| name | string | 导航名称 |
| url | string | 导航地址 |
| target | int | 是否新窗口打开(0/1) |
| child | array | 子导航 |
7.2 添加导航
POST/api/navbar/add
| 参数 | 类型 | 必填 | 说明 |
| name | string | 是 | 导航名称 |
| url | string | 是 | 导航地址 |
| target | int | 否 | 新窗口打开,默认0 |
7.3 编辑导航
POST/api/navbar/edit
| 参数 | 类型 | 必填 | 说明 |
| index | int | 是 | 导航索引 |
| name | string | 是 | 导航名称 |
| url | string | 是 | 导航地址 |
| target | int | 否 | 新窗口打开 |
7.4 批量保存导航
POST/api/navbar/save
参数格式同链接批量保存。
7.5 删除导航
POST/api/navbar/delete
8. 设置模块
8.1 获取设置
GET/api/setting/get
| 字段 | 类型 | 说明 |
| title | string | 网站标题 |
| name | string | 网站名称 |
| intro | string | 网站描述 |
| mood | string | 心情记录 |
| key | string | SEO关键字 |
| desc | string | SEO描述 |
| brief | int | 摘要字数限制 |
| avatar | string | 头像完整URL |
| username | string | 管理员昵称 |
| compile | bool | 自动编译 |
| debug | int | 调试模式(0/1/2) |
| rewrite | bool | 伪静态 |
| articlePaging | int | 每页文章数 |
| commentRestrict | int | 每日评论限制 |
| commentPaging | int | 每页评论数 |
| vcodeOpen | bool | 验证码开关 |
| thumbOpen | bool | 缩略图开关 |
| thumbWidth | int | 缩略图宽度 |
| thumbHeight | int | 缩略图高度 |
| thumbType | int | 缩略方式(1=裁剪,2=等比) |
| icp | string | ICP备案号 |
| prn | string | 公安备案号 |
| views | int | 总浏览量 |
| sensitive | string | 敏感词(空格分隔) |
| blacklist | string | IP黑名单(空格分隔) |
| js | string | 自定义JS代码 |
8.2 保存设置
POST/api/setting/save
只需传递需要修改的字段,未传递的字段保持原值。
| 参数 | 类型 | 必填 | 说明 |
| title | string | 否 | 网站标题 |
| name | string | 否 | 网站名称 |
| intro | string | 否 | 网站描述 |
| mood | string | 否 | 心情记录 |
| key | string | 否 | SEO关键字 |
| desc | string | 否 | SEO描述 |
| brief | int | 否 | 摘要字数 |
| avatar | string | 否 | 头像(完整URL会自动转相对路径) |
| username | string | 否 | 管理员昵称 |
| password | string | 否 | 新密码(MD5加密存储) |
| compile | bool | 否 | 自动编译 |
| debug | int | 否 | 调试模式 |
| rewrite | bool | 否 | 伪静态 |
| articlePaging | int | 否 | 每页文章数 |
| commentRestrict | int | 否 | 每日评论限制 |
| commentPaging | int | 否 | 每页评论数 |
| vcodeOpen | bool | 否 | 验证码开关 |
| thumbOpen | bool | 否 | 缩略图开关 |
| thumbWidth | int | 否 | 缩略图宽度 |
| thumbHeight | int | 否 | 缩略图高度 |
| thumbType | int | 否 | 缩略方式 |
| icp | string | 否 | ICP备案号 |
| prn | string | 否 | 公安备案号 |
| views | int | 否 | 浏览量(不传则不修改) |
| sensitive | string | 否 | 敏感词 |
| blacklist | string | 否 | IP黑名单 |
| js | string | 否 | 自定义JS |
重要:views 字段仅在明确传值时才会更新,不传或传空不会覆盖现有值。password 仅在传入非空值时更新。
9. 文件上传模块
9.1 上传文件
POST/api/upload/upload
请求格式:multipart/form-data
允许的文件类型:jpg, jpeg, png, gif, bmp, webp, mp4, mp3, pdf, doc, docx, xls, xlsx, zip, rar, txt, md
| 字段 | 类型 | 说明 |
| uploadName | string | 原始文件名 |
| newName | string | 新文件名 |
| ext | string | 文件扩展名 |
| url | string | 文件完整URL |
| path | string | 相对存储路径 |
| size | int | 文件大小(字节) |
| thumb | string | 缩略图路径(仅图片且开启缩略图时) |
请求示例(JavaScript)
const formData = new FormData();
formData.append('file', fileInput.files[0]);
fetch('https://blog.com/api/upload/upload', {
method: 'POST',
headers: { 'X-API-Key': 'your_api_key' },
body: formData
})
注意:上传文件时不要设置 Content-Type 请求头,浏览器会自动设置 multipart/form-data 及 boundary。
10. 主题模块
10.1 主题列表
GET/api/theme/list
| 字段 | 类型 | 说明 |
| list | array | 主题列表 |
| list[].id | string | 主题ID |
| list[].name | string | 主题名称 |
| list[].intro | string | 主题介绍 |
| list[].version | string | 主题版本 |
| list[].icon | string | 主题图标完整URL |
| list[].setting | bool | 是否有设置项 |
| list[].isActive | bool | 是否当前使用 |
| count | int | 主题总数 |
| current | string | 当前主题ID |
10.2 启用主题
POST/api/theme/install
10.3 删除主题
POST/api/theme/delete
| 参数 | 类型 | 必填 | 说明 |
| id | string | 是 | 主题ID(不能删除当前使用的主题) |
10.4 获取主题设置页
POST/api/theme/setting
| 字段 | 类型 | 说明 |
| html | string | 主题设置页HTML内容 |
10.5 保存主题设置
POST/api/theme/saveSetting
| 参数 | 类型 | 必填 | 说明 |
| id | string | 是 | 主题ID |
| data | any | 是 | 设置数据 |
11. 扩展模块
11.1 扩展列表
GET/api/ext/list
| 字段 | 类型 | 说明 |
| installList | array | 已安装扩展列表 |
| installList[].id | string | 扩展ID |
| installList[].name | string | 扩展名称 |
| installList[].intro | string | 扩展介绍 |
| installList[].version | string | 扩展版本 |
| installList[].icon | string | 扩展图标完整URL |
| installList[].setting | bool | 是否有设置项 |
| installList[].installed | bool | 是否已安装(true) |
| notInstallList | array | 未安装扩展列表(字段同上,installed=false) |
| installCount | int | 已安装数量 |
| count | int | 扩展总数 |
11.2 安装扩展
POST/api/ext/install
11.3 卸载扩展
POST/api/ext/uninstall
| 参数 | 类型 | 必填 | 说明 |
| id | string | 是 | 扩展ID(不能卸载 api 扩展) |
11.4 删除扩展
POST/api/ext/delete
| 参数 | 类型 | 必填 | 说明 |
| id | string | 是 | 扩展ID(需先卸载才能删除) |
11.5 获取扩展设置页
POST/api/ext/setting
| 字段 | 类型 | 说明 |
| html | string | 扩展设置页HTML内容 |
12. 日志模块
12.1 日志统计
GET/api/log/stats
| 字段 | 类型 | 说明 |
| total | int | 日志总数 |
| today | int | 今日请求数 |
| fail | int | 失败请求数 |
| authFail | int | 认证失败数 |
| topIps | object | TOP10 IP 及请求数 |
| topModules | object | TOP10 模块及请求数 |
12.2 获取日志开关
GET/api/log/setting
| 字段 | 类型 | 说明 |
| logEnabled | int | 日志开关(1=开启,0=关闭) |
12.3 日志列表
POST/api/log/list
| 参数 | 类型 | 必填 | 说明 |
| page | int | 否 | 页码,默认1 |
| pageSize | int | 否 | 每页条数,默认20,最大100 |
| status | string | 否 | 筛选状态:success/fail/auth_fail |
| module | string | 否 | 筛选模块名 |
| ip | string | 否 | 筛选IP(模糊匹配) |
| startDate | string | 否 | 开始日期(YYYY-MM-DD) |
| endDate | string | 否 | 结束日期(YYYY-MM-DD) |
| 字段 | 类型 | 说明 |
| time | string | 请求时间 |
| ip | string | 请求IP |
| module | string | 模块名 |
| action | string | 操作名 |
| method | string | HTTP方法 |
| status | string | 状态 |
| ua | string | User-Agent |
| extra | string | 附加信息 |
12.4 清空日志
POST/api/log/clear
无参数。
12.5 修改日志开关
POST/api/log/setting
| 参数 | 类型 | 必填 | 说明 |
| logEnabled | int | 是 | 1=开启,0=关闭 |
API 权限管理(api-auth 插件)
概述
api-auth 插件为 API 提供多 Key 权限分配功能。安装后,除原始 API Key(超级管理员)外,可创建多个子 Key,每个 Key 拥有独立的权限配置。
认证优先级
- 原始 API Key → 超级管理员,拥有全部权限
- api-auth 子 Key → 根据权限配置校验
- 无匹配 Key → 401 错误
权限格式
| 格式 | 说明 |
* | 全部权限 |
module:* | 指定模块的全部权限 |
module:read | 模块读取权限(GET 请求) |
module:write | 模块写入权限(POST 创建/编辑) |
module:delete | 模块删除权限 |
权限模块列表
| 模块 | 可用权限 |
| article | read, write, delete |
| category | read, write, delete |
| tag | read |
| comment | read, delete |
| link | read, write, delete |
| navbar | read, write, delete |
| setting | read, write |
| upload | write |
| theme | read, write, delete |
| ext | read, write, delete |
| log | read, write |
| system | read, write |
权限示例
{
"permissions": [
"article:read",
"article:write",
"category:read",
"tag:read"
]
}
此配置允许查看和编辑文章、查看分类和标签,但不能删除文章或修改其他模块。
开发指南
伪静态自适应
开发客户端时,应先调用 api/check 接口获取 rewrite 字段,据此构建后续请求的 URL:
// 检测伪静态并构建基础URL
const checkRes = await fetch(blogUrl + '/?api/check');
const checkData = await checkRes.json();
const rewrite = checkData.data.rewrite;
const baseUrl = rewrite
? blogUrl + '/api/'
: blogUrl + '/?api/';
// 后续请求
fetch(baseUrl + 'article/list/1', {
headers: { 'X-API-Key': apiKey }
});
图片路径处理
- 读取:API 返回的图片路径(
img、avatar、内容中的 src)均为完整 URL
- 写入:提交内容时,完整 URL 会自动转换为相对路径存储
- 上传:上传接口返回的
url 为完整 URL,path 为相对路径
内容编辑注意事项
- 文章
content 是解析后的 HTML,rawContent 是原始内容
- 编辑文章时应使用
rawContent 作为编辑器初始内容
- 保存时提交
content 字段即可
错误处理建议
async function apiRequest(url, options = {}) {
const res = await fetch(url, {
...options,
headers: {
'X-API-Key': apiKey,
'Content-Type': 'application/x-www-form-urlencoded',
...options.headers
}
});
const data = await res.json();
if (data.error) {
switch (data.code) {
case 401: // API Key 无效,跳转配置页
break;
case 403: // 权限不足
break;
case 404: // 接口不存在
break;
default: // 其他错误
break;
}
throw new Error(data.message);
}
return data;
}
CORS 支持
API 已内置 CORS 支持,响应头包含:
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: X-API-Key, Authorization, Content-Type
Access-Control-Allow-Methods: GET, POST, OPTIONS
OPTIONS 预检请求会自动返回 200,无需额外处理。
常见问题
Q:API Key 在哪里获取?
A:在 PHLI 后台 → 扩展管理 → API接口 → 设置 中查看,支持重新生成。
Q:如何判断伪静态是否开启?
A:调用 GET /api/check 接口,返回的 rewrite 字段为 true 表示开启。
Q:文章的 id 是什么?
A:文章 ID 是 URL 名称(如 my-first-post),不是数字自增ID。创建时可自定义,留空则自动生成 T + 时间戳格式。
Q:分类的 id 是什么?
A:分类 ID 是分类别称(如 php、tech),是字符串类型,不是数字。
Q:上传文件时为什么返回 400?
A:请确保使用 multipart/form-data 格式提交,且不要手动设置 Content-Type 请求头。文件字段名必须为 file。
Q:保存设置后浏览量变成 0 了?
A:views 字段仅在明确传值时更新。保存设置时,如果不需修改浏览量,请不要传递 views 参数。
Q:如何实现多用户权限?
A:安装 api-auth 扩展插件,在后台创建多个 API Key 并分配不同权限。