You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
6.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Furion;
using GDZZ.Core.Entity;
using GDZZ.Core;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;
namespace GDZZ.Application.Help
{
public static class Utils
{
//自定义进制(0、O没有加入,容易混淆同时排除X,用X补位)
private static char[] r = new char[] { 'Q', 'W', 'E', '8', 'A', 'S', '2', 'D', 'Z', '9', 'C', '7', 'P', '5', 'I', 'K', '3', 'M', 'J', 'U', 'F', 'R', '4', 'V', 'Y', 'L', 'T', 'N', '6', 'B', 'G', 'H' };
//不能与自定义进制有重复
private static char b = 'X';
//进制长度
private static int binLen = r.Length;
//生成的邀请码长度
private static int length = 6;
static Random random = new Random();
/// <summary>
///
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static bool ShowHelp(DateTime date)
{
bool isTheDay = false;
//判断日期是否是当天
DateTime time = DateTime.Now;//当天
DateTime time2 = new DateTime(time.Year, time.Month, time.Day);//当天的零时零分
DateTime time3 = time.AddDays(1);//后一天
DateTime time4 = new DateTime(time3.Year, time3.Month, time3.Day);//后一天的零时零分
if (date > time2 && date < time4)
{
isTheDay = true;
}
else
{
isTheDay = false;
}
return isTheDay;
}
/// <summary>
/// 根据ID生成六位随机邀请码
/// </summary>
/// <param name="id">用户id</param>
/// <returns>返回6位邀请码</returns>
public static string Encode(long id)
{
char[] buf = new char[32];
int charPos = 32;
while ((id / binLen) > 0)
{
int ind = (int)(id % binLen);
buf[--charPos] = r[ind];
id /= binLen;
}
buf[--charPos] = r[(int)(id % binLen)];
String str = new String(buf, charPos, (32 - charPos));
//不够长度的自动随机补全
if (str.Length < length)
{
StringBuilder sb = new StringBuilder();
sb.Append(b);
Random rnd = new Random();
for (int i = 1; i < length - str.Length; i++)
{
sb.Append(r[rnd.Next(binLen)]);
}
str += sb.ToString();
}
return str;
}
public static long NextLong(long minValue, long maxValue)
{
byte[] buffer = new byte[8];
random.NextBytes(buffer);
long result = BitConverter.ToInt64(buffer, 0);
return (Math.Abs(result % (maxValue - minValue)) + minValue);
}
public static string stringToUnicode(string s)
{
if (!containsEmoji(s))
return s;
string str = "";
for (int i = 0; i < s.Length; i++)
{
int ch = (int)s[i];
if (ch > 255)
{
str += "\\u" + String.Format("{0:X}", ch);
}
else
{
str += s[i].ToString();
}
}
return str;
}
public static bool containsEmoji(String str)
{
int len = str.Length;
for (int i = 0; i < len; i++)
{
if (isEmojiCharacter(str[i]))
{
return true;
}
}
return false;
}
public static bool isEmojiCharacter(char codePoint)
{
return !((codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA)
|| (codePoint == 0xD)
|| ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
|| ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
|| ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)));
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="file"></param>
/// <param name="pathType"></param>
/// <param name="fileLocation"></param>
/// <returns></returns>
public static async Task<LiveFileOutput> UploadFile(IFormFile file, string pathType, FileLocation fileLocation)
{
var fileSizeKb = (long)(file.Length / 1024.0); // 文件大小KB
var originalFilename = file.FileName; // 文件原始名称
var fileSuffix = Path.GetExtension(file.FileName).ToLower(); // 文件后缀
// 先存库获取Id
var id = YitIdHelper.NextId();
var newFile = new SysFile
{
Id = id,
FileLocation = (int)FileLocation.LOCAL,
FileBucket = FileLocation.LOCAL.ToString(),
FileObjectName = $"{YitIdHelper.NextId()}{fileSuffix}",
FileOriginName = originalFilename,
FileSuffix = fileSuffix.TrimStart('.'),
FileSizeKb = fileSizeKb.ToString(),
FilePath = pathType
};
newFile = await App.GetService<ISqlSugarClient>().Insertable(newFile).ExecuteReturnEntityAsync();
var finalName = newFile.FileObjectName; // 生成文件的最终名称
if (fileLocation == FileLocation.LOCAL) // 本地存储
{
var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, pathType);
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
using (var stream = File.Create(Path.Combine(filePath, finalName)))
{
await file.CopyToAsync(stream);
}
}
else if (fileLocation == FileLocation.ALIYUN) // 阿里云OSS
{
var filePath = pathType + finalName;
OSSClientUtil.DeletefileCode(filePath);
var stream = file.OpenReadStream();
OSSClientUtil.PushMedia(stream, filePath);
}
newFile.FileObjectName = finalName;
return new LiveFileOutput() { Id = finalName, FileUrl = "/" + pathType + "/" + finalName }; // 返回文件唯一标识
}
}
}