refactor: 项目结构合理性优化

This commit is contained in:
haoxr
2023-06-03 11:03:12 +08:00
parent 808c33789c
commit 532b632ccc
124 changed files with 419 additions and 414 deletions

View File

@@ -0,0 +1,30 @@
package com.youlai.system.common.annotation;
import java.lang.annotation.*;
/**
* MP数据权限注解
* <p>
* https://gitee.com/baomidou/mybatis-plus/issues/I37I90
*
* @author <a href="mailto:2256222053@qq.com">zc</a>
* @since 2021-12-10
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface DataPermission {
/**
* 数据权限 {@link com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor}
*/
String deptAlias() default "";
String deptIdColumnName() default "dept_id";
String userAlias() default "";
String userIdColumnName() default "create_by";
}

View File

@@ -0,0 +1,25 @@
package com.youlai.system.common.annotation;
import java.lang.annotation.*;
/**
* 防止重复提交注解
*
* @author haoxr
* @since 2.3.0
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface PreventDuplicateSubmit {
/**
* 防重提交锁过期时间(秒)
* <p>
* 默认5秒内不允许重复提交
*/
int expire() default 5;
}

View File

@@ -8,7 +8,7 @@ import lombok.Data;
* 基础分页请求对象
*
* @author haoxr
* @date 2021/2/28
* @since 2021/2/28
*/
@Data
@Schema

View File

@@ -9,7 +9,7 @@ import java.io.Serializable;
* 视图对象基类
*
* @author haoxr
* @date 2022/10/22
* @since 2022/10/22
*/
@Data
@ToString

View File

