Search in sources :

Example 11 with BusinessException

use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.

the class WxMpUserAuthServiceImpl method bindWxMp.

@Override
public IamMember bindWxMp(String code, String state) throws Exception {
    // 校验STATE
    if (V.notEmpty(STATE) && !STATE.equals(state)) {
        throw new BusinessException(Status.FAIL_INVALID_PARAM, "非法来源");
    }
    if (V.isEmpty(code)) {
        log.error("请求参数有误: code = null");
        throw new BusinessException(Status.FAIL_INVALID_PARAM, "请求参数有误: code is null");
    }
    IamUser iamUser = IamSecurityUtils.getCurrentUser();
    if (V.isEmpty(iamUser)) {
        throw new BusinessException(Status.FAIL_OPERATION, "请登陆后绑定");
    }
    WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
    // 获取用户信息
    IamMember iamMember = iamMemberService.getSingleEntity(Wrappers.<IamMember>lambdaQuery().eq(IamMember::getUserType, IamUser.class.getSimpleName()).eq(IamMember::getUserId, iamUser.getId()));
    if (V.notEmpty(iamMember)) {
        throw new BusinessException(Status.FAIL_OPERATION, "请勿重新绑定");
    }
    // 创建绑定
    WxOAuth2UserInfo userInfo = wxMpService.getOAuth2Service().getUserInfo(accessToken, null);
    iamMember = mpInfo2IamMemberEntity(userInfo).setUserId(iamUser.getId()).setOrgId(iamUser.getOrgId()).setUserType(IamUser.class.getSimpleName());
    iamMemberService.createEntity(iamMember);
    // 基于openId 创建iam_account账号
    IamAccount iamAccount = createIamAccountEntity(iamMember, iamMember.getUserId(), IamUser.class);
    iamAccountService.createEntity(iamAccount);
    return iamMember;
}
Also used : BusinessException(com.diboot.core.exception.BusinessException) IamAccount(com.diboot.iam.entity.IamAccount) IamUser(com.diboot.iam.entity.IamUser) IamMember(com.diboot.mobile.entity.IamMember) WxOAuth2AccessToken(me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken) WxOAuth2UserInfo(me.chanjar.weixin.common.bean.WxOAuth2UserInfo)

Example 12 with BusinessException

use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.

the class MessageServiceImpl method send.

@Override
public boolean send(Message message, BaseVariableData variableData) {
    // 获取发送通道
    ChannelStrategy channelStrategy = channelStrategyMap.get(message.getChannel());
    if (V.isEmpty(channelStrategy)) {
        log.error("[获取发送通道失败],当前发送通道为:{}", message.getChannel());
        throw new InvalidUsageException("获取发送通道失败! " + message.getChannel());
    }
    // 是否根据模板构建邮件内容
    LambdaQueryWrapper<MessageTemplate> queryWrapper = Wrappers.<MessageTemplate>lambdaQuery().eq(V.notEmpty(message.getTemplateId()), MessageTemplate::getId, message.getTemplateId()).eq(V.notEmpty(message.getTemplateCode()), MessageTemplate::getCode, message.getTemplateCode());
    if (queryWrapper.nonEmptyOfNormal()) {
        MessageTemplate messageTemplate = messageTemplateService.getSingleEntity(queryWrapper);
        if (V.isEmpty(messageTemplate)) {
            if (V.isEmpty(message.getTemplateCode())) {
                log.error("[获取模版失败] 模版id为:{}", message.getTemplateId());
            } else if (V.isEmpty(message.getTemplateId())) {
                log.error("[获取模版失败] 模版code为:{}", message.getTemplateCode());
            } else {
                log.error("[获取模版失败] 模版id为:{} ,模版code为:{}", message.getTemplateId(), message.getTemplateCode());
            }
            throw new BusinessException(Status.FAIL_OPERATION, "获取模版失败!");
        }
        message.setTemplateId(messageTemplate.getId());
        try {
            // 设置模版内容
            String content = templateVariableService.parseTemplate2Content(messageTemplate.getContent(), variableData);
            message.setContent(content);
        } catch (Exception e) {
            log.error("[消息解析失败],消息体为:{}", message);
            throw new BusinessException(Status.FAIL_OPERATION, "消息解析失败!");
        }
    }
    if (V.isEmpty(message.getContent())) {
        throw new BusinessException("消息内容不能为 null");
    }
    // 设置定时发送,则等待定时任务发送
    if (V.notEmpty(message.getScheduleTime())) {
        message.setStatus("SCHEDULE");
    }
    // 创建Message
    boolean success = createEntity(message);
    // 停止发送
    if (V.notEmpty(message.getScheduleTime())) {
        return true;
    }
    if (!success) {
        log.error("[消息创建失败],消息体为:{}", message);
        throw new BusinessException(Status.FAIL_OPERATION, "消息发送失败!");
    }
    // 异步发送消息
    channelStrategy.send(message);
    return true;
}
Also used : MessageTemplate(com.diboot.message.entity.MessageTemplate) BusinessException(com.diboot.core.exception.BusinessException) ChannelStrategy(com.diboot.message.channel.ChannelStrategy) InvalidUsageException(com.diboot.core.exception.InvalidUsageException) BusinessException(com.diboot.core.exception.BusinessException) InvalidUsageException(com.diboot.core.exception.InvalidUsageException)

