feat: Implement Oauth2 login log service and repository

- 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.
This commit is contained in:
TsMask
2025-04-27 11:07:34 +08:00
parent b29a36e7b5
commit 56991a0b49
72 changed files with 2334 additions and 873 deletions

View File

@@ -13,7 +13,6 @@ import (
"be.ems/src/framework/constants"
"be.ems/src/framework/token"
"github.com/gorilla/mux"
"golang.org/x/text/language"
)
// GetParam 地址栏参数{id}
@@ -147,63 +146,33 @@ func Authorization(r *http.Request) string {
return arr[1]
}
// AcceptLanguage 解析客户端接收语言 zh中文 en: 英文
func AcceptLanguage(r *http.Request) string {
preferredLanguage := language.English
// Query请求查询
if v := GetQuery(r, "language"); v != "" {
tags, _, _ := language.ParseAcceptLanguage(v)
if len(tags) > 0 {
preferredLanguage = tags[0]
}
}
// Header请求头
if v := GetHeader(r, "Accept-Language"); v != "" {
tags, _, _ := language.ParseAcceptLanguage(v)
if len(tags) > 0 {
preferredLanguage = tags[0]
}
}
// 只取前缀
lang := preferredLanguage.String()
arr := strings.Split(lang, "-")
return arr[0]
}
// ContextKey 定义自定义类型作为键
type ContextKey string
// LoginUser 登录用户信息需要Authorize中间件
func LoginUser(r *http.Request) (token.TokenInfo, error) {
func LoginUser(r *http.Request) (token.UserInfo, error) {
// 获取请求头标识信息
tokenStr := Authorization(r)
if tokenStr == "" {
return token.TokenInfo{}, fmt.Errorf("not token info")
return token.UserInfo{}, fmt.Errorf("not token info")
}
if tokenStr == "" {
return token.UserInfo{}, fmt.Errorf("authorization token is empty")
}
// 验证令牌
claims, err := token.Verify(tokenStr)
claims, err := token.UserTokenVerify(tokenStr, "access")
if err != nil {
return token.TokenInfo{}, fmt.Errorf("token verify fail")
return token.UserInfo{}, err
}
// 获取缓存的用户信息
loginUser := token.Info(claims)
if loginUser.UserId <= 0 {
return token.TokenInfo{}, fmt.Errorf("not user info")
info := token.UserInfoGet(claims)
if info.UserId <= 0 {
return token.UserInfo{}, fmt.Errorf("invalid login user information")
}
return loginUser, nil
}
// LoginUserToUserID 登录用户信息-用户ID
func LoginUserToUserID(r *http.Request) int64 {
loginUser, err := LoginUser(r)
if err != nil {
return 0
}
return loginUser.UserId
return info, nil
}
// LoginUserToUserName 登录用户信息-用户名称