赞
踩
<!-- 阿里云依赖 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.6.3</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>2.2.1</version>
</dependency>
# 阿里云短信配置
sms:
aliyun:
enabled: true
endpoint: cn-hangzhou
access-key-id: xxxxxxxx
access-key-secret: xxxxxxxx
sign-name: xxxxxxxx
template-code: xxxxxxxx
package com.qiangesoft.sms.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; /** * 阿里云短信属性 * * @author qiangesoft * @date 2024-04-30 */ @Data @ConfigurationProperties(prefix = "sms.aliyun") public class AliyunSmsProperties { /** * 是否启用 */ private boolean enabled = true; /** * Endpoint */ private String endpoint; /** * accessKeyId */ private String accessKeyId; /** * accessKeySecret */ private String accessKeySecret; /** * 短信签名 */ private String signName; /** * 模板code */ private String templateCode; }
package com.qiangesoft.sms.config; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 阿里云短信配置 * * @author qiangesoft * @date 2024-04-30 */ @Configuration @EnableConfigurationProperties(AliyunSmsProperties.class) @ConditionalOnProperty(prefix = "sms.aliyun", name = "enabled", havingValue = "true") public class AliyunSmsConfiguration { private static final String PRODUCT = "Dysmsapi"; private static final String DOMAIN = "dysmsapi.aliyuncs.com"; @Autowired private AliyunSmsProperties aliyunSmsProperties; @Bean @ConditionalOnMissingBean public IAcsClient acsClient() { String endPoint = aliyunSmsProperties.getEndpoint(); String accessKeyId = aliyunSmsProperties.getAccessKeyId(); String accessKeySecret = aliyunSmsProperties.getAccessKeySecret(); IClientProfile profile = DefaultProfile.getProfile(endPoint, accessKeyId, accessKeySecret); DefaultProfile.addEndpoint(endPoint, PRODUCT, DOMAIN); return new DefaultAcsClient(profile); } }
package com.qiangesoft.sms.handler; import org.apache.commons.lang3.StringUtils; import java.util.Collection; import java.util.Collections; import java.util.Map; /** * 短信处理器 * * @author qiangesoft * @date 2024-04-30 */ public interface SendHandler { /** * 发送短信 * * @param phones * @param param * @return */ boolean send(Collection<String> phones, Map<String, Object> param); /** * 发送短信 * * @param phone * @param param * @return */ default boolean send(String phone, Map<String, Object> param) { if (StringUtils.isBlank(phone)) { return false; } return send(Collections.singletonList(phone), param); } /** * 发送短信 * * @param phones * @param templateCode * @param param * @return */ boolean send(Collection<String> phones, String templateCode, Map<String, Object> param); /** * 发送短信 * * @param phone * @param templateCode * @param param * @return */ default boolean send(String phone, String templateCode, Map<String, Object> param) { if (StringUtils.isBlank(phone)) { return false; } return send(Collections.singletonList(phone), param); } }
package com.qiangesoft.sms.handler; import com.alibaba.fastjson2.JSONObject; import com.aliyuncs.IAcsClient; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; import com.aliyuncs.http.MethodType; import com.qiangesoft.sms.config.AliyunSmsProperties; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import java.util.Collection; import java.util.Map; /** * 阿里云短信处理器 * * @author qiangesoft * @date 2023-05-16 */ @Slf4j @RequiredArgsConstructor @Service public class AliyunSendHandler implements SendHandler { private final IAcsClient acsClient; private final AliyunSmsProperties aliyunSmsProperties; @Override public boolean send(Collection<String> phones, Map<String, Object> param) { return this.send(phones, aliyunSmsProperties.getTemplateCode(), param); } @Override public boolean send(Collection<String> phones, String templateCode, Map<String, Object> param) { SendSmsRequest request = new SendSmsRequest(); request.setSysMethod(MethodType.POST); request.setPhoneNumbers(StringUtils.join(phones, ",")); request.setSignName(aliyunSmsProperties.getSignName()); request.setTemplateCode(aliyunSmsProperties.getTemplateCode()); request.setTemplateParam(JSONObject.toJSONString(param)); try { SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); if ("OK".equals(sendSmsResponse.getCode())) { return true; } log.debug("send fail[code={}, message={}]", sendSmsResponse.getCode(), sendSmsResponse.getMessage()); } catch (Exception e) { log.debug(e.getMessage(), e); } return false; } }
package com.qiangesoft.sms.controller; import com.qiangesoft.sms.handler.SendHandler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; /** * 短信测试 * * @author qiangesoft * @date 2024-04-30 */ @RequestMapping("/sms") @RestController public class SmsController { @Autowired private SendHandler sendHandler; @GetMapping("/send") public Boolean send() { Map<String, Object> param = new HashMap<>(); param.put("name", "张三"); param.put("jds", 111); param.put("adds", "顶顶顶帆帆"); return sendHandler.send("15222222222", param); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。