簡單的介紹一下阿里雲的短信繼承方法,和封裝的一些工具類:具體的能夠參考官方文檔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');