Yoko.Tool.Core 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Yoko.Tool.Core --version 1.0.2
NuGet\Install-Package Yoko.Tool.Core -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Yoko.Tool.Core" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Yoko.Tool.Core --version 1.0.2
#r "nuget: Yoko.Tool.Core, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Yoko.Tool.Core as a Cake Addin
#addin nuget:?package=Yoko.Tool.Core&version=1.0.2

// Install Yoko.Tool.Core as a Cake Tool
#tool nuget:?package=Yoko.Tool.Core&version=1.0.2

[TOC]

# Yoko.Tool

懒人必备工具箱

NuGet Badge

更新日志

1.0.1

  • 精确经纬度计算的方法GetDistance

1.0.0

  • 全面支持Net7

  • 移除 Newtonsoft.Json,全面拥抱 System.Text.Json

  • 移除 仅在 Windows 上支持的 System.Drawing.Common

    跨平台推荐使用 SkiaSharp

    SkiaSharp 是适用于 .NET 平台的跨平台 2D 图形 API,基于 Google 的 Skia 图形库

    它提供了一个全面的 2D API,可以跨移动、服务器和桌面模型使用来渲染图像

💎 常用操作扩展

// 值类型转换
.ToInt32()
.ToBool()
.ToLong()
.ToDouble()
.ToDecimal()
.ToFloat()
    
// 时间操作
.ToDateTime()  // 字符串 转 时间 
.ToStartTime() // DateTime转换成开始时间00:00:00
.ToEndTime()   // DateTime转换成结束时间23:59:59
.ToUnixTime()  // DateTime时间转Unix时间戳(默认精确到毫秒13位)
.ToUnixTimeS() // DateTime时间转Unix时间戳(默认精确到秒10位)
.ToLocalTime() // Unix时间戳转DateTime本地时间(yyyy/MM/dd hh:MM:ss)
.DateDiff()    // 返回时间差(N小时前,N分钟前···)
.GetDiffTime() // 计算2个时间差(相差多少年月日时分秒)
.GetDaysOfMonth()      // 本月有多少天
.GetWeekNameOfDay()    // 返回当前日期的星期名称
DateTime.Now.GetDateTime();    //返回标准时间格式(yyyy-MM-dd HH:mm:ss)
DateTime.Now.GetDateTimeF();    //返回标准时间格式(yyyy-MM-dd HH:mm:ss:fffffff)
DateTime.Now.GetTotalSeconds(); // 获取该时间相对于1970-01-01 00:00:00的秒数
DateTime.Now.GetTotalMilliseconds(); // 获取该时间相对于1970-01-01 00:00:00的毫秒数
DateTime.Now.GetTotalMicroseconds(); // 获取该时间相对于1970-01-01 00:00:00的微秒数

//字符串操作
.SplitCsv()      // 分割逗号的字符串为List[]、string[]
.SplitCsvToInt() // 分割逗号的字符串为int[]   
.ToIntList()     // string数组转int数组
.ToStringEscape()   // 特殊字符转义,避免XSS跨站脚本攻击
.ToByteArray()   // string转换成字节数组
.ToJson()        //序列化一个字符串为JSON格式
.ToModel()     // 反序列化一个JSON格式字符串为对象
.ToMask()        // 字符串敏感信息掩码  (123***789)
.ToMaskEmail()   // 邮箱敏感信息掩码  (123****@163.com)
.ToMathRound()  //保留两位小数
    
//值计算
//除法计算 a/b
//type=1 浮点数,保留2位小数(四舍五入):12.34
//type=2 百分比:12.23%
//type=3 带有逗号分隔符,保留小数位数(四舍五入):2,500,000.00
//type=4 货币表示,保留2位小数(四舍五入):¥12.34
DividedBy()     

//集合操作
// 指定要拆分成几个集合,集合中子集个数动态分配。如果原集合中子集的个数小于等于要分组数,既source.Count <= size,建议使用source.Chunk(1).ToList()
source.ChunkList(3);  //对集合进行分组

//将集合参数ASCII码从小到大排序(默认严格模式)
dict.OrderByASCII();

//DataTable 操作
DataTableHelper<T>.ToList(dt);
DataTableHelper<T>.ToModel(dt);

//URL 操作
UrlSplit();   // 分解URL信息
GetFileName(); // 获取网络文件(图片)的文件名

💎 值判断

// 在范围?  
/**
int num = 50;
if (num.IsInRange(100, 1000)) { }
string value = "a";
if (value.IsIn("a", "b", "c")) { }
*/
IsInRange()       //int、DateTime值在的范围?
IsIn()            //在里面吗?
IsContainsIn()    //在里面吗?
IsNullOrEmpty()   // 是null或"" ? 
IsValuable()      // 有值?(与IsNullOrEmpty相反)
IsZero()          // 是零?
IsInt()           // 是Int?
IsNoInt()         // 不是Int?
IsMoney()         // 是金钱? 
IsDate()          // 是时间?
IsDate()          // 是邮箱?
IsMobile()        // 是手机号?(弱验证)
IsPhoneNumber()   // 是手机号?(强验证)
IsTelephone()     // 是座机?  
IsIDcard()        // 是身份证?
IsFax()           // 是传真? 
IsMatch()         // 是适合正则匹配? 
IsTrue()          // 是true?
IsFalse()         // 是false?

💎 随机数

// 按照年月时分秒随机数生成的文件名
YokoRandom.GetFileRndName() 

