简单的介绍一下阿里云的短信继承方法,和封装的一些工具类:具体的能够参考官方文档html
1 先须要将打包阿里云的core和 sms包导入到项目中java
<!-- 阿里云短信 --> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>3.5.0</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dysmsapi</artifactId> <version>1.1.0</version> </dependency>
2 建立工具类spring
package com.wonlymall.mall.core.sms; import java.util.Date; import java.util.List; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import com.alibaba.fastjson.JSONObject; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.wonlymall.mall.core.constant.CommonConstant; import com.wonlymall.mall.core.util.CommonUtil; import com.wonlymall.mall.modular.sys.model.SysSmsSendLog; import com.wonlymall.mall.modular.sys.model.SysSmsTemplate; import com.wonlymall.mall.modular.sys.service.ISysSmsSendLogService; import com.wonlymall.mall.modular.sys.service.ISysSmsTemplateService; @Component public class AliSmsSendTask { @Value("${aliyun.sms.accessKeyId}") public String accessKeyId; @Value("${aliyun.sms.accessKeySecret}") public String accessKeySecret; @Value("${aliyun.sms.product}") public String product; @Value("${aliyun.sms.domain}") public String domain; @Value("${aliyun.sms.region}") public String region; @Resource private ISysSmsTemplateService iSysSmsTemplateService; @Resource private ISysSmsSendLogService iSysSmsSendLogService; private static final String SIGN=""; #购买就有 @Async public void sendSms(String templateCode, String mobile, String...content){ SysSmsSendLog sysSmsSendLog=new SysSmsSendLog(); try { // 可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); // 初始化acsClient,暂不支持region化 IClientProfile profile = DefaultProfile.getProfile(region, accessKeyId, accessKeySecret); DefaultProfile.addEndpoint(region, region, product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); // 组装请求对象-具体描述见控制台-文档部份内容 SendSmsRequest request = new SendSmsRequest(); // 必填:待发送手机号 request.setPhoneNumbers(mobile); // 必填:短信签名-可在短信控制台中找到 request.setSignName(SIGN); // 必填:短信模板-可在短信控制台中找到 request.setTemplateCode(templateCode); SysSmsTemplate sysSmsTemplate=this.iSysSmsTemplateService.selectOne(new EntityWrapper<SysSmsTemplate>() .eq("is_deleted", 0).eq("is_enable", 1).eq("template_code", templateCode)); // 可选:模板中的变量替换JSON串,如模板内容为"您正在申请手机注册,验证码为:${number},5分钟内有效!"时,此处的值为 List<String>templateParamList=CommonUtil.parseStringFromSeat(sysSmsTemplate.getDetail()); JSONObject jsonObject=new JSONObject(); for(int i=0;i<templateParamList.size();i++) { jsonObject.put(templateParamList.get(i), content[i]); } request.setTemplateParam(jsonObject.toJSONString()); //request.setTemplateParam("{\"number\":\"123456\"}"); // 选填-上行短信扩展码(无特殊需求用户请忽略此字段) // request.setSmsUpExtendCode("90997"); // 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 request.setOutId("yourOutId"); // hint 此处可能会抛出异常,注意catch SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); sysSmsSendLog.setCreated(new Date()); sysSmsSendLog.setModified(new Date()); sysSmsSendLog.setDetail(sysSmsTemplate.getDetail()); sysSmsSendLog.setMobile(mobile); sysSmsSendLog.setIsDeleted(CommonConstant.NO); sysSmsSendLog.setIsEnable(CommonConstant.YES); if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) { //请求成功 sysSmsSendLog.setIsSuccess(CommonConstant.YES); }else { sysSmsSendLog.setIsSuccess(CommonConstant.NO); } //sysSmsSendLog.setBackMessage(sendSmsResponse.getMessage()); } catch(Exception e) { e.printStackTrace(); sysSmsSendLog.setIsSuccess(CommonConstant.NO); //sysSmsSendLog.setBackMessage(""); } this.iSysSmsSendLogService.insert(sysSmsSendLog); } }
/** * 从占位符解析字符串 * @return * 亲爱的${name},您的验证码为${code} */ public static List<String> parseStringFromSeat(String str) { if(StringUtils.isBlank(str)) { return new ArrayList<>(); } String[]strArr=str.split("\\{"); List<String>list=new ArrayList<>(); for(String s:strArr) { if(StringUtils.contains(s, "}")) { int index=s.indexOf("}"); list.add(s.substring(0, index)); } } return list; }
3 后台数据库中模板库配置数据库
DROP TABLE IF EXISTS `p_sys_sms_template`; CREATE TABLE `p_sys_sms_template` ( `id` int(11) NOT NULL AUTO_INCREMENT, `type` int(1) NOT NULL DEFAULT '0' COMMENT '模板类型:0短信消息;1APP推送消息;2微信服务消息', `name` varchar(20) NOT NULL COMMENT '模板名称', `template_code` varchar(200) DEFAULT NULL COMMENT '模板code', `detail` varchar(200) NOT NULL COMMENT '模板内容', `is_enable` int(1) DEFAULT '0' COMMENT '是否启用,0不启用,1启用', `creator` int(11) NOT NULL DEFAULT '0' COMMENT '建立人', `modifier` int(11) NOT NULL DEFAULT '0' COMMENT '修改人', `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '建立时间', `modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后修改时间', `is_deleted` int(1) unsigned NOT NULL DEFAULT '0' COMMENT '0正常,1删除', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8 COMMENT='短信模板配置表';
例如json
INSERT INTO `p_sys_sms_template` VALUES ('1', '0', '注册登陆短信验证码', '1111', '验证码为${code},该验证码5分钟内有效,请勿泄露于他人,如非本人操做请忽略。', '1', '0', '0', '2018-12-13 16:27:46', '2018-12-13 16:27:46', '0'); INSERT INTO `p_sys_sms_template` VALUES ('19', '0', '指派短信模板', '222', '您的${projectname}项目报务信息已成功委任,已指派大客户经理${name},电话${phone}协助您跟进,请及时跟踪工程单。', '1', '0', '0', '2018-12-24 15:17:30', '2018-12-24 15:17:30', '0');