2023-12-21 08:58:24 +08:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"chatplus/core"
|
|
|
|
"chatplus/core/types"
|
|
|
|
"chatplus/handler"
|
|
|
|
"chatplus/store/model"
|
|
|
|
"chatplus/store/vo"
|
2023-12-21 18:06:09 +08:00
|
|
|
"chatplus/utils"
|
2023-12-21 08:58:24 +08:00
|
|
|
"chatplus/utils/resp"
|
2023-12-23 22:30:27 +08:00
|
|
|
|
2023-12-24 22:12:12 +08:00
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
|
|
|
2023-12-21 08:58:24 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FunctionHandler struct {
|
|
|
|
handler.BaseHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFunctionHandler(app *core.AppServer, db *gorm.DB) *FunctionHandler {
|
2024-03-19 18:25:01 +08:00
|
|
|
return &FunctionHandler{BaseHandler: handler.BaseHandler{App: app, DB: db}}
|
2023-12-21 08:58:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *FunctionHandler) Save(c *gin.Context) {
|
|
|
|
var data vo.Function
|
|
|
|
if err := c.ShouldBindJSON(&data); err != nil {
|
|
|
|
resp.ERROR(c, types.InvalidArgs)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-21 18:06:09 +08:00
|
|
|
var f = model.Function{
|
|
|
|
Id: data.Id,
|
|
|
|
Name: data.Name,
|
2023-12-23 22:30:27 +08:00
|
|
|
Label: data.Label,
|
2023-12-21 18:06:09 +08:00
|
|
|
Description: data.Description,
|
|
|
|
Parameters: utils.JsonEncode(data.Parameters),
|
|
|
|
Action: data.Action,
|
2023-12-23 22:30:27 +08:00
|
|
|
Token: data.Token,
|
|
|
|
Enabled: data.Enabled,
|
2023-12-21 18:06:09 +08:00
|
|
|
}
|
|
|
|
|
2024-03-19 18:25:01 +08:00
|
|
|
res := h.DB.Save(&f)
|
2023-12-23 22:30:27 +08:00
|
|
|
if res.Error != nil {
|
|
|
|
resp.ERROR(c, "error with save data:"+res.Error.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data.Id = f.Id
|
2023-12-21 18:06:09 +08:00
|
|
|
resp.SUCCESS(c, data)
|
2023-12-21 08:58:24 +08:00
|
|
|
}
|
|
|
|
|
2023-12-23 22:30:27 +08:00
|
|
|
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
|
|
|
|
}
|
2023-12-21 08:58:24 +08:00
|
|
|
|
2024-03-19 18:25:01 +08:00
|
|
|
res := h.DB.Model(&model.Function{}).Where("id = ?", data.Id).Update(data.Filed, data.Value)
|
2023-12-23 22:30:27 +08:00
|
|
|
if res.Error != nil {
|
|
|
|
resp.ERROR(c, "更新数据库失败!")
|
|
|
|
return
|
|
|
|
}
|
2023-12-21 08:58:24 +08:00
|
|
|
resp.SUCCESS(c)
|
|
|
|
}
|
|
|
|
|
2023-12-23 22:30:27 +08:00
|
|
|
func (h *FunctionHandler) List(c *gin.Context) {
|
|
|
|
var items []model.Function
|
2024-03-19 18:25:01 +08:00
|
|
|
res := h.DB.Find(&items)
|
2023-12-23 22:30:27 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-12-21 08:58:24 +08:00
|
|
|
func (h *FunctionHandler) Remove(c *gin.Context) {
|
|
|
|
id := h.GetInt(c, "id", 0)
|
|
|
|
|
|
|
|
if id > 0 {
|
2024-03-19 18:25:01 +08:00
|
|
|
res := h.DB.Delete(&model.Function{Id: uint(id)})
|
2023-12-21 08:58:24 +08:00
|
|
|
if res.Error != nil {
|
|
|
|
resp.ERROR(c, "更新数据库失败!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resp.SUCCESS(c)
|
|
|
|
}
|
2023-12-24 22:12:12 +08:00
|
|
|
|
|
|
|
// GenToken generate function api access token
|
|
|
|
func (h *FunctionHandler) GenToken(c *gin.Context) {
|
|
|
|
// 创建 token
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
|
|
|
"user_id": 0,
|
|
|
|
"expired": 0,
|
|
|
|
})
|
|
|
|
tokenString, err := token.SignedString([]byte(h.App.Config.Session.SecretKey))
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("error with generate token", err)
|
|
|
|
resp.ERROR(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.SUCCESS(c, tokenString)
|
|
|
|
}
|