// 生成随机数字  
YokoRandom.GetFormatedNumeric(1, 100)

// 生成随机汉字  
YokoRandom.GetRandChinese(6)

// 根据指定的样式生成:"?"代表一个字符,"#"代表一个一位数字,"*"代表一个字符串或一个一位数字
YokoRandom.GetRandStringByPattern(@"##??*")

// 获取指定长度的纯字母随机字符串  
YokoRandom.GetRandWord(10)

// 获取指定长度的随机字符串
YokoRandom.GetRandomString(10)

💎 加解密/验证

// MD5 加密、加盐 
"123456".ToMD5()
"123456".ToMD5("abc123")

// 两次加密、加盐  
"123456".ToMD5Double()
"123456".ToMD5Double("abc123")
    
//AES、DES对称加密解密
AESEncrypt、DesEncrypt
AESDecrypt、DesDecrypt

//HA256函数
SHA256
// 生成一个密码  
"123456".CreateHash("是否需要MD5加密 ?")

// 验证密码 
"要检查的密码".VerifyPassword("正确密码")

💎 经纬度操作

//根据经纬度计算两点间距离
YokoLngLat.GetDistance(x1,y1,x2,y2)
    
//求一个包含经纬度集合的距离和
YokoLngLat.GetDistanceSum(dict);

//坐标系互转
YokoLngLat.GPSToAMap()   // GPS转高德(WGS84 --> GCJ-02)
YokoLngLat.AMapToGPS()   // 高德转GPS(GCJ-02 --> WGS84)
YokoLngLat.AMapToBMap()  // 高德转百度(GCJ-02 --> BD-09)
YokoLngLat.BMapToAMap()  // 百度转高德(BD-09 --> GCJ-02)

💎 文件/图像操作

"WebImgUrl".ImageToBase64()    // 把weburl图片转换为base64格式字符串
"WebImgUrl".GetStream()        // 获取Stream流
ReadByte(Stream input)         //读[最大支持16M的图片]

🔍 图片文件操作 Yoko.Images.Webp

从静态路径或者网络路径转换、调整大小和压缩 jpeg、png 和 webp 图像

💎 时间段判断

🔍 判断是否在某个时间段内、是否包含某个时间段、两个时间段是否相交、连接两个时间段

var range = new DateTimeRange("2021-9-3", "2021-9-5".ToDateTime());

//连接两个时间段,结果:2021-9-3~2021-9-6
range.Union("2021-9-4".ToDateTime(), "2021-9-6".ToDateTime()); 

//判断是否在某个时间段内,true
range.In("2021-9-3".ToDateTime(), "2021-9-6".ToDateTime());

//两个时间段是否相交,(true,2021-9-3~2021-9-4)
var (intersected,range2) = range.Intersect("2021-9-4".ToDateTime(), "2021-9-6".ToDateTime());

//判断是否包含某个时间段,true
range.Contains("2021-9-3".ToDateTime(), "2021-9-4".ToDateTime());


💎 邮件发送

// 异步发送邮件 SendEmailAsync  
// 同步发送 SendEmail
new Email()
{
    SmtpServer = "smtp.qq.com",// SMTP服务器
    SmtpPort = 587, // SMTP服务器端口
    EnableSsl = true,//使用SSL
    Username = "88888888888@qq.com",// 邮箱用户名
    Password = "123456789",// 邮箱密码
    Tos = "111111111@qq.com,22222222@qq.com", //多个收件人用,隔开
    Subject = "测试邮件",//邮件标题
    Body = "Hello,WoCao",//邮件内容
}.SendEmailAsync(s =>
{
    // 发送成功后的回调
    // 异步发送邮件,如果回调方法中参数不为"true"则表示发送失败
    Console.WriteLine(s);
});

💎 雪花ID优化版

  • 生成的ID更短、速度更快
  • 新增预留位
  • 解决服务器时间回拨问题
  • 兼容传统的经典雪花算法

🔍 调用示例

1、全局初始化

//全局初始化IdGenerator 只需全局一次(必须在第2步之前设置)
YokoId.SetIdGenerator(new IdGeneratorOptions(1));

2、生成ID

var newId = YokoId.NextId();

🔍 参数设置

  • WorkerId

    机器码,最重要参数,无默认值,必须 全局唯一,必须 程序设定,最大值63

    如果一台服务器部署多个独立服务,需要为每个服务指定不同的 WorkerId。

  • WorkerIdBitLength

    机器码位长,决定 WorkerId 的最大值,默认值6,取值范围 [1, 19]

💎 枚举操作

// 获取枚举对象Key与显示名称的字典
var dict = typeof(枚举类).GetDictionary();

// 获取枚举值对应的字符串
var enumStr = 0.ToEnumString(typeof(枚举类));
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.2 75 5/13/2024
3.0.1 83 5/10/2024
3.0.0-beta.3 52 4/28/2024
3.0.0-beta.2 49 4/28/2024
3.0.0-beta.1 53 4/28/2024
2.0.0-beta.1 66 4/24/2024
1.0.10-rc.4 80 12/14/2023
1.0.10-rc.3 71 12/14/2023
1.0.10-rc.1 78 11/28/2023
1.0.9 131 11/16/2023
1.0.7 106 10/11/2023
1.0.6 100 10/11/2023
1.0.5 94 9/19/2023
1.0.4 152 5/22/2023
1.0.3 230 2/24/2023
1.0.2 201 2/22/2023
1.0.1 217 2/14/2023
1.0.0 246 11/18/2022