package com.rjnetwork.software.dev.crsd.util;
import java.io.IOException;
import java.util.Date;
import com.alibaba.fastjson.JSONException;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.json.JSONObject;
import com.xiaoleilu.hutool.json.JSONUtil;
import com.xiaoleilu.hutool.util.RandomUtil;
public class TencentCloudSmsUtil {
/**appid**/
public static final Integer appid = new Integer(140009*****);
/**appkey**/
public static final String appkey = new String("d0e864b34326bfb4155d421f6ea1c476");
/** 短信模板ID,需要在短信应用中申请**/
public static final Integer templateId = new Integer(129784);
/** 签名**/
public static final String smsSign = "光速车行";
/**发送短信**/
public static JSONObject sendSms(String phone) {
JSONObject jsonObject = JSONUtil.createObj();//{"result":0,"errmsg":"OK","ext":"","sid":"8:QDWcB3amfNukSewXgyR20180530","fee":1}
try {
SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
String code = RandomUtil.randomNumbers(6);
Date date = null;
Date finishDate = null;
String[] params = {code,"5"};
SmsSingleSenderResult result = ssender.sendWithParam("86", phone,
templateId, params, smsSign, "", ""); // 签名参数未提供或者为空时,会使用默认签名发送短信
if(result.errMsg.equals("OK")) {
date = new Date();//返回时间计算 返回时间是毫秒
finishDate=DateUtil.offsetMinute(date, 5);//验证码失效时间 返回时间是毫秒
}
jsonObject.put("state", "ok");
jsonObject.put("errmsg", "发送成功");
jsonObject.put("date", date);
jsonObject.put("finishDate", finishDate);
jsonObject.put("code", code);
jsonObject.put("phone", phone);
} catch (HTTPException e) {
jsonObject.put("phone", phone);
jsonObject.put("state", "failed");
jsonObject.put("errmsg", "http状态码错误:"+e);
} catch (JSONException e) {
jsonObject.put("phone", phone);
jsonObject.put("state", "failed");
jsonObject.put("errmsg", "json格式化错误:"+e);
} catch (IOException e) {
jsonObject.put("phone", phone);
jsonObject.put("state", "failed");
jsonObject.put("errmsg", "网络连接错误:"+e);
}
return jsonObject;
}
public static void main(String[] args) {
System.out.println(TencentCloudSmsUtil.sendSms("135****2695"));
}
import java.io.IOException;
import java.util.Date;
import com.alibaba.fastjson.JSONException;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.json.JSONObject;
import com.xiaoleilu.hutool.json.JSONUtil;
import com.xiaoleilu.hutool.util.RandomUtil;
public class TencentCloudSmsUtil {
/**appid**/
public static final Integer appid = new Integer(140009*****);
/**appkey**/
public static final String appkey = new String("d0e864b34326bfb4155d421f6ea1c476");
/** 短信模板ID,需要在短信应用中申请**/
public static final Integer templateId = new Integer(129784);
/** 签名**/
public static final String smsSign = "光速车行";
/**发送短信**/
public static JSONObject sendSms(String phone) {
JSONObject jsonObject = JSONUtil.createObj();//{"result":0,"errmsg":"OK","ext":"","sid":"8:QDWcB3amfNukSewXgyR20180530","fee":1}
try {
SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
String code = RandomUtil.randomNumbers(6);
Date date = null;
Date finishDate = null;
String[] params = {code,"5"};
SmsSingleSenderResult result = ssender.sendWithParam("86", phone,
templateId, params, smsSign, "", ""); // 签名参数未提供或者为空时,会使用默认签名发送短信
if(result.errMsg.equals("OK")) {
date = new Date();//返回时间计算 返回时间是毫秒
finishDate=DateUtil.offsetMinute(date, 5);//验证码失效时间 返回时间是毫秒
}
jsonObject.put("state", "ok");
jsonObject.put("errmsg", "发送成功");
jsonObject.put("date", date);
jsonObject.put("finishDate", finishDate);
jsonObject.put("code", code);
jsonObject.put("phone", phone);
} catch (HTTPException e) {
jsonObject.put("phone", phone);
jsonObject.put("state", "failed");
jsonObject.put("errmsg", "http状态码错误:"+e);
} catch (JSONException e) {
jsonObject.put("phone", phone);
jsonObject.put("state", "failed");
jsonObject.put("errmsg", "json格式化错误:"+e);
} catch (IOException e) {
jsonObject.put("phone", phone);
jsonObject.put("state", "failed");
jsonObject.put("errmsg", "网络连接错误:"+e);
}
return jsonObject;
}
public static void main(String[] args) {
System.out.println(TencentCloudSmsUtil.sendSms("135****2695"));
}
}
更多精彩内容 可以加qq群 进行交流哦!
QQ群:566421339
微信公众号:仁杰网络工作室
微信号:XHKJ_IT