1. 程式人生 > >極光短息傳送登入驗證碼功能

極光短息傳送登入驗證碼功能

//簡訊應用 APP_KEY 值
@Value("${jgclient.APP_KEY}")
public String APP_KEY;
//簡訊應用 MASTER_SECRET 的值
@Value("${jgclient.MASTER_SECRET}")
public String MASTER_SECRET;

//生成驗證碼

//傳送驗證碼

@PostMapping("/sendAuthCode")
public JsonResult sendAuthCode(@RequestParam("userPhone") String userPhone){
    JsonResult jsonResult = new JsonResult(JsonCode.SUCCESS);
    Integer code=(int)((Math.random()*9+1)*100000);
    String s = "";
    if("18500196937".equals(userPhone)){
        s = "666666";
    }else{
        s =String.valueOf(code);
    }
    SmsTempDTO smsTemp = new SmsTempDTO();
    smsTemp.setMobile(userPhone);
    smsTemp.setTemp_id("1");
    Map<String, String> temp_para = new HashMap<>();
    temp_para.put("code", s);//Resources.THIRDPARTY.getString("SMS_TEMP_KEY")
    smsTemp.setTemp_para(temp_para);
    boolean result = JSMSClientUtil.sendVerifyCode(smsTemp,MASTER_SECRET,APP_KEY);
    System.out.println(result);
    String authCodeId=  authCodeService.addAuthCode(userPhone,s,1);
    Map<String,Object> map=new HashMap();
    map.put("authCodeId",authCodeId);
    jsonResult.setResultInfo(map);
    return jsonResult;
}

//傳送設定

public static boolean sendVerifyCode(SmsTempDTO smsTemp,String MASTER_SECRET,String APP_KEY){
       SMSClient client = new SMSClient(MASTER_SECRET, APP_KEY);
       SMSPayload payload = SMSPayload.newBuilder()
             .setMobileNumber(smsTemp.getMobile())
             .setTempId(Integer.parseInt(smsTemp.getTemp_id()))
             .setTempPara(smsTemp.getTemp_para())
             .build();
       String result = null;
       try {
       SendSMSResult res = client.sendTemplateSMS(payload);
       result = res.toString();
       LOG.info(result);
       } catch (APIConnectionException e) {
          LOG.error("Connection error. Should retry later.", e);
       } catch (APIRequestException e) {
          LOG.error("Error response from JPush server. Should review and fix it. ", e);
          LOG.info("HTTP Status:" + e.getStatus());
          LOG.info("Error Message:" + e.getMessage());
       }catch(Exception e){
          LOG.error(">>>>>e:" + e);
          e.printStackTrace();
       }
       if(result.indexOf("msg_id") != -1){
          return true;
       }else{
          return false;
       }
   }