main
parent
a126318d62
commit
57ad54dc42
|
|
@ -221,6 +221,12 @@
|
|||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
<version>1.2.10</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tencentcloudapi</groupId>
|
||||
<artifactId>tencentcloud-sdk-java</artifactId>
|
||||
<version>3.1.1000</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
@ -159,6 +159,10 @@ public class AjaxResult<T> {
|
|||
return new AjaxResult<>(code, msg, new ArrayList<>());
|
||||
}
|
||||
|
||||
public static AjaxResult<Object> selfFailed(String msg) {
|
||||
return new AjaxResult<>(9999, msg, new ArrayList<>());
|
||||
}
|
||||
|
||||
/**
|
||||
* 响应失败结果
|
||||
*
|
||||
|
|
|
|||
|
|
@ -11,23 +11,26 @@ import org.springframework.context.annotation.Configuration;
|
|||
*/
|
||||
@Configuration
|
||||
public class SmsConfig {
|
||||
@Value("${config.aliyun.sms.defaultConnectTimeout}")
|
||||
@Value("${config.tencent.sms.defaultConnectTimeout}")
|
||||
private String defaultConnectTimeout;
|
||||
|
||||
@Value("${config.aliyun.sms.defaultReadTimeout}")
|
||||
@Value("${config.tencent.sms.defaultReadTimeout}")
|
||||
private String defaultReadTimeout;
|
||||
|
||||
@Value("${config.aliyun.sms.accessKeyId}")
|
||||
@Value("${config.tencent.sms.accessKeyId}")
|
||||
private String accessKeyId;
|
||||
|
||||
@Value("${config.aliyun.sms.accessKeySecret}")
|
||||
@Value("${config.tencent.sms.accessKeySecret}")
|
||||
private String accessKeySecret;
|
||||
|
||||
@Value("${config.aliyun.sms.signName}")
|
||||
@Value("${config.tencent.sms.signName}")
|
||||
private String signName;
|
||||
|
||||
@Value("${config.tencent.sms.appId}")
|
||||
private String appId;
|
||||
|
||||
@Bean
|
||||
public SmsUtil smsUtil() {
|
||||
return new SmsUtil(defaultConnectTimeout, defaultReadTimeout, accessKeyId, accessKeySecret,signName);
|
||||
return new SmsUtil(accessKeyId, accessKeySecret,signName,appId);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import cn.binarywang.wx.miniapp.api.WxMaService;
|
|||
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
||||
import com.mdd.common.core.AjaxResult;
|
||||
|
|
@ -22,6 +21,7 @@ import com.mdd.front.service.ILoginService;
|
|||
import com.mdd.front.utils.SmsUtil;
|
||||
import com.mdd.front.validate.login.RegisterValidate;
|
||||
import com.mdd.front.vo.login.LoginTokenVo;
|
||||
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
|
||||
|
|
@ -470,11 +470,13 @@ public class LoginServiceImpl implements ILoginService {
|
|||
try {
|
||||
SendSmsResponse sendSmsResponse = smsUtil.sendSms(mobile, stringBuilder.toString());
|
||||
System.out.println("发送短信返回值:" + JSON.toJSONString(sendSmsResponse));
|
||||
String message = sendSmsResponse.getSendStatusSet()[0].getMessage();
|
||||
if (!"send success".equals(message)) {
|
||||
return AjaxResult.selfFailed(message);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return AjaxResult.success(stringBuilder.toString());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,11 +3,16 @@ package com.mdd.front.utils;
|
|||
import com.alibaba.fastjson2.JSON;
|
||||
import com.aliyuncs.DefaultAcsClient;
|
||||
import com.aliyuncs.IAcsClient;
|
||||
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
|
||||
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
|
||||
import com.aliyuncs.exceptions.ClientException;
|
||||
import com.aliyuncs.profile.DefaultProfile;
|
||||
import com.aliyuncs.profile.IClientProfile;
|
||||
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 lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
|
|
@ -20,13 +25,13 @@ import java.util.Map;
|
|||
*/
|
||||
@Slf4j
|
||||
public class SmsUtil {
|
||||
@Value("${config.aliyun.sms.smsCode}")
|
||||
@Value("${config.tencent.sms.smsCode}")
|
||||
private String smsCode;
|
||||
|
||||
//产品名称:云通信短信API产品,固定值
|
||||
private static final String product = "Dysmsapi";
|
||||
//产品域名,开发者无需替换,固定值
|
||||
private static final String domain = "dysmsapi.aliyuncs.com";
|
||||
private static final String domain = "sms.tencentcloudapi.com";
|
||||
|
||||
private String defaultConnectTimeout;
|
||||
|
||||
|
|
@ -38,87 +43,95 @@ public class SmsUtil {
|
|||
|
||||
private String signName;
|
||||
|
||||
public SmsUtil(String defaultConnectTimeout, String defaultReadTimeout, String accessKeyId, String accessKeySecret, String signName){
|
||||
this.defaultConnectTimeout = defaultConnectTimeout;
|
||||
this.defaultReadTimeout = defaultReadTimeout;
|
||||
private String appId;
|
||||
|
||||
|
||||
|
||||
public SmsUtil(String accessKeyId, String accessKeySecret, String signName, String appId){
|
||||
// this.defaultConnectTimeout = defaultConnectTimeout;
|
||||
// this.defaultReadTimeout = defaultReadTimeout;
|
||||
this.accessKeyId = accessKeyId;
|
||||
this.accessKeySecret = accessKeySecret;
|
||||
this.signName = signName;
|
||||
}
|
||||
/**
|
||||
* 发送短信的方法
|
||||
* @param phone 接收短信的手机号
|
||||
* @param smsCode 模板中的变量替换JSON串,例如验证码
|
||||
*/
|
||||
public SendSmsResponse sendSms(String phone, String code) throws ClientException {
|
||||
//可自助调整超时时间
|
||||
System.setProperty("sun.net.client.defaultConnectTimeout", defaultConnectTimeout);
|
||||
System.setProperty("sun.net.client.defaultReadTimeout", defaultReadTimeout);
|
||||
//初始化acsClient,暂不支持region化
|
||||
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecret);
|
||||
DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", product, domain);
|
||||
IAcsClient acsClient = new DefaultAcsClient(profile);
|
||||
|
||||
//组装请求对象-具体描述见控制台-文档部分内容
|
||||
SendSmsRequest request = new SendSmsRequest();
|
||||
//必填:待发送手机号
|
||||
request.setPhoneNumbers(phone);
|
||||
//必填:短信签名-可在短信控制台中找到
|
||||
request.setSignName(signName);
|
||||
//短信模板-可在短信控制台中找到
|
||||
request.setTemplateCode(smsCode);
|
||||
|
||||
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
|
||||
// smsCOde为随机验证码
|
||||
request.setTemplateParam("{\"code\":\"" + code + "\"}");
|
||||
|
||||
//选填-上行短信扩展码(无特殊需求用户请忽略此字段)
|
||||
//request.setSmsUpExtendCode("90997");
|
||||
//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
|
||||
// request.setOutId("yourOutId");
|
||||
//hint 此处可能会抛出异常,注意catch
|
||||
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
|
||||
log.info("发送短信状态:{}", sendSmsResponse.getCode());
|
||||
log.info("发送短信消息:{}", sendSmsResponse.getMessage());
|
||||
return sendSmsResponse;
|
||||
this.appId = appId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
* @param phone 接收短信的手机号
|
||||
* @param type 模板编号
|
||||
* @param map 模板中变量map
|
||||
*/
|
||||
public SendSmsResponse sendSms(String phone, String type, Map<String,Object> map) throws ClientException {
|
||||
//可自助调整超时时间
|
||||
System.setProperty("sun.net.client.defaultConnectTimeout", defaultConnectTimeout);
|
||||
System.setProperty("sun.net.client.defaultReadTimeout", defaultReadTimeout);
|
||||
//初始化acsClient,暂不支持region化
|
||||
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
|
||||
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
|
||||
IAcsClient acsClient = new DefaultAcsClient(profile);
|
||||
public SendSmsResponse sendSms(String phone, String code) throws ClientException {
|
||||
SendSmsResponse res = null;
|
||||
try{
|
||||
Credential cred = new Credential(accessKeyId, accessKeySecret);
|
||||
|
||||
//组装请求对象-具体描述见控制台-文档部分内容
|
||||
SendSmsRequest request = new SendSmsRequest();
|
||||
//必填:待发送手机号
|
||||
request.setPhoneNumbers(phone);
|
||||
//必填:短信签名-可在短信控制台中找到s
|
||||
request.setSignName(signName);
|
||||
//必填:短信模板-可在短信控制台中找到
|
||||
request.setTemplateCode(type);
|
||||
|
||||
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
|
||||
// smsCOde为随机验证码
|
||||
request.setTemplateParam(JSON.toJSONString(map));
|
||||
// 实例化一个http选项,可选,没有特殊需求可以跳过
|
||||
HttpProfile httpProfile = new HttpProfile();
|
||||
/* SDK默认使用POST方法。
|
||||
* 如果您一定要使用GET方法,可以在这里设置。GET方法无法处理一些较大的请求 */
|
||||
httpProfile.setReqMethod("POST");
|
||||
/* SDK有默认的超时时间,非必要请不要进行调整
|
||||
* 如有需要请在代码中查阅以获取最新的默认值 */
|
||||
httpProfile.setConnTimeout(60);
|
||||
/* 指定接入地域域名,默认就近地域接入域名为 sms.tencentcloudapi.com ,也支持指定地域域名访问,例如广州地域的域名为 sms.ap-guangzhou.tencentcloudapi.com */
|
||||
httpProfile.setEndpoint(domain);
|
||||
|
||||
//选填-上行短信扩展码(无特殊需求用户请忽略此字段)
|
||||
//request.setSmsUpExtendCode("90997");
|
||||
//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
|
||||
// request.setOutId("yourOutId");
|
||||
//hint 此处可能会抛出异常,注意catch
|
||||
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
|
||||
log.info("发送短信状态:{}", sendSmsResponse.getCode());
|
||||
log.info("发送短信消息:{}", sendSmsResponse.getMessage());
|
||||
return sendSmsResponse;
|
||||
|
||||
/* 非必要步骤:
|
||||
* 实例化一个客户端配置对象,可以指定超时时间等配置 */
|
||||
ClientProfile clientProfile = new ClientProfile();
|
||||
/* SDK默认用TC3-HMAC-SHA256进行签名
|
||||
* 非必要请不要修改这个字段 */
|
||||
clientProfile.setSignMethod("HmacSHA256");
|
||||
clientProfile.setHttpProfile(httpProfile);
|
||||
/* 实例化要请求产品(以sms为例)的client对象
|
||||
* 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,支持的地域列表参考 https://cloud.tencent.com/document/api/382/52071#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8 */
|
||||
SmsClient client = new SmsClient(cred, "ap-nanjing",clientProfile);
|
||||
/* 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数
|
||||
* 您可以直接查询SDK源码确定接口有哪些属性可以设置
|
||||
* 属性可能是基本类型,也可能引用了另一个数据结构
|
||||
* 推荐使用IDE进行开发,可以方便的跳转查阅各个接口和数据结构的文档说明 */
|
||||
SendSmsRequest req = new SendSmsRequest();
|
||||
|
||||
|
||||
/* 填充请求参数,这里request对象的成员变量即对应接口的入参
|
||||
* 您可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义
|
||||
* 基本类型的设置:
|
||||
* 帮助链接:
|
||||
* 短信控制台: https://console.cloud.tencent.com/smsv2
|
||||
* 腾讯云短信小助手: https://cloud.tencent.com/document/product/382/3773#.E6.8A.80.E6.9C.AF.E4.BA.A4.E6.B5.81 */
|
||||
|
||||
|
||||
/* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */
|
||||
// 应用 ID 可前往 [短信控制台](https://console.cloud.tencent.com/smsv2/app-manage) 查看
|
||||
req.setSmsSdkAppId(appId);
|
||||
/* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */
|
||||
// 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看
|
||||
req.setSignName(signName);
|
||||
|
||||
|
||||
/* 模板 ID: 必须填写已审核通过的模板 ID */
|
||||
// 模板 ID 可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-template) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-template) 的正文模板管理查看
|
||||
req.setTemplateId(smsCode);
|
||||
|
||||
|
||||
/* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空 */
|
||||
req.setTemplateParamSet(new String[]{code});
|
||||
|
||||
|
||||
/* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
|
||||
* 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号 */
|
||||
req.setPhoneNumberSet(new String[]{"+86" + phone});
|
||||
|
||||
/* 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
|
||||
* 返回的 res 是一个 SendSmsResponse 类的实例,与请求对象对应 */
|
||||
res = client.SendSms(req);
|
||||
// 输出json格式的字符串回包
|
||||
System.out.println(SendSmsResponse.toJsonString(res));
|
||||
} catch (TencentCloudSDKException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -86,14 +86,15 @@ sa-token:
|
|||
config:
|
||||
shareRedisKey: content_
|
||||
#短信短信配置
|
||||
aliyun:
|
||||
tencent:
|
||||
sms:
|
||||
#ID和Secret是通用的,在控制台可以查看到
|
||||
accessKeyId: LTAI5tM1LeE2pkiS3qEFQkfb
|
||||
accessKeySecret: fEZZyFvWkETS8Clm73f7qmY9ohcTpc
|
||||
accessKeyId: AKIDeIwKewEcNzghH4tz9Ra3Z9mSYtVKC6Lf
|
||||
accessKeySecret: VYw0Ba2fN9zzEV2iN3iVHyjk5Sn9WtDc
|
||||
appId: 1400794040
|
||||
#短信签名-可在短信控制台中找到
|
||||
signName: 丙煜
|
||||
signName: 上海惊喜派科技
|
||||
#配置超时时间
|
||||
defaultConnectTimeout: 10000
|
||||
defaultReadTimeout: 10000
|
||||
smsCode: SMS_465690552
|
||||
smsCode: 2154594
|
||||
Loading…
Reference in New Issue