sunpengfei
9 天以前 a9b167cb33e5c40a41ba09c551702806ab64c230
FlexJobApi.Core/Utils/StringUtils/StringUtils.cs
@@ -1,4 +1,5 @@
using Furion.FriendlyException;
using Furion.DataValidation;
using Furion.FriendlyException;
using Mapster.Utils;
using System;
using System.Collections.Generic;
@@ -35,9 +36,10 @@
            return !str.IsNull();
        }
        public static Guid? ToGuid(this string str)
        public static Guid? ToGuid(this string str, string requiredMessage = null)
        {
            if (str.IsNotNull() && Guid.TryParse(str, out var guid) && guid != Guid.Empty) return guid;
            if (requiredMessage.IsNotNull()) throw Oops.Oh(EnumErrorCodeType.s400, requiredMessage);
            return null;
        }
@@ -76,5 +78,68 @@
                return singularName + "s";
            }
        }
        public static bool CheckIsIdentityNumber18(this string identity)
        {
            return identity.IsNotNull() && identity.TryValidate(EnumValidationTypes.ValiIdentity).IsValid && identity.Length == 18;
        }
        /// <summary>
        /// 获取性别
        /// </summary>
        /// <param name="identity"></param>
        /// <returns></returns>
        public static EnumUserGender? GetGender(this string identity)
        {
            if (identity.CheckIsIdentityNumber18())
            {
                return identity[16] % 2 == 0
                    ? EnumUserGender.Female
                    : EnumUserGender.Male;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获取生日
        /// </summary>
        /// <param name="identity"></param>
        /// <returns></returns>
        public static DateTime? GetBirthday(this string identity)
        {
            if (identity.CheckIsIdentityNumber18())
            {
                return new DateTime(
                    identity.Substring(6, 4).ToInt()!.Value,
                    identity.Substring(10, 2).ToInt()!.Value,
                    identity.Substring(12, 2).ToInt()!.Value);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获取年龄
        /// </summary>
        /// <param name="identity"></param>
        /// <returns></returns>
        public static int? GetAge(this string identity)
        {
            if (identity.CheckIsIdentityNumber18())
            {
                var birthday = identity.GetBirthday();
                return birthday.GetAge();
            }
            else
            {
                return null;
            }
        }
    }
}