JAVA实现阿里短信云平台发送短信(验证码)

1.pom.xml

   <!-- 阿里云短信平台 -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>dysmsapi20170525</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>tea-openapi</artifactId>
            <version>0.0.13</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>tea-console</artifactId>
            <version>0.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>darabonba-env</artifactId>
            <version>0.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>tea-util</artifactId>
            <version>0.2.11</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>darabonba-time</artifactId>
            <version>0.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>darabonba-string</artifactId>
            <version>0.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>tea</artifactId>
            <version>[1.0.3, 2.0.0]</version>
        </dependency>

2.代码部分



@RestController
@RequestMapping("/mobile")
public class SampleController {
   
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config();
        config.accessKeyId = accessKeyId;
        config.accessKeySecret = accessKeySecret;
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    @PostMapping("/getCode/{phone}")
    public AppDataModel getCode(@PathVariable("phone") @RequestBody String phone) throws Exception {
        AppDataModel appDataModel = new AppDataModel();
        // 格式检验,可以正则检验
        if (phone.length() != 11) {
            appDataModel.status = 0;
            appDataModel.message = "手机号格式错误";
            return appDataModel;
        }
       
        String verification_code = createRandomNum(6);
        com.aliyun.dysmsapi20170525.Client client = SampleController.createClient("AccessKeyId", "AccessKeySecret");
        // 1.发送短信
        SendSmsRequest sendReq = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName("签名 例:阿里云")
                .setTemplateCode("xxx_xxxxxxx 模板")
                .setTemplateParam("{" + "\"code\": " + verification_code + "}");
        SendSmsResponse sendResp = client.sendSms(sendReq);
        String code = sendResp.body.code;
        if (!com.aliyun.teautil.Common.equalString(code, "OK")) {
            com.aliyun.teaconsole.Client.log("错误信息: " + sendResp.body.message + "");
            appDataModel.status = 0;
            appDataModel.message = "发送失败";
            return appDataModel;
        }

        String bizId = sendResp.body.bizId;
        // 2. 等待 3 秒后查询结果
        com.aliyun.teautil.Common.sleep(3000);
        // 3.查询结果
        java.util.List<String> phoneNums = com.aliyun.darabonbastring.Client.split(phone, ",", -1);
        for (String phoneNum : phoneNums) {
            QuerySendDetailsRequest queryReq = new QuerySendDetailsRequest()
                    .setPhoneNumber(com.aliyun.teautil.Common.assertAsString(phoneNum))
                    .setBizId(bizId)
                    .setSendDate(com.aliyun.darabonbatime.Client.format("yyyyMMdd"))
                    .setPageSize(10L)
                    .setCurrentPage(1L);
            QuerySendDetailsResponse queryResp = client.querySendDetails(queryReq);
            java.util.List<QuerySendDetailsResponseBody.QuerySendDetailsResponseBodySmsSendDetailDTOsSmsSendDetailDTO> dtos = queryResp.body.smsSendDetailDTOs.smsSendDetailDTO;
            // 打印结果
            for (QuerySendDetailsResponseBody.QuerySendDetailsResponseBodySmsSendDetailDTOsSmsSendDetailDTO dto : dtos) {
                
                if (com.aliyun.teautil.Common.equalString("" + dto.sendStatus + "", "3")) {
                    com.aliyun.teaconsole.Client.log("" + dto.phoneNum + " 发送成功,接收时间: " + dto.receiveDate + "");
                    appDataModel.status = 1;
                    appDataModel.message = "发送成功";
                } else if (com.aliyun.teautil.Common.equalString("" + dto.sendStatus + "", "2")) {
                    com.aliyun.teaconsole.Client.log("" + dto.phoneNum + " 发送失败");
                    appDataModel.status = 0;
                    appDataModel.message = "发送失败";
                } else {
                    com.aliyun.teaconsole.Client.log("" + dto.phoneNum + " 正在发送中...");
                    appDataModel.status = 0;
                    appDataModel.message = "正在发送中...";
                }

            }
        }
        return appDataModel;
    }

    /**
     * 生成随机数
     *
     * @param num 位数
     * @return
     */
    public static String createRandomNum(int num) {
        String randomNumStr = "";
        for (int i = 0; i < num; i++) {
            int randomNum = (int) (Math.random() * 10);
            randomNumStr += randomNum;
        }
        return randomNumStr;
    }

}
上一篇:jmeter压测dubbo接口,参数为dto时如何写传参及有错误时的分析思路


下一篇:Dto中有List children,那么如何把List转为List