手机号码白名单功能:
思路:在发送时与数据库白名单表做校验,为白名单号码无阻发送
缺点:如果一次性发送十几万号码,每个号码都要和白名单库做校验,速度极大降低
,而且可以采用如果是超频,黑名单,或者屏蔽地区的号码的条件下,再做白名单校验
实例:在项目启动时,加载白名单数据到redis内存中,每次发送从redis缓存中获取白名单号码做校验
1、白名单常量类
/**
* @Description SMS模块缓存键值常量
* @Date 2021-06-01 18:26
* @Created by Lxq
*/
public class SmsCacheConst {
/**商家应用信息缓存键值*/
//public static final String CLINETAPP_APPID = "mms:clientApp:appid";
public static final String CLINETAPP_ID = "mms:clientApp:id";
/**通道缓存键值*/
public static final String CHANNEL_ID = "mms:masGdZw:channel";
/**缓存中的黑名单库*/
public static final String BLACK_IDCODE = "mms:black";
/**Redis缓存中白名单号码的键值 */
public static final String WHITE_MOBILES = "SmsWhiteMobile";
}
2、启动时获取白名单号码,并缓存到redis中
package com.chuangci.cxcsp.smsapi.component;
import com.chuangci.cxcsp.common.utils.RedisUtils;
import com.chuangci.cxcsp.sms.domain.SmsWhite;
import com.chuangci.cxcsp.sms.service.ISmsWhiteService;
import com.chuangci.cxcsp.smsapi.constant.SmsCacheConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.List;
@Slf4j
@Component
public class WhiteMobileLoad implements ApplicationRunner {
@Autowired
RedisUtils redisUtils;
@Autowired
ISmsWhiteService smsWhiteService;
@Override
@Async
public void run(ApplicationArguments args) throws Exception {
log.info("开始加载号码白名单到缓存");
List<SmsWhite> whiteList = smsWhiteService.selectSmsWhiteList(new SmsWhite());
if (whiteList==null){
log.warn("不存在白名单号码");
return;
}
redisUtils.del(SmsCacheConst.WHITE_MOBILES);
log.info("删除已存在的白名单缓存");
for (SmsWhite smsWhite : whiteList) {
redisUtils.sSet(SmsCacheConst.WHITE_MOBILES,smsWhite.getIdcode());
}
log.info("加载号码白名单到缓存完成,共{}个",whiteList.size());
}
}
3、发送时获取从redis中获取白名单号码
//从缓存获取白名单号码数据
Set<Object> whiteMobiles = redisUtils.sGet(SmsCacheConst.WHITE_MOBILES);
if (whiteMobiles == null){
whiteMobiles = new HashSet<>();
}
4、发送时校验,非白名单号码做校验
if (!whiteMobiles.contains(mobile)) {
//屏蔽地区号码校验、处理
if (StringUtils.isNotBlank(shieldedArea)) {
String geo = PhoneUtils.getAreaName(mobile);
String province = PhoneUtils.getProvince(mobile);
if (ArrayUtil.contains(shieldedAreaArray, geo) || ArrayUtil.contains(shieldedAreaArray, province)) {
sendResult.setMobileValid(4);
sendResult.setStatusCode("CX:0004");
sendResult.setStatusDesc("屏蔽地区号码");
shieldNum++;
continue;
}
if (allowAreaArray != null && allowAreaArray.length > 0 && !(ArrayUtil.contains(allowAreaArray, geo) || ArrayUtil.contains(allowAreaArray, province))) {
sendResult.setMobileValid(7);
sendResult.setStatusCode("CX:0007");
sendResult.setStatusDesc("非白名单区域号码");
continue;
}
}