140 lines
4.4 KiB
C#
140 lines
4.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace WX.CRM.Common
|
|
{
|
|
public static class IdCardHelper
|
|
{
|
|
// 根据身份证号获取年龄
|
|
public static int GetAge(string idCard)
|
|
{
|
|
if (string.IsNullOrEmpty(idCard) || idCard.Length != 18)
|
|
return 0;
|
|
|
|
var birthDate = GetBirthDate(idCard);
|
|
if (birthDate == DateTime.MinValue)
|
|
return 0;
|
|
|
|
var today = DateTime.Today;
|
|
var age = today.Year - birthDate.Year;
|
|
if (birthDate > today.AddYears(-age))
|
|
age--;
|
|
|
|
return age;
|
|
}
|
|
|
|
// 根据身份证号获取性别
|
|
public static string GetGender(string idCard)
|
|
{
|
|
if (string.IsNullOrEmpty(idCard) || idCard.Length != 18)
|
|
return "未知";
|
|
|
|
var genderCode = idCard.Substring(16, 1);
|
|
return int.Parse(genderCode) % 2 == 0 ? "女" : "男";
|
|
}
|
|
|
|
// 根据身份证号获取出生日期
|
|
public static DateTime GetBirthDate(string idCard)
|
|
{
|
|
if (string.IsNullOrEmpty(idCard) || idCard.Length != 18)
|
|
return DateTime.MinValue;
|
|
|
|
var birthDateStr = idCard.Substring(6, 8);
|
|
if (DateTime.TryParseExact(birthDateStr, "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out var birthDate))
|
|
return birthDate;
|
|
|
|
return DateTime.MinValue;
|
|
}
|
|
|
|
// 根据身份证号获取地区信息
|
|
public static string GetRegion(string idCard)
|
|
{
|
|
if (string.IsNullOrEmpty(idCard) || idCard.Length != 18)
|
|
return "未知";
|
|
|
|
var regionCode = idCard.Substring(0, 6);
|
|
// 这里可以根据地区码查询具体地区名称,需要地区码对照表
|
|
return regionCode;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 对身份证号码进行掩码处理,保留前4位和后4位,中间使用*代替
|
|
/// </summary>
|
|
/// <param name="idCard">身份证号码</param>
|
|
/// <returns>掩码后的身份证号码</returns>
|
|
public static string MaskIdCard(string idCard)
|
|
{
|
|
if (string.IsNullOrEmpty(idCard))
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
// 身份证号长度小于8位直接返回原值
|
|
if (idCard.Length < 8)
|
|
{
|
|
return idCard;
|
|
}
|
|
|
|
// 获取前4位
|
|
string prefix = idCard.Substring(0, 4);
|
|
// 获取后4位
|
|
string suffix = idCard.Substring(idCard.Length - 4);
|
|
// 中间部分用*号代替
|
|
string mask = new string('*', idCard.Length - 8);
|
|
|
|
// 拼接返回
|
|
return $"{prefix}{mask}{suffix}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取身份证类型
|
|
/// </summary>
|
|
/// <param name="idCard">身份证号</param>
|
|
/// <returns>身份证类型(国内或国外)</returns>
|
|
public static string GetIdCardType(string idCard)
|
|
{
|
|
if (string.IsNullOrEmpty(idCard))
|
|
{
|
|
return "未知";
|
|
}
|
|
|
|
// 判断是否为中国大陆身份证号(18位数字,最后一位可能是X)
|
|
if (idCard.Length == 18 &&
|
|
System.Text.RegularExpressions.Regex.IsMatch(idCard, @"^\d{17}[\dXx]$"))
|
|
{
|
|
// 进一步校验前17位是否都是数字
|
|
if (System.Text.RegularExpressions.Regex.IsMatch(idCard.Substring(0, 17), @"^\d+$"))
|
|
{
|
|
return "国内身份证";
|
|
}
|
|
}
|
|
|
|
return "非国内身份证";
|
|
}
|
|
|
|
public static string GetNationality(string idCard)
|
|
{
|
|
if (string.IsNullOrEmpty(idCard))
|
|
{
|
|
return "未知";
|
|
}
|
|
|
|
// 判断是否为中国大陆身份证号(18位数字,最后一位可能是X)
|
|
if (idCard.Length == 18 &&
|
|
System.Text.RegularExpressions.Regex.IsMatch(idCard, @"^\d{17}[\dXx]$"))
|
|
{
|
|
// 进一步校验前17位是否都是数字
|
|
if (System.Text.RegularExpressions.Regex.IsMatch(idCard.Substring(0, 17), @"^\d+$"))
|
|
{
|
|
return "中国";
|
|
}
|
|
}
|
|
|
|
return "非国内";
|
|
}
|
|
}
|
|
}
|