Commit 337d1896 by 刘红梅

发送短信代码

parent cb112b06
...@@ -19,10 +19,10 @@ public interface SmsService { ...@@ -19,10 +19,10 @@ public interface SmsService {
*/ */
boolean sendSmsCode(String phone); boolean sendSmsCode(String phone);
boolean sendMasSmsCode(String phone); boolean sendGlySmsCode(String phone);
boolean sendMasSmsContent(String phone, String content); boolean sendGlySmsContent(String phone, String content, String code);
void asyncSendMasSmsContent(String phone, String content); void asyncSendGlySmsContent(String phone, String content, String code);
} }
...@@ -2,6 +2,7 @@ package com.cnooc.expert.auth.service.impl; ...@@ -2,6 +2,7 @@ package com.cnooc.expert.auth.service.impl;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import com.cnooc.expert.common.constant.TokenConstants; import com.cnooc.expert.common.constant.TokenConstants;
import com.cnooc.expert.common.utils.JsonUtils;
import com.cnooc.expert.common.utils.SmsHttpUtil; import com.cnooc.expert.common.utils.SmsHttpUtil;
import com.cnooc.expert.common.utils.SmsUtil; import com.cnooc.expert.common.utils.SmsUtil;
import com.cnooc.expert.system.entity.vo.SmsConfig; import com.cnooc.expert.system.entity.vo.SmsConfig;
...@@ -20,7 +21,7 @@ import java.util.concurrent.CompletableFuture; ...@@ -20,7 +21,7 @@ import java.util.concurrent.CompletableFuture;
@AllArgsConstructor @AllArgsConstructor
@Slf4j @Slf4j
public class SmsServiceImpl implements SmsService { public class SmsServiceImpl implements SmsService {
public static final String SMS_CODE_CONTENT = "福建通信行业融合创新服务平台,您的验证码是:%s(有效期为2分钟),请勿泄露给他人,如非本人操作,请忽略此消息。"; public static final String SMS_CODE_CONTENT = "您的验证码是:%s(有效期为2分钟),请勿泄露给他人,如非本人操作,请忽略此消息。";
@Autowired @Autowired
private StringRedisTemplate redisTemplate; private StringRedisTemplate redisTemplate;
private final SmsConfig smsConfig; private final SmsConfig smsConfig;
...@@ -59,11 +60,11 @@ public class SmsServiceImpl implements SmsService { ...@@ -59,11 +60,11 @@ public class SmsServiceImpl implements SmsService {
//String storedCode = (String)redisTemplate.opsForValue().get(key); //String storedCode = (String)redisTemplate.opsForValue().get(key);
System.out.println("发送短信验证码:" + phone + " -> " + code);// 模拟发送短信,实际应调用第三方短信服务 System.out.println("发送短信验证码:" + phone + " -> " + code);// 模拟发送短信,实际应调用第三方短信服务
return true; return true;
//return sendMasSmsCode(phone); //return sendGlySmsCode(phone);
} }
@Override @Override
public boolean sendMasSmsCode(String phone) { public boolean sendGlySmsCode(String phone) {
// 生成验证码并构建缓存键 // 生成验证码并构建缓存键
String code = SmsUtil.generateVerificationCode(); String code = SmsUtil.generateVerificationCode();
System.out.println("发送短信验证码:" + phone + " -> " + code); System.out.println("发送短信验证码:" + phone + " -> " + code);
...@@ -76,7 +77,7 @@ public class SmsServiceImpl implements SmsService { ...@@ -76,7 +77,7 @@ public class SmsServiceImpl implements SmsService {
log.info("云MAS业务平台 发送手机号: {},短信验证码:{}", phone, code); log.info("云MAS业务平台 发送手机号: {},短信验证码:{}", phone, code);
System.out.println("云MAS业务平台 短信内容: " + content); System.out.println("云MAS业务平台 短信内容: " + content);
boolean result = sendMasSmsContent(phone, content); boolean result = sendGlySmsContent(phone, content, code);
if(result){ if(result){
redisTemplate.opsForValue().set(key, code, 2 * 60); // 存入 Redis,设置过期时间为2分钟 redisTemplate.opsForValue().set(key, code, 2 * 60); // 存入 Redis,设置过期时间为2分钟
} }
...@@ -89,42 +90,37 @@ public class SmsServiceImpl implements SmsService { ...@@ -89,42 +90,37 @@ public class SmsServiceImpl implements SmsService {
} }
@Override @Override
public boolean sendMasSmsContent(String phone, String content) { public boolean sendGlySmsContent(String phone, String content, String code) {
try { try {
log.info("云MAS业务平台 短信内容: {}", content); log.info("管理云业务平台 短信内容: {}", content);
log.info("云MAS业务平台 发送手机号: {},短信内容:{}", phone, content); log.info("管理云业务平台 发送手机号: {},短信内容:{}", phone, content);
// 生成 MAC 签名
String mac = SmsUtil.calculateMac(smsConfig.getEcName(), smsConfig.getApId(), smsConfig.getSecretKey(),
phone, content, smsConfig.getSign(), smsConfig.getAddSerial());
System.out.println("云MAS业务平台 mac签名: " + mac);
log.info("云MAS业务平台 mac签名: {}", mac);
// 构建请求体并转换为 Base64 编码的 JSON 字符串 // 构建请求体并转换为 Base64 编码的 JSON 字符串
String encodedJson = SmsUtil.encodeRequestBodyToBase64(buildRequestBody(phone, content, mac)); String encodedJson = SmsUtil.encodeRequestBodyToBase64(buildRequestBody(phone, content, code));
System.out.println("云MAS业务平台 请求体Base64编码: " + encodedJson); System.out.println("管理云业务平台 请求体Base64编码: " + encodedJson);
log.info("云MAS业务平台 请求体Base64编码: {}", encodedJson); log.info("管理云业务平台 请求体Base64编码: {}", encodedJson);
// 设置请求头并发送 POST 请求 // 设置请求头并发送 POST 请求
String response = SmsHttpUtil.sendPostRequest(smsConfig.getApiUrl(), encodedJson); String response = SmsHttpUtil.sendPostRequest(smsConfig.getApiUrl(), encodedJson,smsConfig.getAppCode());
System.out.println("云MAS业务平台 响应体: " + response); System.out.println("管理云业务平台 响应体: " + response);
log.info("云MAS业务平台 响应体response: {}", response); log.info("管理云业务平台 响应体response: {}", response);
// 解析响应并处理结果 // 解析响应并处理结果
return SmsUtil.handleResponse(response); return SmsUtil.handleResponse(response);
} catch (Exception e) { } catch (Exception e) {
log.error("云MAS业务平台短信发送失败: {}", e.getMessage(), e); log.error("管理云业务平台短信发送失败: {}", e.getMessage(), e);
return false; return false;
} }
} }
@Override @Override
public void asyncSendMasSmsContent(String phone, String content){ public void asyncSendGlySmsContent(String phone, String content, String code){
CompletableFuture.runAsync(() -> { CompletableFuture.runAsync(() -> {
try { try {
System.out.println("======开始发送短信======"); System.out.println("======开始发送短信======");
sendMasSmsContent(phone, content); sendGlySmsContent(phone, content,code);
System.out.println("======发送短信结束======"); System.out.println("======发送短信结束======");
} catch (Exception e) { } catch (Exception e) {
log.error("asyncSendMasSmsContent异步执行失败", e); log.error("asyncSendMasSmsContent异步执行失败", e);
...@@ -136,16 +132,22 @@ public class SmsServiceImpl implements SmsService { ...@@ -136,16 +132,22 @@ public class SmsServiceImpl implements SmsService {
/** /**
* 构建请求体 * 构建请求体
*/ */
private Map<String, String> buildRequestBody(String phone, String content, String mac) { private Map<String, String> buildRequestBody(String phone, String content, String code) {
Map<String, Object> sms = new HashMap<>();
Map<String, String> dataItems = new HashMap<>();
dataItems.put("code", code);
sms.put("template", "短信验证码");
sms.put("dataItems", JsonUtils.toJsonString((dataItems)));
sms.put("type", "1");
String[] recvNumArray = new String[1];
recvNumArray[0] = phone;
sms.put("recvNum", recvNumArray);
sms.put("content", content);
sms.put("sendNum", "");
Map<String, String> requestBody = new HashMap<>(); Map<String, String> requestBody = new HashMap<>();
requestBody.put("ecName", smsConfig.getEcName()); requestBody.put("priority", "high");
requestBody.put("apId", smsConfig.getApId()); requestBody.put("label", "消息标签");
requestBody.put("mobiles", phone); requestBody.put("sms", JsonUtils.toJsonString(sms));
requestBody.put("content", content);
requestBody.put("secretKey", smsConfig.getSecretKey());
requestBody.put("sign", smsConfig.getSign());
requestBody.put("addSerial", smsConfig.getAddSerial());
requestBody.put("mac", mac);
return requestBody; return requestBody;
} }
......
...@@ -16,11 +16,12 @@ public class SmsHttpUtil { ...@@ -16,11 +16,12 @@ public class SmsHttpUtil {
* @param requestBody 请求体 Map * @param requestBody 请求体 Map
* @return 响应字符串 * @return 响应字符串
*/ */
public static String sendPostRequest(String apiUrl, String requestBody) { public static String sendPostRequest(String apiUrl, String requestBody,String appCode) {
try { try {
// 设置请求头 // 设置请求头
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON); headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization","APPCODE "+appCode);
// 构建请求实体 // 构建请求实体
HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers); HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
......
...@@ -11,10 +11,6 @@ import org.springframework.stereotype.Component; ...@@ -11,10 +11,6 @@ import org.springframework.stereotype.Component;
public class SmsConfig { public class SmsConfig {
private String apiUrl; private String apiUrl;
private String ecName; private String appCode;
private String apId;
private String secretKey;
private String sign;
private String addSerial;
} }
...@@ -37,7 +37,11 @@ spring: ...@@ -37,7 +37,11 @@ spring:
batch-size: 16384 batch-size: 16384
linger-ms: 1 linger-ms: 1
buffer-memory: 33554432 buffer-memory: 33554432
sms:
config:
# 短信发送配置
api_url: https://gly.api.mcmptest.cnooc/msg/mcmp-msg-api/message
app_code: 3F2504E04F8911D39A0C0305E82C3301
app: app:
info: info:
appId: 10000 appId: 10000
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment