Files
jsowell-charger-web/jsowell-common/src/main/java/com/jsowell/common/util/SMSUtil.java
2025-08-22 10:06:16 +08:00

109 lines
3.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.jsowell.common.util;
import com.alibaba.fastjson2.JSON;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
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;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* 发送短信验证码工具类
*/
@Slf4j
@Component
public class SMSUtil {
private static RedisCache redisCache;
@Autowired
public void setRedisCache(RedisCache redisCache) {
SMSUtil.redisCache = redisCache;
}
// 短信应用SDK AppKey
private static final String APP_KEY = "8ebcf52de98416814b440891350cd594";
// 短信模板ID需要在短信应用中申请
private static final int TEMPLATE_ID = 1002460;
// 短信应用SDK AppID 1400开头
private static final int APP_ID = 1400536771;
// 签名使用的是签名内容而不是签名ID
private static final String SMS_SIGN = "举视上海新能源";
// 国家码 如 86 为中国
private static final String NATION_CODE = "86";
public static String sendSMS(HttpServletRequest request, String phoneNumber) throws HTTPException, IOException {
String reStr = "success"; //定义返回值
//随机生成六位验证码
String code = RandomUtil.getSMSVerificationCode();
//参数,一定要对应短信模板中的参数顺序和个数,
String[] params = {code};
//创建ssender对象
SmsSingleSender ssender = new SmsSingleSender(APP_ID, APP_KEY);
//发送
SmsSingleSenderResult result = ssender.sendWithParam(NATION_CODE, phoneNumber, TEMPLATE_ID, params, SMS_SIGN, "", "");
if (result.result != 0) {
reStr = "error";
log.error("发送验证码失败:{}", JSON.toJSONString(result));
} else {
// 改为保存redis
String redisKey = CacheConstants.SMS_VERIFICATION_CODE_KEY + phoneNumber;
redisCache.setCacheObject(redisKey, code, CacheConstants.cache_expire_time_10m);
}
// 新增一个通用验证码 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;
}
/**
* 发送短信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();
}
}