修复GLM无法获取token的加密问题

This commit is contained in:
aixianling
2023-06-01 15:15:58 +08:00
parent 9289996de8
commit 311e91a833
3 changed files with 15 additions and 26 deletions

View File

@@ -12,8 +12,8 @@
"@kangc/v-md-editor": "^2.3.15",
"element-plus": "^2.3.4",
"highlight.js": "^11.8.0",
"jsencrypt": "^3.3.2",
"nanoid": "^4.0.2",
"node-forge": "^1.3.1",
"query-string": "^8.1.0",
"sass": "^1.62.1",
"sass-loader": "^13.2.2",

View File

@@ -12,7 +12,7 @@
</el-form-item>
<el-form-item label="API KEY" v-if="isGPT">
<el-row class="w100">
<el-input v-model="settings.model.apiKey" clearable class="fill mar-r8"/>
<el-input type="password" v-model="settings.model.apiKey" clearable class="fill mar-r8"/>
<el-button type="text" @click="getModelAccount">应用</el-button>
</el-row>
</el-form-item>

View File

@@ -1,8 +1,8 @@
import {dayjs} from "element-plus";
import {nanoid} from "nanoid";
import forge from 'node-forge';
import axios from "./axios";
import {AI_AVATAR, OPEN_AI_KEY} from "./env";
import {JSEncrypt} from "jsencrypt";
class BaseModel {
constructor(props) {
@@ -10,8 +10,7 @@ class BaseModel {
this[k] = props[k];
}
this.headers = {
"Content-Type": "application/json",
Accept: "application/json,text/event-stream",
"Content-Type": "application/json", Accept: "application/json,text/event-stream",
}
}
@@ -62,14 +61,11 @@ export class ChatGPT extends BaseModel {
if (endDate) {
const startDate = new Date(endDate - 90 * 24 * 60 * 60);
const formattedDate = time => dayjs(time).format("YYYY-MM-DD")
return await axios.get(`${ChatGPT.base}/v1/dashboard/billing/usage?start_date=${formattedDate(startDate * 1000)}&end_date=${formattedDate(endDate * 1000)}`,
{headers}).then(res => res.json()).then(res => {
return await axios.get(`${ChatGPT.base}/v1/dashboard/billing/usage?start_date=${formattedDate(startDate * 1000)}&end_date=${formattedDate(endDate * 1000)}`, {headers}).then(res => res.json()).then(res => {
usages.total_usage = res.total_usage
const names = usages.account_name.split(" ")
return {
...usages, username: names.at(-1) + names[0],
usage: (usages.total_usage / 100)?.toFixed(2),
total: usages.hard_limit_usd?.toFixed(2)
...usages, username: names.at(-1) + names[0], usage: (usages.total_usage / 100)?.toFixed(2), total: usages.hard_limit_usd?.toFixed(2)
}
});
} else return Promise.reject("没有权限或者网络异常,请重新尝试!")
@@ -142,8 +138,7 @@ export class ChatGLM extends BaseModel {
history.pop()
const prompt = history.pop()
return await axios.post(ChatGLM.base + "/model/v1/open/engines/chatGLM/chatGLM", JSON.stringify({
history, prompt,
temperature: 1, top_p: 0.6, requestTaskNo: this.taskId
history, prompt, temperature: 1, top_p: 0.6, requestTaskNo: this.taskId
}), {headers: this.headers}).then(res => res.json()).then(data => {
if (data?.data.taskStatus == 'PROCESSING') {
return this.getChatResult(data.data.taskOrderNo)
@@ -154,8 +149,7 @@ export class ChatGLM extends BaseModel {
}
async getChatResult(taskOrderNo) {
return await axios.get(ChatGLM.base + `/request-task/query-request-task-result/${taskOrderNo}`,
{headers: this.headers}).then(res => res.json()).then(data => {
return await axios.get(ChatGLM.base + `/request-task/query-request-task-result/${taskOrderNo}`, {headers: this.headers}).then(res => res.json()).then(data => {
if (data?.data.taskStatus == 'PROCESSING') {
return this.getChatResult(data.data.taskOrderNo)
} else {
@@ -170,30 +164,25 @@ export class ChatGLM extends BaseModel {
const prompt = history.pop()
const url = ChatGLM.base + "/model/v1/open/engines/sse/chatGLM/chatGLM"
return await axios.post(url, JSON.stringify({
history, prompt,
temperature: 1, top_p: 0.6, requestTaskNo: this.taskId
history, prompt, temperature: 1, top_p: 0.6, requestTaskNo: this.taskId
}), {
headers: this.headers,
}).then(res => res?.body?.getReader());
}
static encrypt(publicKey, timestamp) {
const publicKeyDer = forge.util.decode64(publicKey)
const key = forge.pki.publicKeyFromAsn1(forge.asn1.fromDer(publicKeyDer)); // 使用 publicKeyFromAsn1 方法导入公钥
timestamp = new TextEncoder().encode(Date.now().toFixed(0))
const encrypted = key.encrypt(timestamp);
return forge.util.encode64(encrypted);
timestamp = Date.now().toFixed(0)
const encryptor = new JSEncrypt()
encryptor.setPublicKey(publicKey)
return encryptor.encrypt(timestamp);
}
async getAccount() {
const usages = await axios.get("https://open.bigmodel.ai/api/paas/account/query-customer-account-report",
{headers: this.headers}).then(res => res.json());
const usages = await axios.get("https://open.bigmodel.ai/api/paas/account/query-customer-account-report", {headers: this.headers}).then(res => res.json());
if (usages.code == 200) {
const {data} = usages
return {
...data, username: "Kubbo",
usage: data.totalSpendAmount?.toFixed(4),
total: data.rechargeAmount?.toFixed(4)
...data, username: "Kubbo", usage: data.totalSpendAmount?.toFixed(4), total: data.rechargeAmount?.toFixed(4)
}
} else return Promise.reject("没有权限或者网络异常,请重新尝试!")
}