com.hn.sms.chuanglan.ChuangLanSms Maven / Gradle / Ivy
package com.hn.sms.chuanglan;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.hn.config.HnConfigUtils;
import com.hn.sms.Sms;
import com.hn.sms.chuanglan.domain.ChuangLanParam;
import com.hn.config.exception.ConfigException;
import com.hn.sms.exception.SmsException;
import com.hn.utils.AssertUtils;
/**
* 描述:
* 创蓝253
*
* @author fei
* 2019-03-19 10:54
*/
public class ChuangLanSms implements Sms {
private static final Log log = LogFactory.get();
/**
* 创蓝253 请求账户地址
*/
private static final String API_REQUEST_URL = "http://smssh1.253.com/msg/send/json";
private ChuangLanParam param;
public ChuangLanSms(ChuangLanParam param) {
this.param = param;
}
public ChuangLanSms(String scene) {
this.param = getRequestParam(scene);
}
public void scene(String scene){
this.param = getRequestParam(scene);
}
/**
* 发送普通短信验证码
*
* @param phone 手机号
* @param code 短信验证码
* @return {boolean}
*/
@Override
public boolean send(String phone, String code) {
AssertUtils.notNull(param, "param is null");
AssertUtils.notNull(phone, "手机号必填");
// AssertUtils.notNull(code, "短信验证码必填");
param.setMobile(phone);
param.setAuthCode(code);
param.setReport("true");
String requestJson = JSONUtil.toJsonStr(param);
log.info("创蓝253短信原始请求报文: {}", requestJson);
String response = HttpRequest.post(API_REQUEST_URL)
.body(requestJson)
.execute().body();
log.info("创蓝253短信响应报文: {}", response);
JSONObject result = JSONUtil.parseObj(response);
if ("0".equals(result.getStr("code"))) {
return true;
}
throw new SmsException("短信发送失败," + result.getStr("errorMsg"));
}
/**
* 配置前缀名
*/
private static final String CONFIG_KEY = "sms.chuangLan";
/**
* 获取数据库配置参数
*
* @return ChuangLanParam
*/
private ChuangLanParam getRequestParam(String scene) {
if (StrUtil.isNotBlank(scene)) {
scene = "-".concat(scene);
}
// 从数据库读取配置
String apiAccount = AssertUtils.notNull(HnConfigUtils.getConfig(CONFIG_KEY.concat(scene).concat(".apiAccount")),
ConfigException.exception("创蓝253账号未配置"));
String apiSecret = AssertUtils.notNull(HnConfigUtils.getConfig(CONFIG_KEY.concat(scene).concat(".apiSecret")),
ConfigException.exception("创蓝253密码未配置"));
String msgModel = AssertUtils.notNull(HnConfigUtils.getConfig(CONFIG_KEY.concat(scene).concat(".msgModel")),
ConfigException.exception("创蓝253消息模板未配置"));
ChuangLanParam param = new ChuangLanParam();
// 从数据库读取配置
param.setApiAccount(apiAccount);
param.setApiSecret(apiSecret);
param.setMsgModel(msgModel);
return param;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy