Преглед на файлове

【功能新增】新增返回结果包装的wrapper,可选用后端进行返回结果中字典等内容翻译

就是那个锅 преди 4 години
родител
ревизия
a5a1e081ed

+ 47 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/annotion/Wrapper.java

@ -0,0 +1,47 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.core.annotion;
import cn.stylefeng.guns.core.pojo.base.wrapper.BaseWrapper;
import java.lang.annotation.*;
/**
 * 结果包装的注解,一般用在Controller层,给最后响应结果做包装
 *
 * @author fengshuonan
 * @date 2020/7/24 17:10
 */
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Wrapper {
    /**
     * 具体包装类
     */
    Class<? extends BaseWrapper<?>>[] value();
}

+ 5 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/consts/AopSortConstant.java

@ -39,6 +39,11 @@ public interface AopSortConstant {
     */
    int GLOBAL_EXP_HANDLER_AOP = -120;
    /**
     * 结果包装的aop
     */
    int WRAPPER_AOP = -110;
    /**
     * 接口资源权限校验
     */

+ 6 - 1
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/consts/ExpEnumConstant.java

@ -85,8 +85,13 @@ public interface ExpEnumConstant {
    int SERVER_EXCEPTION_ENUM = 1600;
    /**
     * 状态枚举
     * 状态相关异常枚举
     */
    int STATUS_EXCEPTION_ENUM = 1700;
    /**
     * 包装相关异常枚举
     */
    int WRAPPER_EXCEPTION_ENUM = 1800;
}

+ 75 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/exception/enums/WrapperExceptionEnum.java

@ -0,0 +1,75 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.core.exception.enums;
import cn.stylefeng.guns.core.annotion.ExpEnumType;
import cn.stylefeng.guns.core.consts.ExpEnumConstant;
import cn.stylefeng.guns.core.exception.enums.abs.AbstractBaseExceptionEnum;
import cn.stylefeng.guns.core.factory.ExpEnumCodeFactory;
/**
 * 对象包装异常
 *
 * @author fengshuonan
 * @date 2020/7/24 14:36
 */
@ExpEnumType(module = ExpEnumConstant.GUNS_CORE_MODULE_EXP_CODE, kind = ExpEnumConstant.WRAPPER_EXCEPTION_ENUM)
public enum WrapperExceptionEnum implements AbstractBaseExceptionEnum {
    /**
     * 被包装的值不能是基本类型
     */
    BASIC_TYPE_ERROR(1, "被包装的值不能是基本类型"),
    /**
     * 获取转化字段的值异常
     */
    TRANSFER_FILED_VALUE_ERROR(2, "获取转化字段的值异常"),
    /**
     * 字段包装转化异常
     */
    TRANSFER_ERROR(3, "字段包装转化异常");
    private final Integer code;
    private final String message;
    WrapperExceptionEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
    @Override
    public Integer getCode() {
        return ExpEnumCodeFactory.getExpEnumCode(this.getClass(), code);
    }
    @Override
    public String getMessage() {
        return message;
    }
}

+ 47 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/pojo/base/wrapper/BaseWrapper.java

@ -0,0 +1,47 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.core.pojo.base.wrapper;
import java.util.Map;
/**
 * 基础包装接口,
 *
 * @author fengshuonan
 * @date 2020/7/24 17:18
 */
public interface BaseWrapper<T> {
    /**
     * 具体包装的过程
     *
     * @param beWrappedModel 被包装的原始对象,可以是obj,list,page,PageResult
     * @return 包装后增加的增量集合
     * @author fengshuonan
     * @date 2020/7/24 17:22
     */
    Map<String, Object> doWrap(T beWrappedModel);
}

+ 241 - 0
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/core/aop/WrapperAop.java

@ -0,0 +1,241 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.sys.core.aop;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.log.Log;
import cn.stylefeng.guns.core.annotion.Wrapper;
import cn.stylefeng.guns.core.consts.AopSortConstant;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.exception.enums.WrapperExceptionEnum;
import cn.stylefeng.guns.core.pojo.base.wrapper.BaseWrapper;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
/**
 * controller结果包装的aop
 *
 * @author fengshuonan
 * @date 2020/7/24 17:42
 */
@Aspect
@Order(AopSortConstant.WRAPPER_AOP)
public class WrapperAop {
    private static final Log log = Log.get();
    /**
     * 切入点
     *
     * @author fengshuonan
     * @date 2020/7/24 17:42
     */
    @Pointcut("@annotation(cn.stylefeng.guns.core.annotion.Wrapper)")
    private void wrapperPointcut() {
    }
    /**
     * 执行具体的包装过程
     *
     * @author fengshuonan
     * @date 2020/7/24 17:44
     */
    @Around("wrapperPointcut()")
    public Object doWrapper(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // 直接执行原有业务逻辑
        Object proceedResult = proceedingJoinPoint.proceed();
        return processWrapping(proceedingJoinPoint, proceedResult);
    }
    /**
     * 具体包装过程
     *
     * @author fengshuonan
     * @date 2020/7/24 17:53
     */
    @SuppressWarnings("all")
    private Object processWrapping(ProceedingJoinPoint proceedingJoinPoint, Object originResult) throws IllegalAccessException, InstantiationException {
        // 获取@Wrapper注解
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = methodSignature.getMethod();
        Wrapper wrapperAnnotation = method.getAnnotation(Wrapper.class);
        // 获取注解上的处理类
        Class<? extends BaseWrapper<?>>[] baseWrapperClasses = wrapperAnnotation.value();
        // 如果注解上的为空直接返回
        if (ObjectUtil.isEmpty(baseWrapperClasses)) {
            return originResult;
        }
        // 获取原有返回结果,如果不是ResponseData则不进行处理(需要遵守这个约定)
        if (!(originResult instanceof ResponseData)) {
            log.warn(">>> 当前请求的返回结果不是ResponseData类型,直接返回原值!");
            return originResult;
        }
        // 获取ResponseData中的值
        ResponseData responseData = (ResponseData) originResult;
        Object beWrapped = responseData.getData();
        // 如果是基本类型,不进行加工处理
        if (ObjectUtil.isBasicType(beWrapped)) {
            throw new ServiceException(WrapperExceptionEnum.BASIC_TYPE_ERROR);
        }
        // 如果是Page类型
        if (beWrapped instanceof Page) {
            // 获取Page原有对象
            Page page = (Page) beWrapped;
            // 将page中所有records都包装一遍
            ArrayList<Map<String, Object>> maps = new ArrayList<>();
            for (Object wrappedItem : page.getRecords()) {
                maps.add(this.wrapPureObject(wrappedItem, baseWrapperClasses));
            }
            page.setRecords(maps);
            responseData.setData(page);
        }
        // 如果是PageResult类型
        else if (beWrapped instanceof PageResult) {
            // 获取PageResult原有对象
            PageResult pageResult = (PageResult) beWrapped;
            // 将PageResult中所有rows都包装一遍
            ArrayList<Map<String, Object>> maps = new ArrayList<>();
            for (Object wrappedItem : pageResult.getRows()) {
                maps.add(this.wrapPureObject(wrappedItem, baseWrapperClasses));
            }
            pageResult.setRows(maps);
            responseData.setData(pageResult);
        }
        // 如果是List类型
        else if (beWrapped instanceof Collection) {
            // 获取原有的List
            Collection collection = (Collection) beWrapped;
            // 将page中所有records都包装一遍
            ArrayList<Map<String, Object>> maps = new ArrayList<>();
            for (Object wrappedItem : collection) {
                maps.add(this.wrapPureObject(wrappedItem, baseWrapperClasses));
            }
            responseData.setData(maps);
        }
        // 如果是Array类型
        else if (ArrayUtil.isArray(beWrapped)) {
            // 获取原有的Array
            Object[] objects = this.objToArray(beWrapped);
            // 将array中所有records都包装一遍
            ArrayList<Map<String, Object>> maps = new ArrayList<>();
            for (Object wrappedItem : objects) {
                maps.add(this.wrapPureObject(wrappedItem, baseWrapperClasses));
            }
            responseData.setData(maps);
        }
        // 如果是Object类型
        else {
            responseData.setData(this.wrapPureObject(beWrapped, baseWrapperClasses));
        }
        return responseData;
    }
    /**
     * 原始对象包装成一个map的过程
     * <p>
     * 期间多次根据BaseWrapper接口方法执行包装过程
     *
     * @author fengshuonan
     * @date 2020/7/24 21:40
     */
    @SuppressWarnings("all")
    private Map<String, Object> wrapPureObject(Object originModel, Class<? extends BaseWrapper<?>>[] baseWrapperClasses) {
        // 首先将原始的对象转化为map
        Map<String, Object> originMap = BeanUtil.beanToMap(originModel);
        // 经过多个包装类填充属性
        try {
            for (Class<? extends BaseWrapper<?>> baseWrapperClass : baseWrapperClasses) {
                BaseWrapper baseWrapper = baseWrapperClass.newInstance();
                Map<String, Object> incrementFieldsMap = baseWrapper.doWrap(originModel);
                originMap.putAll(incrementFieldsMap);
            }
        } catch (Exception e) {
            log.error(">>> 原始对象包装过程,字段转化异常", e);
            throw new ServiceException(WrapperExceptionEnum.TRANSFER_ERROR);
        }
        return originMap;
    }
    /**
     * Object转为一个array,确保object为数组类型
     *
     * @author fengshuonan
     * @date 2020/7/24 22:06
     */
    private Object[] objToArray(Object object) {
        int length = Array.getLength(object);
        Object[] result = new Object[length];
        for (int i = 0; i < result.length; i++) {
            result[i] = Array.get(object, i);
        }
        return result;
    }
}