Files
ppgo_job/controller/base.go
T
2026-07-10 11:28:16 +08:00

51 lines
1.2 KiB
Go

package controller
import (
"strings"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
// ajaxMsg 返回JSON消息(兼容新旧两种格式)
func ajaxMsg(r *ghttp.Request, msg interface{}, msgno int) {
r.Response.WriteJson(g.Map{
"status": msgno,
"code": msgno,
"message": msg,
})
}
// ajaxList 返回JSON列表(LayUI table 格式)
func ajaxList(r *ghttp.Request, msg interface{}, msgno int, count int, data interface{}) {
r.Response.WriteJson(g.Map{
"code": msgno,
"msg": msg,
"count": count,
"data": data,
})
}
// parseSort 解析并验证排序参数(防 SQL 注入),field/order 均通过返回 field+" "+order,否则返回 ""
func parseSort(r *ghttp.Request) string {
field := strings.TrimSpace(r.Get("field", "").String())
order := strings.TrimSpace(r.Get("order", "").String())
if field == "" || order == "" {
return ""
}
// 字段名校验:只允许字母、数字、下划线
for _, c := range field {
if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_') {
return ""
}
}
// 标准化排序方向
switch order {
case "asc", "ascending":
return field + " asc"
case "desc", "descending":
return field + " desc"
}
return ""
}