Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

gRPC API 参考

RustBill 使用 gRPC 作为后端 API 协议,同时支持 gRPC-Web(浏览器端)。共 10 个 gRPC 服务,分布在 6 个 proto package 中。

通用约定

约定说明
金额所有金额字段使用字符串类型(如 "50.00"),不使用浮点数
ID所有 ID 均为 UUID v7 字符串
时间戳RFC3339 格式字符串(如 "2024-01-15T10:30:00Z"
分页PageRequest { page: uint32, page_size: uint32 },响应含 PageMeta
可选字段proto3 optional 关键字标记的字段,未设置时不出现在 JSON payload 中

认证机制

RustBill 使用三种认证方式,分别对应不同的用户类型和中间件:

认证方式用户类型Token 格式中间件
Session CookieAdminhttpOnly CookieSessionAuthLayer
JWT BearerCustomerAuthorization: Bearer <token>JwtAuthLayer
API Key BearerDownstreamAuthorization: Bearer <api_key>ApiKeyAuthLayer

Admin 认证流程:登录 → 服务端返回 Set-Cookie → 浏览器自动携带 cookie。Admin SPA 使用 credentials: 'include' 发送同源请求。

Customer 认证流程:登录 → 返回 access_token + refresh_token → 前端存储到 localStorage → 每次请求在 Authorization 头携带。

API Key 认证流程:在 ApiKeyService 创建 → 在 Authorization 头中作为 Bearer token 携带 → 注入 DownstreamUser

gRPC 服务路径

所有 gRPC 方法遵循路径格式:

/rustbill.{package}.{Service}/{Method}

例如:

  • /rustbill.identity.IdentityService/Login
  • /rustbill.product.ProductService/ListProducts
  • /rustbill.integration.IntegrationService/ListPlugins

gRPC-Web

浏览器端使用 gRPC-Web 协议。请求需携带 grpc-web 头,响应可能为 trailers-only(状态码 200 + grpc-status header)。CORS 需服务端配置允许的 origin。

权限级别

权限级别说明
公开无需认证
已认证admin 或 customer 任一认证通过即可
admin需 admin 登录(Admin 或 Operator 角色)
admin-only仅 Admin 角色(不含 Operator)
customercustomer 用户自动限定数据范围

服务清单

服务Package文件职责
IdentityServicerustbill.identityidentity.md认证、用户管理、密码管理
ProductServicerustbill.productproduct.md商品 CRUD、批量操作、上游导入
CustomerServicerustbill.customercustomer.md客户 CRUD
OrderServicerustbill.orderorder.md订单全生命周期、支付
BillingServicerustbill.billingbilling.md发票管理、周期账单生成
PaymentServicerustbill.paymentpayment.md支付创建、回调、退款
IntegrationServicerustbill.integrationintegration.md插件管理、供应商操作、上游实例
NotificationServicerustbill.notificationnotification.md通知渠道、手动发送
TicketServicerustbill.ticketticket.md工单全生命周期管理
ApiKeyServicerustbill.downstreamapikey.mdAPI Key 创建与管理

公共类型

定义在 rustbill.common package(common.proto)。

PageRequest

字段类型编号描述
pageuint321页码(从 1 开始)
page_sizeuint322每页条数

PageMeta

字段类型编号描述
totaluint641总记录数
pageuint322当前页码
page_sizeuint323每页条数
total_pagesuint324总页数

EmptyRequest / EmptyResponse

无字段的空消息。

IdRequest

字段类型编号描述
idstring1UUID v7 格式 ID

错误码

所有 gRPC 错误通过标准 grpc-status header 返回:

错误码gRPC Status说明
InvalidArgument3参数校验失败
NotFound5资源不存在
AlreadyExists6资源已存在(重复创建)
PermissionDenied7权限不足
Unauthenticated16未认证(需登录)
Internal13服务器内部错误
Unimplemented12功能未实现
Unavailable14服务不可用