From 369dbd31c3a828d99b33a39e6be76e36a300e716 Mon Sep 17 00:00:00 2001 From: zyc404 Date: Sun, 5 May 2024 00:40:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=90=8E=E5=8F=B0=E5=AF=B9?= =?UTF-8?q?=E7=BC=BA=E5=B0=91=E7=9A=84=E9=94=85=E5=B7=B4=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E8=AF=BB=E5=8F=96=EF=BC=8C=E5=B0=86=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E7=99=BB=E9=99=86=E4=BF=A1=E6=81=AF=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=88=B0redis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/view/setting_view.json | 2 +- server/index.js | 7 ++-- server/modules/setting_view.js | 67 +++++++++++++++++++++++++++++++- server/modules/user.js | 6 +-- server/modules/user_data.js | 6 ++- 5 files changed, 79 insertions(+), 9 deletions(-) diff --git a/resources/view/setting_view.json b/resources/view/setting_view.json index 271a117..b2cddd0 100644 --- a/resources/view/setting_view.json +++ b/resources/view/setting_view.json @@ -488,7 +488,7 @@ "type": "select", "label": "Bing模式", "data": "toneStyle", - "items": [ { "label": "创意", "value": "Creative" }, { "label": "精确", "value": "Precise" } ] + "items": [ { "label": "创意", "value": "Creative" }, { "label": "均衡", "value": "Balanced" }, { "label": "精确", "value": "Precise" } ] }, { "type": "check", diff --git a/server/index.js b/server/index.js index 1a75d52..ebdf9da 100644 --- a/server/index.js +++ b/server/index.js @@ -10,7 +10,7 @@ import os from 'os' import websocketclient from 'ws' import { Config } from '../utils/config.js' -import { UserInfo, GetUser, AddUser } from './modules/user_data.js' +import { UserInfo, GetUser, AddUser, ReplaceUsers } from './modules/user_data.js' import { getPublicIP, getUserData, getMasterQQ, randomString, getUin } from '../utils/common.js' import webRoute from './modules/web_route.js' @@ -78,12 +78,12 @@ async function mediaLink () { if (data.qq && data.passwd) { const token = randomString(32) if (data.qq == getUin() && await redis.get('CHATGPT:ADMIN_PASSWD') == data.passwd) { - AddUser({ user: data.qq, token, autho: 'admin' }) + await AddUser({ user: data.qq, token, autho: 'admin' }) ws.send(JSON.stringify({ command: data.command, state: true, autho: 'admin', token, region: getUin(), type: 'server' })) } else { const user = await getUserData(data.qq) if (user.passwd != '' && user.passwd === data.passwd) { - AddUser({ user: data.qq, token, autho: 'user' }) + await AddUser({ user: data.qq, token, autho: 'user' }) ws.send(JSON.stringify({ command: data.command, state: true, autho: 'user', token, region: getUin(), type: 'server' })) } else { ws.send(JSON.stringify({ command: data.command, state: false, error: `用户名密码错误,如果忘记密码请私聊机器人输入 ${data.qq == getUin() ? '#修改管理密码' : '#修改用户密码'} 进行修改`, region: getUin(), type: 'server' })) @@ -593,6 +593,7 @@ export async function runServer () { server.log.info(`server listening on ${server.server.address().port}`) } }) + await ReplaceUsers() } export async function stopServer () { diff --git a/server/modules/setting_view.js b/server/modules/setting_view.js index 81aa570..4793b35 100644 --- a/server/modules/setting_view.js +++ b/server/modules/setting_view.js @@ -1,7 +1,23 @@ import { UserInfo } from './user_data.js' +import { supportGuoba } from '../../guoba.support.js' import fs from 'fs' import path from 'path' +function getAttributeValues(obj, attributeName, results = []) { + if (Array.isArray(obj)) { + obj.forEach(item => getAttributeValues(item, attributeName, results)); + } else if (typeof obj === 'object' && obj !== null) { + Object.keys(obj).forEach(key => { + if (key === attributeName) { + results.push(obj[key]); + } else if (typeof obj[key] === 'object') { + getAttributeValues(obj[key], attributeName, results); + } + }); + } + return results; +} + async function SettingView(fastify, options) { // 获取配置视图 fastify.post('/settingView', async (request, reply) => { @@ -11,7 +27,56 @@ async function SettingView(fastify, options) { reply.send({ err: '未登录' }) } else if (user.autho === 'admin') { const filepath = path.join('plugins/chatgpt-plugin/resources/view', 'setting_view.json') - const configView = JSON.parse(fs.readFileSync(filepath, 'utf8')) + let configView = JSON.parse(fs.readFileSync(filepath, 'utf8')) + + // 从锅巴配置获取额外配置视图 + const guoba = supportGuoba() + const guobaConfig = guoba.configInfo.schemas + const viewDataList = getAttributeValues(configView, 'data') + const guobaDataList = getAttributeValues(guobaConfig, 'field') + const otherDataList = guobaDataList.filter(item => !viewDataList.includes(item)) + const otherData = guobaConfig.filter(item => otherDataList.includes(item.field)) + // 转换视图 + if (otherData.length > 0) { + let otherView = [] + for (const data of otherData) { + let view = { + 'label': data.label, + 'placeholder': data.bottomHelpMessage || undefined, + 'data': data.field, + } + switch (data.component) { + case 'Input': + view.type = 'text' + break + case 'Switch': + view.type = 'check' + break + case 'InputNumber': + view.type = 'number' + break + case 'InputPassword': + view.type = 'password' + break + case 'InputTextArea': + view.type = 'textarea' + break + case 'Select': + view.type = 'textarea' + view.items = data.componentProps.options + break + default: + continue + } + otherView.push(view) + } + configView.push({ + "id": "OtherSettings", + "title": "其他设置", + "view": otherView + }) + } + reply.send(configView) } else { reply.send({ err: '权限不足' }) diff --git a/server/modules/user.js b/server/modules/user.js index 0bb7157..ee18a0a 100644 --- a/server/modules/user.js +++ b/server/modules/user.js @@ -25,13 +25,13 @@ async function User (fastify, options) { const token = randomString(32) if (body.qq == getUin() && await redis.get('CHATGPT:ADMIN_PASSWD') == body.passwd) { const guobaToken = await guobaLoginService.signToken(body.qq) - AddUser({ user: body.qq, token, autho: 'admin' }) + await AddUser({ user: body.qq, token, autho: 'admin' }) reply.setCookie('token', token, { path: '/' }) reply.send({ login: true, autho: 'admin', token, guobaToken, guoba: guobaAPI }) } else { const user = await getUserData(body.qq) if (user.passwd != '' && user.passwd === body.passwd) { - AddUser({ user: body.qq, token, autho: 'user' }) + await AddUser({ user: body.qq, token, autho: 'user' }) reply.setCookie('token', token, { path: '/' }) reply.send({ login: true, autho: 'user', token }) } else { @@ -43,7 +43,7 @@ async function User (fastify, options) { const opt = await redis.get('CHATGPT:SERVER_QUICK') if (opt && body.otp == opt) { const guobaToken = await guobaLoginService.signToken(getUin()) - AddUser({ user: getUin(), token, autho: 'admin' }) + await AddUser({ user: getUin(), token, autho: 'admin' }) reply.setCookie('token', token, { path: '/' }) reply.send({ login: true, autho: 'admin', token, user: getUin(), guobaToken, guoba: guobaAPI }) } else { diff --git a/server/modules/user_data.js b/server/modules/user_data.js index 51fb997..cf49c09 100644 --- a/server/modules/user_data.js +++ b/server/modules/user_data.js @@ -25,7 +25,7 @@ export function GetUser(user) { return users.user.find(user => user === user) } // 添加用户token -export function AddUser(data) { +export async function AddUser(data) { const userIndex = users.user.findIndex(user => user === data.user) if (userIndex >= 0) { users.user[userIndex].token.push(data.token) @@ -38,4 +38,8 @@ export function AddUser(data) { tiem: new Date() }) } + await redis.set('CHATGPT:SERVER_USER', JSON.stringify(users)) +} +export async function ReplaceUsers() { + users = JSON.parse(await redis.get('CHATGPT:SERVER_USER') || '{"user": []}') } \ No newline at end of file