Sms multi-platform short message service provider system ~ complete Ali cloud short message service sending can be configured by yourself

1. Introduce Maven into the project

Aliyun address

There are corresponding SDKs for different programming languages, just download what you need.

insert image description here
Add maven coordinates to pom.xml

    <!--阿里云短信服务-->
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>alibabacloud-dysmsapi20170525</artifactId>
        <version>2.0.22</version>
    </dependency>

insert image description here

2. Business code

The following code is not simply using the sending code provided by the SMS service provider to complete the sending of the SMS. It is too simple. I don’t think you want to read such articles. CSDN grabs a lot, or just read the official documents. . For general SMS service providers, it can be SMS services provided by Tencent Cloud, Alibaba Cloud, etc.


        The update of this article has been postponed for more than ten days, and there is no time to update it. The business code and project have been completed and I put it on gitee. At that time, I will make the warehouse public, so that you can also pull it down and change it to the corresponding one. SMS sending system.

project use

The following is to start talking about the data that needs to be configured for the project to be used out of the box.

system requirement

  • JDK >= 1.8
  • MySQL >= 5.7
  • Maven >= 3.0

necessary configuration

  • Modify the database connection, edit resourcesthe directoryapplication-dev.yml
# 应用服务 WEB 访问端口
server:
  #应用的端口号
  port: 8080
  servlet:
    #应用的访问路径
    context-path: /

# DataSource Config
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/itmei-sms?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=true
    username: root
    password: root
  • docThere are ready-to-use itmei-sms.sqlfiles in the directory, and then create a library to import sql into
    insert image description here
  • To modify the SMS service provider configuration information, edit resourcestheSmsParamsConfig.yml
## 配置短信
itmei-sms-config:
  #是否开启短信发送
  sms_send_or_not: true
  # 对接的短信服务商
  aliyun_sms:
    accessKeyId: xxx
    accessKeySecret: xxx
    signName: xxx
    #场景
    sms_scene:
      #场景类型
      appointment_success: xxx
      will_arrive_store: xxx
      send_it_the_night_before: xxx
      verification_code_sms: xxx
  # 腾讯云服务商
  tencent_sms:
    accessKeyId: #你自己的accessKeyId
    accessKeySecret: #你自己的accessKeySecret
    signName: #你自己的签名
    #场景
    sms_scene:
      #场景类型
      appointment_success: #你自己的短信模板
      will_arrive_store: #你自己的短信模板
      send_it_the_night_before: #你自己的短信模板
      verification_code_sms: #你自己的短信模板
      

Note:
      The name of the connected service provider tencent_sms,aliyun_smsis not random, each service provider corresponds to a service, and this service needs to implement SmsInterfacethe interface

insert image description here
The completed Aliyun SMS implementation class:

insert image description here

Regarding 场景类型名称的keythe need to configure SmsSceneEnumthe definition in the enumeration class.

insert image description here
      Because SmsParamsConfig.ymlthe configuration data will be converted into JSONObject objects for use. In this way, in the subsequent code, we can use the corresponding configuration to call through the characteristics of the corresponding service provider as shown in the figure below.
insert image description here

SmsSupplierConfigThe class is used to obtain the yml file we defined and convert the data Map<String, Object>into JSONObjectobjects.

SMS Service Provider

Take the Alibaba Cloud SMS service that I have completed as an example:
    create the corresponding SMS sending service class under the service package. If you SmsAliyunServiceImplneed to implement SmsInterfacethe interface rewriting method sendSmsand the sending method in it getDepartmentTemplateParam, the corresponding service provider will provide sample code and the corresponding jar package or maven We can introduce the coordinates into the system, write the sample code sendSmsinto the method and create a unique identifier in the service class and add it to SmsSupplierEnumthe SMS service provider enumeration
insert image description here

insert image description here
After writing the SMS service provider you are connected to according to the Alibaba Cloud sample code I wrote, we will start to assemble the required parameters and send the message.

Send SMS test

The test class is introduced in the project, and I wrote the code to send the test in the SmsApplicationTests class

    @Test
    void testSmsSend() {
    
    
        //配置需要使用的短信服务商信息等
        SmsTemplateConfig config = new SmsTemplateConfig();
        config.setSceneEnum(SmsSceneEnum.APPOINTMENT_SUCCESS.getCode());
        config.setSupplierEnum(SmsSupplierEnum.ALIYUN_SMS.getCode());
        config.setSourceEnum(SmsSendSourceEnum.PC.getCode());
        //自己短信模板的对应参数
        Map<String, Object> templateParam = new HashMap<>();
        templateParam.put("name", "itmei");
        templateParam.put("date_time", DateUtil.now());
        templateParam.put("address", "万达广场");
        templateParam.put("customer_phone", "19999999999");
        templateParam.put("landline", "wxid_itmei");
        config.setTemplateParam(templateParam);
        smsServiec.sendSms("19999999999", config, true);
    }

Finish

The SMS is successfully sent and saved to the database.
insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/qq_45502336/article/details/128844490