Skip to content

API overview

This page mirrors the Chinese documentation for accuracy. Switch language to 简体中文, or open the Chinese version.


  • Base URL: http://localhost:3000/api/admin
  • 认证方式: JWT Bearer Token
  • 请求格式: JSON
  • 响应格式: JSON

routes/admin.go 和 Swagger 产物为准。关键接口:

  • 认证与会话:
    • POST /login
    • GET /login/captcha
    • GET /info
    • POST /logout
    • GET /heartbeat
  • 通知中心:
    • GET /notifications
    • GET /notifications/unread-count
    • GET /notifications/recent
    • POST /notifications/ws-ticket(获取一次性 WS ticket)
    • POST /notifications/{id}/read
    • POST /notifications/read-all
  • WebSocket:
    • GET /ws/admin/notifications(非 /api/admin/* 前缀)
    • 仅支持 ticket 参数进行鉴权,不再支持 token query
  • 日志:
    • GET /operation-logs
    • GET /login-logs
    • GET /system-logs
  • 监控:
    • GET /monitor/system-info
    • GET /monitor/system-info/stream

与上文或路由不一致时,以 routes/admin.go / Swagger 为准。

接口速查表(联调用)

认证与会话

  • POST /login
  • GET /login/captcha
  • GET /info
  • POST /logout
  • GET /heartbeat
  • PUT /profile
  • PUT /password

通知与实时

  • GET /notifications
  • GET /notifications/unread-count
  • GET /notifications/recent
  • POST /notifications/ws-ticket
  • POST /notifications/{id}/read
  • POST /notifications/read-all
  • POST /notifications
  • GET /ws/admin/notifications?ticket={ticket}

日志与监控

  • GET /operation-logs
  • GET /login-logs
  • GET /system-logs
  • GET /dashboard/count
  • GET /dashboard/user-access-source
  • GET /dashboard/weekly-user-activity
  • GET /dashboard/monthly-sales
  • GET /dashboard/recent-activities
  • GET /dashboard/stream
  • GET /monitor/system-info
  • GET /monitor/system-info/stream

用户与订单支付

  • GET /users
  • POST /users/{id}/update-balance
  • PUT /users/{id}/password
  • POST /users/export
  • GET /user-balance-logs
  • POST /user-balance-logs
  • GET /user-balance-logs/statistics
  • GET /orders
  • POST /orders/export
  • POST /orders/import
  • GET /orders/export/status/{id}
  • GET /payments
  • GET /payments/{id}
  • POST /payments/export
  • GET /payments/export/status/{id}

附件与导出

  • GET /attachments
  • POST /attachments/upload
  • POST /attachments/chunkaction=init|upload|merge
  • GET /attachments/chunkaction=progress
  • GET /attachments/{id}/preview
  • GET /attachments/{id}/download
  • PUT /attachments/{id}/display-name
  • DELETE /attachments/{id}
  • POST /attachments/batch-delete
  • GET /exports
  • GET /exports/{id}/download
  • DELETE /exports/{id}
  • POST /exports/batch-delete

通用响应格式

成功响应

json
{
  "code": 200,
  "message": "操作成功",
  "data": {}
}

分页响应

json
{
  "code": 200,
  "message": "操作成功",
  "data": {
    "list": [],
    "page": 1,
    "page_size": 10,
    "total": 100
  }
}

错误响应

json
{
  "code": 400,
  "message": "错误信息",
  "data": null
}

状态码说明

状态码说明
200成功
400请求参数错误
401未认证或 Token 过期
403无权限访问
404资源不存在
422验证失败
500服务器内部错误

认证接口

登录

http
POST /login

请求参数:

参数类型必填说明
usernamestring用户名
passwordstring密码
captcha_idstring验证码ID
captcha_codestring验证码

请求示例:

json
{
  "username": "admin",
  "password": "admin123"
}

响应示例:

json
{
  "code": 200,
  "message": "登录成功",
  "data": {
    "token": "eyJhbGciOiJIUzI1NiIs...",
    "expires_at": "2024-01-01T12:00:00Z"
  }
}

退出登录

http
POST /logout
Authorization: Bearer {token}

响应示例:

json
{
  "code": 200,
  "message": "退出成功",
  "data": null
}

获取验证码

http
GET /login/captcha

响应示例:

json
{
  "code": 200,
  "data": {
    "captcha_id": "abc123",
    "captcha_image": "data:image/png;base64,..."
  }
}

管理员信息

获取当前用户信息

http
GET /info
Authorization: Bearer {token}

响应示例:

json
{
  "code": 200,
  "data": {
    "id": 1,
    "username": "admin",
    "nickname": "超级管理员",
    "email": "admin@example.com",
    "avatar": "",
    "department_id": 1,
    "department": {
      "id": 1,
      "name": "总部"
    },
    "roles": [
      {
        "id": 1,
        "name": "超级管理员",
        "slug": "super-admin"
      }
    ],
    "permissions": ["admin.index", "admin.store", "..."],
    "menus": []
  }
}

修改密码

http
PUT /password
Authorization: Bearer {token}

请求参数:

参数类型必填说明
old_passwordstring原密码
passwordstring新密码
password_confirmationstring确认新密码

管理员管理

获取管理员列表

http
GET /admins
Authorization: Bearer {token}

查询参数:

参数类型说明
pageint页码,默认 1
page_sizeint每页条数,默认 10
usernamestring用户名(模糊搜索)
nicknamestring昵称(模糊搜索)
statusint状态:0-禁用,1-启用
department_idint部门ID
order_bystring排序,如 id:desc

响应示例:

json
{
  "code": 200,
  "data": {
    "list": [
      {
        "id": 1,
        "username": "admin",
        "nickname": "超级管理员",
        "email": "admin@example.com",
        "phone": "",
        "avatar": "",
        "status": 1,
        "department_id": 1,
        "department": {"id": 1, "name": "总部"},
        "roles": [{"id": 1, "name": "超级管理员"}],
        "created_at": "2024-01-01T00:00:00Z",
        "last_login_at": "2024-01-01T12:00:00Z"
      }
    ],
    "page": 1,
    "page_size": 10,
    "total": 1
  }
}

创建管理员

http
POST /admins
Authorization: Bearer {token}

请求参数:

参数类型必填说明
usernamestring用户名(唯一)
passwordstring密码
nicknamestring昵称
emailstring邮箱
phonestring手机号
avatarstring头像URL
department_idint部门ID
role_ids[]int角色ID数组
statusint状态,默认 1

获取管理员详情

http
GET /admins/{id}
Authorization: Bearer {token}

更新管理员

http
PUT /admins/{id}
Authorization: Bearer {token}

删除管理员

http
DELETE /admins/{id}
Authorization: Bearer {token}

重置密码

http
PUT /admins/{id}/password
Authorization: Bearer {token}

请求参数:

参数类型必填说明
passwordstring新密码

角色管理

获取角色列表

http
GET /roles
Authorization: Bearer {token}

查询参数:

参数类型说明
pageint页码
page_sizeint每页条数
namestring角色名称(模糊搜索)
statusint状态

创建角色

http
POST /roles
Authorization: Bearer {token}

请求参数:

参数类型必填说明
namestring角色名称
slugstring角色标识(唯一)
descriptionstring描述
permission_ids[]int权限ID数组
menu_ids[]int菜单ID数组
statusint状态
sortint排序

获取角色详情

http
GET /roles/{id}
Authorization: Bearer {token}

更新角色

http
PUT /roles/{id}
Authorization: Bearer {token}

删除角色

http
DELETE /roles/{id}
Authorization: Bearer {token}

菜单管理

获取菜单列表(树形)

http
GET /menus
Authorization: Bearer {token}

响应示例:

json
{
  "code": 200,
  "data": {
    "list": [
      {
        "id": 1,
        "parent_id": 0,
        "title": "系统管理",
        "slug": "system",
        "icon": "Setting",
        "path": "/system",
        "component": "",
        "type": 1,
        "status": 1,
        "sort": 1,
        "children": [
          {
            "id": 2,
            "parent_id": 1,
            "title": "管理员管理",
            "slug": "admin",
            "path": "/system/admin",
            "component": "admin/AdminList",
            "type": 2,
            "status": 1
          }
        ]
      }
    ]
  }
}

创建菜单

http
POST /menus
Authorization: Bearer {token}

请求参数:

参数类型必填说明
parent_idint父级ID,默认 0
titlestring菜单标题
slugstring菜单标识
iconstring图标名称
pathstring路由路径
componentstring组件路径
typeint类型:1-目录,2-菜单,3-按钮
statusint状态
sortint排序

获取菜单详情

http
GET /menus/{id}
Authorization: Bearer {token}

更新菜单

http
PUT /menus/{id}
Authorization: Bearer {token}

删除菜单

http
DELETE /menus/{id}
Authorization: Bearer {token}

权限管理

获取权限列表

http
GET /permissions
Authorization: Bearer {token}

创建权限

http
POST /permissions
Authorization: Bearer {token}

请求参数:

参数类型必填说明
namestring权限名称
slugstring权限标识(唯一)
methodstringHTTP 方法(GET/POST/PUT/DELETE)
pathstringAPI 路径
menu_idint关联菜单ID
statusint状态

更新权限

http
PUT /permissions/{id}
Authorization: Bearer {token}

删除权限

http
DELETE /permissions/{id}
Authorization: Bearer {token}

部门管理

获取部门列表(树形)

http
GET /departments
Authorization: Bearer {token}

创建部门

http
POST /departments
Authorization: Bearer {token}

请求参数:

参数类型必填说明
namestring部门名称
parent_idint父部门ID
leaderstring负责人
phonestring联系电话
emailstring邮箱
statusint状态
sortint排序

更新部门

http
PUT /departments/{id}
Authorization: Bearer {token}

删除部门

http
DELETE /departments/{id}
Authorization: Bearer {token}

字典管理

获取字典列表

http
GET /dictionaries
Authorization: Bearer {token}

创建字典

http
POST /dictionaries
Authorization: Bearer {token}

请求参数:

参数类型必填说明
parent_idint父级ID(0为字典类型)
namestring名称
codestring编码
valuestring
descriptionstring描述
statusint状态
sortint排序

更新字典

http
PUT /dictionaries/{id}
Authorization: Bearer {token}

删除字典

http
DELETE /dictionaries/{id}
Authorization: Bearer {token}

黑名单管理

获取黑名单列表

http
GET /blacklists
Authorization: Bearer {token}

创建黑名单

http
POST /blacklists
Authorization: Bearer {token}

请求参数:

参数类型必填说明
ipstringIP地址(支持单IP、CIDR、范围)
remarkstring备注
statusint状态

IP 格式示例:

  • 单个 IP: 192.168.1.100
  • CIDR 格式: 192.168.1.0/24
  • IP 范围: 192.168.1.1-192.168.1.100

更新黑名单

http
PUT /blacklists/{id}
Authorization: Bearer {token}

删除黑名单

http
DELETE /blacklists/{id}
Authorization: Bearer {token}

批量删除黑名单

http
DELETE /blacklists/batch
Authorization: Bearer {token}

请求参数:

json
{
  "ids": [1, 2, 3]
}

日志管理

操作日志

http
GET /operation-logs
Authorization: Bearer {token}

查询参数:

参数类型说明
admin_idint管理员ID
modulestring模块
actionstring操作
start_datestring开始日期
end_datestring结束日期

登录日志

http
GET /login-logs
Authorization: Bearer {token}

查询参数:

参数类型说明
admin_idint管理员ID
ipstringIP地址
statusint状态:0-失败,1-成功
start_datestring开始日期
end_datestring结束日期

系统日志

http
GET /system-logs
Authorization: Bearer {token}

查询参数:

参数类型说明
levelstring日志级别(error/warning/info/debug)
trace_idstring追踪ID
start_datestring开始日期
end_datestring结束日期

仪表盘

获取统计数据

http
GET /dashboard/count
Authorization: Bearer {token}

响应示例:

json
{
  "code": 200,
  "data": {
    "admin_count": 10,
    "role_count": 5,
    "menu_count": 30,
    "login_count_today": 15,
    "operation_count_today": 100,
    "recent_logins": [],
    "recent_operations": []
  }
}

在线管理员

获取在线管理员列表

http
GET /online-admins
Authorization: Bearer {token}

强制下线

http
DELETE /online-admins/{id}
Authorization: Bearer {token}

批量强制下线

http
POST /online-admins/batch-kick-out
Authorization: Bearer {token}

请求参数:

json
{
  "token_ids": "1,2,3"
}

服务监控

获取系统信息

http
GET /monitor/system-info
Authorization: Bearer {token}

实时系统信息(SSE)

http
GET /monitor/system-info/stream
Authorization: Bearer {token}

响应示例:

json
{
  "code": 200,
  "data": {
    "cpu": {
      "cores": 8,
      "usage": 25.5
    },
    "memory": {
      "total": 16384,
      "used": 8192,
      "usage": 50.0
    },
    "disk": {
      "total": 512000,
      "used": 256000,
      "usage": 50.0
    },
    "go": {
      "version": "go1.21",
      "goroutines": 50,
      "gc_pause": "1.2ms"
    }
  }
}

文件上传

上传文件

http
POST /attachments/upload
Authorization: Bearer {token}
Content-Type: multipart/form-data

请求参数:

参数类型必填说明
filefile文件
typestring类型(image/document/video)

分片上传

http
POST /attachments/chunk
Authorization: Bearer {token}
Content-Type: multipart/form-data

说明:当前分片上传采用统一接口,通过 action 参数区分:

  • action=init:初始化
  • action=upload:上传分片
  • action=merge:合并分片
  • action=progress:查询进度(通常 GET)

请求参数:

参数类型必填说明
filefile分片文件
chunk_idstring分片标识
chunk_indexint分片索引
total_chunksint总分片数
filenamestring原始文件名

合并分片

http
POST /attachments/chunk
Authorization: Bearer {token}
Content-Type: multipart/form-data

请求参数:

json
{
  "chunk_id": "abc123",
  "filename": "large-file.zip",
  "total_chunks": 10
}

通知中心

获取 WebSocket Ticket

http
POST /notifications/ws-ticket
Authorization: Bearer {token}

响应示例:

json
{
  "code": 200,
  "message": "success",
  "data": {
    "ticket": "01jvxxxxxxxxxxxxxxxxxxxxxx",
    "expires_in": 60
  }
}

WebSocket 连接

ws://localhost:3000/ws/admin/notifications?ticket={ticket}

消息格式:

json
{
  "type": "notification",
  "data": {
    "id": 1,
    "title": "新通知",
    "content": "通知内容",
    "created_at": "2024-01-01T12:00:00Z"
  }
}

获取通知列表

http
GET /notifications
Authorization: Bearer {token}

标记已读

http
POST /notifications/{id}/read
Authorization: Bearer {token}

全部标记已读

http
POST /notifications/read-all
Authorization: Bearer {token}

错误码说明

错误码定义在 app/errors/codes.go,格式为 XXYYYY

  • XX: 模块 (10-认证, 20-权限, 30-验证, 40-业务, 50-系统)
  • YYYY: 具体错误

认证模块 (10xxx)

错误码说明
10001用户名或密码错误
10002账号已被禁用
10003Token 已过期
10004Token 已被撤销
10005验证码错误
10006登录尝试次数超限
10007Token 无效

权限模块 (20xxx)

错误码说明
20001无权限访问
20002资源不存在
20003访问被拒绝

验证模块 (30xxx)

错误码说明
30001参数验证失败
30002数据已存在
30003数据不存在
30004格式无效

业务模块 (40xxx)

错误码说明
40001操作失败
40002删除失败
40003更新失败
40004创建失败
40005上传失败
40006导出失败

系统模块 (50xxx)

错误码说明
50001服务器内部错误
50002数据库错误
50003缓存错误
50004队列错误
50005第三方服务错误

附录

认证头格式

http
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...

分页参数

所有列表接口支持以下分页参数:

参数类型默认值说明
pageint1页码
page_sizeint10每页条数(最大 100)
order_bystringid:desc排序(字段:方向)

时间格式

所有时间字段使用 ISO 8601 格式:2024-01-01T12:00:00Z

时区请求头(响应时间字段转换)

  • 后端审计时间(如 created_at / updated_at)以统一基准存储与返回。
  • 仅当请求显式携带时区信息时,后端才会对响应中的时间字段做展示层转换。
  • 支持以下时区输入(优先级从高到低):timezone 参数、X-Timezone 请求头、Timezone 请求头。
  • 未携带时区时,不执行响应时间字段转换,返回后端原值。
  • 默认转换字段白名单:created_atupdated_atdeleted_at(兼容驼峰命名)。
  • 可通过配置 app.response_time_fields(逗号分隔)扩展需要转换的字段。

多语言支持

通过请求头指定语言:

http
Accept-Language: zh-CN

支持的语言:

  • zh-CN - 简体中文
  • en-US - English