package com.nuvole.util.sms;
|
|
import cn.hutool.core.util.RandomUtil;
|
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.http.HttpUtil;
|
import com.alibaba.fastjson.JSONObject;
|
import com.nuvole.common.domain.emnu.SMSResultEmnu;
|
import com.nuvole.common.domain.result.SMSResult;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 短信(云片)
|
*
|
* @Author: lc
|
* @Date: 2019/6/1 11:09
|
*/
|
public class YpSMSUtil {
|
|
|
private static String YP_SMS_K = "faf531146ca1e38abacd3862fb3fc32b";
|
private static String YP_SMS_URL = "https://sms.yunpian.com/v2/sms/single_send.json";
|
|
|
/**
|
* 发送短信
|
*
|
* @param mobile 手机号码字符串 多个号码以逗号分割
|
* @param message 短信内容
|
* @Author: lc
|
* @Date: 2019/6/1 11:27
|
*/
|
public static SMSResult sendSMS(String mobile, String message) {
|
|
if (StrUtil.isEmpty(mobile)) {
|
return new SMSResult(SMSResultEmnu.ERROR, "手机号不能为空!");
|
}
|
if (StrUtil.isEmpty(message)) {
|
return new SMSResult(SMSResultEmnu.ERROR, "短信内容不能为空!");
|
}
|
try {
|
Map<String, Object> param = new HashMap<>();
|
param.put("apikey", YP_SMS_K);
|
param.put("mobile", mobile);
|
param.put("text", message);
|
String msg = HttpUtil.post(YP_SMS_URL, param);
|
JSONObject jsonObject = JSONObject.parseObject(msg);
|
if (!"0".equals(jsonObject.getString("code"))) {
|
return new SMSResult(SMSResultEmnu.ERROR, jsonObject.getString("msg"));
|
}
|
return new SMSResult(SMSResultEmnu.OK);
|
} catch (Exception e) {
|
e.printStackTrace();
|
return new SMSResult(SMSResultEmnu.ERROR, "发送异常!");
|
}
|
}
|
|
public static void main(String[] args) {
|
System.out.println(sendSMS("17803846500", SMSTemplate.getVaildCodeMsg(RandomUtil.randomNumbers(6))));
|
}
|
|
}
|