Search in sources :

Example 1 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Flowable-Plus by KonBAI-Q.

the class RateLimiterAspect method doBefore.

@Before("@annotation(rateLimiter)")
public void doBefore(JoinPoint point, RateLimiter rateLimiter) throws Throwable {
    int time = rateLimiter.time();
    int count = rateLimiter.count();
    String combineKey = getCombineKey(rateLimiter, point);
    try {
        RateType rateType = RateType.OVERALL;
        if (rateLimiter.limitType() == LimitType.CLUSTER) {
            rateType = RateType.PER_CLIENT;
        }
        long number = RedisUtils.rateLimiter(combineKey, rateType, count, time);
        if (number == -1) {
            throw new ServiceException(MessageUtils.message("rate.limiter.message"));
        }
        log.info("限制令牌 => {}, 剩余令牌 => {}, 缓存key => '{}'", count, number, combineKey);
    } catch (ServiceException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException("服务器限流异常,请稍候再试");
    }
}
Also used : ServiceException(com.ruoyi.common.exception.ServiceException) RateType(org.redisson.api.RateType) JoinPoint(org.aspectj.lang.JoinPoint) ServiceException(com.ruoyi.common.exception.ServiceException) Before(org.aspectj.lang.annotation.Before)

Example 2 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Flowable-Plus by KonBAI-Q.

the class RepeatSubmitAspect method doBefore.

@Before("@annotation(repeatSubmit)")
public void doBefore(JoinPoint point, RepeatSubmit repeatSubmit) throws Throwable {
    // 如果注解不为0 则使用注解数值
    long interval = repeatSubmitProperties.getInterval();
    if (repeatSubmit.interval() > 0) {
        interval = repeatSubmit.timeUnit().toMillis(repeatSubmit.interval());
    }
    if (interval < 1000) {
        throw new ServiceException("重复提交间隔时间不能小于'1'秒");
    }
    HttpServletRequest request = ServletUtils.getRequest();
    String nowParams = argsArrayToString(point.getArgs());
    // 请求地址(作为存放cache的key值)
    String url = request.getRequestURI();
    // 唯一值(没有消息头则使用请求地址)
    String submitKey = StringUtils.trimToEmpty(request.getHeader(SaManager.getConfig().getTokenName()));
    submitKey = SecureUtil.md5(submitKey + ":" + nowParams);
    // 唯一标识(指定key + url + 消息头)
    String cacheRepeatKey = Constants.REPEAT_SUBMIT_KEY + url + submitKey;
    String key = RedisUtils.getCacheObject(cacheRepeatKey);
    if (key == null) {
        RedisUtils.setCacheObject(cacheRepeatKey, "", interval, TimeUnit.MILLISECONDS);
        KEY_CACHE.set(cacheRepeatKey);
    } else {
        String message = repeatSubmit.message();
        if (StringUtils.startsWith(message, "{") && StringUtils.endsWith(message, "}")) {
            message = MessageUtils.message(StringUtils.substring(message, 1, message.length() - 1));
        }
        throw new ServiceException(message);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServiceException(com.ruoyi.common.exception.ServiceException) Before(org.aspectj.lang.annotation.Before)

Example 3 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Flowable-Plus by KonBAI-Q.

the class GenTableServiceImpl method synchDb.

/**
 * 同步数据库
 *
 * @param tableName 表名称
 */
@Override
public void synchDb(String tableName) {
    GenTable table = baseMapper.selectGenTableByName(tableName);
    List<GenTableColumn> tableColumns = table.getColumns();
    Map<String, GenTableColumn> tableColumnMap = tableColumns.stream().collect(Collectors.toMap(GenTableColumn::getColumnName, Function.identity()));
    List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
    if (CollUtil.isEmpty(dbTableColumns)) {
        throw new ServiceException("同步数据失败,原表结构不存在");
    }
    List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
    List<GenTableColumn> saveColumns = new ArrayList<>();
    dbTableColumns.forEach(column -> {
        GenUtils.initColumnField(column, table);
        if (tableColumnMap.containsKey(column.getColumnName())) {
            GenTableColumn prevColumn = tableColumnMap.get(column.getColumnName());
            column.setColumnId(prevColumn.getColumnId());
            if (column.isList()) {
                // 如果是列表,继续保留查询方式/字典类型选项
                column.setDictType(prevColumn.getDictType());
                column.setQueryType(prevColumn.getQueryType());
            }
            if (StringUtils.isNotEmpty(prevColumn.getIsRequired()) && !column.isPk() && (column.isInsert() || column.isEdit()) && ((column.isUsableColumn()) || (!column.isSuperColumn()))) {
                // 如果是(新增/修改&非主键/非忽略及父属性),继续保留必填/显示类型选项
                column.setIsRequired(prevColumn.getIsRequired());
                column.setHtmlType(prevColumn.getHtmlType());
            }
            genTableColumnMapper.updateById(column);
        } else {
            genTableColumnMapper.insert(column);
        }
    });
    if (CollUtil.isNotEmpty(saveColumns)) {
        genTableColumnMapper.insertBatch(saveColumns);
    }
    List<GenTableColumn> delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList());
    if (CollUtil.isNotEmpty(delColumns)) {
        List<Long> ids = delColumns.stream().map(GenTableColumn::getColumnId).collect(Collectors.toList());
        genTableColumnMapper.deleteBatchIds(ids);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) JsonUtils(com.ruoyi.common.utils.JsonUtils) java.util(java.util) TableDataInfo(com.ruoyi.common.core.page.TableDataInfo) GenTableColumn(com.ruoyi.generator.domain.GenTableColumn) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectUtil(cn.hutool.core.util.ObjectUtil) VelocityUtils(com.ruoyi.generator.util.VelocityUtils) ServiceException(com.ruoyi.common.exception.ServiceException) RequiredArgsConstructor(lombok.RequiredArgsConstructor) IdUtil(cn.hutool.core.util.IdUtil) DS(com.baomidou.dynamic.datasource.annotation.DS) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) Function(java.util.function.Function) PageQuery(com.ruoyi.common.core.domain.PageQuery) Template(org.apache.velocity.Template) StringUtils(com.ruoyi.common.utils.StringUtils) Service(org.springframework.stereotype.Service) ZipEntry(java.util.zip.ZipEntry) FileUtils(com.ruoyi.common.utils.file.FileUtils) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Wrappers(com.baomidou.mybatisplus.core.toolkit.Wrappers) IoUtil(cn.hutool.core.io.IoUtil) Snowflake(cn.hutool.core.lang.Snowflake) GenConstants(com.ruoyi.common.constant.GenConstants) StringWriter(java.io.StringWriter) Dict(cn.hutool.core.lang.Dict) IOException(java.io.IOException) GenUtils(com.ruoyi.generator.util.GenUtils) LoginHelper(com.ruoyi.common.helper.LoginHelper) GenTableMapper(com.ruoyi.generator.mapper.GenTableMapper) VelocityContext(org.apache.velocity.VelocityContext) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) VelocityInitializer(com.ruoyi.generator.util.VelocityInitializer) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) CollUtil(cn.hutool.core.collection.CollUtil) Slf4j(lombok.extern.slf4j.Slf4j) GenTable(com.ruoyi.generator.domain.GenTable) GenTableColumnMapper(com.ruoyi.generator.mapper.GenTableColumnMapper) Velocity(org.apache.velocity.app.Velocity) Constants(com.ruoyi.common.constant.Constants) ServiceException(com.ruoyi.common.exception.ServiceException) GenTable(com.ruoyi.generator.domain.GenTable) GenTableColumn(com.ruoyi.generator.domain.GenTableColumn)

