mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-16 13:27:08 +00:00
增加后台对缺少的锅巴配置自动读取,将后台登陆信息添加到redis
This commit is contained in:
parent
5acf874e0b
commit
369dbd31c3
5 changed files with 79 additions and 9 deletions
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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 () {
|
||||
|
|
|
|||
|
|
@ -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: '权限不足' })
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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": []}')
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue