在 foodie-dev-common 模块中的 com.imooc.utils 包内新建 DesensitizationUtil 类

    1. package com.imooc.utils;
    2. /**
    3. * 通用脱敏工具类
    4. * 可用于:
    5. * 用户名
    6. * 手机号
    7. * 邮箱
    8. * 地址等
    9. */
    10. public class DesensitizationUtil {
    11. private static final int SIZE = 6;
    12. private static final String SYMBOL = "*";
    13. public static void main(String[] args) {
    14. String name = commonDisplay("慕课网");
    15. String mobile = commonDisplay("13900000000");
    16. String mail = commonDisplay("admin@imooc.com");
    17. String address = commonDisplay("北京大运河东路888号");
    18. System.out.println(name);
    19. System.out.println(mobile);
    20. System.out.println(mail);
    21. System.out.println(address);
    22. }
    23. /**
    24. * 通用脱敏方法
    25. * @param value
    26. * @return
    27. */
    28. public static String commonDisplay(String value) {
    29. if (null == value || "".equals(value)) {
    30. return value;
    31. }
    32. int len = value.length();
    33. int pamaone = len / 2;
    34. int pamatwo = pamaone - 1;
    35. int pamathree = len % 2;
    36. StringBuilder stringBuilder = new StringBuilder();
    37. if (len <= 2) {
    38. if (pamathree == 1) {
    39. return SYMBOL;
    40. }
    41. stringBuilder.append(SYMBOL);
    42. stringBuilder.append(value.charAt(len - 1));
    43. } else {
    44. if (pamatwo <= 0) {
    45. stringBuilder.append(value.substring(0, 1));
    46. stringBuilder.append(SYMBOL);
    47. stringBuilder.append(value.substring(len - 1, len));
    48. } else if (pamatwo >= SIZE / 2 && SIZE + 1 != len) {
    49. int pamafive = (len - SIZE) / 2;
    50. stringBuilder.append(value.substring(0, pamafive));
    51. for (int i = 0; i < SIZE; i++) {
    52. stringBuilder.append(SYMBOL);
    53. }
    54. if ((pamathree == 0 && SIZE / 2 == 0) || (pamathree != 0 && SIZE % 2 != 0)) {
    55. stringBuilder.append(value.substring(len - pamafive, len));
    56. } else {
    57. stringBuilder.append(value.substring(len - (pamafive + 1), len));
    58. }
    59. } else {
    60. int pamafour = len - 2;
    61. stringBuilder.append(value.substring(0, 1));
    62. for (int i = 0; i < pamafour; i++) {
    63. stringBuilder.append(SYMBOL);
    64. }
    65. stringBuilder.append(value.substring(len - 1, len));
    66. }
    67. }
    68. return stringBuilder.toString();
    69. }
    70. }

    在 foodie-dev-service 模块中修改 ItemServiceImpl 类的 queryPagedComments 方法,对信息进行脱敏

    package com.imooc.service.impl;
    
    import com.github.pagehelper.PageHelper;
    import com.github.pagehelper.PageInfo;
    import com.imooc.enums.CommentLevel;
    import com.imooc.mapper.*;
    import com.imooc.pojo.*;
    import com.imooc.pojo.vo.CommentLevelCountsVO;
    import com.imooc.pojo.vo.ItemCommentVO;
    import com.imooc.service.ItemService;
    import com.imooc.utils.DesensitizationUtil;
    import com.imooc.utils.PagedGridResult;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    import tk.mybatis.mapper.entity.Example;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * Created by 92578 on 2020/8/22 16:18
     **/
    @Service
    public class ItemServiceImpl implements ItemService {
    
        @Autowired
        private ItemsMapper itemsMapper;
    
        @Autowired
        private ItemsImgMapper itemsImgMapper;
    
        @Autowired
        private ItemsSpecMapper itemsSpecMapper;
    
        @Autowired
        private ItemsParamMapper itemsParamMapper;
    
        @Autowired
        private ItemsCommentsMapper itemsCommentsMapper;
    
        @Autowired
        private ItemsMapperCustom itemsMapperCustom;
    
        /**
         * 根据商品 id 查询详情
         *
         * @param itemId
         * @return
         */
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public Items queryItemById(String itemId) {
            return itemsMapper.selectByPrimaryKey(itemId);
        }
    
        /**
         * 根据商品 id 查询商品图片列表
         *
         * @param itemId
         * @return
         */
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public List<ItemsImg> queryItemImgList(String itemId) {
            Example itemsImgExp = new Example(ItemsImg.class);
            Example.Criteria criteria = itemsImgExp.createCriteria();
            criteria.andEqualTo("itemId", itemId);
    
            return itemsImgMapper.selectByExample(itemsImgExp);
        }
    
        /**
         * 根据商品 id 查询商品规格
         *
         * @param itemId
         * @return
         */
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public List<ItemsSpec> queryItemSpecList(String itemId) {
            Example itemsSpecExp = new Example(ItemsSpec.class);
            Example.Criteria criteria = itemsSpecExp.createCriteria();
            criteria.andEqualTo("itemId", itemId);
    
            return itemsSpecMapper.selectByExample(itemsSpecExp);
        }
    
        /**
         * 根据商品 id 查询商品参数
         *
         * @param itemId
         * @return
         */
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public ItemsParam queryItemsParam(String itemId) {
            Example itemsParamExp = new Example(ItemsParam.class);
            Example.Criteria criteria = itemsParamExp.createCriteria();
            criteria.andEqualTo("itemId", itemId);
    
            return itemsParamMapper.selectOneByExample(itemsParamExp);
        }
    
        /**
         * 根据商品 id 查询商品的评价等级数量
         *
         * @param itemId
         */
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public CommentLevelCountsVO queryCommentCounts(String itemId) {
            Integer goodCounts = getCommentCounts(itemId, CommentLevel.GOOD.type);
            Integer normalCounts = getCommentCounts(itemId, CommentLevel.NORMAL.type);
            Integer badCounts = getCommentCounts(itemId, CommentLevel.BAD.type);
            Integer totalCounts = goodCounts + normalCounts + badCounts;
    
            CommentLevelCountsVO countsVO = new CommentLevelCountsVO();
            countsVO.setTotalCounts(totalCounts);
            countsVO.setGoodCounts(goodCounts);
            countsVO.setNormalCounts(normalCounts);
            countsVO.setBadCounts(badCounts);
    
            return countsVO;
        }
    
        /**
         * 根据商品 id 查询商品的评价(分页)
         *
         * @param itemId
         * @param level
         * @return
         */
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public PagedGridResult queryPagedComments(String itemId, Integer level, Integer page, Integer pageSize) {
            Map<String, Object> map = new HashMap<>();
            map.put("itemId", itemId);
            map.put("level", level);
    
            /**
             * page: 第几页
             * pageSize: 每页显示条数
             */
            PageHelper.startPage(page, pageSize);
    
            List<ItemCommentVO> list = itemsMapperCustom.queryItemComments(map);
            for(ItemCommentVO vo:list){
                vo.setNickname(DesensitizationUtil.commonDisplay(vo.getNickname()));
            }
    
            return setterPagedGrid(list, page);
        }
    
        private PagedGridResult setterPagedGrid(List<?> list, Integer page) {
            PageInfo<?> pageList = new PageInfo<>(list);
            PagedGridResult grid = new PagedGridResult();
            grid.setPage(page);
            grid.setRows(list);
            grid.setTotal(pageList.getPages());
            grid.setRecords(pageList.getTotal());
            return grid;
        }
    
        @Transactional(propagation = Propagation.SUPPORTS)
        Integer getCommentCounts(String itemId, Integer level) {
            ItemsComments condition = new ItemsComments();
            condition.setItemId(itemId);
            if (level != null) {
                condition.setCommentLevel(level);
            }
    
            return itemsCommentsMapper.selectCount(condition);
        }
    }
    

    启动项目,打开浏览器,访问 http://localhost:8080/foodie-shop/item.html?itemId=cake-1001
    image.png