LAPTOP-KB9HII50\70708 vor 4 Monaten
Ursprung
Commit
09c8da0bf7

+ 5 - 0
svr/svr-cloud-transfor/pom.xml

@ -74,6 +74,11 @@
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.tencentcloudapi</groupId>
            <artifactId>tencentcloud-sdk-java</artifactId>
            <version>3.1.272</version>
        </dependency>
    </dependencies>
    <build>

+ 31 - 0
svr/svr-cloud-transfor/src/main/java/com/yihu/jw/care/controller/TransforController.java

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.jw.care.RequestParamUtil;
import com.yihu.jw.care.service.AqgService;
import com.yihu.jw.care.service.TXYSmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
@ -33,8 +34,38 @@ public class TransforController {
    private static Logger logger = LoggerFactory.getLogger(TransforController.class);
    @Autowired
    private AqgService aqgService;
    @Autowired
    private TXYSmsService txySmsService;
    private static final RestTemplate restTemplate = new RestTemplate();
    @ApiOperation("腾讯云短信发送")
    @RequestMapping(value = "txSendMessage", method = {RequestMethod.POST,RequestMethod.GET})
    public String txSendMessage(
            @ApiParam(name="mobile",required = true,value="手机号")
            @RequestParam(value = "mobile",required = true) String mobile,
            @ApiParam(name="signName",required = true,value="短信签名")
            @RequestParam(value = "signName",required = true) String signName,
            @ApiParam(name="verificationCode",required = true,value="短信模板id")
            @RequestParam(value = "verificationCode",required = true) String verificationCode,
            @ApiParam(name="size",required = true,value="短信模板参数个数")
            @RequestParam(value = "size",required = true) Integer size,
            @ApiParam(name="templateParam1",required = false)
            @RequestParam(value = "templateParam1",required = false) String templateParam1,
            @ApiParam(name="templateParam2",required = false)
            @RequestParam(value = "templateParam2",required = false) String templateParam2,
            @ApiParam(name="templateParam3",required = false)
            @RequestParam(value = "templateParam3",required = false) String templateParam3,
            @ApiParam(name="templateParam4",required = false)
            @RequestParam(value = "templateParam4",required = false) String templateParam4) {
        try {
            String res = txySmsService.sendMessage(mobile,signName,verificationCode,size,templateParam1,templateParam2,templateParam3,templateParam4);
            return write(200,res);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1,"send failure");
        }
    }
    @ApiOperation("请求转发")
    @RequestMapping(value = "transfor", method = {RequestMethod.POST,RequestMethod.GET})
    public String transfor(HttpServletRequest request,String baseUrl) {

+ 135 - 0
svr/svr-cloud-transfor/src/main/java/com/yihu/jw/care/service/TXYSmsService.java

@ -0,0 +1,135 @@
package com.yihu.jw.care.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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 org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
/**
 * 腾讯短信发送
 * @Author: yeshijie
 * @Date: 2024/11/25
 * @Description:
 */
@Service
public class TXYSmsService {
    private Logger logger= LoggerFactory.getLogger(TXYSmsService.class);
    private String SecretId = "AKIDa2IQM8i7QWQ6fATZmncDey50hGj0302x";
    private String SecretKey = "teDe0BBye8QdmkdLxPhlfOwJUYSCrSyP";
//    private String signName = "厦门i健康";
    private String smsSdkAppId = "1400951100";
//    private String VerificationCode = "2317340";//冠心病风险评估结果
    public String sendMessage(String mobile,String signName,String verificationCode,Integer size,String templateParam1,String templateParam2,
                              String templateParam3,String templateParam4){
        try{
            Credential cred = new Credential(SecretId, SecretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {"+86"+mobile};
            req.setPhoneNumberSet(phoneNumberSet1);
            req.setSmsSdkAppId(smsSdkAppId);
            req.setSignName(signName);
            req.setTemplateId(verificationCode);
            if(size>0){
                String[] templateParamSet = new String[size];
                templateParamSet[0] = StringUtils.isBlank(templateParam1)?"":templateParam1;
                if(size>1){
                    templateParamSet[1] = StringUtils.isBlank(templateParam2)?"":templateParam2;
                }
                if(size>2){
                    templateParamSet[2] = StringUtils.isBlank(templateParam3)?"":templateParam3;
                }
                if(size>3){
                    templateParamSet[3] = StringUtils.isBlank(templateParam4)?"":templateParam4;
                }
                req.setTemplateParamSet(templateParamSet);
            }
            SendSmsResponse resp = client.SendSms(req);
            JSONObject json = JSON.parseObject(SendSmsResponse.toJsonString(resp));
            String res = json.getJSONArray("SendStatusSet").getJSONObject(0).getString("Code");
            if(!"Ok".equals(res)){
                logger.info("腾讯短信发送失败:"+SendSmsResponse.toJsonString(resp));
            }
            return res;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            logger.info("腾讯短信报错:"+e.getMessage());
        }
        return "error";
    }
//    /**
//     *
//     * @param mobile
//     * @param jsonObj {"templateCode":"974603","templateParamArr":["param1","param2","param3"]}
//     * @return   模板id暂时写死
//     */
//    public String sendMessageJson(String mobile,JSONObject jsonObj){
//        try {
//            Credential cred = new Credential(SecretId, SecretKey);
//
//            String templateId = jsonObj.getString("templateCode");
//            JSONArray templateParamArr = jsonObj.getJSONArray("templateParamArr");
//            String[] templateParamSet1 = templateParamArr.toArray(new String[0]);
//
//            HttpProfile httpProfile = new HttpProfile();
//            httpProfile.setEndpoint("sms.tencentcloudapi.com");
//
//            ClientProfile clientProfile = new ClientProfile();
//            clientProfile.setHttpProfile(httpProfile);
//
//            SmsClient client = new SmsClient(cred, "ap-nanjing", clientProfile);
//
//            SendSmsRequest req = new SendSmsRequest();
//            String[] phoneNumberSet1 = {"+86"+mobile};
//
//            req.setPhoneNumberSet(phoneNumberSet1);
//
//            req.setSmsSdkAppId(smsSdkAppId);
//            req.setSignName(signName);
//            req.setTemplateId(templateId);
//
//            if(templateParamSet1.length>0){
//                req.setTemplateParamSet(templateParamSet1);
//            }
//            SendSmsResponse resp = client.SendSms(req);
//
//            JSONObject json = JSON.parseObject(SendSmsResponse.toJsonString(resp));
//            String res = json.getJSONArray("SendStatusSet").getJSONObject(0).getString("Code");
//            if(!"Ok".equals(res)){
//                logger.info("腾讯短信发送失败:"+SendSmsResponse.toJsonString(resp));
//            }
//            return res;
//
//
//        }catch (TencentCloudSDKException e){
//            e.printStackTrace();
//            logger.info("腾讯短信报错:"+e.toString());
//        }
//        return "error";
//    }
}