From 9546a47f2b4bba5d34cc2c84081468403de7a62d Mon Sep 17 00:00:00 2001 From: CaIon Date: Sun, 6 Jul 2025 20:56:09 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(tokens):=20add=20cherryConfig?= =?UTF-8?q?=20support=20for=20URL=20generation=20and=20base64=20encoding?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/str.go | 5 +++++ setting/chat.go | 5 ++++- web/src/components/table/TokensTable.js | 19 ++++++++++++++++--- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/common/str.go b/common/str.go index d42fd837..9eb19fe7 100644 --- a/common/str.go +++ b/common/str.go @@ -1,6 +1,7 @@ package common import ( + "encoding/base64" "encoding/json" "math/rand" "strconv" @@ -68,3 +69,7 @@ func StringToByteSlice(s string) []byte { tmp2 := [3]uintptr{tmp1[0], tmp1[1], tmp1[1]} return *(*[]byte)(unsafe.Pointer(&tmp2)) } + +func EncodeBase64(str string) string { + return base64.StdEncoding.EncodeToString([]byte(str)) +} diff --git a/setting/chat.go b/setting/chat.go index ef308000..53cb655a 100644 --- a/setting/chat.go +++ b/setting/chat.go @@ -6,8 +6,11 @@ import ( ) var Chats = []map[string]string{ + //{ + // "ChatGPT Next Web 官方示例": "https://app.nextchat.dev/#/?settings={\"key\":\"{key}\",\"url\":\"{address}\"}", + //}, { - "ChatGPT Next Web 官方示例": "https://app.nextchat.dev/#/?settings={\"key\":\"{key}\",\"url\":\"{address}\"}", + "Cherry Studio": "cherrystudio://providers/api-keys?v=1&data={cherryConfig}", }, { "Lobe Chat 官方示例": "https://chat-preview.lobehub.com/?settings={\"keyVaults\":{\"openai\":{\"apiKey\":\"{key}\",\"baseURL\":\"{address}/v1\"}}}", diff --git a/web/src/components/table/TokensTable.js b/web/src/components/table/TokensTable.js index f91f7b82..a6d669a6 100644 --- a/web/src/components/table/TokensTable.js +++ b/web/src/components/table/TokensTable.js @@ -432,9 +432,22 @@ const TokensTable = () => { if (serverAddress === '') { serverAddress = window.location.origin; } - let encodedServerAddress = encodeURIComponent(serverAddress); - url = url.replaceAll('{address}', encodedServerAddress); - url = url.replaceAll('{key}', 'sk-' + record.key); + if (url.includes('{cherryConfig}') === true) { + let cherryConfig = { + id: 'new-api', + baseUrl: serverAddress, + apiKey: 'sk-' + record.key, + } + // 替换 {cherryConfig} 为base64编码的JSON字符串 + let encodedConfig = encodeURIComponent( + btoa(JSON.stringify(cherryConfig)) + ); + url = url.replaceAll('{cherryConfig}', encodedConfig); + } else { + let encodedServerAddress = encodeURIComponent(serverAddress); + url = url.replaceAll('{address}', encodedServerAddress); + url = url.replaceAll('{key}', 'sk-' + record.key); + } window.open(url, '_blank'); };