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;
}
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;
}
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());
}
}
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);
}
}
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);
}
}
Aggregations