Example 13 with BusinessException

use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.

the class WxAuthServiceImpl method applyToken.

@Override
public String applyToken(AuthCredential credential) {
    BaseJwtAuthToken authToken = initBaseJwtAuthToken(credential);
    try {
        Subject subject = SecurityUtils.getSubject();
        subject.login(authToken);
        if (subject.isAuthenticated()) {
            log.debug("申请token成功!authtoken={}", authToken.getCredentials());
            saveLoginTrace(authToken, true);
            // 跳转到首页
            return (String) authToken.getCredentials();
        } else {
            log.error("认证失败");
            saveLoginTrace(authToken, false);
            throw new BusinessException(Status.FAIL_OPERATION, "认证失败");
        }
    } catch (Exception e) {
        log.error("登录异常", e);
        saveLoginTrace(authToken, false);
        throw new BusinessException(Status.FAIL_OPERATION, e.getMessage());
    }
}
Also used : BaseJwtAuthToken(com.diboot.iam.jwt.BaseJwtAuthToken) BusinessException(com.diboot.core.exception.BusinessException) Subject(org.apache.shiro.subject.Subject) BusinessException(com.diboot.core.exception.BusinessException) AuthenticationException(org.apache.shiro.authc.AuthenticationException)

Example 14 with BusinessException

use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.

the class QuartzSchedulerService method addJobExecuteOnce.

/**
 * 添加一个立即执行一次的job
 *
 * @param job
 */
public void addJobExecuteOnce(ScheduleJob job) {
    TriggerKey triggerKey = TriggerKey.triggerKey(job.getId().toString());
    // 构建参数
    JobDetail jobDetail = JobBuilder.newJob(getJobClass(job.getJobKey())).withIdentity(job.getId().toString()).build();
    if (V.notEmpty(job.getParamJson())) {
        Map<String, Object> jsonData = JSON.toMap(job.getParamJson());
        jobDetail.getJobDataMap().putAll(jsonData);
    }
    try {
        // 立即执行,且只执行一次
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(0)).build();
        scheduler.scheduleJob(jobDetail, trigger);
    } catch (Exception e) {
        log.error("添加定时任务异常", e);
        throw new BusinessException(Status.FAIL_OPERATION, "添加定时任务异常", e);
    }
}
Also used : BusinessException(com.diboot.core.exception.BusinessException) BusinessException(com.diboot.core.exception.BusinessException)

Example 15 with BusinessException

use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.

the class QuartzSchedulerService method addJob.

/**
 * 添加 cron表达式job
 *
 * @param job
 */
public void addJob(ScheduleJob job) {
    TriggerKey triggerKey = TriggerKey.triggerKey(job.getId().toString());
    // 构建参数
    JobDetail jobDetail = JobBuilder.newJob(getJobClass(job.getJobKey())).withIdentity(job.getId().toString()).build();
    if (V.notEmpty(job.getParamJson())) {
        Map<String, Object> jsonData = JSON.toMap(job.getParamJson());
        jobDetail.getJobDataMap().putAll(jsonData);
    }
    try {
        // 表达式调度构建器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCron());
        // 设置定时任务初始化策略
        if (INIT_STRATEGY.FIRE_AND_PROCEED.name().equals(job.getInitStrategy())) {
            cronScheduleBuilder.withMisfireHandlingInstructionFireAndProceed();
        } else if (INIT_STRATEGY.IGNORE_MISFIRES.name().equals(job.getInitStrategy())) {
            cronScheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();
        }
        // 定时任务
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, trigger);
    } catch (Exception e) {
        log.error("添加定时任务异常", e);
        throw new BusinessException(Status.FAIL_OPERATION, "添加定时任务异常", e);
    }
}
Also used : BusinessException(com.diboot.core.exception.BusinessException) BusinessException(com.diboot.core.exception.BusinessException)

Aggregations

BusinessException (com.diboot.core.exception.BusinessException)29 Transactional (org.springframework.transaction.annotation.Transactional)8 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)5 IamAccount (com.diboot.iam.entity.IamAccount)5 InvalidUsageException (com.diboot.core.exception.InvalidUsageException)4 IamMember (com.diboot.mobile.entity.IamMember)4 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)3 Dictionary (com.diboot.core.entity.Dictionary)3 UploadFile (com.diboot.file.entity.UploadFile)3 IamResourcePermission (com.diboot.iam.entity.IamResourcePermission)3 BaseJwtAuthToken (com.diboot.iam.jwt.BaseJwtAuthToken)3 IamResourcePermissionListVO (com.diboot.iam.vo.IamResourcePermissionListVO)3 ScheduleJob (com.diboot.scheduler.entity.ScheduleJob)3 HashMap (java.util.HashMap)3 WxOAuth2AccessToken (me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken)3 AuthenticationException (org.apache.shiro.authc.AuthenticationException)3 Subject (org.apache.shiro.subject.Subject)3 Wrappers (com.baomidou.mybatisplus.core.toolkit.Wrappers)2 BaseService (com.diboot.core.service.BaseService)2 DictionaryService (com.diboot.core.service.DictionaryService)2