API 接口文档

概述

本文档描述了短链接服务的所有 API 接口。

Base URL: https://your-domain.com

API 版本: v1

认证方式

系统支持两种认证方式:

  1. 签名认证(推荐):基于 HMAC-SHA256 的安全认证,详见 签名认证文档
  2. Bearer Token 认证:传统 Token 认证,详见 Bearer Token 文档

通用响应格式

成功响应

{
    "code": 0,
    "message": "success",
    "data": { ... }
}

错误响应

{
    "code": 40001,
    "message": "错误描述"
}

错误码说明

错误码说明
0成功
40001请求参数错误
40101未授权
40301禁止访问
40401资源不存在
40901资源冲突
50001服务器内部错误

认证接口

用户登录

登录获取访问令牌(用于 Web 管理后台)。

请求

POST /api/v1/auth/login

请求体

{
    "username": "admin",
    "password": "password123"
}

响应

{
    "code": 0,
    "message": "success",
    "data": {
        "token": "user_1_abc123...",
        "expires_at": "2024-12-31T23:59:59Z",
        "user": {
            "id": 1,
            "username": "admin",
            "real_name": "管理员",
            "email": "admin@example.com",
            "status": 1
        }
    }
}

用户登出

请求

POST /api/v1/auth/logout

响应

{
    "code": 0,
    "message": "登出成功"
}

Token 管理接口

创建 Token

创建 API 访问 Token,支持签名认证和 Bearer Token 两种类型。

请求

POST /api/v1/tokens

请求体

{
    "token_name": "API Token",
    "token_type": "signature",
    "expire_at": "2025-12-31T23:59:59Z"
}
参数类型必填说明
token_namestringToken 名称,最大 100 字符
token_typestringToken 类型:signature(默认)或 bearer
expire_atstring过期时间,ISO 8601 格式,不填则永不过期

响应 - 签名认证类型

{
    "code": 0,
    "message": "success",
    "data": {
        "id": 1,
        "token_name": "API Token",
        "token_type": "signature",
        "app_id": "app_1a2b3c4d5e6f7890",
        "app_secret": "a1b2c3d4e5f6789012345678901234567890123456789012345678901234",
        "expire_at": "2025-12-31T23:59:59Z",
        "created_at": "2024-01-01T00:00:00Z"
    }
}

响应 - Bearer Token 类型

{
    "code": 0,
    "message": "success",
    "data": {
        "id": 2,
        "token_name": "Bearer Token",
        "token_type": "bearer",
        "token": "a1b2c3d4e5f6789012345678901234567890123456789012345678901234",
        "expire_at": "2025-12-31T23:59:59Z",
        "created_at": "2024-01-01T00:00:00Z"
    }
}

⚠️ 重要app_secrettoken 仅在创建时返回一次,请妥善保存!

获取 Token 列表

请求

GET /api/v1/tokens

查询参数

参数类型必填说明
pageint页码,默认 1
page_sizeint每页数量,默认 10,最大 100
token_namestringToken 名称(模糊搜索)
statusint状态:1-启用,0-禁用

响应

{
    "code": 0,
    "message": "success",
    "data": {
        "list": [
            {
                "id": 1,
                "token_name": "API Token",
                "token_type": "signature",
                "app_id": "app_1a2b3c4d5e6f7890",
                "last_used_at": "2024-01-15T10:30:00Z",
                "expire_at": "2025-12-31T23:59:59Z",
                "status": 1,
                "created_at": "2024-01-01T00:00:00Z"
            },
            {
                "id": 2,
                "token_name": "Bearer Token",
                "token_type": "bearer",
                "token": "a1b2c3d4...",
                "last_used_at": null,
                "expire_at": null,
                "status": 1,
                "created_at": "2024-01-02T00:00:00Z"
            }
        ],
        "pagination": {
            "total": 2,
            "page": 1,
            "size": 10
        }
    }
}

删除 Token

请求

DELETE /api/v1/tokens/:token_id

响应

{
    "code": 0,
    "message": "Token删除成功"
}

短链接接口

创建短链接

请求

POST /api/v1/short_links

请求体

{
    "original_url": "https://www.example.com/very/long/url",
    "domain": "https://short.ly",
    "custom_code": "mylink",
    "title": "示例网站",
    "description": "这是一个示例网站",
    "expire_at": "2025-12-31T23:59:59Z"
}
参数类型必填说明
original_urlstring原始 URL
domainstring短链接域名,不填使用默认域名
custom_codestring自定义短码
titlestring标题
descriptionstring描述
expire_atstring过期时间

响应

{
    "code": 0,
    "message": "success",
    "data": {
        "id": 1,
        "short_code": "abc123",
        "domain": "https://short.ly",
        "short_url": "https://short.ly/abc123",
        "original_url": "https://www.example.com/very/long/url",
        "title": "示例网站",
        "description": "这是一个示例网站",
        "expire_at": "2025-12-31T23:59:59Z",
        "is_active": true,
        "click_count": 0,
        "created_at": "2024-01-01T00:00:00Z",
        "updated_at": "2024-01-01T00:00:00Z"
    }
}

批量创建短链接

请求

POST /api/v1/short_links/batch

请求体

{
    "urls": [
        "https://www.example1.com",
        "https://www.example2.com",
        "https://www.example3.com"
    ],
    "domain": "https://short.ly"
}
参数类型必填说明
urlsarrayURL 列表,最多 100 个
domainstring短链接域名

