1. import com.alibaba.fastjson.JSON;
    2. import com.aliyuncs.CommonRequest;
    3. import com.aliyuncs.CommonResponse;
    4. import com.aliyuncs.DefaultAcsClient;
    5. import com.aliyuncs.IAcsClient;
    6. import com.aliyuncs.exceptions.ClientException;
    7. import com.aliyuncs.exceptions.ServerException;
    8. import com.aliyuncs.http.MethodType;
    9. import com.aliyuncs.profile.DefaultProfile;
    10. import com.mega.framework.amqp.config.action.SmsProperties;
    11. import lombok.extern.slf4j.Slf4j;
    12. import org.springframework.stereotype.Service;
    13. import java.util.HashMap;
    14. import java.util.Map;
    15. @Slf4j
    16. @Service
    17. public class SmsService {
    18. private static final String SYS_DOMAIN = "dysmsapi.aliyuncs.com";
    19. private static final String SYS_VERSION = "2017-05-25";
    20. private static final String SYS_ACTION = "SendSms";
    21. private static final String REGION_ID = "RegionId";
    22. private static final String PHONE_NUMBERS = "PhoneNumbers";
    23. private static final String SIGN_NAME = "SignName";
    24. private static final String TEMPLATE_CODE = "TemplateCode";
    25. private static final String TEMPLATE_PARAM = "TemplateParam";
    26. private static final String OUT_ID = "OutId";
    27. private SmsProperties smsProperties;
    28. private CommonRequest commonRequest;
    29. public SmsService(SmsProperties smsProperties) {
    30. this.smsProperties = smsProperties;
    31. CommonRequest request = new CommonRequest();
    32. request.setSysMethod(MethodType.POST);
    33. request.setSysDomain(SYS_DOMAIN);
    34. request.setSysVersion(SYS_VERSION);
    35. request.setSysAction(SYS_ACTION);
    36. request.putQueryParameter(REGION_ID, smsProperties.getRegionId());
    37. request.putQueryParameter(SIGN_NAME, smsProperties.getSignName());
    38. request.putQueryParameter(OUT_ID, smsProperties.getOutId());
    39. commonRequest = request;
    40. }
    41. /**
    42. * 发送通用短信
    43. *
    44. * @param telNumber 手机号
    45. * @return 短信接口返回信息
    46. */
    47. public String sendComCms(String telNumber) {
    48. Map<String, String> templateParamsMap = new HashMap<>();
    49. int code = (int) ((Math.random() * 9 + 1) * 100000);
    50. templateParamsMap.put("code", String.valueOf(code));
    51. return sendSms(telNumber, JSON.toJSONString(templateParamsMap), smsProperties.getTemplateCode());
    52. }
    53. /**
    54. * 发送短信
    55. *
    56. * @param telNumber 手机号
    57. * @param templateParamsJson {'code':'255232'}
    58. * @param templateCode 模板编号
    59. * @return response.data
    60. */
    61. private String sendSms(String telNumber, String templateParamsJson, String templateCode) {
    62. DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", smsProperties.getAccessKeyId(), smsProperties.getAccessSecret());
    63. IAcsClient client = new DefaultAcsClient(profile);
    64. try {
    65. commonRequest.putQueryParameter(PHONE_NUMBERS, telNumber);
    66. commonRequest.putQueryParameter(TEMPLATE_CODE, templateCode);
    67. commonRequest.putQueryParameter(TEMPLATE_PARAM, templateParamsJson);
    68. CommonResponse response = client.getCommonResponse(commonRequest);
    69. log.info(response.getData());
    70. return response.getData();
    71. } catch (ServerException e) {
    72. e.printStackTrace();
    73. } catch (ClientException e) {
    74. e.printStackTrace();
    75. }
    76. return "error";
    77. }
    78. }
    1. import lombok.Getter;
    2. import org.springframework.boot.context.properties.ConfigurationProperties;
    3. import org.springframework.context.annotation.Configuration;
    4. @Getter
    5. @Configuration
    6. @ConfigurationProperties(prefix = "application.sms")
    7. public class SmsProperties {
    8. private String accessKeyId = "xx";
    9. private String accessSecret = "xx";
    10. private String regionId = "cn-hangzhou";
    11. private String signName = "达智绿洲工业云平台";
    12. private String templateCode = "xxx";
    13. private String outId = "34";
    14. }
    1. sms:
    2. accessKeyId: xx
    3. accessSecret: xx
    4. regionId: cn-hangzhou
    5. signName: 达智绿洲工业云平台
    6. templateCode: xx
    7. outId: 34