diff --git a/crmeb/crmeb-common/pom.xml b/crmeb/crmeb-common/pom.xml index f5a1a24..62e6a3c 100644 --- a/crmeb/crmeb-common/pom.xml +++ b/crmeb/crmeb-common/pom.xml @@ -335,6 +335,11 @@ dysmsapi20170525 3.1.0 + + com.tencentcloudapi + tencentcloud-sdk-java + 3.1.270 + diff --git a/crmeb/crmeb-common/src/main/java/com/zbkj/common/config/TencentConfig.java b/crmeb/crmeb-common/src/main/java/com/zbkj/common/config/TencentConfig.java new file mode 100644 index 0000000..b54b8c8 --- /dev/null +++ b/crmeb/crmeb-common/src/main/java/com/zbkj/common/config/TencentConfig.java @@ -0,0 +1,23 @@ +package com.zbkj.common.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +/** + * 腾讯云配置 + * @Version: 1.0 + * @since: 2025/1/9 + * @author: yxz + */ +@Configuration +@Data +@ConfigurationProperties(prefix = "tencent.sms") +public class TencentConfig { + private String appId; + private String signName; + private String secretId; + private String secretKey; + private String templateId; + +} diff --git a/crmeb/crmeb-front/src/main/java/com/zbkj/front/controller/LoginController.java b/crmeb/crmeb-front/src/main/java/com/zbkj/front/controller/LoginController.java index 598d194..2465a94 100644 --- a/crmeb/crmeb-front/src/main/java/com/zbkj/front/controller/LoginController.java +++ b/crmeb/crmeb-front/src/main/java/com/zbkj/front/controller/LoginController.java @@ -82,7 +82,7 @@ public class LoginController { @ApiImplicitParam(name="phone", value="手机号码", required = true) }) public CommonResult sendCode(@RequestParam String phone) throws Exception { - if(smsService.sendAliCommonCode(phone)){ + if(smsService.sendTencentCommonCode(phone)){ return CommonResult.success("发送成功"); }else{ return CommonResult.failed("发送失败"); diff --git a/crmeb/crmeb-front/src/main/resources/application.yml b/crmeb/crmeb-front/src/main/resources/application.yml index ebab8ed..9507587 100644 --- a/crmeb/crmeb-front/src/main/resources/application.yml +++ b/crmeb/crmeb-front/src/main/resources/application.yml @@ -59,10 +59,11 @@ swagger: username: #访问swagger的账号 password: #访问swagger的密码 -#阿里云配置 -ali: +# 腾讯云配置 +tencent: sms: - access-key-id: LTAI5tRJzMoNC2uF17u6Wx15 - access-key-secret: gZA90P9d1peFfuAaqROf4yXFLT34KY - sign-name: 蘑菇童话王国 - template-code: SMS_476680164 \ No newline at end of file + sign-name: 厦门六本木传媒 + app-id: 1400765943 + secret-id: AKIDyoedR2NWYN0QgkigcVcbUFm5JLCrHXZO + secret-key: jIfIrHYebN4qQoIekePbhfbbWJ8qNwZ5 + template-id: 2171347 \ No newline at end of file diff --git a/crmeb/crmeb-service/src/main/java/com/zbkj/service/service/SmsService.java b/crmeb/crmeb-service/src/main/java/com/zbkj/service/service/SmsService.java index acc23a9..83fd7bc 100644 --- a/crmeb/crmeb-service/src/main/java/com/zbkj/service/service/SmsService.java +++ b/crmeb/crmeb-service/src/main/java/com/zbkj/service/service/SmsService.java @@ -51,14 +51,14 @@ public interface SmsService { Boolean sendCommonCode(String phone); /** - * 发送阿里云通用验证码 + * 发送腾讯云通用验证码 * * @param phone 电话 * @return {@link Boolean } * @author yxz * @since 2024/12/26 */ - Boolean sendAliCommonCode(String phone) throws Exception; + Boolean sendTencentCommonCode(String phone) throws Exception; /** * 发送支付成功短信 diff --git a/crmeb/crmeb-service/src/main/java/com/zbkj/service/service/impl/SmsServiceImpl.java b/crmeb/crmeb-service/src/main/java/com/zbkj/service/service/impl/SmsServiceImpl.java index 8b7ce2a..c8d407b 100644 --- a/crmeb/crmeb-service/src/main/java/com/zbkj/service/service/impl/SmsServiceImpl.java +++ b/crmeb/crmeb-service/src/main/java/com/zbkj/service/service/impl/SmsServiceImpl.java @@ -6,14 +6,22 @@ import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.aliyun.dysmsapi20170525.Client; -import com.aliyun.dysmsapi20170525.models.SendSmsRequest; import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody; import com.aliyun.tea.TeaException; import com.aliyun.teaopenapi.models.Config; import com.aliyun.teautil.Common; import com.aliyun.teautil.models.RuntimeOptions; import com.fasterxml.jackson.databind.ObjectMapper; +import com.tencentcloudapi.common.Credential; +import com.tencentcloudapi.common.profile.ClientProfile; +import com.tencentcloudapi.common.profile.HttpProfile; +import com.tencentcloudapi.common.exception.TencentCloudSDKException; +import com.tencentcloudapi.sms.v20210111.SmsClient; +import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest; +import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse; +import com.google.gson.Gson; import com.zbkj.common.config.AliConfig; +import com.zbkj.common.config.TencentConfig; import com.zbkj.common.request.SmsApplyTempRequest; import com.zbkj.common.request.SmsModifySignRequest; import com.zbkj.common.utils.CrmebUtil; @@ -83,7 +91,7 @@ public class SmsServiceImpl implements SmsService { private OnePassService onePassService; @Autowired - private AliConfig aliConfig; + private TencentConfig config; private static final Logger logger = LoggerFactory.getLogger(SmsServiceImpl.class); @@ -363,7 +371,7 @@ public class SmsServiceImpl implements SmsService { } /** - * 发送阿里云通用验证码 + * 发送腾讯云通用验证码 * * @param phone 电话 * @return {@link Boolean } @@ -371,39 +379,51 @@ public class SmsServiceImpl implements SmsService { * @since 2024/12/26 */ @Override - public Boolean sendAliCommonCode(String phone) throws Exception { + public Boolean sendTencentCommonCode(String phone) throws Exception { ValidateFormUtil.isPhone(phone,"手机号码错误"); Integer code = CrmebUtil.randomCount(111111, 999999); redisUtil.set(userService.getValidateCodeRedisKey(phone), code, Long.valueOf(1), TimeUnit.MINUTES); - // 发送短信 - Config config = new Config() - // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 - .setAccessKeyId(aliConfig.getAccessKeyId()) - // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 - .setAccessKeySecret(aliConfig.getAccessKeySecret()); - // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi - config.endpoint = "dysmsapi.aliyuncs.com"; - Client client = new Client(config); - SendSmsRequest sendSmsRequest = new SendSmsRequest() - .setPhoneNumbers(phone) - .setSignName(aliConfig.getSignName()) - .setTemplateCode(aliConfig.getTemplateCode()) - .setTemplateParam("{\"code\":\""+code+"\"}"); try { - ObjectMapper objectMapper = new ObjectMapper(); - SendSmsResponseBody body = client.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions()).getBody(); - log.info("阿里云短信发送结果: " + objectMapper.writeValueAsString(body)); - } catch (TeaException error) { - // 错误 message - Common.assertAsString(error.message); - log.info("诊断地址: " + error.getData().get("Recommend")); - throw new CrmebException(error.getMessage()); - } catch (Exception _error) { - TeaException error = new TeaException(_error.getMessage(), _error); - Common.assertAsString(error.message); - log.info("诊断地址: " + error.getData().get("Recommend")); - throw new CrmebException(error.getMessage()); + Credential cred = new Credential(config.getSecretId(), config.getSecretKey()); + // (Optional) Instantiate an HTTP option + HttpProfile httpProfile = new HttpProfile(); + httpProfile.setReqMethod("POST"); + /* The SDK has a default timeout period. Do not adjust it unless absolutely necessary + * If needed, check in the code to get the latest default value */ + httpProfile.setConnTimeout(60); + httpProfile.setEndpoint("sms.tencentcloudapi.com"); + /* Optional steps: + * Instantiate a client configuration object. You can specify the timeout period and other configuration items */ + ClientProfile clientProfile = new ClientProfile(); + /* The SDK uses `TC3-HMAC-SHA256` to sign by default + * Do not modify this field unless absolutely necessary */ + clientProfile.setSignMethod("HmacSHA256"); + clientProfile.setHttpProfile(httpProfile); + /* Instantiate the client object of the requested product (with SMS as an example) + * The second parameter is the information on the region you select in Tencent Cloud International. If you select Singapore, you should enter the string `ap-singapore`. Click https://www.tencentcloud.com/document/api/382/40466?lang=en#region-list to view the region list. */ + SmsClient client = new SmsClient(cred, "ap-guangzhou",clientProfile); + SendSmsRequest req = new SendSmsRequest(); + req.setSmsSdkAppId(config.getAppId()); + req.setSignName(config.getSignName()); + req.setSenderId(""); + req.setSessionContext(""); + req.setExtendCode(""); + req.setTemplateId(config.getTemplateId()); + /* Target mobile number in the E.164 standard (+[country/region code][mobile number]) + * Example: +8613711112222, which has a + sign followed by 86 (country/region code) and then by 13711112222 (mobile number). Up to 200 mobile numbers are supported */ + String[] phoneArray = {phone}; + req.setPhoneNumberSet(phoneArray); + /* Template parameters. If there are no template parameters, leave it empty */ + String[] argsArray = {code+""}; + req.setTemplateParamSet(argsArray); + + SendSmsResponse response = client.SendSms(req); + log.info("{}",new Gson().toJson(response)); + } catch (TencentCloudSDKException e) { + e.printStackTrace(); + return Boolean.FALSE; } + return true; }