All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hn.sms.juhe.JuHeSms Maven / Gradle / Ivy

There is a newer version: 1.0.18
Show newest version
package com.hn.sms.juhe;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
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.config.exception.ConfigException;
import com.hn.sms.exception.SmsException;
import com.hn.sms.juhe.domain.JuHeParam;
import com.hn.utils.AssertUtils;

/**
 * 描述:
 * 聚合短信
 *
 * @author fei
 *  2019-03-19 11:04
 */
public class JuHeSms implements Sms {
    private static final Log log = LogFactory.get();

    /**
     * 请求地址
     */
    private static final String REQUEST_URL = "http://47.97.226.4/sms/send";

    private JuHeParam param;

    public JuHeSms(JuHeParam param) {
        this.param = param;
    }
    public JuHeSms(String scene) {
        this.param = getRequestParam(scene);
    }

    /**
     * 读取数据库配置,发送短信验证码
     * @param phone 手机号
     * @param code 验证码
     * @return {boolean}
     */
    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);
        log.info("聚合短信原始请求报文: {}", JSONUtil.toJsonStr(param));
        String repStr = HttpUtil.get(REQUEST_URL, param);
        log.info("聚合短信响应报文 :" + repStr);
        JSONObject repJson = JSONUtil.parseObj(repStr);
        if ("0".equals(repJson.getStr("error_code"))) {
            return true;
        }
        throw new SmsException("短信发送失败," + repJson.getStr("reason"));
    }

    /**
     *  配置前缀名
     */
    private static final String CONFIG_KEY = "sms.juhe";

    /**
     *  获取数据库聚合短信配置
     * @return JuHeParam
     */
    private JuHeParam getRequestParam(String scene){
        if(StrUtil.isNotBlank(scene)){
            scene = "-".concat(scene);
        }

        String appKey = AssertUtils.notNull(HnConfigUtils.getConfig(CONFIG_KEY.concat(scene).concat(".appkey")),
                ConfigException.exception("聚合短信appkey未配置"));
        String msgMobanId = AssertUtils.notNull(HnConfigUtils.getConfig(CONFIG_KEY.concat(scene).concat(".mobanId")),
                ConfigException.exception("聚合短信mobanId未配置"));

        JuHeParam juHeRequest = new JuHeParam();
        // 从数据库读取配置
        juHeRequest.setAppKey(appKey);
        juHeRequest.setMsgMobanId(msgMobanId);
        return juHeRequest;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy