融合云信平台对接_java代码

  • Post author:
  • Post category:java

/**
 * @author lqfeng
 * @desc  调用代码
 * @date 2022/6/8 11:07
 */
public class JYMessageSend implements IShellSmsMessage {
    private static Logger log = LoggerFactory.getLogger(JYMessageSend.class);
    @Override
    public void sendShortMessage(String mobile, String verifyCode, long verifyNo, Date expiredTime, Map<String, Object> params) {
        log.info("收到发送短信请求,手机号为:{},验证码为:{},有效期至:{},请求参数为:{},短信模板id:2764532",mobile,verifyCode,expiredTime,params);
        String templateId = new FrameConfigService9().getFrameConfigValueByNameWithDefault("sms_templateId", "2764532");
        MessageSendUtil.sendMessage(mobile,verifyCode,templateId);
    }
}


/**
 * 短信、公众号信息发送工具类
 * 
 * @version [1.0, 2021/9/9 17:42]
 * @Author 
 */
public class MessageSendUtil
{

    /**
     * 记录短信发送日志
     */
    private static Logger log = Logger.getLogger(MessageSendUtil.class);

    /**
     * 短信发送接口地址
     */
    public static final String MESSAGE_SEND_URL = "";

    /**
     * 短信参数cpcode-渠道在融合云信平台申请的cpcode 登录自服务平台,商户信息页面中的【商户编码】
     */
    public static final String CPCODE = "";

    /**
     * 短信参数AccessKey-登录自服务平台,进入系统设置页,【AccessKey】值为key
     */
    public static final String ACCESS_Key = "";

    /**
     * 消息模板id
     */
//    public static final String TEMPLET_ID = "272472";


    /**
     * 发送短信消息
     * 
     * @param mobiles
     *            手机号码,多个用,隔开
     * @param msg
     *            短信内容
     * @return
     */
    public static void sendMessage(String mobiles, String msg,
            String templetid) {

        String retResult = "";
        // 接口参数
        JSONObject params = new JSONObject();
        params.put("cpcode", CPCODE);
        params.put("msg", msg);
        params.put("mobiles", mobiles);
        params.put("excode", "");
        params.put("templetid", templetid);
        try {
            params.put("sign", md5Sign(msg, mobiles, "", templetid));
            // 设置超时时间10s
            HttpUtil.RequestOptions options = new HttpUtil.RequestOptions(10000, 10000, 10000);
            retResult = HttpUtil.doHttp(MESSAGE_SEND_URL, null, params.toString(), HttpUtil.POST_METHOD,
                    HttpUtil.RTN_TYPE_STRING, options);
        }
        catch (Exception e) {
            log.error("---------发送短信消息接口调用失败,错误信息:" + e.getMessage());
            // 设置短信发送状态 1成功 0失败
            e.printStackTrace();
        }
        if (StringUtil.isNotBlank(retResult) && !"{}".equals(retResult)) {
            JSONObject rtnJson = JSONObject.parseObject(retResult);
            // 判断短信是否发送成功
            if (rtnJson.containsKey("resultcode") && "0".equals(rtnJson.getString("resultcode"))) {
                // 设置短信发送状态 1成功 0失败
            }
            else {
//                projectMessagesendLog.setSendstatus(0);
            }
        }
        else {
//            projectMessagesendLog.setSendstatus(0);
        }
    }
    /**
     * 
     * @param cpcode
     *            渠道在融合云信平台申请的cpcode 登录自服务平台,商户信息页面中的【商户编码】
     * @param msg
     *            模板参数值,如果包含多个参数,以半角英文逗号分隔
     * @param mobiles
     *            11位手机号,如果包含多个手机号,请用半角英文逗号分隔,最多支持300个手机号
     * @param excode
     *            渠道自定义接入号的扩展码,可为空;为空时传空字符串””
     * @param templetid
     *            渠道在融合云信平台申请的模板短信ID
     * @param key
     *            MD5签名,签名字符串为cpcode+
     *            msg+mobiles+excode+templetid+key,其中key为融合云信平台分配的私钥,将签名字符串用MD5加密后转换为小写字符串。
     * @return
     */
    public static String md5Sign(String msg, String mobiles, String excode, String templetid) {
        String md5source = CPCODE + msg + mobiles + excode + templetid + ACCESS_Key;
        String md5str = "";
        try {
            md5str = StringUtil.toLowerCase(makeMD5(new String(md5source.getBytes("UTF-8"), "UTF-8")));
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return md5str;
    }

    /**
     * md5加密
     * 
     * @param plainText
     *            要md5加密的字符串
     * @return
     */
    public static String makeMD5(String plainText) {
        String re_md5 = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes("UTF-8"));
            byte[] b = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
            re_md5 = StringUtil.toLowerCase(buf.toString());
        }
        catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return re_md5;
    }

    /**
     * 拼接GET请求url
     * @param apiUrl url
     * @param params 参数
     * @return
     */
    public static String getFullApiUrl(String apiUrl, Object params) {
        if (StringUtil.isBlank(params)) {
            return apiUrl;
        }
        else {
            StringBuffer param = new StringBuffer();
            if (params instanceof Map) {
                Map<String, Object> paramsConvert = (Map)params;
                int i = 0;
                String key = "";

                for(Iterator paramsData = paramsConvert.entrySet().iterator(); paramsData.hasNext(); ++i) {
                    Map.Entry<String, Object> entry = (Map.Entry)paramsData.next();
                    key = entry.getKey();
                    if (i == 0) {
                        param.append("?");
                    }
                    else {
                        param.append("&");
                    }

                    param.append(key).append("=").append(paramsConvert.get(key));
                }
            }
            else {
                param.append("?" + params.toString());
            }

            return apiUrl + param;
        }
    }
}

版权声明:本文为qq_44275894原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。