Zxd.Core/code/DG.Tool/PhoneHelper.cs

435 lines
16 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace DG.Tool
{
public class PhoneHelper
{
/// <summary>
/// 格式化以手机号码作为用户名的username
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
public static string FormatPhoneUserName(string username)
{
string newUsername = GetFormatPhoneUserName(username);//手机号码格式化
//newUsername = FormatUserName(newUsername);//用户名格式化
//return newUsername;
return newUsername;
}
/// <summary>
/// 格式化以手机号码作为用户名的username(存在多个用户名)
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
public static string FormatPhoneMoreUserName(string username)
{
string newUsername = GetFormatPhoneUserName(username);//手机号码格式化
if (string.IsNullOrEmpty(newUsername))
return newUsername;
string userNames = string.Empty;
foreach (var item in newUsername.Split(','))
{
userNames += FormatUserName(item) + ",";//用户名格式化
}
userNames = userNames.Substring(0, userNames.Length - 1);
return userNames;
}
/// <summary>
/// 格式化以手机号码作为用户名的username
/// </summary>
/// <param name="username">需要改动的用户名</param>
/// <param name="ntype"></param>
/// <returns></returns>
public static string FormatPhoneUserNameContent(string username)
{
string newUsername = GetFormatPhoneUserName(username);//手机号码格式化
return newUsername;
}
/// <summary>
/// 格式化以手机号码作为用户名的username
/// </summary>
/// <param name="username">需要改动的用户名</param>
/// <param name="username">需要改动的用户名</param>
/// <param name="ntype"></param>
/// <returns></returns>
public static string FormatPhoneUserNameContent(string username, string subTypeCode)
{
string newUsername = GetFormatPhoneUserName(username);//手机号码格式化
if (subTypeCode != "SMS_ResetPwd")
return newUsername;
//创建对象
Regex regex = new Regex("账号(?<value>[\\s\\S]*?)的", RegexOptions.Multiline);
//匹配
MatchCollection match = regex.Matches(username);
//取得第一个值
if (match.Count > 0)
{
string value = match[0].Groups["value"].ToString();
string newValue = FormatUserName(value);
newUsername = newUsername.Replace(string.Format("账号{0}的", value), string.Format("账号{0}的", newValue));
}
//创建对象
Regex regex2 = new Regex("帐号(?<value>[\\s\\S]*?)", RegexOptions.Multiline);
//匹配
MatchCollection match2 = regex2.Matches(newUsername);
//取得第一个值
if (match2.Count > 0)
{
string value1 = match2[0].Groups["value"].ToString();
string newValue1 = FormatUserName(value1);
newUsername = newUsername.Replace(string.Format("帐号{0}", value1), string.Format("帐号{0}", newValue1));
}
return newUsername;
}
/// <summary>
/// 用户名
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static string FormatUserName(string userName)
{
/*
用户名屏蔽规则:
1、手机号形式的按原处理规则中间几位屏蔽
2、否则看用户名长度小于等于5位的第一位不屏蔽后面的屏蔽比如“test1”处理后为“t****”;
3、5位以上的用户名长度除2向下取整再减1做为起始值从起始值开始后面4位用*号比如“test123”处理后为“te****3”
*/
if (string.IsNullOrEmpty(userName))
return "";
if (userName == "未设置")
return userName;
string newUserName = userName;
//判断 是否已经在手机号屏蔽的时候屏蔽过一次
if (userName.IndexOf("*****") > -1)
{
return newUserName;
}
int nameLth = newUserName.Length;
if (nameLth <= 5)
{
newUserName = newUserName.Substring(0, 1) + GetAsterisk(nameLth - 1);
}
else
{
int startIndex = nameLth / 2;
startIndex = startIndex - 1;
newUserName = newUserName.Substring(0, startIndex) + "****" + newUserName.Substring(startIndex + 4, nameLth - startIndex - 4);
}
return newUserName;
}
/// <summary>
/// 格式化以手机号码作为用户名的username
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
private static string GetFormatPhoneUserName(string username)
{
string newUsername = username;
if (string.IsNullOrWhiteSpace(newUsername))
return newUsername;
while (ContainMobile(newUsername))
{
string phone = GetMobile(newUsername);
if (string.IsNullOrWhiteSpace(phone))
break;
newUsername = newUsername.Replace(phone, (phone.Substring(0, 3) + "*****" + phone.Substring(8, 3)));
}
return newUsername;
}
#region
/// <summary>
/// 00852+8位是香港的电话
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static bool ChekMobile(string number)
{
string rgs = @"^(13|14|15|16|17|18|19|0085)\d{9}$";
Regex reg = new Regex(rgs);
return reg.IsMatch(number);
}
/// <summary>
/// 检查是否包含手机号码
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static bool ContainMobile(string number)
{
string rgs = @".*(13|14|15|16|17|18|19)\d{9}.*";
Regex reg = new Regex(rgs);
return reg.IsMatch(number);
}
/// <summary>
/// 从文本中返回号码字符串
/// </summary>
/// <param name="txt"></param>
/// <returns></returns>
public static string GetMobile(string txt)
{
string rgs = @"(12|13|14|15|16|17|18|19)\d{9}";
string result = Regex.Match(txt, rgs).Value;
return result;
}
public static bool CheckIsNum(string txt)
{
string rgs = "^\\d{6,16}$";
Regex reg = new Regex(rgs);
return reg.IsMatch(txt);
}
public static bool IsNum(string txt)
{
string rgs = "^\\d+$";
Regex reg = new Regex(rgs);
return reg.IsMatch(txt);
}
public static bool IsChinese(string txt)
{
string rgs = "[\u4E00-\u9FA5]";
Regex reg = new Regex(rgs);
return reg.IsMatch(txt);
}
public static bool IsNumOrStr(string txt)
{
string rgs = "^[A-Za-z0-9]+$";
Regex reg = new Regex(rgs);
return reg.IsMatch(txt);
}
#endregion
/// <summary>
/// 获取星号
/// </summary>
/// <param name="number">需要返回的星号数量</param>
/// <returns></returns>
private static string GetAsterisk(int number)
{
string xingHao = string.Empty;
if (number == 0)
return xingHao;
for (int i = 0; i < number; i++)
{
xingHao += "*";
}
return xingHao;
}
#region 8*
/// <summary>
/// 超过8个数字的数据替换成*
/// </summary>
/// <param name="txt"></param>
/// <returns></returns>
public static string Replace8Number(object txt)
{
if (txt == null)
{
return "";
}
if (string.IsNullOrEmpty(txt.ToString()))
{
return "";
}
var content = txt.ToString();
//普通数字
string numReg = @"\d+";
var result = Regex.Matches(content, numReg).Cast<Match>().Select(t => t.Value).ToList();
if (result != null && result.Count > 0)
{
foreach (var s in result)
{
if (s.Length > 7)
{
content = content.Replace(s, GetHideNumber(s));
}
}
}
//下标数字
numReg = @"[₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{8,}";
result = Regex.Matches(content, numReg).Cast<Match>().Select(t => t.Value).ToList();
if (result != null && result.Count > 0)
{
foreach (var s in result)
{
if (s.Length > 7)
{
content = content.Replace(s, GetHideDownNumber(s));
}
}
}
//上标数字
numReg = @"[¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰]{8,}";
result = Regex.Matches(content, numReg).Cast<Match>().Select(t => t.Value).ToList();
if (result != null && result.Count > 0)
{
foreach (var s in result)
{
if (s.Length > 7)
{
content = content.Replace(s, GetHideUpNumber(s));
}
}
}
//全角数字
numReg = @"[,,,,,,,,,]{8,}";
result = Regex.Matches(content, numReg).Cast<Match>().Select(t => t.Value).ToList();
if (result != null && result.Count > 0)
{
foreach (var s in result)
{
if (s.Length > 7)
{
content = content.Replace(s, GetHideRoundNumber(s));
}
}
}
return content;
}
//全角数字屏蔽
public static string GetHideRoundNumber(string phoneNo)
{
string rexstr = "";
string xinghao = "";
if (phoneNo.Length == 8)
{
rexstr = @"([,,,,,,,,,]{2})([,,,,,,,,,]{4})([,,,,,,,,,]{2})";
xinghao = "****";
}
else if (phoneNo.Length == 9)
{
rexstr = @"([,,,,,,,,,]{2})([,,,,,,,,,]{5})([,,,,,,,,,]{2})";
xinghao = "*****";
}
else if (phoneNo.Length == 10)
{
rexstr = @"([,,,,,,,,,]{2})([,,,,,,,,,]{5})([,,,,,,,,,]{3})";
xinghao = "*****";
}
else
{
rexstr = @"([,,,,,,,,,]{3})([,,,,,,,,,]{" + (phoneNo.Length - 6) + @"})([,,,,,,,,,]{3})";
for (int i = 0; i < (phoneNo.Length - 6); i++)
{
xinghao += "*";
}
}
Regex re = new Regex(rexstr, RegexOptions.None);
phoneNo = re.Replace(phoneNo, "$1" + xinghao + "$3");
return phoneNo;
}
//上标数字屏蔽
public static string GetHideUpNumber(string phoneNo)
{
string rexstr = "";
string xinghao = "";
if (phoneNo.Length == 8)
{
rexstr = @"([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{2})([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{4})([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{2})";
xinghao = "****";
}
else if (phoneNo.Length == 9)
{
rexstr = @"([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{2})([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{5})([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{2})";
xinghao = "*****";
}
else if (phoneNo.Length == 10)
{
rexstr = @"([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{2})([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{5})([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{3})";
xinghao = "*****";
}
else
{
rexstr = @"([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{3})([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{" + (phoneNo.Length - 6) + @"})([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{3})";
for (int i = 0; i < (phoneNo.Length - 6); i++)
{
xinghao += "*";
}
}
Regex re = new Regex(rexstr, RegexOptions.None);
phoneNo = re.Replace(phoneNo, "$1" + xinghao + "$3");
return phoneNo;
}
//下标数字屏蔽
public static string GetHideDownNumber(string phoneNo)
{
string rexstr = "";
string xinghao = "";
if (phoneNo.Length == 8)
{
rexstr = @"([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{2})([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{4})([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{2})";
xinghao = "****";
}
else if (phoneNo.Length == 9)
{
rexstr = @"([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{2})([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{5})([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{2})";
xinghao = "*****";
}
else if (phoneNo.Length == 10)
{
rexstr = @"([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{2})([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{5})([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{3})";
xinghao = "*****";
}
else
{
rexstr = @"([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{3})([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{" + (phoneNo.Length - 6) + @"})([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{3})";
for (int i = 0; i < (phoneNo.Length - 6); i++)
{
xinghao += "*";
}
}
Regex re = new Regex(rexstr, RegexOptions.None);
phoneNo = re.Replace(phoneNo, "$1" + xinghao + "$3");
return phoneNo;
}
//数字屏蔽
public static string GetHideNumber(string phoneNo)
{
string rexstr = "";
string xinghao = "";
if (phoneNo.Length == 8)
{
rexstr = @"(\d{2})(\d{4})(\d{2})";
xinghao = "****";
}
else if (phoneNo.Length == 9)
{
rexstr = @"(\d{2})(\d{5})(\d{2})";
xinghao = "*****";
}
else if (phoneNo.Length == 10)
{
rexstr = @"(\d{2})(\d{5})(\d{3})";
xinghao = "*****";
}
else
{
rexstr = @"(\d{3})(\d{" + (phoneNo.Length - 6) + @"})(\d{3})";
for (int i = 0; i < (phoneNo.Length - 6); i++)
{
xinghao += "*";
}
}
Regex re = new Regex(rexstr, RegexOptions.None);
phoneNo = re.Replace(phoneNo, "$1" + xinghao + "$3");
return phoneNo;
}
#endregion
}
}