yml文件内配置
aliyun: accessKeyID: LTAIfyfd345itvUTqJRhhVx accessKeySecret: DEdgwTdlkfuREHodiflhHozNiem6l signName: 生态环保院数字园区 template: systemnotice: SMS_227249650 meetingrefuse: SMS_227249645 meetingjoin: SMS_227249644 meetingapproval: SMS_227259508 departmentrefuse: SMS_227259505 departmasterrefuse: SMS_22726444 carapply: SMS_227259502 onclass: SMS_227249320 offclass: SMS_227264152 carusenotice: SMS_227254616 carapproval: SMS_227264439 outnotice: SMS_227249633 restaurant: SMS_227249631
pom文件
<!-- aliyun sms --> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.4.6</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dysmsapi</artifactId> <version>2.1.0</version> </dependency>
模板图片:
工具包内容:
package com.cccc.common.utils.aliyun; import java.util.Date; /** * 描述:阿里云短信发送实体类 * * @Author: Yue Zejian * @Date: Created in 2021/11/6 10:54 */ public class AliyunMessage { /* 消息内容参数,接收者姓名 {"name":"李白"}*/ private String name; /* 消息内容参数,任意数字类型参数 {"number":"5"}*/ private Integer number; /* 消息内容参数,任意地点类型参数 {"place":"中国天津滨海新区中心酒店"}*/ private String place; /* 消息内容参数,任意时间类型参数 {"number":"2021-11-06 10:22:30"}*/ private Date date; /** * 阿里云上申请的签名 : 生态环保院数字园区 */ private String signName; /** * 阿里云上申请的模板码: SMS_227264152 */ private String templateCode; private String accessKeyID; private String accessKeySecret; public AliyunMessage() { } public AliyunMessage(String name, Integer number, String place, Date date, String signName, String templateCode, String accessKeyID, String accessKeySecret) { this.name = name; this.number = number; this.place = place; this.date = date; this.signName = signName; this.templateCode = templateCode; this.accessKeyID = accessKeyID; this.accessKeySecret = accessKeySecret; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getNumber() { return number; } public void setNumber(Integer number) { this.number = number; } public String getPlace() { return place; } public void setPlace(String place) { this.place = place; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public String getSignName() { return signName; } public void setSignName(String signName) { this.signName = signName; } public String getTemplateCode() { return templateCode; } public void setTemplateCode(String templateCode) { this.templateCode = templateCode; } public String getAccessKeyID() { return accessKeyID; } public void setAccessKeyID(String accessKeyID) { this.accessKeyID = accessKeyID; } public String getAccessKeySecret() { return accessKeySecret; } public void setAccessKeySecret(String accessKeySecret) { this.accessKeySecret = accessKeySecret; } }
package com.cccc.common.utils.aliyun; import java.util.Date; /** * 描述:阿里云短信消息建造者抽象 * * @Author: Yue Zejian * @Date: Created in 2021/11/6 11:01 */ public abstract class AliyunMessageBuilder { public abstract void buildName(String name); public abstract void buildNumber(Integer number); public abstract void buildPlace(String place); public abstract void buildDate(Date date); public abstract void buildSignName(String signName); public abstract void buildTemplateCode(String templateCode); public abstract void buildAccessKeyID(String accessKeyID); public abstract void buildAccessKeySecret(String accessKeySecret); }
package com.cccc.common.utils.aliyun; import java.util.Date; /** * 描述:阿里云短信消息建造者具体 * * @Author: Yue Zejian * @Date: Created in 2021/11/6 11:22 */ public class AliyunMessageConcreteBuilder extends AliyunMessageBuilder{ private AliyunMessage aliyunMessage; /** * 开始构建 */ public AliyunMessageConcreteBuilder() { aliyunMessage = new AliyunMessage(); } @Override public void buildName(String name) { aliyunMessage.setName(name); } @Override public void buildNumber(Integer number) { aliyunMessage.setNumber(number); } @Override public void buildPlace(String place) { aliyunMessage.setPlace(place); } @Override public void buildDate(Date date) { aliyunMessage.setDate(date); } @Override public void buildSignName(String signName) { aliyunMessage.setSignName(signName); } @Override public void buildTemplateCode(String templateCode) { aliyunMessage.setTemplateCode(templateCode); } @Override public void buildAccessKeyID(String accessKeyID) { aliyunMessage.setAccessKeyID(accessKeyID); } @Override public void buildAccessKeySecret(String accessKeySecret) { aliyunMessage.setAccessKeySecret(accessKeySecret); } /** * 结束建造 * @return */ public AliyunMessage build() { return aliyunMessage; } }
package com.cccc.common.utils.aliyun; import com.alibaba.fastjson.JSON; import com.aliyuncs.CommonRequest; import com.aliyuncs.CommonResponse; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.DefaultProfile; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.Map; /** * 描述:aliyun sms 短信发送工具类 * * @Author: Yue Zejian * @Date: Created in 2021/11/6 9:56 */ public class AliyunMessageSendUtils { private static final Logger LOG = LoggerFactory.getLogger(AliyunMessageSendUtils.class); private static final String REGION_ID = "cn-hangzhou"; private static final String DOMAIN = "dysmsapi.aliyuncs.com"; /** * * @param aliyunMessage {@link AliyunMessage} * @param phoneNum 接收人电话号码 * @return */ public static boolean sendSms(AliyunMessage aliyunMessage, String phoneNum, Map<String, Object> params) { // 指定地域名称 短信API的就是 cn-hangzhou 不能改变 后边填写您的 accessKey 和 accessKey Secret DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, aliyunMessage.getAccessKeyID(), aliyunMessage.getAccessKeySecret()); IAcsClient client = new DefaultAcsClient(profile); // 创建通用的请求对象 CommonRequest request = new CommonRequest(); // 指定请求方式 request.setSysMethod(MethodType.POST); // 短信api的请求地址 固定 request.setSysDomain(DOMAIN); //签名算法版本 固定 request.setSysVersion("2017-05-25"); //请求 API 的名称 request.setSysAction("SendSms"); //指定地域名称 request.putQueryParameter("RegionId", REGION_ID); // 要给哪个手机号发送短信 指定手机号 request.putQueryParameter("PhoneNumbers", phoneNum); // 您的申请签名 request.putQueryParameter("SignName", aliyunMessage.getSignName()); // 您申请的模板 code request.putQueryParameter("TemplateCode", aliyunMessage.getTemplateCode()); //这里的key就是短信模板中的 ${xxxx} // 放入参数 需要把 map转换为json格式 使用fastJson进行转换 request.putQueryParameter("TemplateParam", JSON.toJSONString(params)); try { CommonResponse response = client.getCommonResponse(request); LOG.info("==================================================================="+phoneNum+"短信消息返回:"+JSON.parseObject(response.getData(), Map.class).get("Message").toString()); return response.getHttpResponse().isSuccess(); } catch (ServerException e) { System.out.println("短信发生服务器异常"); } catch (ClientException e) { System.out.println("短信发生客户端异常"); } return false; } }
使用方式:下面是在往数据库插入消息之前,将信息通过短信方式进行通知。
import org.springframework.core.env.Environment; @Autowired private Environment env; /** * 新增消息通知 * * @param ccccMessage 消息通知 * @return 结果 */ @Override public int insertCcccMessage(CcccMessage ccccMessage) { //查询接收者消息 SysUser user = userMapper.selectUserById(ccccMessage.getReceiveUserId()); //开始建造阿里云短信发送实体类 AliyunMessageConcreteBuilder aliyunMessageConcreteBuilder = new AliyunMessageConcreteBuilder(); aliyunMessageConcreteBuilder.buildAccessKeyID(env.getProperty("aliyun.accessKeyID", String.class)); aliyunMessageConcreteBuilder.buildAccessKeySecret(env.getProperty("aliyun.accessKeySecret", String.class)); aliyunMessageConcreteBuilder.buildName(user.getNickName()); aliyunMessageConcreteBuilder.buildSignName(env.getProperty("aliyun.signName", String.class)); //给模板code赋默认值 String templateCode = env.getProperty("aliyun.template.systemnotice", String.class); //构建短信消息内容 Map<String, Object> params = new HashMap<>(); params.put("name", user.getNickName()); //不同类型信息选不同模板 switch (ccccMessage.getType()) { case "会议室拒绝": templateCode = env.getProperty("aliyun.template.meetingrefuse", String.class); break; case "参会通知": templateCode = env.getProperty("aliyun.template.meetingjoin", String.class); break; case "会议室审批": templateCode = env.getProperty("aliyun.template.meetingapproval", String.class); break; case "党政部拒绝": templateCode = env.getProperty("aliyun.template.departmentrefuse", String.class); break; case "部门长拒绝": templateCode = env.getProperty("aliyun.template.departmasterrefuse", String.class); break; case "用车申请": templateCode = env.getProperty("aliyun.template.carapply", String.class); break; case "用车通知": templateCode = env.getProperty("aliyun.template.carusenotice", String.class); break; case "用车审批": templateCode = env.getProperty("aliyun.template.carapproval", String.class); break; case "外出通知": templateCode = env.getProperty("aliyun.template.outnotice", String.class); break; case "打卡提醒": if (ccccMessage.getTitle().equals("下班打卡提醒")) { templateCode = env.getProperty("aliyun.template.offclass", String.class); } else { templateCode = env.getProperty("aliyun.template.onclass", String.class); } break; case "报餐通知": templateCode = env.getProperty("aliyun.template.restaurant", String.class); //TODO: 待确定,128 为示例数据 params.put("name", 128); break; } aliyunMessageConcreteBuilder.buildTemplateCode(templateCode); //完成构建并利用工具类进行短信发送 AliyunMessageSendUtils.sendSms(aliyunMessageConcreteBuilder.build(), user.getPhonenumber(), params); return ccccMessageMapper.insertCcccMessage(ccccMessage); }
文章参考了:
https://www.cnblogs.com/xianshen/p/13361470.html