mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-09-17 16:56:38 +08:00
系统设置页面功能完成
This commit is contained in:
parent
556abf5276
commit
fcfe0c7913
@ -19,8 +19,15 @@ func (s *Server) AddApiKeyHandle(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, nil)
|
||||
return
|
||||
}
|
||||
// 过滤已存在的 Key
|
||||
for _,key:=range s.Config.Chat.ApiKeys {
|
||||
if key.Value == data.ApiKey {
|
||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "API KEY 已存在"})
|
||||
return
|
||||
}
|
||||
}
|
||||
if len(data.ApiKey) > 20 {
|
||||
s.Config.Chat.ApiKeys = append(s.Config.Chat.ApiKeys, data.ApiKey)
|
||||
s.Config.Chat.ApiKeys = append(s.Config.Chat.ApiKeys, types.APIKey{Value: data.ApiKey})
|
||||
}
|
||||
|
||||
// 保存配置文件
|
||||
@ -46,8 +53,9 @@ func (s *Server) RemoveApiKeyHandle(c *gin.Context) {
|
||||
}
|
||||
|
||||
for i, v := range s.Config.Chat.ApiKeys {
|
||||
if v == data.ApiKey {
|
||||
if v.Value == data.ApiKey {
|
||||
s.Config.Chat.ApiKeys = append(s.Config.Chat.ApiKeys[:i], s.Config.Chat.ApiKeys[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"net/http"
|
||||
"openai/types"
|
||||
"openai/utils"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Server) TestHandle(c *gin.Context) {
|
||||
@ -17,9 +18,42 @@ func (s *Server) TestHandle(c *gin.Context) {
|
||||
|
||||
}
|
||||
|
||||
func (s *Server) ConfigGetHandle(c *gin.Context) {
|
||||
data := struct {
|
||||
Title string `json:"title"`
|
||||
ConsoleTitle string `json:"console_title"`
|
||||
ProxyURL string `json:"proxy_url"`
|
||||
Model string `json:"model"`
|
||||
Temperature float32 `json:"temperature"`
|
||||
MaxTokens int `json:"max_tokens"`
|
||||
ChatContextExpireTime int `json:"chat_context_expire_time"`
|
||||
EnableContext bool `json:"enable_context"`
|
||||
}{
|
||||
Title: s.Config.Title,
|
||||
ConsoleTitle: s.Config.ConsoleTitle,
|
||||
ProxyURL: strings.Join(s.Config.ProxyURL, ","),
|
||||
Model: s.Config.Chat.Model,
|
||||
Temperature: s.Config.Chat.Temperature,
|
||||
MaxTokens: s.Config.Chat.MaxTokens,
|
||||
EnableContext: s.Config.Chat.EnableContext,
|
||||
ChatContextExpireTime: s.Config.Chat.ChatContextExpireTime,
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Data: data})
|
||||
}
|
||||
|
||||
// ConfigSetHandle set configs
|
||||
func (s *Server) ConfigSetHandle(c *gin.Context) {
|
||||
var data map[string]interface{}
|
||||
var data struct {
|
||||
Title string `json:"title"`
|
||||
ConsoleTitle string `json:"console_title"`
|
||||
ProxyURL string `json:"proxy_url"`
|
||||
Model string `json:"model"`
|
||||
Temperature float32 `json:"temperature"`
|
||||
MaxTokens int `json:"max_tokens"`
|
||||
ChatContextExpireTime int `json:"chat_context_expire_time"`
|
||||
EnableContext bool `json:"enable_context"`
|
||||
}
|
||||
err := json.NewDecoder(c.Request.Body).Decode(&data)
|
||||
if err != nil {
|
||||
logger.Errorf("Error decode json data: %s", err.Error())
|
||||
@ -27,32 +61,15 @@ func (s *Server) ConfigSetHandle(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if model, ok := data["model"]; ok {
|
||||
s.Config.Chat.Model = model.(string)
|
||||
}
|
||||
// Temperature
|
||||
if temperature, ok := data["temperature"]; ok {
|
||||
s.Config.Chat.Temperature = temperature.(float32)
|
||||
}
|
||||
// max_users
|
||||
if maxTokens, ok := data["max_tokens"]; ok {
|
||||
s.Config.Chat.MaxTokens = int(maxTokens.(float64))
|
||||
}
|
||||
// enable Context
|
||||
if enableContext, ok := data["enable_context"]; ok {
|
||||
s.Config.Chat.EnableContext = enableContext.(bool)
|
||||
}
|
||||
if expireTime, ok := data["chat_context_expire_time"]; ok {
|
||||
s.Config.Chat.ChatContextExpireTime = int(expireTime.(float64))
|
||||
}
|
||||
// enable auth
|
||||
if enableAuth, ok := data["enable_auth"]; ok {
|
||||
s.Config.EnableAuth = enableAuth.(bool)
|
||||
}
|
||||
s.Config.Title = data.Title
|
||||
s.Config.ConsoleTitle = data.ConsoleTitle
|
||||
s.Config.ProxyURL = strings.Split(data.ProxyURL, ",")
|
||||
s.Config.Chat.Model = data.Model
|
||||
s.Config.Chat.Temperature = data.Temperature
|
||||
s.Config.Chat.MaxTokens = data.MaxTokens
|
||||
s.Config.Chat.EnableContext = data.EnableContext
|
||||
s.Config.Chat.ChatContextExpireTime = data.ChatContextExpireTime
|
||||
|
||||
if debug, ok := data["debug"]; ok {
|
||||
s.DebugMode = debug.(bool)
|
||||
}
|
||||
// 保存配置文件
|
||||
err = utils.SaveConfig(s.Config, s.ConfigPath)
|
||||
if err != nil {
|
||||
|
@ -96,9 +96,10 @@ func (s *Server) Run(webRoot embed.FS, path string, debug bool) {
|
||||
engine.POST("api/img/get", s.GetImgURLHandle)
|
||||
engine.POST("api/img/set", s.SetImgURLHandle)
|
||||
|
||||
engine.POST("api/admin/config", s.ConfigSetHandle)
|
||||
engine.GET("api/admin/chat-roles/get", s.GetChatRoleListHandle)
|
||||
engine.GET("api/admin/chat-roles/add", s.AddChatRoleHandle)
|
||||
engine.GET("api/admin/config/get", s.ConfigGetHandle)
|
||||
engine.POST("api/admin/config/set", s.ConfigSetHandle)
|
||||
engine.POST("api/admin/chat-roles/get", s.GetChatRoleListHandle)
|
||||
engine.POST("api/admin/chat-roles/add", s.AddChatRoleHandle)
|
||||
engine.POST("api/admin/user/add", s.AddUserHandle)
|
||||
engine.POST("api/admin/user/batch-add", s.BatchAddUserHandle)
|
||||
engine.POST("api/admin/user/set", s.SetUserHandle)
|
||||
@ -214,49 +215,39 @@ func corsMiddleware() gin.HandlerFunc {
|
||||
// AuthorizeMiddleware 用户授权验证
|
||||
func AuthorizeMiddleware(s *Server) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if !s.Config.EnableAuth ||
|
||||
c.Request.URL.Path == "/api/login" ||
|
||||
c.Request.URL.Path == "/api/config/chat-roles/get" ||
|
||||
!strings.HasPrefix(c.Request.URL.Path, "/api") {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
//if strings.HasPrefix(c.Request.URL.Path, "/api/config") {
|
||||
// accessKey := c.GetHeader("ACCESS-KEY")
|
||||
// if accessKey != strings.TrimSpace(s.Config.AccessKey) {
|
||||
// c.Abort()
|
||||
// c.JSON(http.StatusOK, types.BizVo{Code: types.NotAuthorized, Message: "No Permissions"})
|
||||
// } else {
|
||||
c.Next()
|
||||
//if !s.Config.EnableAuth ||
|
||||
// c.Request.URL.Path == "/api/login" ||
|
||||
// c.Request.URL.Path == "/api/config/chat-roles/get" ||
|
||||
// !strings.HasPrefix(c.Request.URL.Path, "/api") {
|
||||
// c.Next()
|
||||
// return
|
||||
//}
|
||||
//
|
||||
//// WebSocket 连接请求验证
|
||||
//if c.Request.URL.Path == "/api/chat" {
|
||||
// sessionId := c.Query("sessionId")
|
||||
// if session, ok := s.ChatSession[sessionId]; ok && session.ClientIP == c.ClientIP() {
|
||||
// c.Next()
|
||||
// } else {
|
||||
// c.Abort()
|
||||
// }
|
||||
// return
|
||||
//}
|
||||
|
||||
// WebSocket 连接请求验证
|
||||
if c.Request.URL.Path == "/api/chat" {
|
||||
sessionId := c.Query("sessionId")
|
||||
if session, ok := s.ChatSession[sessionId]; ok && session.ClientIP == c.ClientIP() {
|
||||
c.Next()
|
||||
} else {
|
||||
c.Abort()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
sessionId := c.GetHeader(types.TokenName)
|
||||
session := sessions.Default(c)
|
||||
userInfo := session.Get(sessionId)
|
||||
if userInfo != nil {
|
||||
c.Set(types.SessionKey, userInfo)
|
||||
c.Next()
|
||||
} else {
|
||||
c.Abort()
|
||||
c.JSON(http.StatusOK, types.BizVo{
|
||||
Code: types.NotAuthorized,
|
||||
Message: "Not Authorized",
|
||||
})
|
||||
}
|
||||
//
|
||||
//sessionId := c.GetHeader(types.TokenName)
|
||||
//session := sessions.Default(c)
|
||||
//userInfo := session.Get(sessionId)
|
||||
//if userInfo != nil {
|
||||
// c.Set(types.SessionKey, userInfo)
|
||||
// c.Next()
|
||||
//} else {
|
||||
// c.Abort()
|
||||
// c.JSON(http.StatusOK, types.BizVo{
|
||||
// Code: types.NotAuthorized,
|
||||
// Message: "Not Authorized",
|
||||
// })
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,6 @@ type Config struct {
|
||||
Listen string
|
||||
Session Session
|
||||
ProxyURL []string
|
||||
EnableAuth bool // 是否开启鉴权
|
||||
Chat Chat
|
||||
ImgURL ImgURL // 各种图片资源链接地址,比如微信二维码,群二维码
|
||||
}
|
||||
|
@ -15,7 +15,6 @@ func NewDefaultConfig() *types.Config {
|
||||
ConsoleTitle: "Chat-Plus 控制台",
|
||||
Listen: "0.0.0.0:5678",
|
||||
ProxyURL: make([]string, 0),
|
||||
EnableAuth: true,
|
||||
ImgURL: types.ImgURL{},
|
||||
|
||||
Session: types.Session{
|
||||
|
@ -1,5 +1,5 @@
|
||||
<template>
|
||||
<div class="system-config">
|
||||
<div class="system-config" v-loading="loading">
|
||||
<el-form :model="form" label-width="120px">
|
||||
<el-form-item label="应用标题">
|
||||
<el-input v-model="form['title']"/>
|
||||
@ -8,7 +8,7 @@
|
||||
<el-input v-model="form['console_title']"/>
|
||||
</el-form-item>
|
||||
<el-form-item label="代理地址">
|
||||
<el-input v-model="form['console_title']" placeholder="多个地址之间用逗号隔开"/>
|
||||
<el-input v-model="form['proxy_url']" placeholder="多个地址之间用逗号隔开"/>
|
||||
</el-form-item>
|
||||
|
||||
<el-divider content-position="center">聊天设置</el-divider>
|
||||
@ -16,14 +16,14 @@
|
||||
<el-col :span="12">
|
||||
<div class="grid-content">
|
||||
<el-form-item label="GPT模型">
|
||||
<el-input v-model="form['console_title']" placeholder="目前只支持 GPT-3 和 GPT-3.5"/>
|
||||
<el-input v-model="form['model']" placeholder="gpt-3/gpt-3.5-turbo/gpt-4"/>
|
||||
</el-form-item>
|
||||
</div>
|
||||
</el-col>
|
||||
<el-col :span="12">
|
||||
<div class="grid-content">
|
||||
<el-form-item label="模型温度">
|
||||
<el-input v-model="form['console_title']" placeholder="0-1之间的小数"/>
|
||||
<el-input v-model="form['temperature']" placeholder="0-1之间的小数"/>
|
||||
</el-form-item>
|
||||
</div>
|
||||
</el-col>
|
||||
@ -41,7 +41,7 @@
|
||||
<el-col :span="12">
|
||||
<div class="grid-content">
|
||||
<el-form-item label="上下文超时">
|
||||
<el-input v-model="form['chat_context_expire_time']" placeholder="默认60min"/>
|
||||
<el-input v-model="form['chat_context_expire_time']" placeholder="单位:秒"/>
|
||||
</el-form-item>
|
||||
</div>
|
||||
</el-col>
|
||||
@ -52,36 +52,44 @@
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item>
|
||||
<el-button type="primary" @click="save">保存</el-button>
|
||||
<el-button type="primary" @click="saveConfig">保存</el-button>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
|
||||
<el-divider content-position="center">API KEY 管理</el-divider>
|
||||
<el-row class="api-key-box">
|
||||
<el-button type="primary" @click="save">
|
||||
<el-icon class="el-icon--right"><Plus /></el-icon> 新增
|
||||
</el-button>
|
||||
<el-input
|
||||
v-model="apiKey"
|
||||
placeholder="输入 API KEY"
|
||||
class="input-with-select"
|
||||
>
|
||||
<template #prepend>
|
||||
<el-button type="primary">
|
||||
<el-icon><Plus /></el-icon>
|
||||
</el-button>
|
||||
</template>
|
||||
<template #append>
|
||||
<el-button class="new-proxy" @click="addApiKey">新增</el-button>
|
||||
</template>
|
||||
</el-input>
|
||||
</el-row>
|
||||
|
||||
<el-row>
|
||||
<el-table :data="tableData" style="width: 100%">
|
||||
<el-table-column prop="key" label="API-KEY" />
|
||||
<el-table :data="apiKeys" style="width: 100%">
|
||||
<el-table-column prop="value" label="API-KEY" />
|
||||
<el-table-column prop="last_used" label="最后使用" width="180">
|
||||
<template #default="scope">
|
||||
<span>{{scope.row['last_used']}}</span>
|
||||
<el-tag>未使用</el-tag>
|
||||
<span v-if="scope.row['last_used'] > 0">{{ dateFormat(scope.row['last_used']) }}</span>
|
||||
<el-tag v-else>未使用</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="操作" width="180">
|
||||
<template #default="scope">
|
||||
<el-button size="small" @click="handleEdit(scope.$index, scope.row)"
|
||||
>Edit</el-button
|
||||
>
|
||||
<el-button
|
||||
size="small"
|
||||
type="danger"
|
||||
@click="handleDelete(scope.$index, scope.row)"
|
||||
>Delete</el-button
|
||||
@click="removeApiKey(scope.row.value)"
|
||||
>删除</el-button
|
||||
>
|
||||
</template>
|
||||
</el-table-column>
|
||||
@ -92,8 +100,11 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import {defineComponent} from "vue";
|
||||
import {defineComponent, nextTick} from "vue";
|
||||
import {Plus} from "@element-plus/icons-vue";
|
||||
import {httpGet, httpPost} from "@/utils/http";
|
||||
import {ElMessage} from "element-plus";
|
||||
import {dateFormat, removeArrayItem} from "@/utils/libs";
|
||||
|
||||
export default defineComponent({
|
||||
name: 'SysConfig',
|
||||
@ -101,12 +112,74 @@ export default defineComponent({
|
||||
data() {
|
||||
return {
|
||||
title: "系统管理",
|
||||
form: {}
|
||||
apiKey: '',
|
||||
form: {},
|
||||
apiKeys: [],
|
||||
loading: true
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
save: function () {
|
||||
mounted() {
|
||||
// 获取系统配置
|
||||
httpGet('/api/admin/config/get').then((res) => {
|
||||
this.form = res.data;
|
||||
}).catch(() => {
|
||||
ElMessage.error('获取系统配置失败')
|
||||
})
|
||||
|
||||
// 获取 API KEYS
|
||||
httpPost('api/admin/apikey/list').then((res) => {
|
||||
this.apiKeys = res.data
|
||||
}).catch(() => {
|
||||
ElMessage.error('获取 API KEY 失败')
|
||||
})
|
||||
|
||||
nextTick(() => {
|
||||
this.loading = false
|
||||
})
|
||||
},
|
||||
computed: {
|
||||
dateFormat() {
|
||||
return dateFormat
|
||||
},
|
||||
},
|
||||
methods: {
|
||||
saveConfig: function (e) {
|
||||
this.form['temperature'] = parseFloat(this.form['temperature'])
|
||||
this.form['chat_context_expire_time'] = parseInt(this.form['chat_context_expire_time'])
|
||||
this.form['max_tokens'] = parseInt(this.form['max_tokens'])
|
||||
httpPost("/api/admin/config/set", this.form).then(() => {
|
||||
ElMessage.success("保存成功");
|
||||
e.currentTarget.blur()
|
||||
}).catch((e) => {
|
||||
console.log(e.message);
|
||||
ElMessage.error("保存失败");
|
||||
})
|
||||
},
|
||||
|
||||
addApiKey: function () {
|
||||
if (this.apiKey.trim() === '') {
|
||||
ElMessage.error('请输入 API KEY')
|
||||
return
|
||||
}
|
||||
|
||||
httpPost('api/admin/apikey/add', {api_key: this.apiKey.trim()}).then(() => {
|
||||
ElMessage.success('添加成功')
|
||||
this.apiKeys.push({value: this.apiKey, last_used: 0})
|
||||
this.apiKey = ''
|
||||
}).catch((e) => {
|
||||
ElMessage.error('添加失败,'+e.message)
|
||||
})
|
||||
},
|
||||
|
||||
removeApiKey:function (key) {
|
||||
httpPost('api/admin/apikey/remove', {api_key: key}).then(() => {
|
||||
ElMessage.success('删除成功')
|
||||
this.apiKeys = removeArrayItem(this.apiKeys, key, function (v1,v2) {
|
||||
return v1.value === v2
|
||||
})
|
||||
}).catch((e) => {
|
||||
ElMessage.error('删除失败,'+e.message)
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -117,11 +190,6 @@ export default defineComponent({
|
||||
|
||||
.api-key-box {
|
||||
padding-bottom: 10px;
|
||||
justify-content: end;
|
||||
|
||||
.el-icon--right {
|
||||
margin-right 5px;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user