发表于: 2018-03-28 23:40:53

4 592


今日完成:

1.       荣联通过maven添加到项目中

<!--荣联SDK-->
<dependency>
    <groupId>
cn.bestwu</groupId>
    <artifactId>
ccp-rest</artifactId>
    <version>
2.7</version>
</dependency>

编写一个工具类调用荣联SDK

@Component
public class CCPRestSDKUtil {

   
private static Logger logger = Logger.getLogger(CCPRestSDKUtil.class);

    private
HashMap<String, Object> result = null;
    private static
CCPRestSDK restAPI = new CCPRestSDK();

    public
CCPRestSDKUtil(){}

   
public CCPRestSDKUtil(String serverIP, String serverPort, String accountSid, String accountToken, String appId) {
       
restAPI.init(serverIP, serverPort);// 初始化服务器地址和端口
       
restAPI.setAccount(accountSid, accountToken);// 初始化主帐号和主帐号TOKEN
       
restAPI.setAppId(appId);// 初始化应用ID
   
}

   
public boolean sendVerificationCode(String to, String templateId, String[] datas) {
       
result = restAPI.sendTemplateSMS(to, templateId, datas);
        boolean
flag = false;
        if
("000000".equals(result.get("statusCode"))) {
            flag =
true;
            
//正常返回输出data包体信息(map
           
String log = null;
           
HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
           
Set<String> keySet = data.keySet();
            for
(String key : keySet) {
                Object object = data.get(key)
;
               
log += (key + " = " + object);
           
}
           
logger.info(log);
       
} else {
           
//异常返回输出错误码和错误信息
           
flag = false;
           
logger.error("phoneNum=" + to + "错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
       
}
       
return flag;
   
}

}

将工具类注入到spring,设置初始化参数

<bean id="ccpRestSDKUtil" class="com.wyq.taskSeven.util.CCPRestSDKUtil">
    <constructor-arg
name="serverIP" value="app.cloopen.com"/>
    <constructor-arg
name="serverPort" value="8883"/>
    <constructor-arg
name="accountSid" value="8a216da862466676c2700152156cef"/>
    <constructor-arg
name="accountToken" value="abe30176b7335588f265265d01269a"/>
    <constructor-arg
name="appId" value="8a216da86246622545456676c2cc0cf5"/>
</bean>

controller传来的参数(手机号,密码,确认密码)存入一个map中,密码使用MD加密,并判断确认密码与密码是否一致,然后随机生成6为验证码,使用荣联像对应手机发送,并将手机号,密码,验证码存入redis

@Component
public class PhoneVerificationUtil {

   
private static Logger logger = Logger.getLogger(PhoneVerification.class);

   
@Autowired
   
private RedisTemplate redisTemplate;

   
@Autowired
   
private CCPRestSDKUtil ccpRestSDKUtil;

    private final static
String verificationType = "1";
    private final static
String verificationTime = "5";

    public boolean
sendVerificationCode(PhoneVerification phoneVerification) {
        String verificationCodeService = String.valueOf((
int) (Math.random() * 9 + 1) * 100000);
       
Map<String, String> map = pullMap(phoneVerification, verificationCodeService);
        boolean
flag = baseVerification(map);
        if
(flag) {
            String[] datas =
new String[]{map.get("verificationCodeService"), verificationTime};
           
flag = ccpRestSDKUtil.sendVerificationCode(map.get("phone"), verificationType, datas);
       
}
       
if (flag) {
           
redisTemplate.opsForSet().add(map.get("phone"), map);
           
Map<String, String> test = (Map)redisTemplate.opsForSet().pop("phone");
           
System.out.println(redisTemplate.opsForSet().pop("phone"));
       
}
       
logger.info("验证码发送情况:" + flag);
        return
flag;
   
}

   
private Map<String, String> pullMap(PhoneVerification phoneVerification, String verificationCodeService) {

        Map<String
, String> map = new HashMap<String, String>();
       
String phone = phoneVerification.getPhone();
       
String password = MD5.getResult(phoneVerification.getPassword());
       
String passwordAgain = MD5.getResult(phoneVerification.getPasswordAgain());

       
System.out.println(phone);
       
map.put("phone", phone);
       
map.put("password", password);
       
map.put("passwordAgain", passwordAgain);
       
map.put("verificationCodeService", verificationCodeService);
        return
map;
   
}

   
private boolean baseVerification(Map<String, String> map) {
       
boolean flag = true;
        if
(!AccountValidatorUtil.isMobile(map.get("phone"))) {
            flag =
false;
       
}
       
if (!AccountValidatorUtil.isPassword(map.get("password"))) {
            flag =
false;
       
}
       
if (!(map.get("password").equals(map.get("passwordAgain")))) {
            flag =
false;
       
}
       
if (flag) {
            map.remove(
"passwordAgain");
       
}
       
return flag;
   
}
}

jsp页面测试能正常发出验证码。

明日计划:

1.       部署好jsp页面实现验证码注册和账号密码登陆

2.       配置邮箱

遇到的问题:

1.       代码耦合特别严重,完全不知道如何设计,慢慢来吧

收获:

1.       将荣联短信api接入项目。



返回列表 返回列表
评论

    分享到