From 4936896ff7fc58cff9c6d3f02d97949867148885 Mon Sep 17 00:00:00 2001 From: RockYang Date: Sat, 23 Dec 2023 22:30:27 +0800 Subject: [PATCH] feat: function add for admin page is ready --- api/core/types/function.go | 39 ++++---------------- api/handler/admin/function_handler.go | 49 +++++++++++++++++++++++-- api/main.go | 4 ++- api/store/model/function.go | 1 + api/store/vo/function.go | 2 ++ database/update-v3.2.3.sql | 4 ++- web/src/views/admin/Functions.vue | 52 +++++++++++++++++++++------ 7 files changed, 103 insertions(+), 48 deletions(-) diff --git a/api/core/types/function.go b/api/core/types/function.go index 62237133..048c54df 100644 --- a/api/core/types/function.go +++ b/api/core/types/function.go @@ -32,46 +32,21 @@ const ( var InnerFunctions = []Function{ { Name: FuncZaoBao, - Description: "每日早报,获取当天全球的热门新闻事件列表", + Description: "每日早报,获取当天新闻事件列表", Parameters: Parameters{ - Type: "object", - Properties: map[string]Property{ - "text": { - Type: "string", - Description: "", - }, - }, - Required: []string{}, + Type: "object", + Properties: map[string]Property{}, + Required: []string{}, }, }, { Name: FuncWeibo, Description: "新浪微博热搜榜,微博当日热搜榜单", Parameters: Parameters{ - Type: "object", - Properties: map[string]Property{ - "text": { - Type: "string", - Description: "", - }, - }, - Required: []string{}, - }, - }, - - { - Name: FuncHeadLine, - Description: "今日头条,给用户推荐当天的头条新闻,周榜热文", - Parameters: Parameters{ - Type: "object", - Properties: map[string]Property{ - "text": { - Type: "string", - Description: "", - }, - }, - Required: []string{}, + Type: "object", + Properties: map[string]Property{}, + Required: []string{}, }, }, diff --git a/api/handler/admin/function_handler.go b/api/handler/admin/function_handler.go index 816161b5..59669b93 100644 --- a/api/handler/admin/function_handler.go +++ b/api/handler/admin/function_handler.go @@ -8,6 +8,7 @@ import ( "chatplus/store/vo" "chatplus/utils" "chatplus/utils/resp" + "github.com/gin-gonic/gin" "gorm.io/gorm" ) @@ -33,22 +34,64 @@ func (h *FunctionHandler) Save(c *gin.Context) { var f = model.Function{ Id: data.Id, Name: data.Name, + Label: data.Label, Description: data.Description, Parameters: utils.JsonEncode(data.Parameters), Required: utils.JsonEncode(data.Required), Action: data.Action, - Enabled: false, + Token: data.Token, + Enabled: data.Enabled, } - logger.Info(f) + res := h.db.Save(&f) + if res.Error != nil { + resp.ERROR(c, "error with save data:"+res.Error.Error()) + return + } + data.Id = f.Id resp.SUCCESS(c, data) } -func (h *FunctionHandler) List(c *gin.Context) { +func (h *FunctionHandler) Set(c *gin.Context) { + var data struct { + Id uint `json:"id"` + Filed string `json:"filed"` + Value interface{} `json:"value"` + } + if err := c.ShouldBindJSON(&data); err != nil { + resp.ERROR(c, types.InvalidArgs) + return + } + + res := h.db.Model(&model.Function{}).Where("id = ?", data.Id).Update(data.Filed, data.Value) + if res.Error != nil { + resp.ERROR(c, "更新数据库失败!") + return + } resp.SUCCESS(c) } +func (h *FunctionHandler) List(c *gin.Context) { + var items []model.Function + res := h.db.Find(&items) + if res.Error != nil { + resp.ERROR(c, "No data found") + return + } + + functions := make([]vo.Function, 0) + for _, v := range items { + var f vo.Function + err := utils.CopyObject(v, &f) + if err != nil { + continue + } + functions = append(functions, f) + } + resp.SUCCESS(c, functions) +} + func (h *FunctionHandler) Remove(c *gin.Context) { id := h.GetInt(c, "id", 0) diff --git a/api/main.go b/api/main.go index 70f10497..600cb35d 100644 --- a/api/main.go +++ b/api/main.go @@ -17,7 +17,6 @@ import ( "chatplus/store" "context" "embed" - "github.com/go-redis/redis/v8" "io" "log" "os" @@ -26,6 +25,8 @@ import ( "syscall" "time" + "github.com/go-redis/redis/v8" + "github.com/lionsoul2014/ip2region/binding/golang/xdb" "go.uber.org/fx" "gorm.io/gorm" @@ -351,6 +352,7 @@ func main() { fx.Invoke(func(s *core.AppServer, h *admin.FunctionHandler) { group := s.Engine.Group("/api/admin/function/") group.POST("save", h.Save) + group.POST("set", h.Set) group.GET("list", h.List) group.GET("remove", h.Remove) }), diff --git a/api/store/model/function.go b/api/store/model/function.go index 72f588d6..098750c5 100644 --- a/api/store/model/function.go +++ b/api/store/model/function.go @@ -8,5 +8,6 @@ type Function struct { Parameters string Required string Action string + Token string Enabled bool } diff --git a/api/store/vo/function.go b/api/store/vo/function.go index 09f1f7bc..afa1d705 100644 --- a/api/store/vo/function.go +++ b/api/store/vo/function.go @@ -14,9 +14,11 @@ type Property struct { type Function struct { Id uint `json:"id"` Name string `json:"name"` + Label string `json:"label"` Description string `json:"description"` Parameters Parameters `json:"parameters"` Required []string `json:"required"` Action string `json:"action"` + Token string `json:"token"` Enabled bool `json:"enabled"` } diff --git a/database/update-v3.2.3.sql b/database/update-v3.2.3.sql index 6a4f7635..4c5f917e 100644 --- a/database/update-v3.2.3.sql +++ b/database/update-v3.2.3.sql @@ -21,4 +21,6 @@ ALTER TABLE `chatgpt_functions` ADD `enabled` TINYINT(1) NOT NULL DEFAULT '0' CO ALTER TABLE `chatgpt_functions` ADD `label` VARCHAR(30) NULL COMMENT '函数标签' AFTER `name`; ALTER TABLE `chatgpt_mj_jobs` ADD `use_proxy` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '是否使用反代' AFTER `progress`; -ALTER TABLE `chatgpt_mj_jobs` CHANGE `img_url` `img_url` VARCHAR(400) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '图片URL'; \ No newline at end of file +ALTER TABLE `chatgpt_mj_jobs` CHANGE `img_url` `img_url` VARCHAR(400) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '图片URL'; + +ALTER TABLE `chatgpt_functions` ADD `token` VARCHAR(255) NULL COMMENT 'API授权token' AFTER `action`; diff --git a/web/src/views/admin/Functions.vue b/web/src/views/admin/Functions.vue index b514004d..fe961c29 100644 --- a/web/src/views/admin/Functions.vue +++ b/web/src/views/admin/Functions.vue @@ -1,7 +1,7 @@ + @@ -39,6 +40,7 @@ @@ -113,6 +115,19 @@ + + + + + + + @@ -130,21 +145,19 @@