Browse Source

商城端身份证号添加校验业务功能

multiwx
dy-hu 7 months ago
parent
commit
0e66d022d3
  1. 17
      cereshop-app/src/main/java/com/shop/cereshop/app/controller/buyer/BuyerUserController.java
  2. 3
      cereshop-commons/src/main/java/com/shop/cereshop/commons/constant/CoReturnFormat.java
  3. 130
      cereshop-commons/src/main/java/com/shop/cereshop/commons/utils/ValidationUtil.java

17
cereshop-app/src/main/java/com/shop/cereshop/app/controller/buyer/BuyerUserController.java

@ -5,6 +5,7 @@
*/
package com.shop.cereshop.app.controller.buyer;
import cn.hutool.core.util.ObjectUtil;
import com.shop.cereshop.app.annotation.NoRepeatSubmit;
import com.shop.cereshop.app.annotation.NoRepeatWebLog;
import com.shop.cereshop.app.page.buyer.MyUser;
@ -20,9 +21,11 @@ import com.shop.cereshop.commons.domain.common.PageParam;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.utils.GsonUtil;
import com.shop.cereshop.commons.utils.ValidationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@ -93,6 +96,13 @@ public class BuyerUserController {
@NoRepeatWebLog
public Result update(@RequestBody CereBuyerUser buyerUser, HttpServletRequest request) throws CoBusinessException{
//获取当前登录账户
if (ObjectUtil.isNotEmpty(buyerUser.getIdCard())){
//身份证号码校验
String resp = ValidationUtil.checkIdNo(buyerUser.getIdCard());
if(StringUtils.isNotBlank(resp)) {
return new Result(CoReturnFormat.IDCARD_ERROR);
}
}
CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
buyerUser.setBuyerUserId(user.getBuyerUserId());
cereBuyerUserService.update(buyerUser,user);
@ -184,6 +194,13 @@ public class BuyerUserController {
// return new Result(CoReturnFormat.CODE_ERROR);
// }
// }
if (ObjectUtil.isNotEmpty(param.getIdCard())) {
//身份证号码校验
String resp = ValidationUtil.checkIdNo(param.getIdCard());
if(StringUtils.isNotBlank(resp)) {
return new Result(CoReturnFormat.IDCARD_ERROR);
}
}
//获取当前登录账户
CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
cereBuyerUserService.updateRealName(param,user);

3
cereshop-commons/src/main/java/com/shop/cereshop/commons/constant/CoReturnFormat.java

@ -56,6 +56,7 @@ public class CoReturnFormat {
put(CoReturnFormat.SYS_TOKEN_NO_PARAM, "[服务器] Token参数缺失");
put(CoReturnFormat.SYS_DATE_PARSE_EXCEPTION, "[服务器] 日期格式解析异常");
put(CoReturnFormat.CODE_ERROR, "验证码有误");
put(CoReturnFormat.IDCARD_ERROR, "身份证号有误");
//============== 基础参数 ===============================
put(CoReturnFormat.PARAM_MISSING, "必填参数不能为空");
@ -364,6 +365,8 @@ public class CoReturnFormat {
public static final String CODE_ERROR = "11005";
/** 原密码错误 */
public static final String OLD_PASSWORD_ERROR = "11006";
/** 身份证号有误 */
public static final String IDCARD_ERROR = "11007";
//============ 登录 ===============
/** 用户未注册 */

130
cereshop-commons/src/main/java/com/shop/cereshop/commons/utils/ValidationUtil.java

@ -1,29 +1,17 @@
/*
* Copyright (C) 2017-2021
* All rights reserved, Designed By 深圳中科鑫智科技有限公司
* Copyright authorization contact 18814114118
*/
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
/**
* Copyright (C) 2018-2021
* All rights reserved, Designed By www.yixiang.co
*/
package com.shop.cereshop.commons.utils;
import cn.hutool.core.util.ObjectUtil;
import org.hibernate.validator.internal.constraintvalidators.hv.EmailValidator;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* 验证工具
@ -33,32 +21,88 @@ import java.math.BigDecimal;
public class ValidationUtil {
/**
* 验证空
* 身份证号码校验
*/
public static void isNull(Object obj, String entity, String parameter , Object value){
if(ObjectUtil.isNull(obj)){
String msg = entity + " 不存在: "+ parameter +" is "+ value;
private static final Map<String, String> cityMap = Stream.of(new String[][] {
{"11", "北京"},
{"12", "天津"},
{"13", "河北"},
{"14", "山西"},
{"15", "内蒙古"},
{"21", "辽宁"},
{"22", "吉林"},
{"23", "黑龙江"},
{"31", "上海"},
{"32", "江苏"},
{"33", "浙江"},
{"34", "安徽"},
{"35", "福建"},
{"36", "江西"},
{"37", "山东"},
{"41", "河南"},
{"42", "湖北"},
{"43", "湖南"},
{"44", "广东"},
{"45", "广西"},
{"46", "海南"},
{"50", "重庆"},
{"51", "四川"},
{"52", "贵州"},
{"53", "云南"},
{"54", "西藏"},
{"61", "陕西"},
{"62", "甘肃"},
{"63", "青海"},
{"64", "宁夏"},
{"65", "新疆"},
{"71", "台湾"},
{"81", "香港"},
{"82", "澳门"},
{"91", "国外"},
}).collect(Collectors.toMap(data -> data[0], data -> data[1]));
private static final Pattern pattern15 = Pattern.compile("^\\d{6}\\d{4}(0[1-9]|[12][0-9]|3[01])\\d{3}$");
private static final Pattern pattern18 = Pattern.compile("^\\d{6}\\d{4}(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])\\d{3}[0-9xX]$");
public static String checkIdNo(String idNo) {
if(StringUtils.isBlank(idNo) || (idNo.length() != 15 && idNo.length() != 18)) {
return "身份证号格式错误";
}
if(pattern18.matcher(idNo).find()) {
String currDate = DateTimeFormatter.ofPattern("yyyyMMdd").withZone(ZoneId.systemDefault()).format(Instant.now());
if(idNo.substring(6, 14).compareTo(currDate) > 0) {
return "身份证号出生日期不能大于当前日期";
}
/**
* 验证是否为邮箱
*/
public static boolean isEmail(String email) {
return new EmailValidator().isValid(email, null);
// 18位身份证需要验证最后一位校验位
String[] idNoArr = idNo.split("");
// (ai×Wi)(mod 11)
// 加权因子
int[] factor = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
// 校验位
String[] parity = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
int sum = 0;
int ai = 0;
int wi = 0;
for (int i = 0; i < 17; i++) {
ai = Integer.valueOf(idNoArr[i]);
wi = factor[i];
sum += ai * wi;
}
/**
* 检测价格区间
* @param price
* @param min
* @param max
* @return
*/
public static boolean checkPrice(BigDecimal price, BigDecimal min, BigDecimal max) {
if (price == null || min == null || max == null) {
return false;
if(!parity[sum % 11].equalsIgnoreCase(idNoArr[17])) {
return "身份证号校验位错误";
}
} else if(pattern15.matcher(idNo).find()) {
// passed
} else {
return "身份证号格式错误";
}
return price.compareTo(min) >= 0 && price.compareTo(max) <= 0;
if(cityMap.get(idNo.substring(0, 2)) == null) {
return "身份证号地址编码错误";
}
return null;
}
}
Loading…
Cancel
Save