mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-14 21:23:47 +08:00
1、泛型化参数,预留泛型约束,方便以后代码扩展的类型安全校验
2、升级goframe至v2.7.3
This commit is contained in:
@@ -14,25 +14,25 @@ import (
|
||||
)
|
||||
|
||||
// Init 初始化上下文对象指针到上下文对象中,以便后续的请求流程中可以修改
|
||||
func Init(r *ghttp.Request, customCtx *model.Context) {
|
||||
func Init[T any](r *ghttp.Request, customCtx *model.Context[T]) {
|
||||
r.SetCtxVar(consts.ContextHTTPKey, customCtx)
|
||||
}
|
||||
|
||||
// Get 获得上下文变量,如果没有设置,那么返回nil
|
||||
func Get(ctx context.Context) *model.Context {
|
||||
func Get[T any](ctx context.Context) *model.Context[T] {
|
||||
value := ctx.Value(consts.ContextHTTPKey)
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
if localCtx, ok := value.(*model.Context); ok {
|
||||
if localCtx, ok := value.(*model.Context[T]); ok {
|
||||
return localCtx
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetUser 将上下文信息设置到上下文请求中,注意是完整覆盖
|
||||
func SetUser(ctx context.Context, user *model.Identity) {
|
||||
c := Get(ctx)
|
||||
func SetUser[T any](ctx context.Context, user *model.Identity) {
|
||||
c := Get[T](ctx)
|
||||
if c == nil {
|
||||
g.Log().Warning(ctx, "contexts.SetUser, c == nil ")
|
||||
return
|
||||
@@ -41,8 +41,8 @@ func SetUser(ctx context.Context, user *model.Identity) {
|
||||
}
|
||||
|
||||
// SetResponse 设置组件响应 用于访问日志使用
|
||||
func SetResponse(ctx context.Context, response *model.Response) {
|
||||
c := Get(ctx)
|
||||
func SetResponse[T any](ctx context.Context, response *model.Response[T]) {
|
||||
c := Get[T](ctx)
|
||||
if c == nil {
|
||||
g.Log().Warning(ctx, "contexts.SetResponse, c == nil ")
|
||||
return
|
||||
@@ -51,8 +51,8 @@ func SetResponse(ctx context.Context, response *model.Response) {
|
||||
}
|
||||
|
||||
// SetModule 设置应用模块
|
||||
func SetModule(ctx context.Context, module string) {
|
||||
c := Get(ctx)
|
||||
func SetModule[T any](ctx context.Context, module string) {
|
||||
c := Get[T](ctx)
|
||||
if c == nil {
|
||||
g.Log().Warning(ctx, "contexts.SetModule, c == nil ")
|
||||
return
|
||||
@@ -61,8 +61,8 @@ func SetModule(ctx context.Context, module string) {
|
||||
}
|
||||
|
||||
// GetUser 获取用户信息
|
||||
func GetUser(ctx context.Context) *model.Identity {
|
||||
c := Get(ctx)
|
||||
func GetUser[T any](ctx context.Context) *model.Identity {
|
||||
c := Get[T](ctx)
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -70,8 +70,8 @@ func GetUser(ctx context.Context) *model.Identity {
|
||||
}
|
||||
|
||||
// GetUserId 获取用户ID
|
||||
func GetUserId(ctx context.Context) int64 {
|
||||
user := GetUser(ctx)
|
||||
func GetUserId[T any](ctx context.Context) int64 {
|
||||
user := GetUser[T](ctx)
|
||||
if user == nil {
|
||||
return 0
|
||||
}
|
||||
@@ -79,8 +79,8 @@ func GetUserId(ctx context.Context) int64 {
|
||||
}
|
||||
|
||||
// GetRoleId 获取用户角色ID
|
||||
func GetRoleId(ctx context.Context) int64 {
|
||||
user := GetUser(ctx)
|
||||
func GetRoleId[T any](ctx context.Context) int64 {
|
||||
user := GetUser[T](ctx)
|
||||
if user == nil {
|
||||
return 0
|
||||
}
|
||||
@@ -88,8 +88,8 @@ func GetRoleId(ctx context.Context) int64 {
|
||||
}
|
||||
|
||||
// GetRoleKey 获取用户角色唯一编码
|
||||
func GetRoleKey(ctx context.Context) string {
|
||||
user := GetUser(ctx)
|
||||
func GetRoleKey[T any](ctx context.Context) string {
|
||||
user := GetUser[T](ctx)
|
||||
if user == nil {
|
||||
return ""
|
||||
}
|
||||
@@ -97,8 +97,8 @@ func GetRoleKey(ctx context.Context) string {
|
||||
}
|
||||
|
||||
// GetDeptType 获取用户部门类型
|
||||
func GetDeptType(ctx context.Context) string {
|
||||
user := GetUser(ctx)
|
||||
func GetDeptType[T any](ctx context.Context) string {
|
||||
user := GetUser[T](ctx)
|
||||
if user == nil {
|
||||
return ""
|
||||
}
|
||||
@@ -106,28 +106,28 @@ func GetDeptType(ctx context.Context) string {
|
||||
}
|
||||
|
||||
// IsCompanyDept 是否为公司部门
|
||||
func IsCompanyDept(ctx context.Context) bool {
|
||||
return GetDeptType(ctx) == consts.DeptTypeCompany
|
||||
func IsCompanyDept[T any](ctx context.Context) bool {
|
||||
return GetDeptType[T](ctx) == consts.DeptTypeCompany
|
||||
}
|
||||
|
||||
// IsTenantDept 是否为租户部门
|
||||
func IsTenantDept(ctx context.Context) bool {
|
||||
return GetDeptType(ctx) == consts.DeptTypeTenant
|
||||
func IsTenantDept[T any](ctx context.Context) bool {
|
||||
return GetDeptType[T](ctx) == consts.DeptTypeTenant
|
||||
}
|
||||
|
||||
// IsMerchantDept 是否为商户部门
|
||||
func IsMerchantDept(ctx context.Context) bool {
|
||||
return GetDeptType(ctx) == consts.DeptTypeMerchant
|
||||
func IsMerchantDept[T any](ctx context.Context) bool {
|
||||
return GetDeptType[T](ctx) == consts.DeptTypeMerchant
|
||||
}
|
||||
|
||||
// IsUserDept 是否为普通用户部门
|
||||
func IsUserDept(ctx context.Context) bool {
|
||||
return GetDeptType(ctx) == consts.DeptTypeUser
|
||||
func IsUserDept[T any](ctx context.Context) bool {
|
||||
return GetDeptType[T](ctx) == consts.DeptTypeUser
|
||||
}
|
||||
|
||||
// GetModule 获取应用模块
|
||||
func GetModule(ctx context.Context) string {
|
||||
c := Get(ctx)
|
||||
func GetModule[T any](ctx context.Context) string {
|
||||
c := Get[T](ctx)
|
||||
if c == nil {
|
||||
return ""
|
||||
}
|
||||
@@ -135,59 +135,59 @@ func GetModule(ctx context.Context) string {
|
||||
}
|
||||
|
||||
// SetAddonName 设置插件信息
|
||||
func SetAddonName(ctx context.Context, name string) {
|
||||
c := Get(ctx)
|
||||
func SetAddonName[T any](ctx context.Context, name string) {
|
||||
c := Get[T](ctx)
|
||||
if c == nil {
|
||||
g.Log().Warning(ctx, "contexts.SetAddonName, c == nil ")
|
||||
return
|
||||
}
|
||||
Get(ctx).AddonName = name
|
||||
Get[T](ctx).AddonName = name
|
||||
}
|
||||
|
||||
// IsAddonRequest 是否为插件模块请求
|
||||
func IsAddonRequest(ctx context.Context) bool {
|
||||
c := Get(ctx)
|
||||
func IsAddonRequest[T any](ctx context.Context) bool {
|
||||
c := Get[T](ctx)
|
||||
if c == nil {
|
||||
return false
|
||||
}
|
||||
return GetAddonName(ctx) != ""
|
||||
return GetAddonName[T](ctx) != ""
|
||||
}
|
||||
|
||||
// GetAddonName 获取插件信息
|
||||
func GetAddonName(ctx context.Context) string {
|
||||
c := Get(ctx)
|
||||
func GetAddonName[T any](ctx context.Context) string {
|
||||
c := Get[T](ctx)
|
||||
if c == nil {
|
||||
return ""
|
||||
}
|
||||
return Get(ctx).AddonName
|
||||
return Get[T](ctx).AddonName
|
||||
}
|
||||
|
||||
// SetData 设置额外数据
|
||||
func SetData(ctx context.Context, k string, v interface{}) {
|
||||
c := Get(ctx)
|
||||
func SetData[T any](ctx context.Context, k string, v interface{}) {
|
||||
c := Get[T](ctx)
|
||||
if c == nil {
|
||||
g.Log().Warning(ctx, "contexts.SetData, c == nil ")
|
||||
return
|
||||
}
|
||||
Get(ctx).Data[k] = v
|
||||
Get[T](ctx).Data[k] = v
|
||||
}
|
||||
|
||||
// SetDataMap 设置额外数据
|
||||
func SetDataMap(ctx context.Context, vs g.Map) {
|
||||
c := Get(ctx)
|
||||
func SetDataMap[T any](ctx context.Context, vs g.Map) {
|
||||
c := Get[T](ctx)
|
||||
if c == nil {
|
||||
g.Log().Warning(ctx, "contexts.SetData, c == nil ")
|
||||
return
|
||||
}
|
||||
|
||||
for k, v := range vs {
|
||||
Get(ctx).Data[k] = v
|
||||
Get[T](ctx).Data[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
// GetData 获取额外数据
|
||||
func GetData(ctx context.Context) g.Map {
|
||||
c := Get(ctx)
|
||||
func GetData[T any](ctx context.Context) g.Map {
|
||||
c := Get[T](ctx)
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user