first commit
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"xiawan/wx/api/req"
|
||||
"xiawan/wx/api/vo"
|
||||
"xiawan/wx/db"
|
||||
"xiawan/wx/db/table"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// SetCallbackApi 设置消息回调配置
|
||||
func SetCallbackApi(ctx *gin.Context) {
|
||||
config := new(req.MessageCallbackConfigModel)
|
||||
queryKey, isExist := ctx.GetQuery("key")
|
||||
if !isExist || strings.Trim(queryKey, "") == "" || strings.Trim(queryKey, "") == "null" {
|
||||
//确保每次都有Key
|
||||
ctx.JSON(http.StatusOK, vo.NewFailUUId(""))
|
||||
return
|
||||
}
|
||||
|
||||
if !validateData(ctx, &config) {
|
||||
return
|
||||
}
|
||||
|
||||
// 使用传入的queryKey作为UUID
|
||||
configInDB := table.MessageCallbackConfig{
|
||||
UUID: queryKey,
|
||||
CallbackURL: config.CallbackURL,
|
||||
Enabled: config.Enabled,
|
||||
}
|
||||
|
||||
// 保存配置到数据库
|
||||
err := db.SaveMessageCallbackConfig(&configInDB)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, vo.NewFail("保存消息回调配置失败: "+err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, vo.NewSuccess(gin.H{}, "设置消息回调成功"))
|
||||
}
|
||||
|
||||
// GetCallbackApi 获取消息回调配置
|
||||
func GetCallbackApi(ctx *gin.Context) {
|
||||
queryKey, isExist := ctx.GetQuery("key")
|
||||
if !isExist || strings.Trim(queryKey, "") == "" || strings.Trim(queryKey, "") == "null" {
|
||||
//确保每次都有Key
|
||||
ctx.JSON(http.StatusOK, vo.NewFailUUId(""))
|
||||
return
|
||||
}
|
||||
|
||||
// 查询数据库获取回调配置
|
||||
config, err := db.GetMessageCallbackConfig(queryKey)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, vo.NewFail("获取消息回调配置失败: "+err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
if config == nil {
|
||||
ctx.JSON(http.StatusOK, vo.NewFail("未找到消息回调配置"))
|
||||
return
|
||||
}
|
||||
|
||||
// 转换为响应模型
|
||||
response := req.MessageCallbackConfigModel{
|
||||
CallbackURL: config.CallbackURL,
|
||||
Enabled: config.Enabled,
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, vo.NewSuccessObj(response, "获取消息回调配置成功"))
|
||||
}
|
||||
|
||||
// DeleteCallbackApi 删除消息回调配置
|
||||
func DeleteCallbackApi(ctx *gin.Context) {
|
||||
queryKey, isExist := ctx.GetQuery("key")
|
||||
if !isExist || strings.Trim(queryKey, "") == "" || strings.Trim(queryKey, "") == "null" {
|
||||
//确保每次都有Key
|
||||
ctx.JSON(http.StatusOK, vo.NewFailUUId(""))
|
||||
return
|
||||
}
|
||||
|
||||
// 从数据库删除回调配置
|
||||
err := db.DeleteMessageCallbackConfig(queryKey)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, vo.NewFail("删除消息回调配置失败: "+err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, vo.NewSuccess(gin.H{}, "删除消息回调配置成功"))
|
||||
}
|
||||
Reference in New Issue
Block a user