130 lines
3.3 KiB
Go
130 lines
3.3 KiB
Go
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{}, "删除消息回调配置成功"))
|
|
}
|
|
|
|
// TestCallbackApi 测试消息回调配置
|
|
func TestCallbackApi(ctx *gin.Context) {
|
|
queryKey, isExist := ctx.GetQuery("key")
|
|
if !isExist || strings.Trim(queryKey, "") == "" || strings.Trim(queryKey, "") == "null" {
|
|
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
|
|
}
|
|
|
|
if !config.Enabled {
|
|
ctx.JSON(http.StatusOK, vo.NewFail("回调未启用"))
|
|
return
|
|
}
|
|
|
|
// 发送测试回调
|
|
success, message := db.TestMessageCallback(config)
|
|
if success {
|
|
ctx.JSON(http.StatusOK, vo.NewSuccess(gin.H{
|
|
"callback_url": config.CallbackURL,
|
|
"message": message,
|
|
}, "测试回调成功"))
|
|
} else {
|
|
ctx.JSON(http.StatusOK, vo.NewFail("测试回调失败: "+message))
|
|
}
|
|
}
|