Browse Source

🎉 增加短信远程调用,增加短信发送工具类SmsUtil

test
smallchill 5 years ago
parent
commit
82f1f96450
  1. 10
      blade-ops-api/blade-flow-api/src/main/java/org/springblade/flow/core/feign/IFlowClient.java
  2. 28
      blade-ops-api/blade-resource-api/pom.xml
  3. 0
      blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/entity/Oss.java
  4. 0
      blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/entity/Sms.java
  5. 62
      blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/enums/SmsCodeEnum.java
  6. 72
      blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/feign/ISmsClient.java
  7. 43
      blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/feign/ISmsClientFallback.java
  8. 107
      blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/utils/SmsUtil.java
  9. 0
      blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/vo/OssVO.java
  10. 4
      blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/vo/SmsVO.java
  11. 1
      blade-ops-api/pom.xml
  12. 5
      blade-ops/blade-resource/pom.xml
  13. 24
      blade-ops/blade-resource/src/main/java/org/springblade/resource/builder/oss/OssBuilder.java
  14. 24
      blade-ops/blade-resource/src/main/java/org/springblade/resource/builder/sms/SmsBuilder.java
  15. 3
      blade-ops/blade-resource/src/main/java/org/springblade/resource/endpoint/OssEndpoint.java
  16. 16
      blade-ops/blade-resource/src/main/java/org/springblade/resource/endpoint/SmsEndpoint.java
  17. 67
      blade-ops/blade-resource/src/main/java/org/springblade/resource/feign/SmsClient.java

10
blade-ops-api/blade-flow-api/src/main/java/org/springblade/flow/core/feign/IFlowClient.java

