工具類--發送驗證碼短信

SmsController層
@Slf4j
@RestController
@RequestMapping("/third/allincloud/sms")
public class SmsController {

private Logger logger = LoggerFactory.getLogger(SmsController.class);

@Autowired
SmsService smsService;

@ApiOperation(value = "發送驗證碼短信", notes = "發送驗證碼短信")
@ApiImplicitParams({
@ApiImplicitParam(name = "phone", paramType = "query", value = "會員手機號", dataType = "String"),
@ApiImplicitParam(name = "message", paramType = "query", value = "手機驗證碼", dataType = "String")
})
@GetMapping(value = "sendSms")
public int sendSms(
@RequestParam(value = "phone", required = true) String phone,
@RequestParam(value = "message", required = true) String message
) throws Exception {
return smsService.sendSms(Const.SEND_SMS_USERNAME, Const.SEND_SMS_PASSWORD, Const.SEND_SMS_URL, phone, message);
}

}

service層
@Service
@Transactional(rollbackFor = Exception.class)
public class SmsServiceImpl implements SmsService{

@Override
public int sendSms(String smsUserName, String smsPassword, String smsUrl, String phone, String message) throws Exception {
return SendSms.sendMessage(smsUserName, smsPassword, smsUrl, phone, message);
}
}

util類
@Slf4jpublic class SendSms {    private static final Logger logger = LoggerFactory.getLogger(SendSms.class);    public static int ERROR_SUCCESS = 0; //發送成功    public static int ERROR_USERNAME = -1; //用戶賬號不存在   用戶帳戶須要管理員在短信平臺上添加    public static int ERROR_PASSWORD = -2; //密碼錯誤    public static int ERROR_CAPTION = -3;  //主題不能爲空    public static int ERROR_CONTENT = -4;  //內容有誤 內容爲空或大於280個字    public static int ERROR_MOBILE = -5;   //手機長度有誤   手機號碼個數爲0或超過1000個    public static int ERROR_TIME_FORMAT = -6;  //定時時間錯誤   時間格式有誤(2012-09-15 17:37:00)    public static int ERROR_NO_MONEY = -7; //餘額不足    public static int ERROR_OTHER = -8;    //未知錯誤    public static int ERROR_RRID = -9; //Rrid長度有誤 不能超過16位    public static int ERROR_AUTHORITY = -10;   //沒有權限    public static int ERROR_SERVER_ERROR = 5;   //短信服務器通訊失敗    public static int ERROR_URL_ERROR = 10;  //請求地址錯誤;    public static int sendMessage(String username,String password,String sendurl,String mobile, String message) throws Exception {        StringBuilder commander = new StringBuilder();        String rrid = new Random(System.nanoTime()).nextInt() + "";        logger.debug("短信標識:{}-{}", mobile, rrid);        commander.append(String.format("%s?username=%s&password=%s&ext=&schtime=&rrid=%s", sendurl, username, password, rrid));        commander.append(String.format("&mobile=%s&content=%s", mobile, URLEncoder.encode(message, "utf-8")));        logger.debug("短信請求地址:{}-{}", mobile, commander.toString());        int nRet = ERROR_SUCCESS;        URL url = null;        HttpURLConnection urlConn = null;        try {            url = new URL(commander.toString());            urlConn = (HttpURLConnection) url.openConnection();            urlConn.connect();            BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));            StringBuffer returnContent = new StringBuffer();            String c = null;            while ((c = reader.readLine()) != null) {                returnContent.append(c);            }            reader.close();            String result = returnContent.toString().replaceAll("<[^>]*>", "");            logger.debug("短信發送結果:", mobile, rrid, result);            if (rrid.equals(result)) {                nRet = 0;            }        } catch (MalformedURLException e) {            log.error(e.getMessage(), e);            nRet =  ERROR_URL_ERROR;        } catch (Exception e) {            log.error(e.getMessage(), e);            nRet = ERROR_SERVER_ERROR;        } finally {            if (urlConn != null) {                urlConn.disconnect();            }        }        return nRet;    }//    public static void main(String[] args){//        String username=Configuration.SEND_SMS_USERNAME;//        String password=Configuration.SEND_SMS_PASSWORD;//        String sendurl=Configuration.SEND_SMS_URL;//        String mobile="18201039586";//        int smscode = (int)((Math.random()*9+1)*100000);//        String message="您的驗證碼是:"+smscode+",請不要把驗證碼泄露給其餘人!";//        try {//            int re=sendMessage(username,password,sendurl,mobile,message);//            System.out.println(re);//        }catch (Exception e){//            log.error(e.getMessage(), e);//        }//    }}
相關文章
相關標籤/搜索