|
@ -1,6 +1,7 @@
|
|
|
package com.yihu.jw.sms.service;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.tencentcloudapi.common.Credential;
|
|
|
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
|
@ -36,6 +37,8 @@ public class TXYSmsService {
|
|
|
@Autowired
|
|
|
private StringRedisTemplate redisTemplate;
|
|
|
|
|
|
private String key = "hz_yxyzh_wx_sms";
|
|
|
|
|
|
private String SecretId;
|
|
|
private String SecretKey;
|
|
|
private String signName;
|
|
@ -46,7 +49,6 @@ public class TXYSmsService {
|
|
|
* 初始化接口参数
|
|
|
*/
|
|
|
private void init(){
|
|
|
String key = "hz_yxyzh_wx_sms";
|
|
|
if(redisTemplate.hasKey(key+ ":SecretId")){
|
|
|
SecretId = redisTemplate.opsForValue().get(key + ":SecretId");
|
|
|
SecretKey = redisTemplate.opsForValue().get(key + ":SecretKey");
|
|
@ -56,7 +58,7 @@ public class TXYSmsService {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
List<WlyyHospitalSysDictDO> dictDOList = sysDictDao.findByDictName("hz_yxyzh_wx_sms");
|
|
|
List<WlyyHospitalSysDictDO> dictDOList = sysDictDao.findByDictName(key);
|
|
|
for (WlyyHospitalSysDictDO wlyyHospitalSysDictDO:dictDOList){
|
|
|
if (wlyyHospitalSysDictDO.getDictCode().equalsIgnoreCase("SecretId")){
|
|
|
SecretId=wlyyHospitalSysDictDO.getDictValue();
|
|
@ -115,4 +117,61 @@ public class TXYSmsService {
|
|
|
return "error";
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* @param mobile
|
|
|
* @param str {"templateCode":"VerificationCode","templateParamArr":["param1","param2","param3"]}
|
|
|
* @return
|
|
|
*/
|
|
|
public String sendMessageJson(String mobile,String str){
|
|
|
|
|
|
try {
|
|
|
init();
|
|
|
Credential cred = new Credential(SecretId, SecretKey);
|
|
|
JSONObject jsonObj = new JSONObject();
|
|
|
try {
|
|
|
jsonObj = JSONObject.parseObject(str);
|
|
|
logger.info("字符串转JSONObject报错:"+str);
|
|
|
}catch (Exception e){
|
|
|
return "error";
|
|
|
}
|
|
|
String templateId = redisTemplate.opsForValue().get(key + ":"+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);
|
|
|
|
|
|
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";
|
|
|
}
|
|
|
|
|
|
}
|