@ -39,11 +39,11 @@ import java.util.Map;
public interface IFlowClient {
String API_PREFIX = "/client";
String START_PROCESS_INSTANCE_BY_ID = API_PREFIX + "start-process-instance-by-id";
String START_PROCESS_INSTANCE_BY_KEY = API_PREFIX + "start-process-instance-by-key";
String COMPLETE_TASK = API_PREFIX + "complete-task";
String TASK_VARIABLE = API_PREFIX + "task-variable";
String TASK_VARIABLES = API_PREFIX + "task-variables";
String START_PROCESS_INSTANCE_BY_ID = API_PREFIX + "/start-process-instance-by-id";
String START_PROCESS_INSTANCE_BY_KEY = API_PREFIX + "/start-process-instance-by-key";
String COMPLETE_TASK = API_PREFIX + "/complete-task";
String TASK_VARIABLE = API_PREFIX + "/task-variable";
String TASK_VARIABLES = API_PREFIX + "/task-variables";
/**
* 开启流程

28
blade-ops-api/blade-resource-api/pom.xml

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>blade-ops-api</artifactId>
<groupId>org.springblade</groupId>
<version>2.3.1.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>blade-resource-api</artifactId>
<name>${project.artifactId}</name>
<version>${bladex.project.version}</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-sms</artifactId>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-starter-tenant</artifactId>
</dependency>
</dependencies>
</project>

0
blade-ops/blade-resource/src/main/java/org/springblade/resource/entity/Oss.java → blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/entity/Oss.java

0
blade-ops/blade-resource/src/main/java/org/springblade/resource/entity/Sms.java → blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/entity/Sms.java

62
blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/enums/SmsCodeEnum.java

@ -0,0 +1,62 @@
/*
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the dreamlu.net developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package org.springblade.resource.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springblade.core.tool.utils.StringPool;
/**
* Sms资源编码枚举类
*
* @author Chill
* @apiNote 该枚举类对应短信配置模块的资源编码可根据业务需求自行拓展
*/
@Getter
@AllArgsConstructor
public enum SmsCodeEnum {
/**
* 默认编号
*/
DEFAULT(StringPool.EMPTY, 1),
/**
* 验证码编号
*/
VALIDATE("validate", 2),
/**
* 通知公告编号
*/
NOTICE("notice", 3),
/**
* 下单通知编号
*/
ORDER("order", 4),
/**
* 会议通知编号
*/
MEETING("meeting", 5),
;
final String name;
final int category;
}

72
blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/feign/ISmsClient.java

@ -0,0 +1,72 @@
/*
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the dreamlu.net developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package org.springblade.resource.feign;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.tool.api.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* ISmsClient
*
* @author Chill
*/
@FeignClient(
value = AppConstant.APPLICATION_RESOURCE_NAME,
fallback = ISmsClientFallback.class
)
public interface ISmsClient {
String API_PREFIX = "/client";
String SEND_MESSAGE = API_PREFIX + "/send-message";
String SEND_VALIDATE = API_PREFIX + "/send-validate";
String VALIDATE_MESSAGE = API_PREFIX + "/validate-message";
/**
* 通用短信发送
*
* @param code 资源编号
* @param params 模板参数
* @param phones 手机号集合
* @return R
*/
@PostMapping(SEND_MESSAGE)
R sendMessage(@RequestParam("code") String code, @RequestParam("params") String params, @RequestParam("phones") String phones);
/**
* 短信验证码发送
*
* @param code 资源编号
* @param phone 手机号
* @return R
*/
@PostMapping(SEND_VALIDATE)
R sendValidate(@RequestParam("code") String code, @RequestParam("phone") String phone);
/**
* 校验短信
*
* @param code 资源编号
* @param id 校验id
* @param value 校验值
* @return R
*/
@PostMapping(VALIDATE_MESSAGE)
R validateMessage(@RequestParam("code") String code, @RequestParam("id") String id, @RequestParam("value") String value);
}

43
blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/feign/ISmsClientFallback.java

@ -0,0 +1,43 @@
/*
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the dreamlu.net developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package org.springblade.resource.feign;
import org.springblade.core.tool.api.R;
import org.springframework.stereotype.Component;
/**
* 流程远程调用失败处理类
*
* @author Chill
*/
@Component
public class ISmsClientFallback implements ISmsClient {
@Override
public R sendMessage(String code, String params, String phones) {
return R.fail("远程调用失败");
}
@Override
public R sendValidate(String code, String phone) {
return R.fail("远程调用失败");
}
@Override
public R validateMessage(String code, String id, String value) {
return R.fail("远程调用失败");
}
}

107
blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/utils/SmsUtil.java

@ -0,0 +1,107 @@
/*
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the dreamlu.net developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package org.springblade.resource.utils;
import org.springblade.core.sms.model.SmsCode;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.core.tool.utils.RandomType;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.resource.feign.ISmsClient;
import java.util.HashMap;
import java.util.Map;
/**
* 短信服务工具类
*
* @author Chill
*/
public class SmsUtil {
public static final String PARAM_KEY = "code";
public static final String SEND_SUCCESS = "短信发送成功";
public static final String SEND_FAIL = "短信发送失败";
public static final String VALIDATE_SUCCESS = "短信校验成功";
public static final String VALIDATE_FAIL = "短信校验失败";
private static ISmsClient smsClient;
public static ISmsClient getSmsClient() {
if (smsClient == null) {
smsClient = SpringUtil.getBean(ISmsClient.class);
}
return smsClient;
}
/**
* 获取短信验证码参数
*
* @return 验证码参数
*/
public static Map<String, String> getValidateParams() {
Map<String, String> params = new HashMap<>(1);
params.put(PARAM_KEY, StringUtil.random(6, RandomType.INT));
return params;
}
/**
* 发送短信
*
* @param code 资源编号
* @param params 模板参数
* @param phones 手机号集合
* @return 发送结果
*/
public static boolean sendMessage(String code, Map<String, String> params, String phones) {
R result = getSmsClient().sendMessage(code, JsonUtil.toJson(params), phones);
return result.isSuccess();
}
/**
* 发送验证码
*
* @param code 资源编号
* @param phone 手机号
* @return 发送结果
*/
public static SmsCode sendValidate(String code, String phone) {
SmsCode smsCode = new SmsCode();
R result = getSmsClient().sendValidate(code, phone);
if (result.isSuccess()) {
smsCode = JsonUtil.parse(JsonUtil.toJson(result.getData()), SmsCode.class);
} else {
smsCode.setSuccess(Boolean.FALSE);
}
return smsCode;
}
/**
* 校验短信
*
* @param code 资源编号
* @param id 校验id
* @param value 校验值
* @return 发送结果
*/
public static boolean validateMessage(String code, String id, String value) {
R result = getSmsClient().validateMessage(code, id, value);
return result.isSuccess();
}
}

0
blade-ops/blade-resource/src/main/java/org/springblade/resource/vo/OssVO.java → blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/vo/OssVO.java

4
blade-ops/blade-resource/src/main/java/org/springblade/resource/vo/SmsVO.java → blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/vo/SmsVO.java

@ -16,10 +16,10 @@
*/
package org.springblade.resource.vo;
import org.springblade.resource.entity.Sms;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import io.swagger.annotations.ApiModel;
import org.springblade.resource.entity.Sms;
/**
* 短信配置表视图实体类

1
blade-ops-api/pom.xml

@ -17,6 +17,7 @@
<modules>
<module>blade-flow-api</module>
<module>blade-resource-api</module>
</modules>
<dependencies>

5
blade-ops/blade-resource/pom.xml

@ -35,6 +35,11 @@
<groupId>org.springblade</groupId>
<artifactId>blade-starter-sms-all</artifactId>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-resource-api</artifactId>
<version>${bladex.project.version}</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-dict-api</artifactId>

24
blade-ops/blade-resource/src/main/java/org/springblade/resource/builder/oss/OssBuilder.java

@ -19,6 +19,7 @@ package org.springblade.resource.builder.oss;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.oss.OssTemplate;
import org.springblade.core.oss.enums.OssEnum;
import org.springblade.core.oss.enums.OssStatusEnum;
@ -72,8 +73,18 @@ public class OssBuilder {
* @return OssTemplate
*/
public OssTemplate template() {
return template(StringPool.EMPTY);
}
/**
* 获取template
*
* @param code 资源编号
* @return OssTemplate
*/
public OssTemplate template(String code) {
String tenantId = SecureUtil.getTenantId();
Oss oss = getOss(tenantId);
Oss oss = getOss(tenantId, code);
Oss ossCached = ossPool.get(tenantId);
OssTemplate template = templatePool.get(tenantId);
// 若为空或者不一致,则重新加载
@ -111,18 +122,18 @@ public class OssBuilder {
* @param tenantId 租户ID
* @return Oss
*/
public Oss getOss(String tenantId) {
public Oss getOss(String tenantId, String code) {
String key = tenantId;
LambdaQueryWrapper<Oss> lqw = Wrappers.<Oss>query().lambda().eq(Oss::getTenantId, tenantId);
// 获取传参的资源编号并查询,若有则返回,若没有则调启用的配置
String ossCode = WebUtil.getParameter(OSS_PARAM_KEY);
String ossCode = StringUtil.isBlank(code) ? WebUtil.getParameter(OSS_PARAM_KEY) : code;
if (StringUtil.isNotBlank(ossCode)) {
key = key.concat(StringPool.DASH).concat(ossCode);
lqw.eq(Oss::getOssCode, ossCode);
} else {
lqw.eq(Oss::getStatus, OssStatusEnum.ENABLE.getNum());
}
return CacheUtil.get(RESOURCE_CACHE, OSS_CODE, key, () -> {
Oss oss = CacheUtil.get(RESOURCE_CACHE, OSS_CODE, key, () -> {
Oss o = ossMapper.selectOne(lqw);
// 若为空则调用默认配置
if ((Func.isEmpty(o))) {
@ -137,6 +148,11 @@ public class OssBuilder {
return o;
}
});
if (oss == null || oss.getId() == null) {
throw new ServiceException("未获取到对应的对象存储配置");
} else {
return oss;
}
}

24
blade-ops/blade-resource/src/main/java/org/springblade/resource/builder/sms/SmsBuilder.java

@ -19,6 +19,7 @@ package org.springblade.resource.builder.sms;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.redis.cache.BladeRedisCache;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.sms.SmsTemplate;
@ -75,8 +76,18 @@ public class SmsBuilder {
* @return SmsTemplate
*/
public SmsTemplate template() {
return template(StringPool.EMPTY);
}
/**
* 获取template
*
* @param code 资源编号
* @return SmsTemplate
*/
public SmsTemplate template(String code) {
String tenantId = SecureUtil.getTenantId();
Sms sms = getSms(tenantId);
Sms sms = getSms(tenantId, code);
Sms smsCached = smsPool.get(tenantId);
SmsTemplate template = templatePool.get(tenantId);
// 若为空或者不一致,则重新加载
@ -108,18 +119,18 @@ public class SmsBuilder {
* @param tenantId 租户ID
* @return Sms
*/
public Sms getSms(String tenantId) {
public Sms getSms(String tenantId, String code) {
String key = tenantId;
LambdaQueryWrapper<Sms> lqw = Wrappers.<Sms>query().lambda().eq(Sms::getTenantId, tenantId);
// 获取传参的资源编号并查询,若有则返回,若没有则调启用的配置
String smsCode = WebUtil.getParameter(SMS_PARAM_KEY);
String smsCode = StringUtil.isBlank(code) ? WebUtil.getParameter(SMS_PARAM_KEY) : code;
if (StringUtil.isNotBlank(smsCode)) {
key = key.concat(StringPool.DASH).concat(smsCode);
lqw.eq(Sms::getSmsCode, smsCode);
} else {
lqw.eq(Sms::getStatus, SmsStatusEnum.ENABLE.getNum());
}
return CacheUtil.get(RESOURCE_CACHE, SMS_CODE, key, () -> {
Sms sms = CacheUtil.get(RESOURCE_CACHE, SMS_CODE, key, () -> {
Sms s = smsMapper.selectOne(lqw);
// 若为空则调用默认配置
if ((Func.isEmpty(s))) {
@ -133,6 +144,11 @@ public class SmsBuilder {
return s;
}
});
if (sms == null || sms.getId() == null) {
throw new ServiceException("未获取到对应的短信配置");
} else {
return sms;
}
}
}

3
blade-ops/blade-resource/src/main/java/org/springblade/resource/endpoint/OssEndpoint.java

@ -40,6 +40,9 @@ import org.springframework.web.multipart.MultipartFile;
@Api(value = "对象存储端点", tags = "对象存储端点")
public class OssEndpoint {
/**
* 对象存储构建类
*/
private OssBuilder ossBuilder;
/**

16
blade-ops/blade-resource/src/main/java/org/springblade/resource/endpoint/SmsEndpoint.java

@ -24,8 +24,6 @@ import org.springblade.core.sms.model.SmsData;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.RandomType;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.resource.builder.sms.SmsBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@ -35,6 +33,8 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
import static org.springblade.resource.utils.SmsUtil.*;
/**
* 短信服务端点
*
@ -46,12 +46,9 @@ import java.util.Map;
@Api(value = "短信服务端点", tags = "短信服务端点")
public class SmsEndpoint {
private static final String PARAM_KEY = "code";
private static final String SEND_SUCCESS = "短信发送成功";
private static final String SEND_FAIL = "短信发送失败";
private static final String VALIDATE_SUCCESS = "短信校验成功";
private static final String VALIDATE_FAIL = "短信校验失败";
/**
* 短信服务构建类
*/
private SmsBuilder smsBuilder;
//================================= 短信服务校验 =================================
@ -64,8 +61,7 @@ public class SmsEndpoint {
@SneakyThrows
@PostMapping("/send-validate")
public R sendValidate(@RequestParam String phone) {
Map<String, String> params = new HashMap<>(1);
params.put(PARAM_KEY, StringUtil.random(6, RandomType.INT));
Map<String, String> params = getValidateParams();
SmsCode smsCode = smsBuilder.template().sendValidate(new SmsData(params).setKey(PARAM_KEY), phone);
return smsCode.isSuccess() ? R.data(smsCode, SEND_SUCCESS) : R.fail(SEND_FAIL);
}

67
blade-ops/blade-resource/src/main/java/org/springblade/resource/feign/SmsClient.java

@ -0,0 +1,67 @@
/*
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the dreamlu.net developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package org.springblade.resource.feign;
import lombok.AllArgsConstructor;
import org.springblade.core.sms.model.SmsCode;
import org.springblade.core.sms.model.SmsData;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.resource.builder.sms.SmsBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import static org.springblade.resource.utils.SmsUtil.*;
/**
* 短信远程调用服务
*
* @author Chill
*/
@RestController
@AllArgsConstructor
public class SmsClient implements ISmsClient {
private SmsBuilder smsBuilder;
@Override
@PostMapping(SEND_MESSAGE)
public R sendMessage(String code, String params, String phones) {
SmsData smsData = new SmsData(JsonUtil.readMap(params, String.class, String.class));
boolean temp = smsBuilder.template(code).sendMulti(smsData, Func.toStrList(phones));
return temp ? R.success(SEND_SUCCESS) : R.fail(SEND_FAIL);
}
@Override
@PostMapping(SEND_VALIDATE)
public R sendValidate(String code, String phone) {
Map<String, String> params = getValidateParams();
SmsCode smsCode = smsBuilder.template(code).sendValidate(new SmsData(params).setKey(PARAM_KEY), phone);
return smsCode.isSuccess() ? R.data(smsCode, SEND_SUCCESS) : R.fail(SEND_FAIL);
}
@Override
@PostMapping(VALIDATE_MESSAGE)
public R validateMessage(String code, String id, String value) {
SmsCode smsCode = new SmsCode().setId(id).setValue(value);
boolean validate = smsBuilder.template(code).validateMessage(smsCode);
return validate ? R.success(VALIDATE_SUCCESS) : R.fail(VALIDATE_FAIL);
}
}
Loading…
Cancel
Save