TASK: 试用用户添加测试;
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.ai.da.common.utils;
|
||||
|
||||
import com.ai.da.mapper.entity.TrialOrder;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ai.da.common.config.exception.BusinessException;
|
||||
import com.tencentcloudapi.common.Credential;
|
||||
@@ -11,6 +12,7 @@ import com.tencentcloudapi.ses.v20201002.models.SendEmailRequest;
|
||||
import com.tencentcloudapi.ses.v20201002.models.SendEmailResponse;
|
||||
import com.tencentcloudapi.ses.v20201002.models.Template;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@@ -114,4 +116,102 @@ public class SendEmailUtil {
|
||||
template.setTemplateData(jsonObject.toJSONString());
|
||||
return template;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送不同类型的邮件
|
||||
*
|
||||
* @param receiverAddress 收件人邮箱地址
|
||||
* @param senderAddress 发件人邮箱地址
|
||||
* @param emailType 邮件类型:1 - 提交试用请求,2 - 审批通过,3 - 试用请求通过通知
|
||||
* @return 发送结果
|
||||
*/
|
||||
private final static Long YOUR_TRIAL_TEMPLATE_ID = 117214L;
|
||||
private final static Long APPROVAL_TEMPLATE_ID = 117215L;
|
||||
private final static Long NOTIFICATION_TEMPLATE_ID = 117216L;
|
||||
public static Boolean sendCustomEmail(String receiverAddress, String senderAddress, TrialOrder trialOrder, int emailType) {
|
||||
try {
|
||||
// 实例化一个认证对象
|
||||
Credential cred = new Credential(SECRET_ID, SECRET_KEy);
|
||||
HttpProfile httpProfile = new HttpProfile();
|
||||
httpProfile.setEndpoint("ses.tencentcloudapi.com");
|
||||
ClientProfile clientProfile = new ClientProfile();
|
||||
clientProfile.setHttpProfile(httpProfile);
|
||||
SesClient client = new SesClient(cred, "ap-hongkong", clientProfile);
|
||||
SendEmailRequest req = new SendEmailRequest();
|
||||
if (StringUtils.isEmpty(senderAddress)) {
|
||||
senderAddress = SEND_ADDRESS;
|
||||
}
|
||||
req.setFromEmailAddress(senderAddress);
|
||||
req.setDestination(new String[]{receiverAddress});
|
||||
|
||||
// 根据邮件类型设置不同的主题和模板
|
||||
String subject = "";
|
||||
Template template = new Template();
|
||||
switch (emailType) {
|
||||
case 1:
|
||||
subject = "试用订单请求";
|
||||
template.setTemplateID(YOUR_TRIAL_TEMPLATE_ID);
|
||||
template.setTemplateData(buildTrialOrderData(trialOrder));
|
||||
break;
|
||||
case 2:
|
||||
subject = "试用订单审批通过";
|
||||
template.setTemplateID(APPROVAL_TEMPLATE_ID);
|
||||
template.setTemplateData(buildApprovalData(trialOrder));
|
||||
break;
|
||||
case 3:
|
||||
subject = "试用订单通过通知";
|
||||
template.setTemplateID(NOTIFICATION_TEMPLATE_ID);
|
||||
template.setTemplateData(buildNotificationData(trialOrder));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
req.setSubject(subject);
|
||||
req.setTemplate(template);
|
||||
|
||||
// 发送邮件
|
||||
SendEmailResponse resp = client.SendEmail(req);
|
||||
log.info("短信发送结果res###{}", SendEmailResponse.toJsonString(resp));
|
||||
return Boolean.TRUE;
|
||||
} catch (TencentCloudSDKException e) {
|
||||
log.info("邮件发送失败###{}", e.toString());
|
||||
throw new BusinessException("failed.to.send.mail");
|
||||
}
|
||||
}
|
||||
|
||||
// 构建试用订单数据
|
||||
private static String buildTrialOrderData(TrialOrder trialOrder) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
// 设置试用订单相关数据
|
||||
jsonObject.put("surname", trialOrder.getSurname());
|
||||
jsonObject.put("givenName", trialOrder.getGivenName());
|
||||
jsonObject.put("userName", trialOrder.getUserName());
|
||||
jsonObject.put("email", trialOrder.getEmail());
|
||||
jsonObject.put("country", trialOrder.getCountry());
|
||||
return jsonObject.toJSONString();
|
||||
}
|
||||
|
||||
// 构建审批通过数据
|
||||
private static String buildApprovalData(TrialOrder trialOrder) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
// 设置审批通过相关数据
|
||||
jsonObject.put("surname", trialOrder.getSurname());
|
||||
jsonObject.put("givenName", trialOrder.getGivenName());
|
||||
jsonObject.put("userName", trialOrder.getUserName());
|
||||
jsonObject.put("email", trialOrder.getEmail());
|
||||
jsonObject.put("country", trialOrder.getCountry());
|
||||
return jsonObject.toJSONString();
|
||||
}
|
||||
|
||||
// 构建试用订单通过通知数据
|
||||
private static String buildNotificationData(TrialOrder trialOrder) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
// 设置试用订单通过通知相关数据
|
||||
jsonObject.put("surname", trialOrder.getSurname());
|
||||
jsonObject.put("givenName", trialOrder.getGivenName());
|
||||
jsonObject.put("userName", trialOrder.getUserName());
|
||||
jsonObject.put("email", trialOrder.getEmail());
|
||||
return jsonObject.toJSONString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user