10_ Travel reservation process requirements_ Steps for Alibaba cloud to use SMS

Posted by AchillesForce on Fri, 03 Dec 2021 14:23:11 +0100

Chapter I. requirements of tourism reservation process

Users can make travel reservations through the following operation process:
1. Click travel reservation on the home page of the mobile terminal, and the page will jump to the package list page
2. Click the package to be reserved on the package list page, and the page will jump to the package details page
3. Click make an appointment now on the package details page, and the page will jump to the appointment page
4. Enter the tourist information, including mobile phone number, on the reservation page, and click send to verify
5. Enter the received SMS verification code on the reservation page, click Submit reservation to complete the travel reservation

 package list page:

 package details page:

 appointment page:

Click [submit appointment] to complete the appointment.

Chapter II. SMS sending [Alibaba SMS]

  1. Introduction to SMS service
  2. Register alicloud account
  3. Set SMS signature
  4. Set SMS template
  5. Set access keys
  6. SMS API
  7. Send SMS

2.1. Introduction to SMS service

At present, there are many short message services provided by third parties in the market. These third-party short message services will connect with various operators (China Mobile, China Unicom and Telecom). We only need to register as a member and call according to the provided development documents to send short messages. It should be noted that these SMS services are paid services.
For this project, we choose the SMS service provided by Alibaba cloud.
Short Message Service is a communication service provided by Alibaba cloud for users. It supports sending SMS verification codes and SMS notifications quickly. The three networks in one exclusive channel is interconnected with the number carrying network transfer platform of the Ministry of industry and information technology in real time. Carrier level operation and maintenance support, real-time monitoring and automatic switching, and the arrival rate is as high as 99%. The SMS service API provides the capabilities of SMS sending, sending status query and batch sending. After adding signatures and templates on the SMS service console and passing the approval, you can call the SMS service API to complete SMS sending and other operations.

2.2. Register an alicloud account

Alibaba cloud official website: https://www.aliyun.com/
Click free registration on the homepage of the official website to jump to the following registration page:

After registration, log in with the account name

2.3. Setting SMS signature

After successful registration, click the login button to log in. After logging in, enter the SMS service management page and select the domestic message menu:
Click product classification - > Cloud Computing Foundation - > cloud communication - > SMS service


[signature]:
Select signature management


Click the add signature button:

At present, individual users can only apply for signatures whose applicable scenario is verification code, and general needs enterprise authentication.
Note:

2.4. Set SMS template

On the domestic message menu page, click the template management tab:

Click the add template button:

The content of my template is: American ambassador verification code n u m b e r , you just enter that 's ok beautiful year country Bin system Unified of body share Check card , hit die no Tell Sue other people ! his in {number}, you are in the process of authentication of the American ambassador system. Don't tell anyone! among Number, you are authenticating the national guest system of the United States year. Don't tell others! Where {number} is a dynamic parameter, which needs to be controlled in the code later.

2.5. Setting access keys

Identity authentication is required when sending SMS. SMS can be sent only after authentication. This section is to set the key and key used for identity authentication when sending SMS. Place the mouse over the current user's Avatar in the upper right corner of the page, and a drop-down menu will appear:

Click access keys:

Click the "start using sub user AccessKey" button to specify user permissions instead of assigning all permissions.
Step 1: create a new user
Enter the login name and display name, and click [confirm]

Receive short messages to prevent information leakage. Just enter the verification code.

The new user is created successfully. After that, it is found that ak is not authorized

Step 2: authorization
Select user and add permission


Search "SMS" to indicate SMS service. Select permission and click "start creation".

Step 3: create AccessKeyID
Click the created user to enter the details page.

Successfully created, where AccessKeyID is the ID used to access the SMS service and AccessKeySecret is the key.

Note: Yes, yes. You need to save the AccessKeyID and AccessKeySecret immediately. For security reasons, this is only displayed once. Once you exit the page, it will no longer be displayed. If not saved, delete and recreate
Click View user details to disable the AccessKey just created under the user details page
In the SMS service, click "domestic message settings". You can set the daily and monthly SMS sending limit:

Since the SMS service is a toll service, you need to recharge to send SMS:
In the cost, click "recharge", generally 50 cents is enough.

2.6. SMS service API

Click Help documentation


Find "SMS sending API" in SMS service


The code can be copied into the project for testing:

Modification required:
1: accessKeyId and accessKeySecret

final String accessKeyId = "LTAI4Fbws3CCEzKx3JPHqXQC";//For your accessKeyId, refer to step 2 of this document
final String accessKeySecret = "3XFb3eNq6IDZ5Ik3x10vo9YHHzMGBz";//For your accessKeySecret, refer to step 2 of this document

2: Mobile phone number

request.setPhoneNumbers("1326921xxxx");

3: Signatures and templates

