修复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", "@kangc/v-md-editor": "^2.3.15",
"element-plus": "^2.3.4", "element-plus": "^2.3.4",
"highlight.js": "^11.8.0", "highlight.js": "^11.8.0",
"jsencrypt": "^3.3.2",
"nanoid": "^4.0.2", "nanoid": "^4.0.2",
"node-forge": "^1.3.1",
"query-string": "^8.1.0", "query-string": "^8.1.0",
"sass": "^1.62.1", "sass": "^1.62.1",
"sass-loader": "^13.2.2", "sass-loader": "^13.2.2",

View File

@@ -12,7 +12,7 @@
</el-form-item> </el-form-item>
<el-form-item label="API KEY" v-if="isGPT"> <el-form-item label="API KEY" v-if="isGPT">
<el-row class="w100"> <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-button type="text" @click="getModelAccount">应用</el-button>
</el-row> </el-row>
</el-form-item> </el-form-item>

View File

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