From f01006c4f169f5a9c6cd3c9023050be3cdfd2531 Mon Sep 17 00:00:00 2001 From: Vinlic Date: Wed, 10 Apr 2024 18:45:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0refresh=5Ftoken=E5=AD=98?= =?UTF-8?q?=E6=B4=BB=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 37 +++++++++++++++++++++++++++++-------- src/api/controllers/chat.ts | 30 ++++++++++++++++++++++++++++++ src/api/routes/index.ts | 4 +++- src/api/routes/token.ts | 25 +++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 src/api/routes/token.ts diff --git a/README.md b/README.md index 8a4eac9..c0a66a8 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ Moonshot AI(Kimi.ai)接口转API [kimi-free-api](https://github.com/LLM-Red- * [AI绘图](#AI绘图) * [文档解读](#文档解读) * [图像解析](#图像解析) + * [refresh_token存活检测](#refresh_token存活检测) * [注意事项](#注意事项) * [Nginx反代优化](#Nginx反代优化) @@ -62,37 +63,37 @@ https://udify.app/chat/Pe89TtaX3rKXM8NS ## 效果示例 -### 验明正身 +### 验明正身Demo ![验明正身](./doc/example-1.png) -### 智能体对话 +### 智能体对话Demo 对应智能体链接:[网抑云评论生成器](https://chatglm.cn/main/gdetail/65c046a531d3fcb034918abe) ![智能体对话](./doc/example-9.png) -### 多轮对话 +### 多轮对话Demo ![多轮对话](./doc/example-6.png) -### AI绘图 +### AI绘图Demo ![AI绘图](./doc/example-10.png) -### 联网搜索 +### 联网搜索Demo ![联网搜索](./doc/example-2.png) -### 长文档解读 +### 长文档解读Demo ![长文档解读](./doc/example-5.png) -### 代码调用 +### 代码调用Demo ![代码调用](./doc/example-12.png) -### 图像解析 +### 图像解析Demo ![图像解析](./doc/example-3.png) @@ -428,6 +429,26 @@ Authorization: Bearer [refresh_token] } ``` +### refresh_token存活检测 + +检测refresh_token是否存活,如果存活live未true,否则为false,请不要频繁(小于10分钟)调用此接口。 + +**POST /token/check** + +请求数据: +```json +{ + "token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9..." +} +``` + +响应数据: +```json +{ + "live": true +} +``` + ## 注意事项 ### Nginx反代优化 diff --git a/src/api/controllers/chat.ts b/src/api/controllers/chat.ts index 83a3ee5..cd7d9cb 100644 --- a/src/api/controllers/chat.ts +++ b/src/api/controllers/chat.ts @@ -1118,9 +1118,39 @@ function generateCookie(refreshToken: string, token: string) { }; } +/** + * 获取Token存活状态 + */ +async function getTokenLiveStatus(refreshToken: string) { + const result = await axios.post( + "https://chatglm.cn/chatglm/backend-api/v1/user/refresh", + {}, + { + headers: { + Authorization: `Bearer ${refreshToken}`, + Referer: "https://chatglm.cn/main/alltoolsdetail", + "X-Device-Id": util.uuid(false), + "X-Request-Id": util.uuid(false), + ...FAKE_HEADERS, + }, + timeout: 15000, + validateStatus: () => true, + } + ); + try { + const { result: _result } = checkResult(result, refreshToken); + const { accessToken } = _result; + return !!accessToken; + } + catch(err) { + return false; + } +} + export default { createCompletion, createCompletionStream, generateImages, + getTokenLiveStatus, tokenSplit, }; diff --git a/src/api/routes/index.ts b/src/api/routes/index.ts index 706525a..6ecd2fa 100644 --- a/src/api/routes/index.ts +++ b/src/api/routes/index.ts @@ -4,6 +4,7 @@ import Response from '@/lib/response/Response.ts'; import chat from "./chat.ts"; import images from "./images.ts"; import ping from "./ping.ts"; +import token from './token.js'; export default [ { @@ -21,5 +22,6 @@ export default [ }, chat, images, - ping + ping, + token ]; \ No newline at end of file diff --git a/src/api/routes/token.ts b/src/api/routes/token.ts new file mode 100644 index 0000000..c69c512 --- /dev/null +++ b/src/api/routes/token.ts @@ -0,0 +1,25 @@ +import _ from 'lodash'; + +import Request from '@/lib/request/Request.ts'; +import Response from '@/lib/response/Response.ts'; +import chat from '@/api/controllers/chat.ts'; +import logger from '@/lib/logger.ts'; + +export default { + + prefix: '/token', + + post: { + + '/check': async (request: Request) => { + request + .validate('body.token', _.isString) + const live = await chat.getTokenLiveStatus(request.body.token); + return { + live + } + } + + } + +} \ No newline at end of file