98 lines
3.7 KiB
Go
98 lines
3.7 KiB
Go
// Package mcache
|
|
// @Link https://github.com/xiujiecn/xiu-admin
|
|
// @Copyright Copyright (c) 2025 LiXiujie
|
|
// @Author Lxj <li@example.com>
|
|
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
|
package mcache
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
"git.5iot.com/iotlcm-public/iotlcm-app/internal/consts"
|
|
"git.5iot.com/iotlcm-public/iotlcm-app/internal/library/event"
|
|
"git.5iot.com/iotlcm-public/iotlcm-app/internal/service"
|
|
|
|
"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)
|
|
})
|
|
|
|
}
|
|
|
|
type requestAccessKey struct{}
|
|
|
|
type requestAccessSnapshot struct {
|
|
userId int64
|
|
codes []string
|
|
scopes []string
|
|
}
|
|
|
|
// WithFreshUserAccess 为小程序请求读取当前权限并保留请求内快照。
|
|
// 控制台清理本地缓存不会清理小程序进程内的权限缓存。
|
|
func WithFreshUserAccess(ctx context.Context, userId int64) (context.Context, error) {
|
|
codes, scopes, err := service.SysAuth().GetUserAccessCodeList(ctx, userId)
|
|
if err != nil {
|
|
return ctx, err
|
|
}
|
|
snapshot := requestAccessSnapshot{userId: userId, codes: ensureCurrentUserAccessCode(codes), scopes: scopes}
|
|
return context.WithValue(ctx, requestAccessKey{}, snapshot), nil
|
|
}
|
|
|
|
func GetUserAccessCodeList(ctx context.Context, userId int64) ([]string, []string, error) {
|
|
if snapshot, ok := ctx.Value(requestAccessKey{}).(requestAccessSnapshot); ok && snapshot.userId == userId {
|
|
return snapshot.codes, snapshot.scopes, nil
|
|
}
|
|
accessCodeList, err := Instance().Get(ctx, fmt.Sprintf(consts.MemCacheUserAccessCodeList, userId))
|
|
menuRoleDataAccessCodeList, err := Instance().Get(ctx, fmt.Sprintf(consts.MemCacheUserRoleDataAccessCodeList, userId))
|
|
if err != nil || accessCodeList == nil {
|
|
accessCodeCurrList, menuRoleDataAccessCodeList, err := service.SysAuth().GetUserAccessCodeList(ctx, userId)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
accessCodeCurrList = ensureCurrentUserAccessCode(accessCodeCurrList)
|
|
if len(accessCodeCurrList) == 0 {
|
|
accessCodeCurrList = []string{"null"}
|
|
}
|
|
Instance().Set(ctx, fmt.Sprintf(consts.MemCacheUserAccessCodeList, userId), accessCodeCurrList, time.Hour*24)
|
|
Instance().Set(ctx, fmt.Sprintf(consts.MemCacheUserRoleDataAccessCodeList, userId), menuRoleDataAccessCodeList, time.Hour*24)
|
|
return accessCodeCurrList, menuRoleDataAccessCodeList, nil
|
|
}
|
|
return ensureCurrentUserAccessCode(accessCodeList.Strings()), menuRoleDataAccessCodeList.Strings(), nil
|
|
}
|
|
|
|
func ensureCurrentUserAccessCode(accessCodeList []string) []string {
|
|
for _, code := range accessCodeList {
|
|
if code == consts.SysCheckPermissionCurrentUser {
|
|
return accessCodeList
|
|
}
|
|
}
|
|
return append(accessCodeList, consts.SysCheckPermissionCurrentUser)
|
|
}
|
|
|
|
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().Remove(ctx, fmt.Sprintf(consts.MemCacheUserRoleDataAccessCodeList, userId))
|
|
return err
|
|
}
|
|
|
|
func RemoveUserAccessCodeList(ctx context.Context, userId int64) error {
|
|
_, err := Instance().Remove(ctx, fmt.Sprintf(consts.MemCacheUserAccessCodeList, userId))
|
|
return err
|
|
}
|