Search in sources :

Example 36 with ServiceException

use of com.ruoyi.common.core.exception.ServiceException in project RuoYi-Cloud by yangzongzhuan.

the class SysLoginService method login.

/**
 * 登录
 */
public LoginUser login(String username, String password) {
    // 用户名或密码为空 错误
    if (StringUtils.isAnyBlank(username, password)) {
        recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
        throw new ServiceException("用户/密码必须填写");
    }
    // 密码如果不在指定范围内 错误
    if (password.length() < UserConstants.PASSWORD_MIN_LENGTH || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
        recordLogininfor(username, Constants.LOGIN_FAIL, "用户密码不在指定范围");
        throw new ServiceException("用户密码不在指定范围");
    }
    // 用户名不在指定范围内 错误
    if (username.length() < UserConstants.USERNAME_MIN_LENGTH || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
        recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
        throw new ServiceException("用户名不在指定范围");
    }
    // 查询用户信息
    R<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
    if (R.FAIL == userResult.getCode()) {
        throw new ServiceException(userResult.getMsg());
    }
    if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
        recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
        throw new ServiceException("登录用户:" + username + " 不存在");
    }
    LoginUser userInfo = userResult.getData();
    SysUser user = userResult.getData().getSysUser();
    if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
        recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
        throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
    }
    if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
        recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
        throw new ServiceException("对不起,您的账号:" + username + " 已停用");
    }
    if (!SecurityUtils.matchesPassword(password, user.getPassword())) {
        recordLogininfor(username, Constants.LOGIN_FAIL, "用户密码错误");
        throw new ServiceException("用户不存在/密码错误");
    }
    recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
    return userInfo;
}
Also used : ServiceException(com.ruoyi.common.core.exception.ServiceException) SysUser(com.ruoyi.system.api.domain.SysUser) LoginUser(com.ruoyi.system.api.model.LoginUser)

Example 37 with ServiceException

use of com.ruoyi.common.core.exception.ServiceException in project RuoYi-Cloud by yangzongzhuan.

the class SysLoginService method register.

/**
 * 注册
 */
public void register(String username, String password) {
    // 用户名或密码为空 错误
    if (StringUtils.isAnyBlank(username, password)) {
        throw new ServiceException("用户/密码必须填写");
    }
    if (username.length() < UserConstants.USERNAME_MIN_LENGTH || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
        throw new ServiceException("账户长度必须在2到20个字符之间");
    }
    if (password.length() < UserConstants.PASSWORD_MIN_LENGTH || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
        throw new ServiceException("密码长度必须在5到20个字符之间");
    }
    // 注册用户信息
    SysUser sysUser = new SysUser();
    sysUser.setUserName(username);
    sysUser.setNickName(username);
    sysUser.setPassword(SecurityUtils.encryptPassword(password));
    R<?> registerResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);
    if (R.FAIL == registerResult.getCode()) {
        throw new ServiceException(registerResult.getMsg());
    }
    recordLogininfor(username, Constants.REGISTER, "注册成功");
}
Also used : ServiceException(com.ruoyi.common.core.exception.ServiceException) SysUser(com.ruoyi.system.api.domain.SysUser)

Example 38 with ServiceException

use of com.ruoyi.common.core.exception.ServiceException in project RuoYi-Cloud-Plus by JavaLionLi.

the class SysOssController method upload.

/**
 * 上传OSS对象存储
 */
@ApiOperation("上传OSS对象存储")
@ApiImplicitParams({ @ApiImplicitParam(name = "file", value = "文件", paramType = "query", dataTypeClass = File.class, required = true) })
@SaCheckPermission("system:oss:upload")
@Log(title = "OSS对象存储", businessType = BusinessType.INSERT)
@PostMapping("/upload")
public R<Map<String, String>> upload(@RequestPart("file") MultipartFile file) {
    if (ObjectUtil.isNull(file)) {
        throw new ServiceException("上传文件不能为空");
    }
    SysOss oss = iSysOssService.upload(file);
    Map<String, String> map = new HashMap<>(2);
    map.put("url", oss.getUrl());
    map.put("fileName", oss.getOriginalName());
    map.put("ossId", oss.getOssId().toString());
    return R.ok(map);
}
Also used : SysOss(com.ruoyi.resource.domain.SysOss) ServiceException(com.ruoyi.common.core.exception.ServiceException) HashMap(java.util.HashMap) Log(com.ruoyi.common.log.annotation.Log) SaCheckPermission(cn.dev33.satoken.annotation.SaCheckPermission)

Example 39 with ServiceException

use of com.ruoyi.common.core.exception.ServiceException in project RuoYi-Cloud-Plus by JavaLionLi.

the class RemoteFileServiceImpl method upload.

/**
 * 文件上传请求
 */
