纯粹记录一下自己日常开发的东西
需求: 页面要求显示手机号脱敏,导出或批量导出的时候事正常手机号
1:首先我们要明白做后端不能百分百相信前端,虽然前端也可以做脱敏但是稍微有点尝试的人进行f12查看或其他方式很容易就能获取到正确数据。所以后端能校验的就校验不说废话.上代码
public Page<EnterpriseListResp> getByEnterpriseType(EnterpriseListReq req) {
Page page = corporationMapper.getByEnterpriseType(req.page(), req);
List<EnterpriseListResp> records = page.getRecords();
records.forEach(enterpriseListResp -> { String mobile = enterpriseListResp.getMobile(); mobile = mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
enterpriseListResp.setMobile(mobile);
});
return page; }
看图还没看懂? 没关系我们来分析一下,
首先呢我的手机号肯定是我出参(EnterpriseListResp)给它返出去的,入参可能只是几个ID。
那么问题来了,怎么获取?
(1).我把返回值用Page 去接受 用list去赋值
Page page = corporationMapper.getByEnterpriseType(req.page(), req);
List<EnterpriseListResp> records = page.getRecords();
(2).循环遍历 当前list
records.forEach(enterpriseListResp -> {
String mobile = enterpriseListResp.getMobile(); mobile = mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
执行脱敏替换操作
enterpriseListResp.setMobile(mobile);
赋值给获取到的Mobile手机号。
(3).最终返回page对象(因为我用的是分页查询的方式)
return page; }
2.至此脱敏就完事了,没有问题吧?那么问题来了...我导出的时候也是引用的这同一个查询怎么办?
其实我最开始也是想怎么获取纯净的数据后来发现把东西想复杂了,只需要再起一个方法就行了
(1).
3.顺便说一点导出的吧 具体封装方法就不说了 大同小异
三部曲
第一步:导入依赖
第二步 :确定要导出的字段并且加上
@ExcelProperty注解
第三步: