#AI
zhou zhou
16 小时以前 51877df13075ad10ef51107f15bcd21f1661febe
rsf-server/src/main/java/com/vincent/rsf/server/system/controller/BaseController.java
@@ -5,17 +5,18 @@
import com.vincent.rsf.server.common.domain.BaseParam;
import com.vincent.rsf.server.system.entity.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
 * Created by vincent on 1/30/2024
 */
public class BaseController {
    public User getLoginUser() {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
@@ -41,7 +42,42 @@
        return loginUser == null ? null : loginUser.getTenantId();
    }
    public boolean hasAuthority(String authority) {
        if (authority == null || authority.trim().isEmpty()) {
            return false;
        }
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if (authentication == null || authentication.getAuthorities() == null) {
                return false;
            }
            for (GrantedAuthority grantedAuthority : authentication.getAuthorities()) {
                if (grantedAuthority != null && authority.equals(grantedAuthority.getAuthority())) {
                    return true;
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return false;
    }
    public <T extends BaseParam> T buildParam(Map<String, Object> map, Class<T> clz) {
        if (!Objects.isNull(map.get("meta"))) {
            Map<String, Object> meta = (Map<String, Object>) map.get("meta");
            meta.keySet().forEach(key -> {
                map.put(key, meta.get(key));
            });
            map.remove("meta");
        }
        // 移除筛选条件里面的 $
        for (String key : map.keySet()) {
            Object value = map.get(key);
            if (key.equals("orderBy")) {
                String newValue = value.toString().replace("$", "");
                map.replace("orderBy", value, newValue);
            }
        }
        T t  = null;
        try {
            t = clz.getDeclaredConstructor().newInstance();
@@ -70,3 +106,4 @@
    }
}