{
  "openapi": "3.0.3",
  "info": {
    "title": "GcaChat API",
    "version": "1.1.0",
    "description": "# GcaChat API\n\n## 一、两种身份\n\n| 身份 | 用来做什么 | 怎么拿到 token | 请求头 |\n| --- | --- | --- | --- |\n| **用户账号** | 代表某个真实用户（聊天、橙币、商城、个人主页等） | 用户名 + 密码登录 | `Authorization: Bearer <用户 token>` |\n| **应用（机器人 / 小程序）** | 代表一个应用，以机器人身份收发消息 | 开发者中心创建应用 | `Authorization: Bearer gca_xxx` |\n\n两种 token 不能混用：用户 token 调不了 `/api/v1/*`，应用 token 调不了用户接口。\n\n---\n\n## 二、用 GcaChat 账号登录（用户身份）\n\n登录需要图形验证码，**三步**：\n\n### 第 1 步：取验证码\n\n```http\nGET /api/captcha\n```\n\n返回：\n\n```json\n{ \"id\": \"9f2c1e...\", \"svg\": \"<svg xmlns=...>...</svg>\" }\n```\n\n`svg` 是一段 SVG 字符串，渲染成图片给用户看，例如：\n\n```html\n<img src=\"data:image/svg+xml;base64,....\" />\n```\n\n（浏览器里可以直接 `btoa(svg)` 或 `encodeURIComponent` 后拼成 data URI。）\n\n### 第 2 步：提交账号密码 + 验证码\n\n```http\nPOST /api/login\nContent-Type: application/json\n\n{\n  \"username\": \"yourname\",\n  \"password\": \"your-password\",\n  \"captchaId\": \"9f2c1e...\",\n  \"captchaText\": \"AB3D\"\n}\n```\n\n成功返回：\n\n```json\n{\n  \"token\": \"3f8a...（48 位十六进制）\",\n  \"user\": { \"id\": 12, \"username\": \"yourname\", \"nickname\": \"你的昵称\", \"avatar\": \"\", \"title\": \"\" }\n}\n```\n\n失败：`400` 验证码错误或过期（5 分钟）、`401` 用户名或密码不正确、`403` 账号被封禁（`code: \"banned\"`）、`429` 尝试过于频繁（每 IP 10 分钟 20 次）。\n\n### 第 3 步：带 token 调接口\n\n```http\nGET /api/me\nAuthorization: Bearer 3f8a...\n```\n\ntoken **不会自动过期**，直到你调用 `POST /api/logout` 或用户修改密码。请保存在服务端，不要写进前端代码或公开仓库。\n\n### 完整 curl 示例\n\n```bash\n# 1) 取验证码（把 svg 存成文件人工识别，或交给 OCR）\ncurl -s https://chat.gcblog.asia/api/captcha -o cap.json\n\n# 2) 登录\ncurl -s -X POST https://chat.gcblog.asia/api/login \\\n  -H 'Content-Type: application/json' \\\n  -d '{\"username\":\"yourname\",\"password\":\"your-password\",\"captchaId\":\"<上一步的 id>\",\"captchaText\":\"<图上的 4 位字符>\"}'\n\n# 3) 验证 token\ncurl -s https://chat.gcblog.asia/api/me -H 'Authorization: Bearer <token>'\n\n# 4) 退出登录（让 token 立即失效）\ncurl -s -X POST https://chat.gcblog.asia/api/logout -H 'Authorization: Bearer <token>'\n```\n\n### 还没有账号？\n\n`POST /api/register`，同样需要先取验证码：\n\n```json\n{ \"username\": \"newuser\", \"nickname\": \"新用户\", \"password\": \"123456\", \"captchaId\": \"...\", \"captchaText\": \"AB3D\" }\n```\n\n规则：用户名 4–16 位、字母开头、只能字母数字下划线；昵称 1–20 字；密码至少 6 位。注册成功直接返回 `token`，无需再登录。\n\n---\n\n## 三、机器人 / 小程序（应用身份）\n\n1. 在聊天端「+ → 开发者中心 → 我的应用」创建应用（每个账号最多 5 个）\n2. 创建后得到 `gca_` 开头的 token（仅创建者可见，可重置）\n3. 每个应用会自动生成一个机器人用户，并与创建者建立私聊，方便主动推送通知\n4. 用 `Authorization: Bearer gca_xxx` 调用 `/api/v1/*`\n\n限制：每个应用每分钟 60 次；消息正文最多 2000 字；只能向机器人所在的会话发消息。\n"
  },
  "servers": [
    {
      "url": "https://chat.gcblog.asia",
      "description": "生产环境"
    },
    {
      "url": "http://localhost:3000",
      "description": "本地开发"
    }
  ],
  "tags": [
    {
      "name": "账号",
      "description": "验证码、注册、登录、登出、我的信息"
    },
    {
      "name": "应用",
      "description": "应用与机器人身份（应用 token）"
    },
    {
      "name": "会话",
      "description": "机器人所在的会话"
    },
    {
      "name": "消息",
      "description": "以机器人身份发消息"
    }
  ],
  "components": {
    "securitySchemes": {
      "userToken": {
        "type": "http",
        "scheme": "bearer",
        "description": "用户 token：`POST /api/login` 返回的 48 位十六进制字符串"
      },
      "appToken": {
        "type": "http",
        "scheme": "bearer",
        "description": "应用 token：开发者中心创建应用后得到的 `gca_xxx`"
      }
    },
    "schemas": {
      "User": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "username": {
            "type": "string"
          },
          "nickname": {
            "type": "string"
          },
          "avatar": {
            "type": "string",
            "description": "头像，可能是 data URL 或空"
          },
          "cover": {
            "type": "string"
          },
          "bio": {
            "type": "string"
          },
          "title": {
            "type": "string",
            "description": "头衔，可能为空"
          },
          "bubbleId": {
            "type": "string",
            "description": "已装备的聊天气泡"
          },
          "frameId": {
            "type": "string",
            "description": "已装备的头像框"
          },
          "createdAt": {
            "type": "integer",
            "description": "毫秒时间戳"
          }
        }
      },
      "Captcha": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "description": "验证码 ID，登录/注册时回传"
          },
          "svg": {
            "type": "string",
            "description": "SVG 图片字符串，5 分钟内有效"
          }
        }
      },
      "LoginRequest": {
        "type": "object",
        "required": [
          "username",
          "password",
          "captchaId",
          "captchaText"
        ],
        "properties": {
          "username": {
            "type": "string",
            "example": "yourname"
          },
          "password": {
            "type": "string",
            "format": "password"
          },
          "captchaId": {
            "type": "string",
            "description": "GET /api/captcha 返回的 id"
          },
          "captchaText": {
            "type": "string",
            "description": "图片上的 4 位字符（不区分大小写）",
            "example": "AB3D"
          }
        }
      },
      "LoginResponse": {
        "type": "object",
        "properties": {
          "token": {
            "type": "string",
            "description": "后续请求放进 Authorization: Bearer <token>"
          },
          "user": {
            "$ref": "#/components/schemas/User"
          }
        }
      },
      "RegisterRequest": {
        "type": "object",
        "required": [
          "username",
          "password",
          "captchaId",
          "captchaText"
        ],
        "properties": {
          "username": {
            "type": "string",
            "description": "4–16 位，字母开头，仅字母数字下划线"
          },
          "nickname": {
            "type": "string",
            "description": "1–20 字，不填则用用户名"
          },
          "password": {
            "type": "string",
            "format": "password",
            "description": "至少 6 位"
          },
          "captchaId": {
            "type": "string"
          },
          "captchaText": {
            "type": "string"
          }
        }
      },
      "App": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "kind": {
            "type": "string",
            "enum": [
              "bot",
              "mini"
            ]
          },
          "name": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "createdAt": {
            "type": "integer",
            "description": "毫秒时间戳"
          }
        }
      },
      "Message": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "convId": {
            "type": "string"
          },
          "kind": {
            "type": "string",
            "enum": [
              "text",
              "image",
              "file",
              "voice",
              "redpacket",
              "gift"
            ]
          },
          "body": {
            "type": "string"
          },
          "senderId": {
            "type": "integer"
          },
          "createdAt": {
            "type": "integer"
          }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          },
          "code": {
            "type": "string"
          }
        }
      }
    }
  },
  "paths": {
    "/api/captcha": {
      "get": {
        "summary": "获取图形验证码（登录 / 注册前必须先调用）",
        "tags": [
          "账号"
        ],
        "responses": {
          "200": {
            "description": "验证码 SVG",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Captcha"
                }
              }
            }
          }
        }
      }
    },
    "/api/login": {
      "post": {
        "summary": "用 GcaChat 账号登录，换取用户 token",
        "description": "登录需要图形验证码：先 GET /api/captcha，把返回的 svg 渲染给用户，再提交 id 与识别结果。",
        "tags": [
          "账号"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/LoginRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "登录成功",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/LoginResponse"
                }
              }
            }
          },
          "400": {
            "description": "验证码错误或已过期（5 分钟）",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "用户名或密码不正确",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "账号被封禁（code=banned）",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "尝试过于频繁（每 IP 10 分钟 20 次）",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/api/register": {
      "post": {
        "summary": "注册新账号（返回 token，无需再登录）",
        "tags": [
          "账号"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RegisterRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "注册成功",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/LoginResponse"
                }
              }
            }
          },
          "400": {
            "description": "参数不合法或验证码错误",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "用户名已存在",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "注册过于频繁",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/api/logout": {
      "post": {
        "summary": "退出登录（让当前 token 立即失效）",
        "tags": [
          "账号"
        ],
        "security": [
          {
            "userToken": []
          }
        ],
        "responses": {
          "200": {
            "description": "已登出",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "token 无效",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/api/me": {
      "get": {
        "summary": "获取当前登录用户的资料",
        "tags": [
          "账号"
        ],
        "security": [
          {
            "userToken": []
          }
        ],
        "responses": {
          "200": {
            "description": "成功",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "user": {
                      "$ref": "#/components/schemas/User"
                    },
                    "usernameChangeableAt": {
                      "type": "integer",
                      "description": "下次可改用户名的毫秒时间戳"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "token 无效或已过期",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/me": {
      "get": {
        "summary": "获取当前应用与机器人身份",
        "tags": [
          "应用"
        ],
        "security": [
          {
            "appToken": []
          }
        ],
        "responses": {
          "200": {
            "description": "成功",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "app": {
                      "$ref": "#/components/schemas/App"
                    },
                    "bot": {
                      "type": "object",
                      "properties": {
                        "id": {
                          "type": "integer"
                        },
                        "nickname": {
                          "type": "string"
                        },
                        "username": {
                          "type": "string"
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "token 无效",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/conversations": {
      "get": {
        "summary": "机器人所在的会话列表",
        "tags": [
          "会话"
        ],
        "security": [
          {
            "appToken": []
          }
        ],
        "responses": {
          "200": {
            "description": "成功",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "conversations": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "convId": {
                            "type": "string",
                            "description": "群聊 g:<id>，私聊 p:<小ID>:<大ID>"
                          },
                          "type": {
                            "type": "string",
                            "enum": [
                              "group",
                              "private"
                            ]
                          },
                          "name": {
                            "type": "string"
                          },
                          "updatedAt": {
                            "type": "integer"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/messages": {
      "post": {
        "summary": "以机器人身份发送文本消息",
        "tags": [
          "消息"
        ],
        "security": [
          {
            "appToken": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "convId",
                  "text"
                ],
                "properties": {
                  "convId": {
                    "type": "string",
                    "description": "目标会话 ID，来自 /api/v1/conversations"
                  },
                  "text": {
                    "type": "string",
                    "maxLength": 2000
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "发送成功",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "message": {
                      "$ref": "#/components/schemas/Message"
                    }
                  }
                }
              }
            }
          },
          "403": {
            "description": "机器人不在该会话 / 被禁言"
          },
          "429": {
            "description": "调用过于频繁"
          }
        }
      }
    }
  }
}
