参数

在配置表中,配置以下参数:

名称 配置名 配置值
【创瑞短信】开发key sms.chuangRui.accesskey accesskey
【创瑞短信】开发秘钥 sms.chuangRui.secret secret
【创瑞短信】签名 sms.chuangRui.sign sign
【创瑞短信】模板id sms.chuangRui.templateId templateId

调用工具类插入参数

  1. String accessKey="kwt";
  2. String secret="1C8E70B4C8EB3E3123FC70ED43E7";
  3. String sign="1025";
  4. String templateId="1219";
  5. genUtils.chuangRui(accessKey,secret,sign,templateId);

使用

  1. // 生成6位数短信验证码
  2. String code = SmsUtil.code();
  3. SmsUtil.send(MsgPlatformEnum.CHUANG_RUI,"18715011234",code);

源代码

  1. package com.hn.sms.chuangrui;
  2. import cn.hutool.http.HttpUtil;
  3. import cn.hutool.json.JSONObject;
  4. import cn.hutool.json.JSONUtil;
  5. import cn.hutool.log.Log;
  6. import cn.hutool.log.LogFactory;
  7. import com.hn.config.exception.ConfigException;
  8. import com.hn.sms.Sms;
  9. import com.hn.sms.chuangrui.domain.ChuangRuiParam;
  10. import com.hn.sms.exception.SmsException;
  11. import com.hn.utils.AssertUtils;
  12. /**
  13. * 创瑞短信配置
  14. */
  15. public class ChuangRuiSms implements Sms {
  16. private static final Log log = LogFactory.get();
  17. /**
  18. * 短信网关地址
  19. */
  20. private static final String WEB_URL = "http://api.1cloudsp.com/api/v2/single_send";
  21. private ChuangRuiParam param;
  22. public ChuangRuiSms(ChuangRuiParam param) {
  23. this.param = param;
  24. }
  25. /**
  26. * 短信发送
  27. *
  28. * @param phone 手机号
  29. * @param code 验证码
  30. * @return {boolean}
  31. */
  32. public boolean send(String phone, String code) {
  33. param.setMobile(phone);
  34. param.setAuthCode(code);
  35. String result = HttpUtil.post(WEB_URL, param);
  36. JSONObject jsonObject = JSONUtil.parseObj(result);
  37. if ("0".equals(jsonObject.getStr("code"))) {
  38. log.info("创瑞短信返回结果:{}", result);
  39. return true;
  40. }
  41. log.error("创瑞短信返回结果:{}", result);
  42. throw new SmsException("短信发送失败," + jsonObject.getStr("msg"));
  43. }
  44. }
  1. import java.io.UnsupportedEncodingException;
  2. import java.net.URLEncoder;
  3. import java.util.HashMap;
  4. /**
  5. * 描述:
  6. * 创瑞短信配置
  7. *
  8. * @author fei
  9. * 2019-08-16 09:04
  10. */
  11. public class ChuangRuiParam extends HashMap {
  12. /**
  13. * 用户开发key
  14. *
  15. * @param accessKey 用户开发key
  16. */
  17. public void setAccessKey(String accessKey) {
  18. super.put("accesskey", accessKey);
  19. }
  20. /**
  21. * 用户开发秘钥
  22. *
  23. * @param secret 用户开发秘钥
  24. */
  25. public void setSecret(String secret) {
  26. super.put("secret", secret);
  27. }
  28. /**
  29. * 签名
  30. *
  31. * @param sign 签名
  32. */
  33. public void setSign(String sign) {
  34. super.put("sign", sign);
  35. }
  36. /**
  37. * 模板id
  38. *
  39. * @param templateId 模板id
  40. */
  41. public void setTemplateId(String templateId) {
  42. super.put("templateId", templateId);
  43. }
  44. /**
  45. * 手机号
  46. * @param mobile 手机号
  47. */
  48. public void setMobile(String mobile) {
  49. super.put("mobile", mobile);
  50. }
  51. /**
  52. * 验证码
  53. *
  54. * @param authCode 验证码
  55. */
  56. public void setAuthCode(String authCode) {
  57. if(StrUtil.isNotBlank(authCode)){
  58. try {
  59. authCode = URLEncoder.encode(authCode, "utf-8");
  60. super.put("content", authCode);
  61. } catch (UnsupportedEncodingException e) {
  62. throw new SmsException("【创瑞】短信内容转换异常");
  63. }
  64. }
  65. }
  66. }