@Transactional(rollbackFor = Exception.class)
@Override
public SysFile upload(String name, String originalFilename, String contentType, byte[] file) throws ServiceException {
    try {
        String suffix = StringUtils.substring(originalFilename, originalFilename.lastIndexOf("."), originalFilename.length());
        OssClient storage = OssFactory.instance();
        UploadResult uploadResult = storage.uploadSuffix(file, suffix, contentType);
        // 保存文件信息
        SysOss oss = new SysOss();
        oss.setUrl(uploadResult.getUrl());
        oss.setFileSuffix(suffix);
        oss.setFileName(uploadResult.getFilename());
        oss.setOriginalName(originalFilename);
        oss.setService(storage.getConfigKey());
        sysOssMapper.insert(oss);
        SysFile sysFile = new SysFile();
        sysFile.setName(uploadResult.getFilename());
        sysFile.setUrl(uploadResult.getUrl());
        return sysFile;
    } catch (Exception e) {
        log.error("上传文件失败", e);
        throw new ServiceException("上传文件失败");
    }
}
Also used : SysOss(com.ruoyi.resource.domain.SysOss) OssClient(com.ruoyi.common.oss.core.OssClient) ServiceException(com.ruoyi.common.core.exception.ServiceException) UploadResult(com.ruoyi.common.oss.entity.UploadResult) SysFile(com.ruoyi.resource.api.domain.SysFile) ServiceException(com.ruoyi.common.core.exception.ServiceException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 40 with ServiceException

use of com.ruoyi.common.core.exception.ServiceException in project RuoYi-Cloud-Plus by JavaLionLi.

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) java.util(java.util) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectUtil(cn.hutool.core.util.ObjectUtil) RequiredArgsConstructor(lombok.RequiredArgsConstructor) IdUtil(cn.hutool.core.util.IdUtil) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) Constants(com.ruoyi.common.core.constant.Constants) TableDataInfo(com.ruoyi.common.mybatis.core.page.TableDataInfo) LoginHelper(com.ruoyi.common.satoken.utils.LoginHelper) Function(java.util.function.Function) ServiceException(com.ruoyi.common.core.exception.ServiceException) Template(org.apache.velocity.Template) Wrapper(com.baomidou.mybatisplus.core.conditions.Wrapper) Service(org.springframework.stereotype.Service) JsonUtils(com.ruoyi.common.core.utils.JsonUtils) GenUtils(com.ruoyi.gen.util.GenUtils) GenTableColumnMapper(com.ruoyi.gen.mapper.GenTableColumnMapper) ZipEntry(java.util.zip.ZipEntry) GenTable(com.ruoyi.gen.domain.GenTable) VelocityUtils(com.ruoyi.gen.util.VelocityUtils) 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) GenTableMapper(com.ruoyi.gen.mapper.GenTableMapper) VelocityInitializer(com.ruoyi.gen.util.VelocityInitializer) StringWriter(java.io.StringWriter) Dict(cn.hutool.core.lang.Dict) GenConstants(com.ruoyi.common.core.constant.GenConstants) IOException(java.io.IOException) GenTableColumn(com.ruoyi.gen.domain.GenTableColumn) StringUtils(com.ruoyi.common.core.utils.StringUtils) VelocityContext(org.apache.velocity.VelocityContext) Collectors(java.util.stream.Collectors) FileUtils(com.ruoyi.common.core.utils.file.FileUtils) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) CollUtil(cn.hutool.core.collection.CollUtil) Slf4j(lombok.extern.slf4j.Slf4j) Velocity(org.apache.velocity.app.Velocity) PageQuery(com.ruoyi.common.mybatis.core.page.PageQuery) ServiceException(com.ruoyi.common.core.exception.ServiceException) GenTable(com.ruoyi.gen.domain.GenTable) GenTableColumn(com.ruoyi.gen.domain.GenTableColumn)

Aggregations

ServiceException (com.ruoyi.common.core.exception.ServiceException)49 IOException (java.io.IOException)10 GenTable (com.ruoyi.gen.domain.GenTable)9 SysUser (com.ruoyi.system.api.domain.SysUser)9 Transactional (org.springframework.transaction.annotation.Transactional)7 GenTableColumn (com.ruoyi.gen.domain.GenTableColumn)6 SysDept (com.ruoyi.system.api.domain.SysDept)6 SysRole (com.ruoyi.system.api.domain.SysRole)6 StringWriter (java.io.StringWriter)6 Template (org.apache.velocity.Template)6 VelocityContext (org.apache.velocity.VelocityContext)6 File (java.io.File)5 JSONObject (com.alibaba.fastjson.JSONObject)4 SysOss (com.ruoyi.resource.domain.SysOss)4 LoginUser (com.ruoyi.system.api.model.LoginUser)4 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)3 Constants (com.ruoyi.common.core.constant.Constants)3 GenConstants (com.ruoyi.common.core.constant.GenConstants)3 StringUtils (com.ruoyi.common.core.utils.StringUtils)3 GenTableColumnMapper (com.ruoyi.gen.mapper.GenTableColumnMapper)3