mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-09-17 16:56:38 +08:00
后台管理功能已完成
This commit is contained in:
parent
868ddc1f37
commit
40bf2b5c1b
@ -18,25 +18,27 @@ func (s *Server) TestHandle(c *gin.Context) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) ConfigGetHandle(c *gin.Context) {
|
func (s *Server) ConfigGetHandle(c *gin.Context) {
|
||||||
data := struct {
|
data := struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
ConsoleTitle string `json:"console_title"`
|
ConsoleTitle string `json:"console_title"`
|
||||||
ProxyURL string `json:"proxy_url"`
|
ProxyURL string `json:"proxy_url"`
|
||||||
Model string `json:"model"`
|
Model string `json:"model"`
|
||||||
Temperature float32 `json:"temperature"`
|
Temperature float32 `json:"temperature"`
|
||||||
MaxTokens int `json:"max_tokens"`
|
MaxTokens int `json:"max_tokens"`
|
||||||
ChatContextExpireTime int `json:"chat_context_expire_time"`
|
ChatContextExpireTime int `json:"chat_context_expire_time"`
|
||||||
EnableContext bool `json:"enable_context"`
|
EnableContext bool `json:"enable_context"`
|
||||||
|
ImgURL types.ImgURL `json:"img_url"`
|
||||||
}{
|
}{
|
||||||
Title: s.Config.Title,
|
Title: s.Config.Title,
|
||||||
ConsoleTitle: s.Config.ConsoleTitle,
|
ConsoleTitle: s.Config.ConsoleTitle,
|
||||||
ProxyURL: strings.Join(s.Config.ProxyURL, ","),
|
ProxyURL: strings.Join(s.Config.ProxyURL, ","),
|
||||||
Model: s.Config.Chat.Model,
|
Model: s.Config.Chat.Model,
|
||||||
Temperature: s.Config.Chat.Temperature,
|
Temperature: s.Config.Chat.Temperature,
|
||||||
MaxTokens: s.Config.Chat.MaxTokens,
|
MaxTokens: s.Config.Chat.MaxTokens,
|
||||||
EnableContext: s.Config.Chat.EnableContext,
|
EnableContext: s.Config.Chat.EnableContext,
|
||||||
ChatContextExpireTime: s.Config.Chat.ChatContextExpireTime,
|
ChatContextExpireTime: s.Config.Chat.ChatContextExpireTime,
|
||||||
|
ImgURL: s.Config.ImgURL,
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Data: data})
|
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Data: data})
|
||||||
@ -45,14 +47,15 @@ func (s *Server) ConfigGetHandle(c *gin.Context) {
|
|||||||
// ConfigSetHandle set configs
|
// ConfigSetHandle set configs
|
||||||
func (s *Server) ConfigSetHandle(c *gin.Context) {
|
func (s *Server) ConfigSetHandle(c *gin.Context) {
|
||||||
var data struct {
|
var data struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
ConsoleTitle string `json:"console_title"`
|
ConsoleTitle string `json:"console_title"`
|
||||||
ProxyURL string `json:"proxy_url"`
|
ProxyURL string `json:"proxy_url"`
|
||||||
Model string `json:"model"`
|
Model string `json:"model"`
|
||||||
Temperature float32 `json:"temperature"`
|
Temperature float32 `json:"temperature"`
|
||||||
MaxTokens int `json:"max_tokens"`
|
MaxTokens int `json:"max_tokens"`
|
||||||
ChatContextExpireTime int `json:"chat_context_expire_time"`
|
ChatContextExpireTime int `json:"chat_context_expire_time"`
|
||||||
EnableContext bool `json:"enable_context"`
|
EnableContext bool `json:"enable_context"`
|
||||||
|
ImgURL types.ImgURL `json:"img_url"`
|
||||||
}
|
}
|
||||||
err := json.NewDecoder(c.Request.Body).Decode(&data)
|
err := json.NewDecoder(c.Request.Body).Decode(&data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -63,12 +66,17 @@ func (s *Server) ConfigSetHandle(c *gin.Context) {
|
|||||||
|
|
||||||
s.Config.Title = data.Title
|
s.Config.Title = data.Title
|
||||||
s.Config.ConsoleTitle = data.ConsoleTitle
|
s.Config.ConsoleTitle = data.ConsoleTitle
|
||||||
s.Config.ProxyURL = strings.Split(data.ProxyURL, ",")
|
urls := strings.Split(data.ProxyURL, ",")
|
||||||
|
for k, v := range urls {
|
||||||
|
urls[k] = strings.TrimSpace(v)
|
||||||
|
}
|
||||||
|
s.Config.ProxyURL = urls
|
||||||
s.Config.Chat.Model = data.Model
|
s.Config.Chat.Model = data.Model
|
||||||
s.Config.Chat.Temperature = data.Temperature
|
s.Config.Chat.Temperature = data.Temperature
|
||||||
s.Config.Chat.MaxTokens = data.MaxTokens
|
s.Config.Chat.MaxTokens = data.MaxTokens
|
||||||
s.Config.Chat.EnableContext = data.EnableContext
|
s.Config.Chat.EnableContext = data.EnableContext
|
||||||
s.Config.Chat.ChatContextExpireTime = data.ChatContextExpireTime
|
s.Config.Chat.ChatContextExpireTime = data.ChatContextExpireTime
|
||||||
|
s.Config.ImgURL = data.ImgURL
|
||||||
|
|
||||||
// 保存配置文件
|
// 保存配置文件
|
||||||
err = utils.SaveConfig(s.Config, s.ConfigPath)
|
err = utils.SaveConfig(s.Config, s.ConfigPath)
|
||||||
|
@ -404,7 +404,7 @@ func (s *Server) GetChatHistoryHandle(c *gin.Context) {
|
|||||||
|
|
||||||
history, err := GetChatHistory(session.Username, data.Role)
|
history, err := GetChatHistory(session.Username, data.Role)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "No history message"})
|
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Data: nil, Message: "No history message"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
118
server/handler_login.go
Normal file
118
server/handler_login.go
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/gin-contrib/sessions"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"net/http"
|
||||||
|
"openai/types"
|
||||||
|
"openai/utils"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Server) LoginHandle(c *gin.Context) {
|
||||||
|
var data struct {
|
||||||
|
Token string `json:"token"`
|
||||||
|
}
|
||||||
|
err := json.NewDecoder(c.Request.Body).Decode(&data)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: types.ErrorMsg})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
username := strings.TrimSpace(data.Token)
|
||||||
|
user, err := GetUser(username)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Invalid user"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionId := utils.RandString(42)
|
||||||
|
session := sessions.Default(c)
|
||||||
|
session.Set(sessionId, username)
|
||||||
|
err = session.Save()
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Error for save session: ", err)
|
||||||
|
}
|
||||||
|
// 记录客户端 IP 地址
|
||||||
|
s.ChatSession[sessionId] = types.ChatSession{ClientIP: c.ClientIP(), Username: username, SessionId: sessionId}
|
||||||
|
// 更新用户激活时间
|
||||||
|
user.ActiveTime = time.Now().Unix()
|
||||||
|
if user.ExpiredTime == 0 {
|
||||||
|
activeTime := time.Unix(user.ActiveTime, 0)
|
||||||
|
if user.Term == 0 {
|
||||||
|
user.Term = 30 // 默认 30 天到期
|
||||||
|
}
|
||||||
|
user.ExpiredTime = activeTime.Add(time.Hour * 24 * time.Duration(user.Term)).Unix()
|
||||||
|
}
|
||||||
|
err = PutUser(*user)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Save user info failed"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Data: struct {
|
||||||
|
User types.User `json:"user"`
|
||||||
|
SessionId string `json:"session_id"`
|
||||||
|
}{User: *user, SessionId: sessionId}})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ManagerLoginHandle 管理员登录
|
||||||
|
func (s *Server) ManagerLoginHandle(c *gin.Context) {
|
||||||
|
var data struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
err := json.NewDecoder(c.Request.Body).Decode(&data)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: types.ErrorMsg})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
username := strings.TrimSpace(data.Username)
|
||||||
|
password := strings.TrimSpace(data.Password)
|
||||||
|
if username == s.Config.Manager.Username && password == s.Config.Manager.Password {
|
||||||
|
sessionId := utils.RandString(42)
|
||||||
|
session := sessions.Default(c)
|
||||||
|
session.Set(sessionId, username)
|
||||||
|
err = session.Save()
|
||||||
|
// 记录登录信息
|
||||||
|
s.ChatSession[sessionId] = types.ChatSession{ClientIP: c.ClientIP(), Username: username, SessionId: sessionId}
|
||||||
|
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Data: struct {
|
||||||
|
User types.Manager `json:"user"`
|
||||||
|
SessionId string `json:"session_id"`
|
||||||
|
}{User: data, SessionId: sessionId}})
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "用户名或者密码错误"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogoutHandle 注销
|
||||||
|
func (s *Server) LogoutHandle(c *gin.Context) {
|
||||||
|
sessionId := c.GetHeader(types.TokenName)
|
||||||
|
session := sessions.Default(c)
|
||||||
|
session.Delete(sessionId)
|
||||||
|
err := session.Save()
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Error for save session: ", err)
|
||||||
|
}
|
||||||
|
// 删除 websocket 会话列表
|
||||||
|
delete(s.ChatSession, sessionId)
|
||||||
|
// 关闭 socket 连接
|
||||||
|
if client, ok := s.ChatClients[sessionId]; ok {
|
||||||
|
client.Close()
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, types.BizVo{Code: types.Success})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) GetSessionHandle(c *gin.Context) {
|
||||||
|
sessionId := c.GetHeader(types.TokenName)
|
||||||
|
if session, ok := s.ChatSession[sessionId]; ok && session.ClientIP == c.ClientIP() {
|
||||||
|
c.JSON(http.StatusOK, types.BizVo{Code: types.Success})
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusOK, types.BizVo{
|
||||||
|
Code: types.NotAuthorized,
|
||||||
|
Message: "Not Authorized",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
164
server/server.go
164
server/server.go
@ -3,7 +3,6 @@ package server
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"embed"
|
"embed"
|
||||||
"encoding/json"
|
|
||||||
"github.com/gin-contrib/sessions"
|
"github.com/gin-contrib/sessions"
|
||||||
"github.com/gin-contrib/sessions/cookie"
|
"github.com/gin-contrib/sessions/cookie"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -96,10 +95,10 @@ func (s *Server) Run(webRoot embed.FS, path string, debug bool) {
|
|||||||
engine.POST("api/img/get", s.GetImgURLHandle)
|
engine.POST("api/img/get", s.GetImgURLHandle)
|
||||||
engine.POST("api/img/set", s.SetImgURLHandle)
|
engine.POST("api/img/set", s.SetImgURLHandle)
|
||||||
|
|
||||||
engine.GET("api/admin/config/get", s.ConfigGetHandle)
|
engine.GET("api/config/get", s.ConfigGetHandle)
|
||||||
engine.POST("api/admin/config/set", s.ConfigSetHandle)
|
engine.POST("api/admin/config/set", s.ConfigSetHandle)
|
||||||
|
|
||||||
engine.POST("api/chat-roles/list", s.GetChatRoleListHandle)
|
engine.GET("api/chat-roles/list", s.GetChatRoleListHandle)
|
||||||
engine.POST("api/admin/chat-roles/list", s.GetAllChatRolesHandle)
|
engine.POST("api/admin/chat-roles/list", s.GetAllChatRolesHandle)
|
||||||
engine.POST("api/chat-roles/get", s.GetChatRoleHandle)
|
engine.POST("api/chat-roles/get", s.GetChatRoleHandle)
|
||||||
engine.POST("api/admin/chat-roles/add", s.AddChatRoleHandle)
|
engine.POST("api/admin/chat-roles/add", s.AddChatRoleHandle)
|
||||||
@ -110,6 +109,7 @@ func (s *Server) Run(webRoot embed.FS, path string, debug bool) {
|
|||||||
engine.POST("api/admin/user/set", s.SetUserHandle)
|
engine.POST("api/admin/user/set", s.SetUserHandle)
|
||||||
engine.POST("api/admin/user/list", s.GetUserListHandle)
|
engine.POST("api/admin/user/list", s.GetUserListHandle)
|
||||||
engine.POST("api/admin/user/remove", s.RemoveUserHandle)
|
engine.POST("api/admin/user/remove", s.RemoveUserHandle)
|
||||||
|
engine.POST("api/admin/login", s.ManagerLoginHandle) // 管理员登录
|
||||||
|
|
||||||
engine.POST("api/admin/apikey/add", s.AddApiKeyHandle)
|
engine.POST("api/admin/apikey/add", s.AddApiKeyHandle)
|
||||||
engine.POST("api/admin/apikey/remove", s.RemoveApiKeyHandle)
|
engine.POST("api/admin/apikey/remove", s.RemoveApiKeyHandle)
|
||||||
@ -219,128 +219,54 @@ func corsMiddleware() gin.HandlerFunc {
|
|||||||
// AuthorizeMiddleware 用户授权验证
|
// AuthorizeMiddleware 用户授权验证
|
||||||
func AuthorizeMiddleware(s *Server) gin.HandlerFunc {
|
func AuthorizeMiddleware(s *Server) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
c.Next()
|
if c.Request.URL.Path == "/api/login" ||
|
||||||
//if !s.Config.EnableAuth ||
|
c.Request.URL.Path == "/api/admin/login" ||
|
||||||
// c.Request.URL.Path == "/api/login" ||
|
c.Request.URL.Path == "/api/chat-roles/list" ||
|
||||||
// c.Request.URL.Path == "/api/config/chat-roles/get" ||
|
!strings.HasPrefix(c.Request.URL.Path, "/api") {
|
||||||
// !strings.HasPrefix(c.Request.URL.Path, "/api") {
|
c.Next()
|
||||||
// c.Next()
|
return
|
||||||
// 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",
|
|
||||||
// })
|
|
||||||
//}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) GetSessionHandle(c *gin.Context) {
|
|
||||||
sessionId := c.GetHeader(types.TokenName)
|
|
||||||
if session, ok := s.ChatSession[sessionId]; ok && session.ClientIP == c.ClientIP() {
|
|
||||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Success})
|
|
||||||
} else {
|
|
||||||
c.JSON(http.StatusOK, types.BizVo{
|
|
||||||
Code: types.NotAuthorized,
|
|
||||||
Message: "Not Authorized",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) LoginHandle(c *gin.Context) {
|
|
||||||
var data struct {
|
|
||||||
Token string `json:"token"`
|
|
||||||
}
|
|
||||||
err := json.NewDecoder(c.Request.Body).Decode(&data)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: types.ErrorMsg})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
username := strings.TrimSpace(data.Token)
|
|
||||||
user, err := GetUser(username)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Invalid user"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
sessionId := utils.RandString(42)
|
|
||||||
session := sessions.Default(c)
|
|
||||||
session.Set(sessionId, username)
|
|
||||||
err = session.Save()
|
|
||||||
if err != nil {
|
|
||||||
logger.Error("Error for save session: ", err)
|
|
||||||
}
|
|
||||||
// 记录客户端 IP 地址
|
|
||||||
s.ChatSession[sessionId] = types.ChatSession{ClientIP: c.ClientIP(), Username: username, SessionId: sessionId}
|
|
||||||
// 更新用户激活时间
|
|
||||||
user.ActiveTime = time.Now().Unix()
|
|
||||||
if user.ExpiredTime == 0 {
|
|
||||||
activeTime := time.Unix(user.ActiveTime, 0)
|
|
||||||
if user.Term == 0 {
|
|
||||||
user.Term = 30 // 默认 30 天到期
|
|
||||||
}
|
}
|
||||||
user.ExpiredTime = activeTime.Add(time.Hour * 24 * time.Duration(user.Term)).Unix()
|
|
||||||
}
|
|
||||||
err = PutUser(*user)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Save user info failed"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Data: struct {
|
if strings.HasPrefix(c.Request.URL.Path, "/api/admin") {
|
||||||
User types.User `json:"user"`
|
accessKey := c.GetHeader("ACCESS-KEY")
|
||||||
SessionId string `json:"session_id"`
|
if accessKey == strings.TrimSpace(s.Config.AccessKey) {
|
||||||
}{User: *user, SessionId: sessionId}})
|
c.Next()
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
// 验证当前登录用户是否是管理员
|
||||||
|
sessionId := c.GetHeader(types.TokenName)
|
||||||
|
if m, ok := s.ChatSession[sessionId]; ok && m.Username == s.Config.Manager.Username {
|
||||||
|
c.Next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// LogoutHandle 注销
|
c.Abort()
|
||||||
func (s *Server) LogoutHandle(c *gin.Context) {
|
c.JSON(http.StatusOK, types.BizVo{Code: types.NotAuthorized, Message: "No Permissions"})
|
||||||
var data struct {
|
}
|
||||||
Opt string `json:"opt"`
|
|
||||||
}
|
// WebSocket 连接请求验证
|
||||||
err := json.NewDecoder(c.Request.Body).Decode(&data)
|
if c.Request.URL.Path == "/api/chat" {
|
||||||
if err != nil {
|
sessionId := c.Query("sessionId")
|
||||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: types.ErrorMsg})
|
if session, ok := s.ChatSession[sessionId]; ok && session.ClientIP == c.ClientIP() {
|
||||||
return
|
c.Next()
|
||||||
}
|
} else {
|
||||||
|
c.Abort()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if data.Opt == "logout" {
|
|
||||||
sessionId := c.GetHeader(types.TokenName)
|
sessionId := c.GetHeader(types.TokenName)
|
||||||
session := sessions.Default(c)
|
session := sessions.Default(c)
|
||||||
session.Delete(sessionId)
|
userInfo := session.Get(sessionId)
|
||||||
err := session.Save()
|
if userInfo != nil {
|
||||||
if err != nil {
|
c.Set(types.SessionKey, userInfo)
|
||||||
logger.Error("Error for save session: ", err)
|
c.Next()
|
||||||
|
} else {
|
||||||
|
c.Abort()
|
||||||
|
c.JSON(http.StatusOK, types.BizVo{
|
||||||
|
Code: types.NotAuthorized,
|
||||||
|
Message: "Not Authorized",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
// 删除 websocket 会话列表
|
|
||||||
delete(s.ChatSession, sessionId)
|
|
||||||
// 关闭 socket 连接
|
|
||||||
if client, ok := s.ChatClients[sessionId]; ok {
|
|
||||||
client.Close()
|
|
||||||
}
|
|
||||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Success})
|
|
||||||
} else {
|
|
||||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Hack attempt!"})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ type ChatSession struct {
|
|||||||
SessionId string `json:"session_id"`
|
SessionId string `json:"session_id"`
|
||||||
ClientIP string `json:"client_ip"` // 客户端 IP
|
ClientIP string `json:"client_ip"` // 客户端 IP
|
||||||
Username string `json:"user"` // 当前登录的 user
|
Username string `json:"user"` // 当前登录的 user
|
||||||
ChatId string `json:"chat_id"` // 客户端聊天会话 ID
|
ChatId string `json:"chat_id"` // 客户端聊天会话 ID, 多会话模式专用字段
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChatContext 聊天上下文
|
// ChatContext 聊天上下文
|
||||||
|
@ -10,8 +10,10 @@ type Config struct {
|
|||||||
Listen string
|
Listen string
|
||||||
Session Session
|
Session Session
|
||||||
ProxyURL []string
|
ProxyURL []string
|
||||||
|
ImgURL ImgURL // 各种图片资源链接地址,比如微信二维码,群二维码
|
||||||
|
AccessKey string // 管理员访问 AccessKey, 通过传入这个参数可以访问系统管理 API
|
||||||
|
Manager Manager // 后台管理员账户信息
|
||||||
Chat Chat
|
Chat Chat
|
||||||
ImgURL ImgURL // 各种图片资源链接地址,比如微信二维码,群二维码
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
@ -45,8 +47,8 @@ type Chat struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type APIKey struct {
|
type APIKey struct {
|
||||||
Value string `json:"value"` // Key value
|
Value string `json:"value"` // Key value
|
||||||
LastUsed int64 `json:"last_used"` // 最后使用时间
|
LastUsed int64 `json:"last_used"` // 最后使用时间
|
||||||
}
|
}
|
||||||
|
|
||||||
// Session configs struct
|
// Session configs struct
|
||||||
|
@ -16,6 +16,8 @@ func NewDefaultConfig() *types.Config {
|
|||||||
Listen: "0.0.0.0:5678",
|
Listen: "0.0.0.0:5678",
|
||||||
ProxyURL: make([]string, 0),
|
ProxyURL: make([]string, 0),
|
||||||
ImgURL: types.ImgURL{},
|
ImgURL: types.ImgURL{},
|
||||||
|
Manager: types.Manager{Username: "admin", Password: "admin123"},
|
||||||
|
AccessKey: "yangjian102621@gmail.com",
|
||||||
|
|
||||||
Session: types.Session{
|
Session: types.Session{
|
||||||
SecretKey: RandString(64),
|
SecretKey: RandString(64),
|
||||||
|
@ -110,3 +110,8 @@ export function renderInputText(text) {
|
|||||||
text = text || '';
|
text = text || '';
|
||||||
return text.replace(replaceRegex, "<br/>");
|
return text.replace(replaceRegex, "<br/>");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 拷贝对象
|
||||||
|
export function copyObj(origin) {
|
||||||
|
return JSON.parse(JSON.stringify(origin));
|
||||||
|
}
|
@ -43,6 +43,10 @@
|
|||||||
<span v-if="showSidebar">{{ nav.title }}</span>
|
<span v-if="showSidebar">{{ nav.title }}</span>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
<el-row class="tool-box">
|
||||||
|
<el-button type="primary" plain @click="logout" v-show="isLogin">退出登录</el-button>
|
||||||
|
</el-row>
|
||||||
</el-aside>
|
</el-aside>
|
||||||
|
|
||||||
<el-main>
|
<el-main>
|
||||||
@ -86,7 +90,7 @@
|
|||||||
<el-form :model="user" label-width="80px">
|
<el-form :model="user" label-width="80px">
|
||||||
<el-form-item label="用户名:">
|
<el-form-item label="用户名:">
|
||||||
<el-input
|
<el-input
|
||||||
v-model="user.username"
|
v-model.trim="user.username"
|
||||||
autocomplete="off"
|
autocomplete="off"
|
||||||
placeholder="请输入用户名"
|
placeholder="请输入用户名"
|
||||||
/>
|
/>
|
||||||
@ -94,10 +98,11 @@
|
|||||||
|
|
||||||
<el-form-item label="密码:">
|
<el-form-item label="密码:">
|
||||||
<el-input
|
<el-input
|
||||||
v-model="user.password"
|
v-model.trim="user.password"
|
||||||
autocomplete="off"
|
autocomplete="off"
|
||||||
type="password"
|
type="password"
|
||||||
placeholder="请输入密码"
|
placeholder="请输入密码"
|
||||||
|
@keyup="loginInputKeyup"
|
||||||
/>
|
/>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-form>
|
</el-form>
|
||||||
@ -105,7 +110,7 @@
|
|||||||
<template #footer>
|
<template #footer>
|
||||||
<span class="dialog-footer">
|
<span class="dialog-footer">
|
||||||
<el-button @click="showDialog = false">取消</el-button>
|
<el-button @click="showDialog = false">取消</el-button>
|
||||||
<el-button type="primary" @click="saveHost">提交</el-button>
|
<el-button type="primary" @click="login">提交</el-button>
|
||||||
</span>
|
</span>
|
||||||
</template>
|
</template>
|
||||||
</el-dialog>
|
</el-dialog>
|
||||||
@ -122,6 +127,9 @@ import SysConfig from "@/views/admin/SysConfig.vue";
|
|||||||
import {arrayContains, removeArrayItem} from "@/utils/libs";
|
import {arrayContains, removeArrayItem} from "@/utils/libs";
|
||||||
import UserList from "@/views/admin/UserList.vue";
|
import UserList from "@/views/admin/UserList.vue";
|
||||||
import RoleList from "@/views/admin/RoleList.vue";
|
import RoleList from "@/views/admin/RoleList.vue";
|
||||||
|
import {httpGet, httpPost} from "@/utils/http";
|
||||||
|
import {ElMessage} from "element-plus";
|
||||||
|
import {setLoginUser} from "@/utils/storage";
|
||||||
|
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
@ -156,6 +164,7 @@ export default defineComponent({
|
|||||||
curNav: null,
|
curNav: null,
|
||||||
curTab: 'welcome',
|
curTab: 'welcome',
|
||||||
tabs: [],
|
tabs: [],
|
||||||
|
isLogin: false,
|
||||||
|
|
||||||
showDialog: false,
|
showDialog: false,
|
||||||
|
|
||||||
@ -180,14 +189,54 @@ export default defineComponent({
|
|||||||
},
|
},
|
||||||
|
|
||||||
mounted: function () {
|
mounted: function () {
|
||||||
|
|
||||||
// bind window resize event
|
// bind window resize event
|
||||||
window.addEventListener("resize", function () {
|
window.addEventListener("resize", function () {
|
||||||
this.winHeight = window.innerHeight
|
this.winHeight = window.innerHeight
|
||||||
})
|
})
|
||||||
|
this.checkSession()
|
||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
|
checkSession: function () {
|
||||||
|
httpGet("/api/session/get").then(() => {
|
||||||
|
this.isLogin = true
|
||||||
|
}).catch(() => {
|
||||||
|
this.showDialog = true
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
|
// 登录输入框输入事件处理
|
||||||
|
loginInputKeyup: function (e) {
|
||||||
|
if (e.keyCode === 13) {
|
||||||
|
this.login();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// 登录
|
||||||
|
login: function () {
|
||||||
|
if (!this.user.username || !this.user.password) {
|
||||||
|
ElMessage.error('请输入用户名和密码')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
httpPost('/api/admin/login', this.user).then((res) => {
|
||||||
|
setLoginUser(res.data)
|
||||||
|
this.showDialog = false
|
||||||
|
this.isLogin = true
|
||||||
|
this.user = {}
|
||||||
|
}).catch((e) => {
|
||||||
|
ElMessage.error('登录失败,' + e.message)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
|
logout: function () {
|
||||||
|
httpPost("/api/logout", {opt: "logout"}).then(() => {
|
||||||
|
this.checkSession();
|
||||||
|
this.isLogin = false
|
||||||
|
}).catch(() => {
|
||||||
|
ElMessage.error("注销失败");
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
arrayContains(array, value, compare) {
|
arrayContains(array, value, compare) {
|
||||||
return arrayContains(array, value, compare);
|
return arrayContains(array, value, compare);
|
||||||
},
|
},
|
||||||
@ -315,6 +364,12 @@ $borderColor = #4676d0;
|
|||||||
background-color: #363535
|
background-color: #363535
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.tool-box {
|
||||||
|
display flex
|
||||||
|
justify-content center
|
||||||
|
padding 10px 20px;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.el-main {
|
.el-main {
|
||||||
|
@ -222,6 +222,7 @@ export default defineComponent({
|
|||||||
allChatRoles: [], // 所有角色集合
|
allChatRoles: [], // 所有角色集合
|
||||||
role: 'gpt',
|
role: 'gpt',
|
||||||
inputValue: '', // 聊天内容
|
inputValue: '', // 聊天内容
|
||||||
|
sendHelloMsg: {}, // 是否发送过打招呼信息
|
||||||
|
|
||||||
showConfigDialog: false, // 显示配置对话框
|
showConfigDialog: false, // 显示配置对话框
|
||||||
userInfo: {},
|
userInfo: {},
|
||||||
@ -299,7 +300,7 @@ export default defineComponent({
|
|||||||
socket.addEventListener('open', () => {
|
socket.addEventListener('open', () => {
|
||||||
// 获取聊天角色
|
// 获取聊天角色
|
||||||
if (this.chatRoles.length === 0) {
|
if (this.chatRoles.length === 0) {
|
||||||
httpGet("/api/config/chat-roles/get").then((res) => {
|
httpGet("/api/chat-roles/list").then((res) => {
|
||||||
// ElMessage.success('创建会话成功!');
|
// ElMessage.success('创建会话成功!');
|
||||||
this.chatRoles = res.data;
|
this.chatRoles = res.data;
|
||||||
this.allChatRoles = res.data;
|
this.allChatRoles = res.data;
|
||||||
@ -326,6 +327,10 @@ export default defineComponent({
|
|||||||
reader.readAsText(event.data, "UTF-8");
|
reader.readAsText(event.data, "UTF-8");
|
||||||
reader.onload = () => {
|
reader.onload = () => {
|
||||||
const data = JSON.parse(String(reader.result));
|
const data = JSON.parse(String(reader.result));
|
||||||
|
if (data['is_hello_msg'] && this.sendHelloMsg[this.role]) { // 一定发送过打招呼信息的
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (data.type === 'start') {
|
if (data.type === 'start') {
|
||||||
this.chatData.push({
|
this.chatData.push({
|
||||||
type: "reply",
|
type: "reply",
|
||||||
@ -340,6 +345,8 @@ export default defineComponent({
|
|||||||
this.sending = false;
|
this.sending = false;
|
||||||
if (data['is_hello_msg'] !== true) {
|
if (data['is_hello_msg'] !== true) {
|
||||||
this.showReGenerate = true;
|
this.showReGenerate = true;
|
||||||
|
} else {
|
||||||
|
this.sendHelloMsg[this.role] = true
|
||||||
}
|
}
|
||||||
this.showStopGenerate = false;
|
this.showStopGenerate = false;
|
||||||
this.lineBuffer = ''; // 清空缓冲
|
this.lineBuffer = ''; // 清空缓冲
|
||||||
|
@ -37,7 +37,7 @@
|
|||||||
<el-table-column label="打招呼信息" prop="hello_msg"/>
|
<el-table-column label="打招呼信息" prop="hello_msg"/>
|
||||||
<el-table-column label="操作" width="80" align="right">
|
<el-table-column label="操作" width="80" align="right">
|
||||||
<template #default="scope">
|
<template #default="scope">
|
||||||
<el-button size="small" type="primary" @click="rowEdit(scope.row)">编辑</el-button>
|
<el-button size="small" type="primary" @click="rowEdit(scope.$index, scope.row)">编辑</el-button>
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
</el-table>
|
</el-table>
|
||||||
@ -47,8 +47,6 @@
|
|||||||
v-model="showDialog"
|
v-model="showDialog"
|
||||||
title="编辑用户"
|
title="编辑用户"
|
||||||
width="50%"
|
width="50%"
|
||||||
:destroy-on-close="true"
|
|
||||||
|
|
||||||
>
|
>
|
||||||
<el-form :model="form1" label-width="120px" ref="formRef" :rules="rules">
|
<el-form :model="form1" label-width="120px" ref="formRef" :rules="rules">
|
||||||
<el-form-item label="角色名称:" prop="name">
|
<el-form-item label="角色名称:" prop="name">
|
||||||
@ -140,6 +138,7 @@ import {Plus, RemoveFilled} from "@element-plus/icons-vue";
|
|||||||
import {reactive, ref} from "vue";
|
import {reactive, ref} from "vue";
|
||||||
import {httpPost} from "@/utils/http";
|
import {httpPost} from "@/utils/http";
|
||||||
import {ElMessage} from "element-plus";
|
import {ElMessage} from "element-plus";
|
||||||
|
import {copyObj} from "@/utils/libs";
|
||||||
|
|
||||||
const showDialog = ref(false)
|
const showDialog = ref(false)
|
||||||
const parentBorder = ref(false)
|
const parentBorder = ref(false)
|
||||||
@ -164,8 +163,10 @@ httpPost('/api/admin/chat-roles/list').then((res) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// 编辑
|
// 编辑
|
||||||
const rowEdit = function (row) {
|
const curIndex = ref(0)
|
||||||
form1.value = row
|
const rowEdit = function (index, row) {
|
||||||
|
curIndex.value = index
|
||||||
|
form1.value = copyObj(row)
|
||||||
showDialog.value = true
|
showDialog.value = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,6 +176,8 @@ const doUpdate = function () {
|
|||||||
showDialog.value = false
|
showDialog.value = false
|
||||||
httpPost('/api/admin/chat-roles/set', form1.value).then(() => {
|
httpPost('/api/admin/chat-roles/set', form1.value).then(() => {
|
||||||
ElMessage.success('更新角色成功')
|
ElMessage.success('更新角色成功')
|
||||||
|
// 更新当前数据行
|
||||||
|
tableData.value[curIndex.value] = form1.value
|
||||||
}).catch((e) => {
|
}).catch((e) => {
|
||||||
ElMessage.error('更新角色失败,' + e.message)
|
ElMessage.error('更新角色失败,' + e.message)
|
||||||
})
|
})
|
||||||
|
@ -11,6 +11,14 @@
|
|||||||
<el-input v-model="form['proxy_url']" placeholder="多个地址之间用逗号隔开"/>
|
<el-input v-model="form['proxy_url']" placeholder="多个地址之间用逗号隔开"/>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
|
|
||||||
|
<el-form-item label="微信群聊二维码">
|
||||||
|
<el-input v-model="form['img_url']['wechat_group']" placeholder="群聊二维码地址"/>
|
||||||
|
</el-form-item>
|
||||||
|
|
||||||
|
<el-form-item label="个人微信名片">
|
||||||
|
<el-input v-model="form['img_url']['wechat_card']" placeholder="名片二维码地址"/>
|
||||||
|
</el-form-item>
|
||||||
|
|
||||||
<el-divider content-position="center">聊天设置</el-divider>
|
<el-divider content-position="center">聊天设置</el-divider>
|
||||||
<el-row>
|
<el-row>
|
||||||
<el-col :span="12">
|
<el-col :span="12">
|
||||||
@ -124,14 +132,14 @@ export default defineComponent({
|
|||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
apiKey: '',
|
apiKey: '',
|
||||||
form: {},
|
form: {img_url: {}},
|
||||||
apiKeys: [],
|
apiKeys: [],
|
||||||
loading: true
|
loading: true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
// 获取系统配置
|
// 获取系统配置
|
||||||
httpGet('/api/admin/config/get').then((res) => {
|
httpGet('/api/config/get').then((res) => {
|
||||||
this.form = res.data;
|
this.form = res.data;
|
||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
ElMessage.error('获取系统配置失败')
|
ElMessage.error('获取系统配置失败')
|
||||||
|
@ -190,8 +190,6 @@
|
|||||||
v-model="showUserEditDialog"
|
v-model="showUserEditDialog"
|
||||||
title="编辑用户"
|
title="编辑用户"
|
||||||
width="50%"
|
width="50%"
|
||||||
:destroy-on-close="true"
|
|
||||||
|
|
||||||
>
|
>
|
||||||
<el-form :model="form2" label-width="100px" ref="userEditFormRef" :rules="rules">
|
<el-form :model="form2" label-width="100px" ref="userEditFormRef" :rules="rules">
|
||||||
<el-form-item label="用户名:" prop="name">
|
<el-form-item label="用户名:" prop="name">
|
||||||
|
Loading…
Reference in New Issue
Block a user