添加微信和知识星球的推广二维码

This commit is contained in:
RockYang
2023-03-29 14:27:04 +08:00
parent eb562ea3c9
commit bd41f742d2
8 changed files with 48 additions and 17 deletions

View File

@@ -17,7 +17,7 @@ import (
"time"
)
const ErrorMsg = "抱歉AI 助手开小差了,马上找人去盘它。"
const ErrorMsg = "抱歉AI 助手开小差了,马上联系管理员去盘它。"
// ChatHandle 处理聊天 WebSocket 请求
func (s *Server) ChatHandle(c *gin.Context) {
@@ -70,7 +70,8 @@ func (s *Server) sendMessage(session types.ChatSession, role types.ChatRole, pro
}
if user.MaxCalls > 0 && user.RemainingCalls <= 0 {
replyMessage(ws, "当前 user 点数已经用尽,请充值后再使用或者联系管理员", false)
replyMessage(ws, "当前 TOKEN 点数已经用尽,加入我们的知识星球可以免费领取点卡", false)
replyMessage(ws, "![](images/start.png)", true)
return nil
}
var r = types.ApiRequest{
@@ -148,6 +149,7 @@ func (s *Server) sendMessage(session types.ChatSession, role types.ChatRole, pro
// 如果三次请求都失败的话,则返回对应的错误信息
if err != nil {
replyMessage(ws, ErrorMsg, false)
replyMessage(ws, "![](images/wx.png)", true)
return err
}

View File

@@ -24,6 +24,10 @@ func (s *Server) ConfigSetHandle(c *gin.Context) {
s.Config.Chat.Model = model
}
if accessKey, ok := data["access_key"]; ok {
s.Config.AccessKey = accessKey
}
// Temperature
if temperature, ok := data["temperature"]; ok {
v, err := strconv.ParseFloat(temperature, 32)
@@ -148,8 +152,9 @@ func (s *Server) AddUserHandle(c *gin.Context) {
// BatchAddUserHandle 批量生成 Username
func (s *Server) BatchAddUserHandle(c *gin.Context) {
var data struct {
Number int `json:"number"`
MaxCalls int `json:"max_calls"`
Number int `json:"number"`
MaxCalls int `json:"max_calls"`
EnableHistory bool `json:"enable_history"`
}
err := json.NewDecoder(c.Request.Body).Decode(&data)
if err != nil || data.MaxCalls <= 0 {
@@ -164,7 +169,7 @@ func (s *Server) BatchAddUserHandle(c *gin.Context) {
for err == nil {
name = utils.RandString(12)
}
err = PutUser(types.User{Name: name, MaxCalls: data.MaxCalls, RemainingCalls: data.MaxCalls})
err = PutUser(types.User{Name: name, MaxCalls: data.MaxCalls, RemainingCalls: data.MaxCalls, EnableHistory: data.EnableHistory})
if err == nil {
users = append(users, name)
}
@@ -226,6 +231,11 @@ func (s *Server) RemoveUserHandle(c *gin.Context) {
return
}
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg})
}
// GetUserListHandle 获取用户列表
func (s *Server) GetUserListHandle(c *gin.Context) {
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: GetUsers()})
}

View File

@@ -91,6 +91,7 @@ func (s *Server) Run(webRoot embed.FS, path string, debug bool) {
engine.POST("api/config/user/add", s.AddUserHandle)
engine.POST("api/config/user/batch-add", s.BatchAddUserHandle)
engine.POST("api/config/user/set", s.SetUserHandle)
engine.POST("api/config/user/list", s.GetUserListHandle)
engine.POST("api/config/user/remove", s.RemoveUserHandle)
engine.POST("api/config/apikey/add", s.AddApiKeyHandle)
engine.POST("api/config/apikey/remove", s.RemoveApiKeyHandle)
@@ -173,7 +174,7 @@ func corsMiddleware() gin.HandlerFunc {
c.Header("Access-Control-Allow-Origin", origin)
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
//允许跨域设置可以返回其他子段,可以自定义字段
c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, Content-Type, ChatGPT-TOKEN")
c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, Content-Type, ChatGPT-TOKEN, ACCESS_KEY")
// 允许浏览器(客户端)可以解析的头部 (重要)
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers")
//设置缓存时间
@@ -209,7 +210,7 @@ func AuthorizeMiddleware(s *Server) gin.HandlerFunc {
if strings.HasPrefix(c.Request.URL.Path, "/api/config") {
accessKey := c.GetHeader("ACCESS_KEY")
if accessKey != s.Config.AccessKey {
if accessKey != strings.TrimSpace(s.Config.AccessKey) {
c.Abort()
c.JSON(http.StatusOK, types.BizVo{Code: types.NotAuthorized, Message: "No Permissions"})
} else {