SmsUtils.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package com.caimei.utils;
  2. import com.alibaba.fastjson.JSONObject;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.apache.commons.lang.StringUtils;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.stereotype.Component;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. /**
  14. * @author Aslee
  15. * @date 2021/7/16
  16. */
  17. @Slf4j
  18. @Component
  19. public class SmsUtils {
  20. private static String active;
  21. @Value("${spring.profiles.active}")
  22. public void setActive(String active) {
  23. SmsUtils.active = active;
  24. }
  25. private static String core;
  26. @Value("${caimei.core}")
  27. public void setCore(String core) {
  28. SmsUtils.core = core;
  29. }
  30. public static Boolean sendSms(Integer type, String mobile, String content) {
  31. try {
  32. List<String> passList = new ArrayList<>();
  33. passList.add("15113936829");
  34. passList.add("18476937515");
  35. passList.add("15917362709");
  36. passList.add("15872950940");
  37. passList.add("18229303772");
  38. if ("prod".equals(active) || passList.contains(mobile)) {
  39. if (StringUtils.isNotBlank(mobile) && mobile.length() == 11) {
  40. String regex = "^(1[3-9]\\d{9}$)";
  41. Pattern pattern = Pattern.compile(regex);
  42. Matcher matcher = pattern.matcher(mobile);
  43. if (matcher.matches()) {
  44. Map<String, Object> map = new HashMap<>(3);
  45. // 短信类型:1通知短信,2验证码短信,3营销短信
  46. map.put("type", type);
  47. map.put("content", content);
  48. map.put("mobile", mobile);
  49. String url = core + "/tools/sms/send";
  50. String result = HttpRequest.sendPost(url, map);
  51. JSONObject parseObject = JSONObject.parseObject(result);
  52. Integer code = parseObject.getInteger("code");
  53. if (code != 0) {
  54. log.info("短信发送失败,手机号>>>>" + mobile);
  55. } else {
  56. log.info("挑战赛短信发送成功"+mobile);
  57. return true;
  58. }
  59. }
  60. }
  61. } else {
  62. return true;
  63. }
  64. } catch (Exception e) {
  65. log.error("短信推送异常", e);
  66. }
  67. return false;
  68. }
  69. }