- Added Oauth2LogLoginService for managing user authorization logs. - Implemented methods for inserting logs, cleaning logs, and exporting log data. - Created a new file for Oauth2 login log service. refactor: Remove unused open_api module - Deleted the open_api.go file as it was not utilized in the project. fix: Update error codes in SysProfileController - Changed error codes for binding errors and user authentication errors to more descriptive values. fix: Update cache handling in SysConfig and SysDictType services - Modified Redis set operations to include expiration time for cached values. refactor: Update middleware authorization checks - Replaced PreAuthorize middleware with AuthorizeUser across multiple routes in system and tool modules for consistency. chore: Clean up trace and ws modules - Updated middleware authorization in trace and ws modules to use AuthorizeUser.
107 lines
2.5 KiB
Go
107 lines
2.5 KiB
Go
package reqctx
|
||
|
||
import (
|
||
"strings"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"golang.org/x/text/language"
|
||
|
||
"be.ems/src/framework/constants"
|
||
)
|
||
|
||
// QueryMap Query参数转换Map
|
||
func QueryMap(c *gin.Context) map[string]string {
|
||
queryValues := c.Request.URL.Query()
|
||
queryParams := make(map[string]string, len(queryValues))
|
||
for key, values := range queryValues {
|
||
queryParams[key] = values[0]
|
||
}
|
||
return queryParams
|
||
}
|
||
|
||
// BodyJSONMap JSON参数转换Map
|
||
func BodyJSONMap(c *gin.Context) map[string]any {
|
||
params := make(map[string]any, 0)
|
||
c.ShouldBindBodyWithJSON(¶ms)
|
||
return params
|
||
}
|
||
|
||
// RequestParamsMap 请求参数转换Map
|
||
func RequestParamsMap(c *gin.Context) map[string]any {
|
||
params := make(map[string]any, 0)
|
||
// json
|
||
if strings.HasPrefix(c.ContentType(), "application/json") {
|
||
c.ShouldBindBodyWithJSON(¶ms)
|
||
}
|
||
|
||
// 表单
|
||
formParams := c.Request.PostForm
|
||
for key, value := range formParams {
|
||
if _, ok := params[key]; !ok {
|
||
params[key] = value[0]
|
||
}
|
||
}
|
||
|
||
// 查询
|
||
queryParams := c.Request.URL.Query()
|
||
for key, value := range queryParams {
|
||
if _, ok := params[key]; !ok {
|
||
params[key] = value[0]
|
||
}
|
||
}
|
||
return params
|
||
}
|
||
|
||
// Authorization 解析请求头
|
||
func Authorization(c *gin.Context) string {
|
||
// Query请求查询
|
||
if authQuery, ok := c.GetQuery(constants.ACCESS_TOKEN); ok && authQuery != "" {
|
||
return authQuery
|
||
}
|
||
// Header请求头
|
||
if authHeader := c.GetHeader(constants.ACCESS_TOKEN); authHeader != "" {
|
||
return authHeader
|
||
}
|
||
|
||
// Query请求查询
|
||
if authQuery, ok := c.GetQuery(constants.ACCESS_TOKEN_QUERY); ok && authQuery != "" {
|
||
return authQuery
|
||
}
|
||
// Header请求头
|
||
authHeader := c.GetHeader(constants.HEADER_KEY)
|
||
if authHeader == "" {
|
||
return ""
|
||
}
|
||
// 拆分 Authorization 请求头,提取 JWT 令牌部分
|
||
tokenStr := strings.Replace(authHeader, constants.HEADER_PREFIX, "", 1)
|
||
if len(tokenStr) > 64 {
|
||
return strings.TrimSpace(tokenStr) // 去除可能存在的空格
|
||
}
|
||
return ""
|
||
}
|
||
|
||
// AcceptLanguage 解析客户端接收语言 zh:中文 en: 英文
|
||
func AcceptLanguage(c *gin.Context) string {
|
||
preferredLanguage := language.English
|
||
|
||
// Query请求查询
|
||
if v, ok := c.GetQuery("language"); ok && v != "" {
|
||
tags, _, _ := language.ParseAcceptLanguage(v)
|
||
if len(tags) > 0 {
|
||
preferredLanguage = tags[0]
|
||
}
|
||
}
|
||
// Header请求头
|
||
if v := c.GetHeader("Accept-Language"); v != "" {
|
||
tags, _, _ := language.ParseAcceptLanguage(v)
|
||
if len(tags) > 0 {
|
||
preferredLanguage = tags[0]
|
||
}
|
||
}
|
||
|
||
// 只取前缀
|
||
lang := preferredLanguage.String()
|
||
arr := strings.Split(lang, "-")
|
||
return arr[0]
|
||
}
|