123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package com.caimei.utils;
- import com.alibaba.fastjson.JSONObject;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang.StringUtils;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * @author Aslee
- * @date 2021/7/16
- */
- @Slf4j
- @Component
- public class SmsUtils {
- private static String active;
- @Value("${spring.profiles.active}")
- public void setActive(String active) {
- SmsUtils.active = active;
- }
- private static String core;
- @Value("${caimei.core}")
- public void setCore(String core) {
- SmsUtils.core = core;
- }
- public static Boolean sendSms(Integer type, String mobile, String content) {
- try {
- ArrayList<String> passList = new ArrayList<>();
- passList.add("15113936829");
- if (!"prod".equals(active) && !passList.contains(mobile)) {
- return true;
- }
- if (StringUtils.isNotBlank(mobile) && mobile.length() == 11) {
- String regex = "^(1[3-9]\\d{9}$)";
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(mobile);
- if (matcher.matches()) {
- Map<String, Object> map = new HashMap<>(2);
- // 短信类型:1通知短信,2验证码短信,3营销短信
- map.put("type", type);
- map.put("content", content);
- map.put("mobile", mobile);
- String url = core + "/tools/sms/send";
- String result = HttpRequest.sendPost(url, map);
- JSONObject parseObject = JSONObject.parseObject(result);
- Integer code = parseObject.getInteger("code");
- if (code != 0) {
- log.info("短信发送失败,手机号>>>>" + mobile);
- } else {
- return true;
- }
- }
- }
- } catch (Exception e) {
- log.error("短信推送异常", e);
- }
- return false;
- }
- }
|