request.setSignName("American ambassador");
//Required: SMS template - can be found in the SMS console. When sending international / Hong Kong, Macao and Taiwan messages, please use the international / Hong Kong, Macao and Taiwan SMS template
request.setTemplateCode("SMS_180947117");

4: Verification code and parameter number sent (according to the content of template SMS)

String params = "111111";
request.setTemplateParam("{\"number\":\""+params+"\"}");

2.7. Send SMS

2.7.1. Import maven coordinates

In meinian_ Import coordinates in common

<dependency>
 <groupId>com.aliyun</groupId>
 <artifactId>aliyun-java-sdk-core</artifactId>
 <version>3.3.1</version>
</dependency>
<dependency>
 <groupId>com.aliyun</groupId>
 <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
 <version>1.0.0</version>
</dependency>

2.7.2. Packaging tools

In meinian_ Add tool class in common
1: Signature

2: Template code

stay meinian_common Medium, encapsulated SMSUtils.java
 Pass verification code and mobile phone number

package com.atguigu.utils;

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.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class SMSUtils {
    public static final String VALIDATE_CODE = "SMS_159620392";//Send SMS verification code
    public static final String ORDER_NOTICE = "SMS_159771588";//Appointment success notification

    /**
    * Send SMS
    * @param phoneNumbers
    * @param param
    * AccessKeyID:
    *         LTAI4Fbws3CCEzKx3JPHqXQC
    * AccessKeySecret:
    *         3XFb3eNq6IDZ5Ik3x10vo9YHHzMGBz
    */
    public static void sendShortMessage(String phoneNumbers,String param) throws Exception{
        // Set timeout - self adjustable
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        // Several parameters required to initialize ascClient
        final String product = "Dysmsapi";// SMS API product name (SMS product name is fixed and does not need to be modified)
        final String domain = "dysmsapi.aliyuncs.com";// SMS API product domain name (the interface address is fixed and does not need to be modified)
        // Replace with your AK
        final String accessKeyId = "LTAI4Fbws3CCEzKx3JPHqXQC";// For your accessKeyId, refer to step 2 of this document
        final String accessKeySecret = "3XFb3eNq6IDZ5Ik3x10vo9YHHzMGBz";// For your accessKeySecret, refer to step 2 of this document
        // Initialize ascClient. Multiple region s are not supported temporarily (please do not modify)
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        // Assembly request object
        SendSmsRequest request = new SendSmsRequest();
        // Submit using post
        request.setMethod(MethodType.POST);
        // Required: mobile phone number to be sent. It supports batch calls separated by commas. The maximum batch number is 1000 mobile phone numbers. The timeliness of batch calls is slightly delayed compared with a single call. It is recommended to use a single call for verification code type SMS
        request.setPhoneNumbers(phoneNumbers);
        // Required: SMS signature - can be found in SMS console
        request.setSignName("American ambassador");
        // Required: SMS template - can be found in SMS console
        request.setTemplateCode("SMS_180947117");
        // Optional: the variables in the template replace the JSON string. For example, when the template content is "Dear ${name}, and your verification code is ${code}", the value here is
        // Friendly note: if line breaks are required in JSON, please refer to the requirements of the standard JSON protocol for line breaks. For example, if the SMS content contains \ r\n, it needs to be expressed as \ \ r\n in JSON, otherwise JSON will fail to parse on the server
        request.setTemplateParam("{\"number\":\""+param+"\"}");
        // Optional - uplink SMS extension code (the extension code field is controlled to be 7 digits or less, and users with no special needs should ignore this field)
        // request.setSmsUpExtendCode("90997");
        // Optional: outId is the extension field provided to the business party. Finally, this value will be brought back to the caller in the SMS receipt message
        // request.setOutId("yourOutId");
        // The request fails. A ClientException exception will be thrown here
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
                // Request succeeded
                System.out.println("Request succeeded"+sendSmsResponse.getCode());
        }
    }
}

2.7.3. Test SMS sending

package com.atguigu;

import com.atguigu.utils.SMSUtils;
import com.atguigu.utils.ValidateCodeUtils;
import org.junit.Test;

public class TestSms {
   @Test
   public void test()  throws Exception{
       Integer code = ValidateCodeUtils.generateValidateCode(4);
       SMSUtils.sendShortMessage("133****5560",String.valueOf(code));
  }
}

Test: View phone

[summary]

Alibaba cloud usage steps

  1. Registration, login
  2. Search SMS service and open SMS service
  3. Enter SMS console
  4. Application signature, template
  5. Charge money (about 5 yuan) and check access keys
  6. Add dependencies and copy tool classes to the project
  7. test
    matters needing attention
    In the tool class (you need to change the template code, signature, access keys, verification code code = = > number)

Topics: Mini Program cloud computing Alibaba Cloud