|
|
@ -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; |
|
|
|
} |
|
|
|
} |