Example 4 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Flowable-Plus by KonBAI-Q.

the class GenTableServiceImpl method generatorCode.

/**
 * 生成代码(自定义路径)
 *
 * @param tableName 表名称
 */
@Override
public void generatorCode(String tableName) {
    // 查询表信息
    GenTable table = baseMapper.selectGenTableByName(tableName);
    // 设置主子表信息
    setSubTable(table);
    // 设置主键列信息
    setPkColumn(table);
    VelocityInitializer.initVelocity();
    VelocityContext context = VelocityUtils.prepareContext(table);
    // 获取模板列表
    List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
    for (String template : templates) {
        if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm")) {
            // 渲染模板
            StringWriter sw = new StringWriter();
            Template tpl = Velocity.getTemplate(template, Constants.UTF8);
            tpl.merge(context, sw);
            try {
                String path = getGenPath(table, template);
                FileUtils.writeUtf8String(sw.toString(), path);
            } catch (Exception e) {
                throw new ServiceException("渲染模板失败,表名:" + table.getTableName());
            }
        }
    }
}
Also used : StringWriter(java.io.StringWriter) ServiceException(com.ruoyi.common.exception.ServiceException) GenTable(com.ruoyi.generator.domain.GenTable) VelocityContext(org.apache.velocity.VelocityContext) ServiceException(com.ruoyi.common.exception.ServiceException) IOException(java.io.IOException) Template(org.apache.velocity.Template)

Example 5 with ServiceException

use of com.ruoyi.common.exception.ServiceException in project RuoYi-Flowable-Plus by KonBAI-Q.

the class CreateAndUpdateMetaObjectHandler method insertFill.

@Override
public void insertFill(MetaObject metaObject) {
    try {
        if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
            BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
            Date current = ObjectUtil.isNotNull(baseEntity.getCreateTime()) ? baseEntity.getCreateTime() : new Date();
            baseEntity.setCreateTime(current);
            baseEntity.setUpdateTime(current);
            String username = StringUtils.isNotBlank(baseEntity.getCreateBy()) ? baseEntity.getCreateBy() : getLoginUsername();
            // 当前已登录 且 创建人为空 则填充
            baseEntity.setCreateBy(username);
            // 当前已登录 且 更新人为空 则填充
            baseEntity.setUpdateBy(username);
        }
    } catch (Exception e) {
        throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
    }
}
Also used : ServiceException(com.ruoyi.common.exception.ServiceException) BaseEntity(com.ruoyi.common.core.domain.BaseEntity) Date(java.util.Date) ServiceException(com.ruoyi.common.exception.ServiceException)

Aggregations

ServiceException (com.ruoyi.common.exception.ServiceException)109 IOException (java.io.IOException)21 Transactional (org.springframework.transaction.annotation.Transactional)21 GenTable (com.ruoyi.generator.domain.GenTable)12 StringWriter (java.io.StringWriter)12 Template (org.apache.velocity.Template)12 VelocityContext (org.apache.velocity.VelocityContext)12 SysRole (com.ruoyi.common.core.domain.entity.SysRole)10 File (java.io.File)10 SysUser (com.ruoyi.common.core.domain.entity.SysUser)9 GenTableColumn (com.ruoyi.generator.domain.GenTableColumn)8 Before (org.aspectj.lang.annotation.Before)8 JSONObject (com.alibaba.fastjson.JSONObject)6 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)6 SysDept (com.ruoyi.common.core.domain.entity.SysDept)6 LoginUser (com.ruoyi.common.core.domain.model.LoginUser)6 Constants (com.ruoyi.common.constant.Constants)5 GenConstants (com.ruoyi.common.constant.GenConstants)5 StringUtils (com.ruoyi.common.utils.StringUtils)5 SysOss (com.ruoyi.system.domain.SysOss)5