mirror of
https://github.com/skyle1995/NetworkAuth.git
synced 2026-05-25 02:24:05 +08:00
Add public functions
Fix a large number of bugs Optimize the description information
This commit is contained in:
@@ -19,7 +19,7 @@ var apiBaseController = controllers.NewBaseController()
|
||||
// APIFragmentHandler 接口列表页面片段处理器
|
||||
func APIFragmentHandler(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "apis.html", gin.H{
|
||||
"Title": "接口管理",
|
||||
"Title": "接口设置",
|
||||
})
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ func APIListHandler(c *gin.Context) {
|
||||
// 创建应用UUID到应用名称的映射
|
||||
appMap := make(map[string]string)
|
||||
for _, app := range apps {
|
||||
appMap[app.UUID] = app.Name
|
||||
appMap[app.UUID] = app.Name + "(ID:" + strconv.Itoa(int(app.ID)) + ")"
|
||||
}
|
||||
|
||||
// 构建响应数据
|
||||
@@ -224,25 +224,6 @@ func APIUpdateHandler(c *gin.Context) {
|
||||
apiBaseController.HandleSuccess(c, "接口更新成功", api)
|
||||
}
|
||||
|
||||
// APIGetAppsHandler 获取应用列表(用于接口页面的应用选择器)
|
||||
func APIGetAppsHandler(c *gin.Context) {
|
||||
// 获取数据库连接
|
||||
db, ok := apiBaseController.GetDB(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取所有应用
|
||||
var apps []models.App
|
||||
if err := db.Select("uuid, name").Order("created_at ASC").Find(&apps).Error; err != nil {
|
||||
logrus.WithError(err).Error("Failed to fetch apps")
|
||||
apiBaseController.HandleInternalError(c, "获取应用列表失败", err)
|
||||
return
|
||||
}
|
||||
|
||||
apiBaseController.HandleSuccess(c, "获取应用列表成功", apps)
|
||||
}
|
||||
|
||||
// APIGetTypesHandler 获取接口类型列表API处理器
|
||||
func APIGetTypesHandler(c *gin.Context) {
|
||||
// 构建接口类型列表
|
||||
@@ -252,7 +233,7 @@ func APIGetTypesHandler(c *gin.Context) {
|
||||
}
|
||||
|
||||
var apiTypes []APITypeItem
|
||||
|
||||
|
||||
// 获取所有有效的API类型
|
||||
validTypes := []int{
|
||||
models.APITypeGetBulletin, models.APITypeGetUpdateUrl, models.APITypeCheckAppVersion, models.APITypeGetCardInfo,
|
||||
@@ -328,11 +309,11 @@ func APIGenerateKeysHandler(c *gin.Context) {
|
||||
Side string `json:"side"` // submit | return
|
||||
Algorithm int `json:"algorithm"` // 与 models.Algorithm* 对应
|
||||
}
|
||||
|
||||
|
||||
if !apiBaseController.BindJSON(c, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
if req.Side != "submit" && req.Side != "return" {
|
||||
apiBaseController.HandleValidationError(c, "side参数必须为submit或return")
|
||||
return
|
||||
|
||||
@@ -20,7 +20,7 @@ var appBaseController = controllers.NewBaseController()
|
||||
// AppsFragmentHandler 应用列表页面片段处理器
|
||||
func AppsFragmentHandler(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "apps.html", gin.H{
|
||||
"Title": "应用管理",
|
||||
"Title": "应用程序",
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1398,3 +1398,38 @@ func AppUpdateStatusHandler(c *gin.Context) {
|
||||
"msg": "应用" + statusText + "成功",
|
||||
})
|
||||
}
|
||||
|
||||
// AppsSimpleListHandler 简化应用列表API处理器(用于下拉框选择等场景)
|
||||
func AppsSimpleListHandler(c *gin.Context) {
|
||||
// 获取数据库连接
|
||||
db, ok := appBaseController.GetDB(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// 查询所有启用的应用,只获取必要字段
|
||||
var apps []struct {
|
||||
ID uint `json:"id"`
|
||||
UUID string `json:"uuid"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
if err := db.Model(&models.App{}).
|
||||
Select("id, uuid, name").
|
||||
Where("status = ?", 1). // 只获取启用的应用
|
||||
Order("name ASC").
|
||||
Find(&apps).Error; err != nil {
|
||||
logrus.WithError(err).Error("Failed to query simple apps list")
|
||||
appBaseController.HandleInternalError(c, "获取应用列表失败", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 返回结果
|
||||
response := gin.H{
|
||||
"code": 0,
|
||||
"msg": "success",
|
||||
"data": apps,
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
324
controllers/admin/function.go
Normal file
324
controllers/admin/function.go
Normal file
@@ -0,0 +1,324 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"networkDev/controllers"
|
||||
"networkDev/models"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// 创建基础控制器实例
|
||||
var functionBaseController = controllers.NewBaseController()
|
||||
|
||||
// FunctionFragmentHandler 公共函数列表页面片段处理器
|
||||
func FunctionFragmentHandler(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "functions.html", gin.H{
|
||||
"Title": "公共函数",
|
||||
})
|
||||
}
|
||||
|
||||
// FunctionListHandler 函数列表API处理器
|
||||
func FunctionListHandler(c *gin.Context) {
|
||||
// 获取分页参数
|
||||
page, _ := strconv.Atoi(c.Query("page"))
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
limit, _ := strconv.Atoi(c.Query("limit"))
|
||||
// 兼容前端使用的page_size参数
|
||||
if limit <= 0 {
|
||||
limit, _ = strconv.Atoi(c.Query("page_size"))
|
||||
}
|
||||
if limit <= 0 {
|
||||
limit = 10
|
||||
}
|
||||
|
||||
// 获取搜索关键词参数(支持编号、别名、代码的综合搜索)
|
||||
search := strings.TrimSpace(c.Query("search"))
|
||||
|
||||
// 兼容旧的别名搜索参数
|
||||
if search == "" {
|
||||
search = strings.TrimSpace(c.Query("alias"))
|
||||
}
|
||||
|
||||
// 获取应用筛选参数
|
||||
appUUID := strings.TrimSpace(c.Query("app_uuid"))
|
||||
|
||||
// 构建查询
|
||||
db, ok := functionBaseController.GetDB(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// 构建基础查询
|
||||
query := db.Model(&models.Function{})
|
||||
|
||||
// 如果指定了搜索关键词,则在编号、别名、代码、备注中进行模糊搜索
|
||||
if search != "" {
|
||||
query = query.Where("number LIKE ? OR alias LIKE ? OR code LIKE ? OR remark LIKE ?",
|
||||
"%"+search+"%", "%"+search+"%", "%"+search+"%", "%"+search+"%")
|
||||
}
|
||||
|
||||
// 如果指定了应用筛选,则按应用UUID筛选
|
||||
if appUUID != "" {
|
||||
query = query.Where("app_uuid = ?", appUUID)
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
var total int64
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
logrus.WithError(err).Error("Failed to count functions")
|
||||
functionBaseController.HandleInternalError(c, "查询函数总数失败", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取分页数据
|
||||
var functions []models.Function
|
||||
offset := (page - 1) * limit
|
||||
if err := query.Offset(offset).Limit(limit).Order("created_at DESC").Find(&functions).Error; err != nil {
|
||||
logrus.WithError(err).Error("Failed to fetch functions")
|
||||
functionBaseController.HandleInternalError(c, "查询函数列表失败", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 构建响应数据
|
||||
type FunctionResponse struct {
|
||||
ID uint `json:"id"`
|
||||
UUID string `json:"uuid"`
|
||||
Number string `json:"number"`
|
||||
AppUUID string `json:"app_uuid"`
|
||||
Alias string `json:"alias"`
|
||||
Code string `json:"code"`
|
||||
Remark string `json:"remark"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
var responseData []FunctionResponse
|
||||
for _, function := range functions {
|
||||
responseData = append(responseData, FunctionResponse{
|
||||
ID: function.ID,
|
||||
UUID: function.UUID,
|
||||
Number: function.Number,
|
||||
AppUUID: function.AppUUID,
|
||||
Alias: function.Alias,
|
||||
Code: function.Code,
|
||||
Remark: function.Remark,
|
||||
CreatedAt: function.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
UpdatedAt: function.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
response := gin.H{
|
||||
"code": 0,
|
||||
"msg": "success",
|
||||
"count": total,
|
||||
"data": responseData,
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// FunctionCreateHandler 新增函数API处理器
|
||||
func FunctionCreateHandler(c *gin.Context) {
|
||||
var req struct {
|
||||
Alias string `json:"alias"`
|
||||
AppUUID string `json:"app_uuid"`
|
||||
Code string `json:"code"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
if !functionBaseController.BindJSON(c, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
// 验证必填字段
|
||||
if !functionBaseController.ValidateRequired(c, map[string]interface{}{
|
||||
"函数别名": req.Alias,
|
||||
}) {
|
||||
return
|
||||
}
|
||||
|
||||
// 验证别名格式:必须以英文字母开头,只能包含数字和英文字母
|
||||
aliasPattern := regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9]*$`)
|
||||
if !aliasPattern.MatchString(req.Alias) {
|
||||
functionBaseController.HandleValidationError(c, "别名必须以英文字母开头,只能包含数字和英文字母")
|
||||
return
|
||||
}
|
||||
|
||||
db, ok := functionBaseController.GetDB(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// 处理应用UUID:如果为空或"0",设置为"0"(全局函数)
|
||||
appUUID := strings.TrimSpace(req.AppUUID)
|
||||
if appUUID == "" {
|
||||
appUUID = "0"
|
||||
}
|
||||
|
||||
// 如果指定了应用UUID且不是"0",验证应用是否存在
|
||||
if appUUID != "0" {
|
||||
var appCount int64
|
||||
if err := db.Model(&models.App{}).Where("uuid = ?", appUUID).Count(&appCount).Error; err != nil {
|
||||
logrus.WithError(err).Error("Failed to check app existence")
|
||||
functionBaseController.HandleInternalError(c, "验证应用失败", err)
|
||||
return
|
||||
}
|
||||
if appCount == 0 {
|
||||
functionBaseController.HandleValidationError(c, "指定的应用不存在")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 创建函数
|
||||
function := models.Function{
|
||||
Alias: strings.TrimSpace(req.Alias),
|
||||
AppUUID: appUUID,
|
||||
Code: req.Code,
|
||||
Remark: strings.TrimSpace(req.Remark),
|
||||
}
|
||||
|
||||
if err := db.Create(&function).Error; err != nil {
|
||||
logrus.WithError(err).Error("Failed to create function")
|
||||
functionBaseController.HandleInternalError(c, "创建函数失败", err)
|
||||
return
|
||||
}
|
||||
|
||||
functionBaseController.HandleSuccess(c, "创建成功", function)
|
||||
}
|
||||
|
||||
// FunctionUpdateHandler 更新函数API处理器
|
||||
func FunctionUpdateHandler(c *gin.Context) {
|
||||
var req struct {
|
||||
UUID string `json:"uuid"`
|
||||
AppUUID string `json:"app_uuid"`
|
||||
Code string `json:"code"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
if !functionBaseController.BindJSON(c, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
// 验证必填字段(移除对alias的验证,因为编辑时不允许修改别名)
|
||||
if !functionBaseController.ValidateRequired(c, map[string]interface{}{
|
||||
"函数UUID": req.UUID,
|
||||
}) {
|
||||
return
|
||||
}
|
||||
|
||||
db, ok := functionBaseController.GetDB(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// 处理应用UUID:如果为空或"0",设置为"0"(全局函数)
|
||||
updateAppUUID := strings.TrimSpace(req.AppUUID)
|
||||
if updateAppUUID == "" {
|
||||
updateAppUUID = "0"
|
||||
}
|
||||
|
||||
// 如果指定了应用UUID且不是"0",验证应用是否存在
|
||||
if updateAppUUID != "0" {
|
||||
var appCount int64
|
||||
if err := db.Model(&models.App{}).Where("uuid = ?", updateAppUUID).Count(&appCount).Error; err != nil {
|
||||
logrus.WithError(err).Error("Failed to check app existence")
|
||||
functionBaseController.HandleInternalError(c, "验证应用失败", err)
|
||||
return
|
||||
}
|
||||
if appCount == 0 {
|
||||
functionBaseController.HandleValidationError(c, "指定的应用不存在")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 通过uuid字段查找函数
|
||||
var function models.Function
|
||||
if err := db.Where("uuid = ?", strings.TrimSpace(req.UUID)).First(&function).Error; err != nil {
|
||||
functionBaseController.HandleValidationError(c, "函数不存在")
|
||||
return
|
||||
}
|
||||
|
||||
// 更新函数信息(不允许修改别名)
|
||||
function.AppUUID = updateAppUUID
|
||||
function.Code = req.Code
|
||||
function.Remark = strings.TrimSpace(req.Remark)
|
||||
|
||||
if err := db.Save(&function).Error; err != nil {
|
||||
logrus.WithError(err).Error("Failed to update function")
|
||||
functionBaseController.HandleInternalError(c, "更新函数失败", err)
|
||||
return
|
||||
}
|
||||
|
||||
functionBaseController.HandleSuccess(c, "更新成功", function)
|
||||
}
|
||||
|
||||
// FunctionDeleteHandler 删除函数API处理器
|
||||
func FunctionDeleteHandler(c *gin.Context) {
|
||||
var req struct {
|
||||
ID uint `json:"id"`
|
||||
}
|
||||
|
||||
if !functionBaseController.BindJSON(c, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
if req.ID == 0 {
|
||||
functionBaseController.HandleValidationError(c, "函数ID不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
db, ok := functionBaseController.GetDB(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// 删除函数
|
||||
if err := db.Delete(&models.Function{}, req.ID).Error; err != nil {
|
||||
logrus.WithError(err).Error("Failed to delete function")
|
||||
functionBaseController.HandleInternalError(c, "删除函数失败", err)
|
||||
return
|
||||
}
|
||||
|
||||
logrus.WithField("function_id", req.ID).Info("Successfully deleted function")
|
||||
|
||||
functionBaseController.HandleSuccess(c, "删除成功", nil)
|
||||
}
|
||||
|
||||
// FunctionsBatchDeleteHandler 批量删除函数API处理器
|
||||
func FunctionsBatchDeleteHandler(c *gin.Context) {
|
||||
var req struct {
|
||||
IDs []uint `json:"ids"`
|
||||
}
|
||||
|
||||
if !functionBaseController.BindJSON(c, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
if len(req.IDs) == 0 {
|
||||
functionBaseController.HandleValidationError(c, "请选择要删除的函数")
|
||||
return
|
||||
}
|
||||
|
||||
db, ok := functionBaseController.GetDB(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// 批量删除函数
|
||||
if err := db.Delete(&models.Function{}, req.IDs).Error; err != nil {
|
||||
logrus.WithError(err).Error("Failed to batch delete functions")
|
||||
functionBaseController.HandleInternalError(c, "批量删除失败", err)
|
||||
return
|
||||
}
|
||||
|
||||
logrus.WithField("function_ids", req.IDs).Info("Successfully batch deleted functions")
|
||||
|
||||
functionBaseController.HandleSuccess(c, "批量删除成功", nil)
|
||||
}
|
||||
@@ -66,27 +66,19 @@ func AdminLayoutHandler(c *gin.Context) {
|
||||
utils.SetCSRFToken(c, token)
|
||||
}
|
||||
|
||||
// 准备额外的模板数据
|
||||
extraData := gin.H{}
|
||||
|
||||
// 从数据库读取站点标题
|
||||
db, ok := handlersBaseController.GetDB(c)
|
||||
if !ok {
|
||||
extraData["Title"] = "凌动技术"
|
||||
} else {
|
||||
siteTitle, settingErr := services.FindSettingByName("site_title", db)
|
||||
if settingErr != nil || siteTitle == nil {
|
||||
extraData["Title"] = "凌动技术"
|
||||
} else {
|
||||
extraData["Title"] = siteTitle.Value
|
||||
}
|
||||
}
|
||||
|
||||
// 准备模板数据
|
||||
data := handlersBaseController.GetDefaultTemplateData()
|
||||
data["CSRFToken"] = token
|
||||
|
||||
// 合并额外数据
|
||||
|
||||
// 从数据库读取站点标题,如果失败则使用默认值
|
||||
if db, ok := handlersBaseController.GetDB(c); ok {
|
||||
if siteTitle, err := services.FindSettingByName("site_title", db); err == nil && siteTitle != nil {
|
||||
data["Title"] = siteTitle.Value
|
||||
}
|
||||
}
|
||||
|
||||
// 合并其他数据(如果有的话)
|
||||
extraData := gin.H{}
|
||||
for key, value := range extraData {
|
||||
data[key] = value
|
||||
}
|
||||
|
||||
@@ -15,10 +15,10 @@ import (
|
||||
// 创建基础控制器实例
|
||||
var variableBaseController = controllers.NewBaseController()
|
||||
|
||||
// VariableFragmentHandler 变量列表页面片段处理器
|
||||
// VariableFragmentHandler 公共变量列表页面片段处理器
|
||||
func VariableFragmentHandler(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "variables.html", gin.H{
|
||||
"Title": "变量管理",
|
||||
"Title": "公共变量",
|
||||
})
|
||||
}
|
||||
|
||||
@@ -46,6 +46,9 @@ func VariableListHandler(c *gin.Context) {
|
||||
search = strings.TrimSpace(c.Query("alias"))
|
||||
}
|
||||
|
||||
// 获取应用筛选参数
|
||||
appUUID := strings.TrimSpace(c.Query("app_uuid"))
|
||||
|
||||
// 构建查询
|
||||
db, ok := variableBaseController.GetDB(c)
|
||||
if !ok {
|
||||
@@ -61,6 +64,11 @@ func VariableListHandler(c *gin.Context) {
|
||||
"%"+search+"%", "%"+search+"%", "%"+search+"%", "%"+search+"%")
|
||||
}
|
||||
|
||||
// 如果指定了应用筛选,则按应用UUID筛选
|
||||
if appUUID != "" {
|
||||
query = query.Where("app_uuid = ?", appUUID)
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
var total int64
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
@@ -83,6 +91,7 @@ func VariableListHandler(c *gin.Context) {
|
||||
ID uint `json:"id"`
|
||||
UUID string `json:"uuid"`
|
||||
Number string `json:"number"`
|
||||
AppUUID string `json:"app_uuid"`
|
||||
Alias string `json:"alias"`
|
||||
Data string `json:"data"`
|
||||
Remark string `json:"remark"`
|
||||
@@ -96,6 +105,7 @@ func VariableListHandler(c *gin.Context) {
|
||||
ID: variable.ID,
|
||||
UUID: variable.UUID,
|
||||
Number: variable.Number,
|
||||
AppUUID: variable.AppUUID,
|
||||
Alias: variable.Alias,
|
||||
Data: variable.Data,
|
||||
Remark: variable.Remark,
|
||||
@@ -117,9 +127,10 @@ func VariableListHandler(c *gin.Context) {
|
||||
// VariableCreateHandler 新增变量API处理器
|
||||
func VariableCreateHandler(c *gin.Context) {
|
||||
var req struct {
|
||||
Alias string `json:"alias"`
|
||||
Data string `json:"data"`
|
||||
Remark string `json:"remark"`
|
||||
Alias string `json:"alias"`
|
||||
AppUUID string `json:"app_uuid"`
|
||||
Data string `json:"data"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
if !variableBaseController.BindJSON(c, &req) {
|
||||
@@ -145,11 +156,52 @@ func VariableCreateHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 处理应用UUID:如果为空或"0",设置为"0"(全局变量)
|
||||
updateAppUUID := strings.TrimSpace(req.AppUUID)
|
||||
if updateAppUUID == "" {
|
||||
updateAppUUID = "0"
|
||||
}
|
||||
|
||||
// 如果指定了应用UUID且不是"0",验证应用是否存在
|
||||
if updateAppUUID != "0" {
|
||||
var appCount int64
|
||||
if err := db.Model(&models.App{}).Where("uuid = ?", updateAppUUID).Count(&appCount).Error; err != nil {
|
||||
logrus.WithError(err).Error("Failed to check app existence")
|
||||
variableBaseController.HandleInternalError(c, "验证应用失败", err)
|
||||
return
|
||||
}
|
||||
if appCount == 0 {
|
||||
variableBaseController.HandleValidationError(c, "指定的应用不存在")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 处理应用UUID:如果为空或"0",设置为"0"(全局变量)
|
||||
appUUID := strings.TrimSpace(req.AppUUID)
|
||||
if appUUID == "" {
|
||||
appUUID = "0"
|
||||
}
|
||||
|
||||
// 如果指定了应用UUID且不是"0",验证应用是否存在
|
||||
if appUUID != "0" {
|
||||
var appCount int64
|
||||
if err := db.Model(&models.App{}).Where("uuid = ?", appUUID).Count(&appCount).Error; err != nil {
|
||||
logrus.WithError(err).Error("Failed to check app existence")
|
||||
variableBaseController.HandleInternalError(c, "验证应用失败", err)
|
||||
return
|
||||
}
|
||||
if appCount == 0 {
|
||||
variableBaseController.HandleValidationError(c, "指定的应用不存在")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 创建变量
|
||||
variable := models.Variable{
|
||||
Alias: strings.TrimSpace(req.Alias),
|
||||
Data: req.Data,
|
||||
Remark: strings.TrimSpace(req.Remark),
|
||||
Alias: strings.TrimSpace(req.Alias),
|
||||
AppUUID: appUUID,
|
||||
Data: req.Data,
|
||||
Remark: strings.TrimSpace(req.Remark),
|
||||
}
|
||||
|
||||
if err := db.Create(&variable).Error; err != nil {
|
||||
@@ -164,36 +216,48 @@ func VariableCreateHandler(c *gin.Context) {
|
||||
// VariableUpdateHandler 更新变量API处理器
|
||||
func VariableUpdateHandler(c *gin.Context) {
|
||||
var req struct {
|
||||
UUID string `json:"uuid"`
|
||||
Alias string `json:"alias"`
|
||||
Data string `json:"data"`
|
||||
Remark string `json:"remark"`
|
||||
UUID string `json:"uuid"`
|
||||
AppUUID string `json:"app_uuid"`
|
||||
Data string `json:"data"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
if !variableBaseController.BindJSON(c, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
// 验证必填字段
|
||||
// 验证必填字段(移除对alias的验证,因为编辑时不允许修改别名)
|
||||
if !variableBaseController.ValidateRequired(c, map[string]interface{}{
|
||||
"变量UUID": req.UUID,
|
||||
"变量别名": req.Alias,
|
||||
}) {
|
||||
return
|
||||
}
|
||||
|
||||
// 验证别名格式:必须以英文字母开头,只能包含数字和英文字母
|
||||
aliasPattern := regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9]*$`)
|
||||
if !aliasPattern.MatchString(req.Alias) {
|
||||
variableBaseController.HandleValidationError(c, "别名必须以英文字母开头,只能包含数字和英文字母")
|
||||
return
|
||||
}
|
||||
|
||||
db, ok := variableBaseController.GetDB(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// 处理应用UUID:如果为空或"0",设置为"0"(全局变量)
|
||||
updateAppUUID := strings.TrimSpace(req.AppUUID)
|
||||
if updateAppUUID == "" {
|
||||
updateAppUUID = "0"
|
||||
}
|
||||
|
||||
// 如果指定了应用UUID且不是"0",验证应用是否存在
|
||||
if updateAppUUID != "0" {
|
||||
var appCount int64
|
||||
if err := db.Model(&models.App{}).Where("uuid = ?", updateAppUUID).Count(&appCount).Error; err != nil {
|
||||
logrus.WithError(err).Error("Failed to check app existence")
|
||||
variableBaseController.HandleInternalError(c, "验证应用失败", err)
|
||||
return
|
||||
}
|
||||
if appCount == 0 {
|
||||
variableBaseController.HandleValidationError(c, "指定的应用不存在")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 通过uuid字段查找变量
|
||||
var variable models.Variable
|
||||
if err := db.Where("uuid = ?", strings.TrimSpace(req.UUID)).First(&variable).Error; err != nil {
|
||||
@@ -201,8 +265,8 @@ func VariableUpdateHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 更新字段(不允许修改应用关联)
|
||||
variable.Alias = strings.TrimSpace(req.Alias)
|
||||
// 更新字段(不更新alias,保持原有别名不变)
|
||||
variable.AppUUID = updateAppUUID
|
||||
variable.Data = req.Data
|
||||
variable.Remark = strings.TrimSpace(req.Remark)
|
||||
|
||||
|
||||
@@ -149,7 +149,8 @@ func (bc *BaseController) BindURI(c *gin.Context, obj interface{}) bool {
|
||||
// 返回包含系统基础信息的数据映射,包括站点标题、页脚文本、备案信息等
|
||||
func (bc *BaseController) GetDefaultTemplateData() gin.H {
|
||||
return gin.H{
|
||||
"SystemName": "凌动技术",
|
||||
"Title": "凌动技术",
|
||||
"SystemName": "网络验证系统",
|
||||
"FooterText": "© 2025 凌动技术 保留所有权利",
|
||||
"ICPRecord": "",
|
||||
"ICPRecordLink": "https://beian.miit.gov.cn",
|
||||
|
||||
@@ -32,18 +32,16 @@ func RootHandler(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 获取默认模板数据
|
||||
defaultData := homeBaseController.GetDefaultTemplateData()
|
||||
data := homeBaseController.GetDefaultTemplateData()
|
||||
|
||||
// 准备模板数据,优先使用数据库配置,不存在时使用默认值
|
||||
data := gin.H{
|
||||
"SystemName": getSettingValue("site_title", defaultData["SystemName"].(string), db),
|
||||
"FooterText": getSettingValue("footer_text", defaultData["FooterText"].(string), db),
|
||||
"ICPRecord": getSettingValue("icp_record", defaultData["ICPRecord"].(string), db),
|
||||
"ICPRecordLink": getSettingValue("icp_record_link", defaultData["ICPRecordLink"].(string), db),
|
||||
"PSBRecord": getSettingValue("psb_record", defaultData["PSBRecord"].(string), db),
|
||||
"PSBRecordLink": getSettingValue("psb_record_link", defaultData["PSBRecordLink"].(string), db),
|
||||
"title": "主页",
|
||||
}
|
||||
// 从数据库读取设置,优先使用数据库配置,不存在时使用默认值
|
||||
data["SystemName"] = getSettingValue("site_title", data["SystemName"].(string), db)
|
||||
data["FooterText"] = getSettingValue("footer_text", data["FooterText"].(string), db)
|
||||
data["ICPRecord"] = getSettingValue("icp_record", data["ICPRecord"].(string), db)
|
||||
data["ICPRecordLink"] = getSettingValue("icp_record_link", data["ICPRecordLink"].(string), db)
|
||||
data["PSBRecord"] = getSettingValue("psb_record", data["PSBRecord"].(string), db)
|
||||
data["PSBRecordLink"] = getSettingValue("psb_record_link", data["PSBRecordLink"].(string), db)
|
||||
data["title"] = "主页"
|
||||
|
||||
c.HTML(http.StatusOK, "index.html", data)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user