{
  "tags": [
    "用户流量包"
  ],
  "description": "购买一个流量套餐并创建用户流量套餐记录。普通用户只能为自己购买已启用的流量套餐，系统会校验绑定基础套餐、优惠码和账户余额，并按流量套餐有效天数设置到期时间；管理员可为指定用户分配套餐并覆盖名称、适用套餐、流量总量、到期时间及启用状态。",
  "operationId": "user_post_v1_user_traffic_packages",
  "security": [
    {
      "AccessToken": []
    },
    {
      "ApiKey": [],
      "ApiSecret": []
    }
  ],
  "responses": {
    "200": {
      "description": "请求已被处理。成功时 code 为 0、msg 为“用户流量包添加成功”、data 为新建用户流量套餐 ID；流量套餐不可用、绑定套餐不满足、优惠码无效或余额不足时仍返回 HTTP 200。",
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "required": [
              "code",
              "msg",
              "data"
            ],
            "properties": {
              "code": {
                "type": "integer",
                "description": "业务状态码；成功为 0。",
                "example": 0
              },
              "msg": {
                "type": "string",
                "description": "处理结果说明。",
                "example": "用户流量包添加成功"
              },
              "data": {
                "type": "integer",
                "description": "成功时的新建用户流量套餐 ID；失败时为空字符串。",
                "example": 21
              }
            }
          },
          "examples": {
            "success": {
              "summary": "购买成功",
              "value": {
                "code": 0,
                "msg": "用户流量包添加成功",
                "data": 21
              }
            },
            "noBoundPackage": {
              "summary": "未购买适用基础套餐",
              "value": {
                "code": "user_traffic_package-93",
                "msg": "您未购买有该流量包适用的套餐，无法购买",
                "data": ""
              }
            },
            "insufficientBalance": {
              "summary": "余额不足",
              "value": {
                "code": "user_traffic_package-86",
                "msg": "余额不足",
                "data": ""
              }
            }
          },
          "example": {
            "code": 0,
            "msg": "用户流量包添加成功",
            "data": 21
          }
        }
      }
    }
  },
  "x-python-handler": "package.UserTrafficPackageAPI",
  "x-python-route-line": 471,
  "x-audience": "user",
  "x-auth": "user",
  "x-reviewed": true,
  "requestBody": {
    "required": true,
    "content": {
      "application/json": {
        "schema": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "traffic_package_id"
          ],
          "properties": {
            "traffic_package_id": {
              "type": "integer",
              "minimum": 1,
              "description": "必填。要购买或分配的流量套餐定义 ID，必须存在且已启用。",
              "example": 3
            },
            "coupon_code": {
              "type": "string",
              "description": "可选。购买时使用的优惠码；系统会验证该优惠码是否适用于此流量套餐。",
              "example": "TRAFFIC10"
            },
            "uid": {
              "type": "integer",
              "minimum": 1,
              "description": "管理员专用。指定流量套餐归属的用户 ID，必须存在；未传时归属当前用户。",
              "example": 12
            },
            "name": {
              "type": "string",
              "minLength": 1,
              "description": "管理员专用。用户流量套餐显示名称；未传时使用流量套餐定义名称，内容会进行 XSS 过滤。",
              "example": "客户专属 100 GB 加油包"
            },
            "bind_package": {
              "type": "string",
              "description": "管理员专用。适用的基础套餐定义 ID，以英文逗号分隔；每个 ID 必须存在。空字符串表示不限制基础套餐。",
              "example": "2,3"
            },
            "amount": {
              "type": "string",
              "pattern": "^\\d+(GB|TB|PB)$",
              "description": "管理员专用。流量总量，必须为数字加 GB、TB 或 PB，例如 50GB。",
              "example": "100GB"
            },
            "expire": {
              "type": "string",
              "pattern": "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$",
              "description": "管理员专用。到期时间，格式 YYYY-MM-DD HH:mm:ss；未传时由流量套餐定义的有效天数计算。",
              "example": "2026-12-31 23:59:59"
            },
            "enable": {
              "type": "integer",
              "enum": [
                0,
                1
              ],
              "description": "管理员专用。启用状态；未传时为 1。设为 0 时必须同时传入 reason。",
              "example": 1
            },
            "reason": {
              "type": "string",
              "minLength": 1,
              "description": "管理员专用。当 enable 为 0 时必填，记录禁用原因。",
              "example": "人工锁定"
            }
          },
          "example": {
            "traffic_package_id": 3,
            "coupon_code": "TRAFFIC10"
          }
        }
      }
    }
  },
  "parameters": [],
  "x-itecdn-source-reviewed": true,
  "extensions": [
    {
      "key": "x-python-handler",
      "value": "package.UserTrafficPackageAPI"
    },
    {
      "key": "x-python-route-line",
      "value": 471
    },
    {
      "key": "x-audience",
      "value": "user"
    },
    {
      "key": "x-auth",
      "value": "user"
    },
    {
      "key": "x-reviewed",
      "value": true
    },
    {
      "key": "x-itecdn-source-reviewed",
      "value": true
    }
  ],
  "method": "post",
  "path": "/v1/user-traffic-packages",
  "servers": [
    {
      "url": "https://master.example.invalid",
      "description": "不可调用的示例地址；复制示例后替换为自己的主控域名。"
    }
  ],
  "securitySchemes": {
    "AccessToken": {
      "type": "apiKey",
      "in": "header",
      "name": "access-token",
      "description": "通过登录接口获得的短期访问令牌。"
    },
    "ApiKey": {
      "type": "apiKey",
      "in": "header",
      "name": "api-key",
      "description": "API Key；必须和 api-secret 同时发送。"
    },
    "ApiSecret": {
      "type": "apiKey",
      "in": "header",
      "name": "api-secret",
      "description": "API Secret；必须和 api-key 同时发送。"
    }
  },
  "jsonRequestBodyExample": {
    "traffic_package_id": 3,
    "coupon_code": "TRAFFIC10"
  },
  "info": {
    "title": "itecdn v5 用户 API",
    "version": "5.x",
    "description": "itecdn v5.x Python 主控用户 API。本文档不适用于 v6 或其它实现。"
  },
  "postman": {
    "name": "购买或分配用户流量套餐",
    "description": {
      "content": "购买一个流量套餐并创建用户流量套餐记录。普通用户只能为自己购买已启用的流量套餐，系统会校验绑定基础套餐、优惠码和账户余额，并按流量套餐有效天数设置到期时间；管理员可为指定用户分配套餐并覆盖名称、适用套餐、流量总量、到期时间及启用状态。",
      "type": "text/plain"
    },
    "url": {
      "path": [
        "v1",
        "user-traffic-packages"
      ],
      "host": [
        "{{baseUrl}}"
      ],
      "query": [],
      "variable": []
    },
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "method": "POST",
    "body": {
      "mode": "raw",
      "raw": "",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "auth": {
      "type": "apikey",
      "apikey": [
        {
          "type": "any",
          "value": "access-token",
          "key": "key"
        },
        {
          "type": "any",
          "value": "{{apiKey}}",
          "key": "value"
        },
        {
          "type": "any",
          "value": "header",
          "key": "in"
        }
      ]
    }
  }
}