短信通知换成腾讯sms
This commit is contained in:
parent
d141256dd7
commit
6b800ad69e
|
|
@ -0,0 +1,41 @@
|
||||||
|
package com.bonus.sgzb.system.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "tencent.sms")
|
||||||
|
public class TencentSmsConfig{
|
||||||
|
|
||||||
|
//api秘钥标识
|
||||||
|
private String accessKeyId;
|
||||||
|
|
||||||
|
//api秘钥
|
||||||
|
private String accessKeySecret;
|
||||||
|
|
||||||
|
//请求域名
|
||||||
|
private String endpoint;
|
||||||
|
|
||||||
|
//所属区域
|
||||||
|
private String region;
|
||||||
|
|
||||||
|
//腾讯云申请应用id
|
||||||
|
private String sdkAppId;
|
||||||
|
|
||||||
|
//签名
|
||||||
|
private String smsSign;
|
||||||
|
|
||||||
|
//云平台模板id
|
||||||
|
private List<String> templateId;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Description: 短信控制器
|
* Description: 短信控制器
|
||||||
|
|
@ -26,6 +27,11 @@ public class SysSmsController extends BaseController {
|
||||||
@Resource
|
@Resource
|
||||||
private ISysSmsService smsService;
|
private ISysSmsService smsService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录短信验证码
|
||||||
|
* @param phone
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@PostMapping("codeLogin")
|
@PostMapping("codeLogin")
|
||||||
public AjaxResult codeLogin(@RequestParam(value = "phone") String phone){
|
public AjaxResult codeLogin(@RequestParam(value = "phone") String phone){
|
||||||
try {
|
try {
|
||||||
|
|
@ -35,6 +41,12 @@ public class SysSmsController extends BaseController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 短信验证码校验
|
||||||
|
* @param phone
|
||||||
|
* @param code
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@PostMapping("checkCode")
|
@PostMapping("checkCode")
|
||||||
public AjaxResult codeLogin(@RequestParam(value = "phone") String phone, @RequestParam(value = "code") String code){
|
public AjaxResult codeLogin(@RequestParam(value = "phone") String phone, @RequestParam(value = "code") String code){
|
||||||
try {
|
try {
|
||||||
|
|
@ -44,6 +56,12 @@ public class SysSmsController extends BaseController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验收通知短信
|
||||||
|
* @param phone
|
||||||
|
* @param msg
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@PostMapping("send")
|
@PostMapping("send")
|
||||||
public AjaxResult send(@RequestParam(value = "phone") String phone, @RequestParam(value = "msg",required = false) String msg){
|
public AjaxResult send(@RequestParam(value = "phone") String phone, @RequestParam(value = "msg",required = false) String msg){
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,29 @@
|
||||||
package com.bonus.sgzb.system.service.impl;
|
package com.bonus.sgzb.system.service.impl;
|
||||||
|
|
||||||
import cn.hutool.http.HttpRequest;
|
|
||||||
import com.alibaba.druid.util.StringUtils;
|
import com.alibaba.druid.util.StringUtils;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.bonus.sgzb.common.core.constant.UserConstants;
|
import com.bonus.sgzb.common.core.constant.UserConstants;
|
||||||
import com.bonus.sgzb.common.core.exception.ServiceException;
|
import com.bonus.sgzb.common.core.exception.ServiceException;
|
||||||
import com.bonus.sgzb.common.core.utils.GlobalConstants;
|
import com.bonus.sgzb.common.core.utils.GlobalConstants;
|
||||||
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
|
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
|
||||||
import com.bonus.sgzb.common.redis.service.RedisService;
|
import com.bonus.sgzb.common.redis.service.RedisService;
|
||||||
|
import com.bonus.sgzb.system.config.TencentSmsConfig;
|
||||||
import com.bonus.sgzb.system.service.ISysSmsService;
|
import com.bonus.sgzb.system.service.ISysSmsService;
|
||||||
|
import com.tencentcloudapi.common.Credential;
|
||||||
|
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
||||||
|
import com.tencentcloudapi.common.profile.ClientProfile;
|
||||||
|
import com.tencentcloudapi.common.profile.HttpProfile;
|
||||||
|
import com.tencentcloudapi.sms.v20210111.SmsClient;
|
||||||
|
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
|
||||||
|
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
|
||||||
|
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
|
@ -24,17 +37,27 @@ import static com.bonus.sgzb.common.core.web.domain.AjaxResult.success;
|
||||||
* @Version 1.0
|
* @Version 1.0
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
|
@Slf4j
|
||||||
public class SysSmsServiceImpl implements ISysSmsService {
|
public class SysSmsServiceImpl implements ISysSmsService {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private RedisService redisService;
|
private RedisService redisService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private TencentSmsConfig tencentSmsConfig;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 短信URL
|
* 短信URL
|
||||||
*/
|
*/
|
||||||
private static final String URL = "http://api.ktsms.cn/sms_token?ddtkey=bonus&secretkey=KtyBns@Admin2023!";
|
private static final String URL = "http://api.ktsms.cn/sms_token?ddtkey=bonus&secretkey=KtyBns@Admin2023!";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验收通知短信
|
||||||
|
* @param phone 手机号
|
||||||
|
* @param msg 内容
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public AjaxResult sendSms(String phone, String msg) {
|
public AjaxResult sendSms(String phone, String msg) {
|
||||||
if (phone == null || StringUtils.isEmpty(msg)) {
|
if (phone == null || StringUtils.isEmpty(msg)) {
|
||||||
|
|
@ -43,13 +66,34 @@ public class SysSmsServiceImpl implements ISysSmsService {
|
||||||
if (phone.length() != UserConstants.PHONE_DEFAULT_LENGTH_LOGIN) {
|
if (phone.length() != UserConstants.PHONE_DEFAULT_LENGTH_LOGIN) {
|
||||||
return AjaxResult.error("手机号格式不正确");
|
return AjaxResult.error("手机号格式不正确");
|
||||||
}
|
}
|
||||||
return sendMsgByPhone(phone, msg);
|
try {
|
||||||
|
String[] args = msg.split(",");
|
||||||
|
String body = sendMessageNew(phone,tencentSmsConfig.getTemplateId().get(0),args);
|
||||||
|
return success("发送手机号码:" + phone + ",内容:" + msg + ",返回结果:" + body);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return AjaxResult.error("发送失败:" + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录短信验证码
|
||||||
|
* @param phone 手机号
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public AjaxResult codeLogin(String phone) {
|
public AjaxResult codeLogin(String phone) {
|
||||||
// 校验手机号码
|
|
||||||
return sendCodeByPhone(phone, null);
|
try {
|
||||||
|
//获取六位验证码
|
||||||
|
String code = getSixBitCode();
|
||||||
|
//调用发送短信的方法
|
||||||
|
String body = sendMessageNew(phone,tencentSmsConfig.getTemplateId().get(1),code);
|
||||||
|
// 存储验证码至Redis中,键值为:code_15588886157 , 有效期5,时间颗粒度为MINUTES:分钟
|
||||||
|
redisService.setCacheObject("code_" + phone, code, 5L, TimeUnit.MINUTES);
|
||||||
|
return success("手机号:" + phone + ",用户登录验证码:" + code + ",返回结果:" + body);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return AjaxResult.error("发送失败:" + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -58,7 +102,7 @@ public class SysSmsServiceImpl implements ISysSmsService {
|
||||||
* @param phone 手机号码
|
* @param phone 手机号码
|
||||||
* @return AjaxResult对象
|
* @return AjaxResult对象
|
||||||
*/
|
*/
|
||||||
private AjaxResult sendMsgByPhone(String phone, String msg) {
|
/* private AjaxResult sendMsgByPhone(String phone, String msg) {
|
||||||
// 校验手机号码
|
// 校验手机号码
|
||||||
if (phone == null || phone.length() != UserConstants.PHONE_DEFAULT_LENGTH_LOGIN) {
|
if (phone == null || phone.length() != UserConstants.PHONE_DEFAULT_LENGTH_LOGIN) {
|
||||||
return AjaxResult.error("手机号码不正确");
|
return AjaxResult.error("手机号码不正确");
|
||||||
|
|
@ -76,7 +120,7 @@ public class SysSmsServiceImpl implements ISysSmsService {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return AjaxResult.error("发送失败:" + e.getMessage());
|
return AjaxResult.error("发送失败:" + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -84,7 +128,7 @@ public class SysSmsServiceImpl implements ISysSmsService {
|
||||||
* @param phone 手机号码
|
* @param phone 手机号码
|
||||||
* @return AjaxResult对象
|
* @return AjaxResult对象
|
||||||
*/
|
*/
|
||||||
private AjaxResult sendCodeByPhone(String phone, String msg) {
|
/* private AjaxResult sendCodeByPhone(String phone, String msg) {
|
||||||
// 校验手机号码
|
// 校验手机号码
|
||||||
if (phone == null || phone.length() != UserConstants.PHONE_DEFAULT_LENGTH_LOGIN) {
|
if (phone == null || phone.length() != UserConstants.PHONE_DEFAULT_LENGTH_LOGIN) {
|
||||||
return AjaxResult.error("手机号格式错误,请输入11位数字号码");
|
return AjaxResult.error("手机号格式错误,请输入11位数字号码");
|
||||||
|
|
@ -108,7 +152,7 @@ public class SysSmsServiceImpl implements ISysSmsService {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return AjaxResult.error("发送失败:" + e.getMessage());
|
return AjaxResult.error("发送失败:" + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -146,6 +190,65 @@ public class SysSmsServiceImpl implements ISysSmsService {
|
||||||
return String.valueOf(random.nextInt(900000) + 100000);
|
return String.valueOf(random.nextInt(900000) + 100000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 腾讯sms短信
|
||||||
|
* @param mobilePhone
|
||||||
|
* @param templateId
|
||||||
|
* @param args
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public String sendMessageNew(String mobilePhone,String templateId, String... args) throws Exception {
|
||||||
|
|
||||||
|
try{
|
||||||
|
// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
|
||||||
|
Credential cred = new Credential(tencentSmsConfig.getAccessKeyId(), tencentSmsConfig.getAccessKeySecret());
|
||||||
|
// 实例化一个http选项,可选的,没有特殊需求可以跳过
|
||||||
|
HttpProfile httpProfile = new HttpProfile();
|
||||||
|
httpProfile.setEndpoint(tencentSmsConfig.getEndpoint());
|
||||||
|
// 实例化一个client选项,可选的,没有特殊需求可以跳过
|
||||||
|
ClientProfile clientProfile = new ClientProfile();
|
||||||
|
clientProfile.setHttpProfile(httpProfile);
|
||||||
|
// 实例化要请求产品的client对象,clientProfile是可选的 第二个参数是地域信息
|
||||||
|
SmsClient client = new SmsClient(cred, tencentSmsConfig.getRegion(), clientProfile);
|
||||||
|
// 实例化一个请求对象,每个接口都会对应一个request对象
|
||||||
|
SendSmsRequest req = new SendSmsRequest();
|
||||||
|
//设置固定的参数
|
||||||
|
req.setSmsSdkAppId(tencentSmsConfig.getSdkAppId());// 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId
|
||||||
|
req.setSignName(tencentSmsConfig.getSmsSign());//短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名
|
||||||
|
req.setTemplateId(templateId);//模板 ID: 必须填写已审核通过的模板 ID
|
||||||
|
//设置发送相关的参数
|
||||||
|
//发送对象(最多200个)
|
||||||
|
String[] phoneNumberSet1 = mobilePhone.split(",");
|
||||||
|
for (int i = 0; i < phoneNumberSet1.length; i++) {
|
||||||
|
phoneNumberSet1[i] = "+86" + phoneNumberSet1[i];
|
||||||
|
}
|
||||||
|
req.setPhoneNumberSet(phoneNumberSet1);//发送的手机号
|
||||||
|
if(null != args && args.length > 0 && Arrays.stream(args)
|
||||||
|
.noneMatch(s -> s == null || s.trim().isEmpty())) {
|
||||||
|
String[] templateParamSet1 = args;//模板的参数
|
||||||
|
req.setTemplateParamSet(templateParamSet1);//发送验证码
|
||||||
|
}
|
||||||
|
// 返回的resp是一个SendSmsResponse的实例,与请求对象对应
|
||||||
|
log.info("腾讯云平台短信发送请求参数:{}", JSONObject.toJSONString(req));
|
||||||
|
SendSmsResponse resp = client.SendSms(req);
|
||||||
|
// 输出json格式的字符串回包
|
||||||
|
log.info("腾讯云平台短信发送响应结果:{}", JSONObject.toJSONString(resp));
|
||||||
|
SendStatus[] sendStatusSet = resp.getSendStatusSet();
|
||||||
|
List<SendStatus> sendStatuses = Arrays.asList(sendStatusSet);
|
||||||
|
if (CollectionUtils.isNotEmpty(sendStatuses)){
|
||||||
|
for (SendStatus sendStatus : sendStatuses) {
|
||||||
|
if (!"OK".equalsIgnoreCase(sendStatus.getCode())){
|
||||||
|
throw new ServiceException(sendStatus.getMessage(),Integer.valueOf(sendStatus.getCode()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp.getRequestId();
|
||||||
|
} catch (TencentCloudSDKException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
log.error("短信发送失败:{}", e.getMessage());
|
||||||
|
throw new ServiceException(e.getMessage(),Integer.valueOf(e.getErrorCode()));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,23 @@ spring:
|
||||||
# 共享配置
|
# 共享配置
|
||||||
shared-configs:
|
shared-configs:
|
||||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||||
|
#腾讯云sms
|
||||||
|
tencent:
|
||||||
|
sms:
|
||||||
|
#api秘钥标识
|
||||||
|
accessKeyId: AKIDrreCVaRKDtMcgfU5QW9iEfv67tMfldJn
|
||||||
|
#api秘钥
|
||||||
|
accessKeySecret: OXUgeMo0yhBRTGo6sVu3yiFX4rQtAzc3
|
||||||
|
#请求域名
|
||||||
|
endpoint: sms.tencentcloudapi.com
|
||||||
|
#所属区域
|
||||||
|
region: ap-guangzhou
|
||||||
|
#腾讯云申请应用id
|
||||||
|
sdkAppId: 1400494336
|
||||||
|
#签名
|
||||||
|
smsSign: 南方电网互联网
|
||||||
|
#云平台模板id 2116937-验收通知 2115503-登录验证
|
||||||
|
templateId: 2116937,2115503
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue