mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-20 02:55:04 +08:00
update 发送短信逻辑
This commit is contained in:
@@ -74,7 +74,8 @@ public class MemberController extends BaseController {
|
||||
if (StringUtils.isBlank(dto.getMobileNumber())) {
|
||||
throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR);
|
||||
}
|
||||
String sendSMSResult = SMSUtil.sendSMS(request, dto.getMobileNumber());
|
||||
// String sendSMSResult = SMSUtil.sendSMS(request, dto.getMobileNumber());
|
||||
String sendSMSResult = SMSUtil.sendSMSV2(request, dto.getMobileNumber());
|
||||
response = new RestApiResponse<>(sendSMSResult);
|
||||
} catch (Exception e) {
|
||||
logger.error("下发短信接口 发生异常 error", e);
|
||||
|
||||
@@ -28,6 +28,9 @@ import com.jsowell.service.OrderService;
|
||||
import com.jsowell.service.TempService;
|
||||
import com.jsowell.thirdparty.platform.dto.PushOrderDTO;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.dromara.sms4j.api.SmsBlend;
|
||||
import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
import org.dromara.sms4j.core.factory.SmsFactory;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -963,4 +966,17 @@ public class TempController extends BaseController {
|
||||
logger.info("关闭支付未启动的订单, param:{}, result:{}", JSON.toJSONString(dto), response);
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试发送短信
|
||||
* http://localhost:8080/temp/testSendSMS
|
||||
*/
|
||||
@GetMapping("/testSendSMS")
|
||||
public void testSend(){
|
||||
// 在创建完SmsBlend实例后,再未手动调用注销的情况下框架会持有该实例,可以直接通过指定configId来获取想要的配置,如果你想使用
|
||||
// 负载均衡形式获取实例,只要使用getSmsBlend的无参重载方法即可,如果你仅有一个配置,也可以使用该方法
|
||||
SmsBlend smsBlend = SmsFactory.getSmsBlend("tx1");
|
||||
SmsResponse smsResponse = smsBlend.sendMessage("18521561107","123123");
|
||||
logger.info("发送短信结果:{}", smsResponse);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,3 +148,47 @@ chargeAlgorithm:
|
||||
url: http://150.158.199.92:58910/gateway/api/user/battery/algorithm/json
|
||||
clientId: e488bac5f70b496fa2d82065089e5f81
|
||||
|
||||
# sms4j
|
||||
sms:
|
||||
# 标注从yml读取配置
|
||||
config-type: yaml
|
||||
blends:
|
||||
# 自定义的标识,也就是configId这里可以是任意值(最好不要是中文)
|
||||
tx1:
|
||||
#厂商标识,标定此配置是哪个厂商,详细请看厂商标识介绍部分
|
||||
supplier: tencent
|
||||
#您的accessKey
|
||||
access-key-id: AKIDfDakkZBbqvnSt8Azb3JMKD3jCVcZJxPh
|
||||
#您的accessKeySecret
|
||||
access-key-secret: 95oKgpJeoncGoASXQ6qRp35wa2HKQVPA
|
||||
#您的短信签名
|
||||
signature: 举视新能源
|
||||
#模板ID 非必须配置,如果使用sendMessage的快速发送需此配置
|
||||
template-id: 1002460
|
||||
#您的sdkAppId
|
||||
sdk-app-id: 1400536771
|
||||
# 代理
|
||||
proxy:
|
||||
# 是否启用代理 默认关闭 需手动开启
|
||||
enable: false
|
||||
host: 127.0.0.1
|
||||
port: 8080
|
||||
ali1:
|
||||
#厂商标识,标定此配置是哪个厂商,详细请看厂商标识介绍部分
|
||||
supplier: alibaba
|
||||
#您的accessKey
|
||||
access-key-id: AKIDfDakkZBbqvnSt8Azb3JMKD3jCVcZJxPh
|
||||
#您的accessKeySecret
|
||||
access-key-secret: 95oKgpJeoncGoASXQ6qRp35wa2HKQVPA
|
||||
#您的短信签名
|
||||
signature: 举视上海新能源
|
||||
#模板ID 非必须配置,如果使用sendMessage的快速发送需此配置
|
||||
template-id: 1002460
|
||||
#您的sdkAppId
|
||||
sdk-app-id: 1400536771
|
||||
# 代理
|
||||
proxy:
|
||||
# 是否启用代理 默认关闭 需手动开启
|
||||
enable: false
|
||||
host: 127.0.0.1
|
||||
port: 8080
|
||||
@@ -228,6 +228,19 @@
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-spring-boot-starter</artifactId>
|
||||
<version>3.3.4</version>
|
||||
<exclusions>
|
||||
<!-- 排除京东短信内存在的fastjson等待作者后续修复 -->
|
||||
<exclusion>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
||||
@@ -8,6 +8,9 @@ import com.jsowell.common.constant.CacheConstants;
|
||||
import com.jsowell.common.constant.Constants;
|
||||
import com.jsowell.common.core.redis.RedisCache;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.SmsBlend;
|
||||
import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
import org.dromara.sms4j.core.factory.SmsFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -38,7 +41,7 @@ public class SMSUtil {
|
||||
private static final int APP_ID = 1400536771;
|
||||
|
||||
// 签名,使用的是签名内容,而不是签名ID
|
||||
private static final String SMS_SIGN = "举视新能源";
|
||||
private static final String SMS_SIGN = "举视上海新能源";
|
||||
|
||||
// 国家码 如 86 为中国
|
||||
private static final String NATION_CODE = "86";
|
||||
@@ -67,6 +70,36 @@ public class SMSUtil {
|
||||
return reStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信V2
|
||||
* @param request
|
||||
* @param phoneNumber
|
||||
* @return
|
||||
* @throws HTTPException
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String sendSMSV2(HttpServletRequest request, String phoneNumber) throws HTTPException, IOException {
|
||||
String reStr = "success"; //定义返回值
|
||||
//随机生成六位验证码
|
||||
String code = RandomUtil.getSMSVerificationCode();
|
||||
SmsBlend smsBlend = SmsFactory.getSmsBlend("tx1");
|
||||
SmsResponse smsResponse = smsBlend.sendMessage(phoneNumber, code);
|
||||
boolean success = smsResponse.isSuccess();
|
||||
if (!success) {
|
||||
reStr = "error";
|
||||
log.error("发送验证码失败:{}", JSON.toJSONString(smsResponse));
|
||||
} else {
|
||||
// 改为保存redis
|
||||
String redisKey = CacheConstants.SMS_VERIFICATION_CODE_KEY + phoneNumber;
|
||||
redisCache.setCacheObject(redisKey, code, CacheConstants.cache_expire_time_30m);
|
||||
}
|
||||
|
||||
// 新增一个通用验证码 2025年4月14日14点23分个别手机号无法收到验证码, 新增通用验证码
|
||||
String redisKey2 = CacheConstants.SMS_COMMON_VERIFICATION_CODE_KEY + phoneNumber;
|
||||
redisCache.setCacheObject(redisKey2, Constants.COMMON_VERIFICATION_CODE, CacheConstants.cache_expire_time_30m);
|
||||
return reStr;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println();
|
||||
|
||||
Reference in New Issue
Block a user