1、下载相关服务sdk
地址:http://docs.ucpaas.com/doku.php?id=sms_sdk
2、解压
加压下载好的sdk(restdemo_java_v2.zip)
3、创建项目
4、导入依赖
<!--短信服务依赖-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4</version>
</dependency>
5、修改配置
config.properties 这个文件从解压包中获取
is_test=true
rest_server=open.ucpaas.com
#发送短信参数信息(这些信息要填你们自己的。这些数据从云之讯平台中获取。自己购买短信服务后会有)
yzx.sid=5e52c4a4950184d930d6a8b8b979de8c
yzx.token=0aba0285cbab307904273ca0f6c501af
yzx.appid=d95bf28d7acc4bb1b12c0de0fad5525e
#验证码
yzx.regTemplateid=608353
#通知
yzx.faultTemplateid=608354
6、获取发送短信的工具包
7、使用
package com.lihua.phonecode.service;
import org.springframework.stereotype.Service;
/**
* 获取四位数验证码
* @author 15594
*/
public interface PhoneCodeService {
/**
*
* 返回一个四位的随机数,充当手机号验证码
* @return 字符串类型的验证码
* */
String getRandomCode();
/**
* 将验证码发送到指定手机
* @return 是否发送验证码成功
* @param code 验证码
* @param phone 手机号
* */
boolean sendCode(String code,String phone);
/**
* 验证码是否过期,通过redis zset数据类型判断
* @return 返回是否过期,过期为true
* @param phone 用户手机号,通过手机号标识过期时间
* */
boolean isOutTime(String phone);
}
package com.lihua.phonecode.service.impl;
import com.lihua.phonecode.service.PhoneCodeService;
import com.lihua.phonecode.utils.ucpaas.restDemo.client.JsonReqClient;
import lombok.Data;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.Random;
import java.util.UUID;
/**
* 生产手机验证码,并发送给手机验证。注意:使用properties的方式注入,一定要有set方法
* @author 15594
*/
@Service
@PropertySource(value = {"classpath:config.properties"})
@ConfigurationProperties(prefix = "yzx")
@Data
public class PhoneCodeServiceImpl implements PhoneCodeService {
/**
* 短信接口需要的参数,这些参数的值从config.properties文件中注入。注意:注入时必须提供set方法。这个通过lombok的@Data注解生成。
*/
private String sid;
private String token;
private String appid;
private String regTemplateid;
private String faultTemplateid;
@Override
public String getRandomCode() {
Random random = new Random();
int code = 1000+random.nextInt(8999);
return Integer.toString(code);
}
@Override
public boolean sendCode(String code, String phone) {
System.out.println(sid);
JsonReqClient jsonReqClient = new JsonReqClient();
String result = jsonReqClient.sendSms(sid, token, appid, regTemplateid, code, phone, null);
System.out.println("响应:"+result);
if (result.equals("000000")){
return true;
}else {
return false;
}
}
@Override
public boolean isOutTime(String phone) {
return true;
}
}
调用sendCode方法即可将验证码发送到用户手机。
返回参数:
更多看官网:http://docs.ucpaas.com/doku.php?id=error_code
8、官网提供的例子
官网提供的例子其实就在下载的zip压缩包里