@@ -10,7 +10,7 @@ import java.util.Objects;
* 枚举通用接口
*
* @author haoxr
* @date 2022/3/27 12:06
* @since 2022/3/27 12:06
*/
public interface IBaseEnum<T> {

View File

@@ -3,8 +3,8 @@ package com.youlai.system.common.constant;
/**
* Excel 常量
*
* @author: haoxr
* @date: 2023/03/24
* @author haoxr
* @since 2023/03/24
*/
public interface ExcelConstants {

View File

@@ -3,8 +3,8 @@ package com.youlai.system.common.constant;
/**
* Security 常量
*
* @author: haoxr
* @date: 2023/03/24
* @author haoxr
* @since 2023/03/24
*/
public interface SecurityConstants {

View File

@@ -0,0 +1,28 @@
package com.youlai.system.common.enums;
/**
* EasyCaptcha 验证码类型枚举
*
* @author haoxr
* @since 2023/03/24
*/
public enum CaptchaTypeEnum {
/**
* 算数
*/
ARITHMETIC,
/**
* 中文
*/
CHINESE,
/**
* 中文闪图
*/
CHINESE_GIF,
/**
* 闪图
*/
GIF,
SPEC
}

View File

@@ -7,7 +7,7 @@ import lombok.Getter;
* 数据权限枚举
*
* @author haoxr
* @date 2022/10/14
* @since 2022/10/14
*/
public enum DataScopeEnum implements IBaseEnum<Integer> {

View File

@@ -8,7 +8,7 @@ import lombok.Getter;
* 性别枚举
*
* @author haoxr
* @date 2022/10/14
* @since 2022/10/14
*/
@Schema(enumAsRef = true)
public enum GenderEnum implements IBaseEnum<Integer> {

View File

@@ -8,7 +8,7 @@ import lombok.Getter;
* 菜单类型枚举
*
* @author haoxr
* @date 2022/4/23 9:36
* @since 2022/4/23 9:36
*/
public enum MenuTypeEnum implements IBaseEnum<Integer> {

View File

@@ -7,7 +7,7 @@ import lombok.Getter;
* 状态枚举
*
* @author haoxr
* @date 2022/10/14
* @since 2022/10/14
*/
public enum StatusEnum implements IBaseEnum<Integer> {

View File

@@ -7,7 +7,7 @@ import lombok.Getter;
* 自定义业务异常
*
* @author haoxr
* @date 2022/7/31
* @since 2022/7/31
*/
@Getter
public class BusinessException extends RuntimeException {

View File

@@ -0,0 +1,42 @@
package com.youlai.system.common.model;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* 下拉选项对象
*
* @author haoxr
* @since 2022/1/22
*/
@Schema(description ="下拉选项对象")
@Data
@NoArgsConstructor
public class Option<T> {
public Option(T value, String label) {
this.value = value;
this.label = label;
}
public Option(T value, String label, List<Option> children) {
this.value = value;
this.label = label;
this.children= children;
}
@Schema(description="选项的值")
private T value;
@Schema(description="选项的标签")
private String label;
@Schema(description="子选项列表")
@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
private List<Option> children;
}

View File

@@ -10,7 +10,7 @@ import java.util.List;
* 分页响应结构体
*
* @author haoxr
* @date 2022/2/18 23:29
* @since 2022/2/18 23:29
*/
@Data
public class PageResult<T> implements Serializable {

View File

@@ -8,7 +8,7 @@ import java.io.Serializable;
* 统一响应结构体
*
* @author haoxr
* @date 2022/1/30
* @since 2022/1/30
**/
@Data
public class Result<T> implements Serializable {

View File

@@ -9,7 +9,7 @@ import java.io.Serializable;
* 响应码枚举
*
* @author haoxr
* @date 2020-06-23
* @since 2020-06-23
**/
@AllArgsConstructor
@NoArgsConstructor

View File

@@ -1,21 +1,20 @@
package com.youlai.system.common.util;
import com.alibaba.excel.EasyExcel;
import com.youlai.system.framework.easyexcel.MyAnalysisEventListener;
import com.youlai.system.listener.easyexcel.MyAnalysisEventListener;
import java.io.InputStream;
/**
* Excel 工具类
*
* @author: haoxr
* @date: 2023/03/01
* @author haoxr
* @since 2023/03/01
*/
public class ExcelUtils {
public static <T> String importExcel(InputStream is, Class clazz, MyAnalysisEventListener<T> listener) {
EasyExcel.read(is, clazz, listener).sheet().doRead();
String msg = listener.getMsg();
return msg;
return listener.getMsg();
}
}

View File

@@ -14,7 +14,7 @@ import java.io.IOException;
* 响应工具类
*
* @author haoxr
* @date 2022/10/18
* @since 2022/10/18
*/
public class ResponseUtils {

View File

@@ -0,0 +1,141 @@
package com.youlai.system.common.util;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.youlai.system.common.constant.SystemConstants;
import com.youlai.system.security.userdetails.SysUserDetails;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.PatternMatchUtils;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
public class SecurityUtils {
/**
* 获取当前登录人信息
*
* @return
*/
public static SysUserDetails getUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
Object principal = authentication.getPrincipal();
if (principal instanceof SysUserDetails) {
return (SysUserDetails) authentication.getPrincipal();
}
}
return null;
}
/**
* 获取用户ID
*
* @return
*/
public static Long getUserId() {
Long userId = Convert.toLong(getUser().getUserId());
return userId;
}
/**
* 获取部门ID
*
* @return
*/
public static Long getDeptId() {
Long userId = Convert.toLong(getUser().getDeptId());
return userId;
}
/**
* 获取数据权限范围
*
* @return DataScope
*/
public static Integer getDataScope() {
Integer dataScope = Convert.toInt(getUser().getDataScope());
return dataScope;
}
/**
* 获取用户角色集合
*
* @return
*/
public static Set<String> getRoles() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
if (CollectionUtil.isNotEmpty(authorities)) {
Set<String> roles = authorities.stream().filter(item -> item.getAuthority().startsWith("ROLE_"))
.map(item -> StrUtil.removePrefix(item.getAuthority(), "ROLE_"))
.collect(Collectors.toSet());
return roles;
}
}
return Collections.EMPTY_SET;
}
/**
* 获取用户权限集合
*
* @return
*/
public static Set<String> getPerms() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
if (CollectionUtil.isNotEmpty(authorities)) {
Set<String> perms = authorities.stream().filter(item -> !item.getAuthority().startsWith("ROLE_"))
.map(item -> item.getAuthority())
.collect(Collectors.toSet());
return perms;
}
}
return Collections.EMPTY_SET;
}
/**
* 是否超级管理员
* <p>
* 超级管理员忽视任何权限判断
*
* @return
*/
public static boolean isRoot() {
Set<String> roles = getRoles();
if (roles.contains(SystemConstants.ROOT_ROLE_CODE)) {
return true;
}
return false;
}
/**
* 是否拥有权限判断
* <p>
* 适用业务判断(接口权限判断适用Spring Security 自带注解 PreAuthorize 判断即可 )
*
* @return
*/
public static boolean hasPerm(String perm) {
if (isRoot()) {
return true;
}
Set<String> perms = getPerms();
boolean hasPerm = perms.stream().anyMatch(item -> PatternMatchUtils.simpleMatch(perm, item));
return hasPerm;
}
}