响应

{
    "code": 0,
    "message": "success",
    "data": {
        "success": [
            {
                "id": 1,
                "short_code": "abc123",
                "short_url": "https://short.ly/abc123",
                "original_url": "https://www.example1.com"
            }
        ],
        "failed": [
            {
                "url": "invalid-url",
                "error": "无效的 URL 格式"
            }
        ]
    }
}

获取短链接列表

请求

GET /api/v1/short_links

查询参数

参数类型必填说明
pageint页码,默认 1
page_sizeint每页数量,默认 10,最大 100
domainstring域名筛选
keywordstring关键词搜索(搜索短码、标题、原始 URL)

响应

{
    "code": 0,
    "message": "success",
    "data": {
        "list": [...],
        "total": 100,
        "page": 1,
        "size": 10
    }
}

获取短链接详情

请求

GET /api/v1/short_links/:id

响应

{
    "code": 0,
    "message": "success",
    "data": {
        "id": 1,
        "short_code": "abc123",
        "domain": "https://short.ly",
        "short_url": "https://short.ly/abc123",
        "original_url": "https://www.example.com",
        "title": "示例网站",
        "description": "这是一个示例网站",
        "expire_at": "2025-12-31T23:59:59Z",
        "is_active": true,
        "click_count": 1234,
        "created_at": "2024-01-01T00:00:00Z",
        "updated_at": "2024-01-01T00:00:00Z"
    }
}

更新短链接

请求

PUT /api/v1/short_links/:id

请求体

{
    "original_url": "https://www.new-example.com",
    "title": "新标题",
    "description": "新描述",
    "expire_at": "2026-12-31T23:59:59Z",
    "is_active": true
}

删除短链接

请求

DELETE /api/v1/short_links/:id

响应

{
    "code": 0,
    "message": "删除成功"
}

获取短链接统计

请求

GET /api/v1/short_links/:id/statistics

查询参数

参数类型必填说明
daysint统计天数,默认 7,最大 365

响应

{
    "code": 0,
    "message": "success",
    "data": {
        "total_clicks": 12345,
        "today_clicks": 100,
        "week_clicks": 500,
        "month_clicks": 2000,
        "daily_statistics": [
            {"date": "2024-01-15", "click_count": 100},
            {"date": "2024-01-14", "click_count": 95},
            {"date": "2024-01-13", "click_count": 110}
        ]
    }
}

域名管理接口

创建域名

请求

POST /api/v1/domains

请求体

{
    "domain": "short.ly",
    "protocol": "https",
    "site_name": "短链接服务",
    "icp_number": "京ICP备12345678号",
    "police_number": "京公网安备12345678号",
    "is_active": true,
    "pass_query_params": false,
    "random_suffix_length": 2,
    "enable_checksum": true,
    "description": "主要短链域名"
}
参数类型必填说明
domainstring域名
protocolstring协议:httphttps
site_namestring网站名称
icp_numberstringICP 备案号
police_numberstring公安备案号
is_activebool是否启用
pass_query_paramsbool是否透传查询参数
random_suffix_lengthint随机后缀长度(0-10)
enable_checksumbool是否启用校验位
descriptionstring描述

获取域名列表

请求

GET /api/v1/domains

获取活跃域名列表

请求

GET /api/v1/domains/active

更新域名

请求

PUT /api/v1/domains/:id

更新域名状态

请求

PUT /api/v1/domains/:id/status

请求体

{
    "is_active": true
}

删除域名

请求

DELETE /api/v1/domains/:id

用户管理接口

创建用户

请求

POST /api/v1/users

请求体

{
    "username": "newuser",
    "password": "password123",
    "real_name": "张三",
    "email": "zhangsan@example.com",
    "phone": "13800138000"
}

获取用户列表

请求

GET /api/v1/users

查询参数

参数类型必填说明
pageint页码
page_sizeint每页数量
usernamestring用户名(模糊搜索)
real_namestring真实姓名(模糊搜索)
statusint状态

获取用户详情

请求

GET /api/v1/users/:id

更新用户

请求

PUT /api/v1/users/:id

删除用户

请求

DELETE /api/v1/users/:id

重置用户密码

请求

POST /api/v1/users/:id/reset-password

请求体

{
    "new_password": "newpassword123"
}

当前用户接口

获取当前用户信息

请求

GET /api/v1/profile

修改密码

请求

POST /api/v1/profile/change-password

请求体

{
    "old_password": "oldpassword",
    "new_password": "newpassword123"
}

统计接口

获取系统统计

请求

GET /api/v1/statistics/system

获取仪表盘统计

请求

GET /api/v1/statistics/dashboard

获取短链接统计

请求

GET /api/v1/statistics/short-links

点击统计接口

获取点击统计列表

请求

GET /api/v1/click_statistics

获取点击统计分析

请求

GET /api/v1/click_statistics/analysis

操作日志接口

获取操作日志

请求

GET /api/v1/logs

健康检查接口

完整健康检查

请求

GET /health

响应

{
    "status": "healthy",
    "database": "connected",
    "cache": "connected",
    "timestamp": "2024-01-01T00:00:00Z"
}

简单健康检查

请求

GET /health/simple

响应

{
    "status": "ok"
}