手机发短信

package com.jeemis.framework.util;

import com.alibaba.fastjson.JSON;
import com.jeemis.framework.core.util.HttpUtil;
import com.jeemis.framework.core.util.JsonUtil;
import com.jeemis.framework.core.util.StringUtil;
import com.jeemis.framework.web.JeemisApplicationContext;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 短信发送工具类
 * @author liuz
 * @version v1.0
 * 

* Created on 2017年8月11日 *

* Revision History: Date Reviser Description ---- ------- * ---------------------------------------------------- *

* Description: */ public class SMSUtil { private static final Logger logger = LoggerFactory.getLogger("SMSLOG"); /** * 发送短信验证码 * @param mobile 手机号 * @param verfiyCode 验证码 * @param message 消息 * @return true 发送成功 false 发送失败 * * 接口描述 请求方式:post 描述 发送来过app的短信 @param mobile 手机号码 @param message 调用方传过来的消息 @param verfiyCode 调用方传入的验证码 传入的code需加密加密方式参考/sm/sendAuditSM接口 @return 返回json数据, 成功:{"rspCode":1,"rspDesc":"success","result":null} 失败:{"rspCode":0,"rspDesc":"fail","result":null} */ @SuppressWarnings("unchecked") public static boolean sendVerfiyCodeSms(String mobile, String verfiyCode, String message) { try { String url = "http://app.travelsky.com/csapp/sm/sendLGSM";//JeemisApplicationContext.getContext("SMSGatewayUrl"); String key = "140018cs";//JeemisApplicationContext.getContext("SMSKeyCode"); Map params = new HashMap(); params.put("phone", mobile); params.put("message", message); String code = DESUtils.encryptDES(verfiyCode, key); params.put("code", code); //System.out.println(code); //System.out.println(DESUtils.decryptDES(code, key)); String _sResponseBody = HttpUtil.sendPostRequest(url, params, null, null); Map resultMap = (Map)JsonUtil.parseToObject(_sResponseBody); if(resultMap != null && !resultMap.isEmpty()) { String status = String.valueOf(resultMap.get("status")); if("200".equals(status)){ String result = String.valueOf(resultMap.get("result")); if(StringUtil.isNotBlank(result)) { Map resMap = JSON.parseObject(result, Map.class); if(resMap != null && !resMap.isEmpty() && resMap.get("rspCode") != null) { if((Integer)resMap.get("rspCode") == 1) { logger.error("{},{},{},{},{}",DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"), mobile, message, code, "succ"); return true; } } } } } } catch (Exception e) { e.printStackTrace(); } logger.error("{},{},{},{},{}",DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"), mobile, message, "", "fail"); return false; } public static void main(String[] args) { String verifyCode = VerifyCodeUtils.generateVerifyCode(6, "0123456789"); //调用短信网关发送短信验证码 boolean sendResult = SMSUtil.sendVerfiyCodeSms("15804019477", verifyCode, "您正在使用来过APP(登录),验证码("+verifyCode+"),请及时验证。(为了保证帐号安全,请勿向他人泄露。如非本人操作,请忽略。)"); } }

你可能感兴趣的:(一些有用的工具类)