63 lines
2.1 KiB
Go
63 lines
2.1 KiB
Go
// Package mcache
|
|
// @Link https://github.com/xiujiecn/xiu-admin
|
|
// @Copyright Copyright (c) 2025 LiXiujie
|
|
// @Author Lxj <xxx@example.com>
|
|
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
|
package mcache
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/consts"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/event"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/service"
|
|
"time"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
)
|
|
|
|
func init() {
|
|
event.EventsInstance().Register(consts.EventKeyUserLogout, func(ctx context.Context, eventKey string, args ...interface{}) {
|
|
if len(args) == 0 {
|
|
return
|
|
}
|
|
userId := args[0].(int64)
|
|
RemoveUserAccessCodeList(ctx, userId)
|
|
g.Log().Infof(ctx, "mcache_access_list.go EventKeyUserLogout userId: %d", userId)
|
|
})
|
|
|
|
}
|
|
|
|
var loadUserPermissions = func(ctx context.Context, userID int64) ([]string, []string, error) {
|
|
return service.SysAuth().GetUserAccessCodeList(ctx, userID)
|
|
}
|
|
|
|
func GetUserAccessCodeList(ctx context.Context, userId int64) ([]string, []string, error) {
|
|
codes, dataCodes, err := loadUserPermissions(ctx, userId)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if len(codes) == 0 {
|
|
codes = []string{"null"}
|
|
}
|
|
return codes, dataCodes, nil
|
|
}
|
|
|
|
func SetUserAccessCodeList(ctx context.Context, userId int64, accessCodeList []string) error {
|
|
return Instance().Set(ctx, fmt.Sprintf(consts.MemCacheUserAccessCodeList, userId), accessCodeList, time.Hour*24)
|
|
}
|
|
|
|
func SetUserRoleDataAccessCodeList(ctx context.Context, userId int64, roleDataAccessCodeList []string) error {
|
|
return Instance().Set(ctx, fmt.Sprintf(consts.MemCacheUserRoleDataAccessCodeList, userId), roleDataAccessCodeList, time.Hour*24)
|
|
}
|
|
|
|
func RemoveUserRoleDataAccessCodeList(ctx context.Context, userId int64) error {
|
|
err := Instance().Removes(ctx, []interface{}{fmt.Sprintf(consts.MemCacheUserRoleDataAccessCodeList, userId)})
|
|
return err
|
|
}
|
|
|
|
func RemoveUserAccessCodeList(ctx context.Context, userId int64) error {
|
|
err := Instance().Removes(ctx, []interface{}{fmt.Sprintf(consts.MemCacheUserAccessCodeList, userId)})
|
|
return err
|
|
}
|