240 lines
8.3 KiB
C#
240 lines
8.3 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Configuration;
|
||
using System.Linq;
|
||
using System.Reflection;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Web.Mvc;
|
||
using System.Web.Script.Serialization;
|
||
|
||
namespace WX.CRM.Common
|
||
{
|
||
public class OperationUtil
|
||
{
|
||
public static string GetCodeByEid(int eid)
|
||
{
|
||
//string v = System.DateTime.Now.ToString("ddHHmm");
|
||
//int y = Convert.ToInt16(System.DateTime.Now.ToString("dd"));
|
||
//int x = (y + eid) % 9;
|
||
//long lv = Convert.ToInt64(string.Format("{0}{1}{2}", v, eid.ToString("0000"), x));
|
||
//return Conver10To64(lv);
|
||
|
||
//==================================================================
|
||
System.DateTime cdt = System.DateTime.Now;
|
||
int MM = cdt.Minute + cdt.Day;
|
||
int HH = cdt.Hour + (eid + MM) % 9;
|
||
string LVStr = string.Format("{0}{1}{2}", eid.ToString("0000"), HH.ToString("00"), MM.ToString("00"));
|
||
return LVStr;
|
||
}
|
||
/// <summary>
|
||
/// 远程邀请码
|
||
/// </summary>
|
||
/// <param name="eid"></param>
|
||
/// <param name="msg"></param>
|
||
/// <returns></returns>
|
||
public static string encyptCodeByEid(int eid)
|
||
{
|
||
string deptCode = ConfigurationManager.AppSettings["InviteDeptCode"];
|
||
DateTime curDate = System.DateTime.Now;
|
||
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
|
||
string BM = string.Format("{0}", (int)asciiEncoding.GetBytes(deptCode)[0]);
|
||
int nHH = curDate.Hour + Convert.ToInt32(curDate.Year.ToString().Substring(2, 2));
|
||
int nMM = curDate.Minute + curDate.Day;
|
||
string nLVStr = string.Format("{0}{1}{2}{3}", eid.ToString("000000"), nHH.ToString("00"), nMM.ToString("00"), BM);
|
||
Int64 _nLVStr = Convert.ToInt64(nLVStr);
|
||
string nstr_binary = Convert.ToString(_nLVStr, 2);
|
||
|
||
//除最高位,其余各位取反
|
||
char[] t = nstr_binary.ToArray();
|
||
for (int i = 1; i < t.Length; i++)
|
||
{
|
||
t[i] = t[i] == '1' ? '0' : '1';
|
||
}
|
||
string n_binstr = string.Join("", t);
|
||
|
||
Int64 n_encypt64 = Convert.ToInt64(n_binstr, 2);
|
||
return n_encypt64.ToString(); //返回十进制字符串
|
||
|
||
}
|
||
public static decimal[] ConvertToDecimal(string[] source)
|
||
{
|
||
decimal[] res = new decimal[source.Length];
|
||
for (int i = 0; i < source.Length; i++)
|
||
{
|
||
res[i] = Convert.ToDecimal(source[i]);
|
||
}
|
||
return res;
|
||
}
|
||
/// <summary>
|
||
/// 相同成员的list数据集合转换
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <typeparam name="R"></typeparam>
|
||
/// <param name="resource"></param>
|
||
/// <returns></returns>
|
||
public static List<T> ConvertToList<T, R>(List<R> resource) where T : class, new()
|
||
{
|
||
Type _Ttype = typeof(T);
|
||
var list = new List<T>();
|
||
if (resource.Count == 0)
|
||
return list;
|
||
PropertyInfo[] propertyInfo = resource[0].GetType().GetProperties();
|
||
foreach (var iterm in resource)
|
||
{
|
||
T temp = new T();
|
||
foreach (PropertyInfo column in propertyInfo)
|
||
{
|
||
_Ttype.GetProperty(column.Name).SetValue(temp, column.GetValue(iterm));
|
||
}
|
||
list.Add(temp);
|
||
}
|
||
return list;
|
||
}
|
||
/// <summary>
|
||
/// 相同成员的类之间转换
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="source"></param>
|
||
/// <returns></returns>
|
||
public static T CopySameFieldsObject<T>(Object source)
|
||
{
|
||
Type _SrcT = source.GetType();
|
||
Type _DestT = typeof(T);
|
||
|
||
// 构造一个要转换对象实例
|
||
Object _Instance = _DestT.InvokeMember("", BindingFlags.CreateInstance, null, null, null);
|
||
|
||
// 这里指定搜索所有公开和非公开的字段
|
||
FieldInfo[] _SrcFields = _SrcT.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||
|
||
// 将源头对象的每个字段的值分别赋值到转换对象里,因为假定字段都一样,这里就不做容错处理了
|
||
foreach (FieldInfo field in _SrcFields)
|
||
{
|
||
_DestT.GetField(field.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).SetValue(_Instance, field.GetValue(source));
|
||
}
|
||
|
||
return (T)_Instance;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 过滤字符串只留下数字
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static string GetIntStr(string str)
|
||
{
|
||
StringBuilder sb = new StringBuilder();
|
||
bool isFirst = true;
|
||
foreach (char var in str.Trim().ToCharArray())
|
||
{
|
||
bool flag = Char.IsNumber(var);
|
||
if (flag == false)// 发现不是数字 跳出
|
||
{
|
||
if (isFirst)
|
||
continue;
|
||
else
|
||
break;
|
||
}
|
||
else
|
||
isFirst = false;//第一次发现数字
|
||
sb.Append(flag ? var.ToString() : string.Empty);
|
||
}
|
||
string value = sb.ToString();
|
||
return value;
|
||
}
|
||
/// <summary>
|
||
/// 获取字符串中的数字
|
||
/// </summary>
|
||
/// <param name="str">字符串</param>
|
||
/// <returns>数字</returns>
|
||
public static decimal GetNumber(string str)
|
||
{
|
||
decimal result = 0;
|
||
if (str != null && str != string.Empty)
|
||
{
|
||
// 正则表达式剔除非数字字符(不包含小数点.)
|
||
str = Regex.Replace(str, @"[^\d.\d]", "");
|
||
|
||
// 如果是数字,则转换为decimal类型
|
||
if (str.Trim() != "" && Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$"))
|
||
{
|
||
result = decimal.Parse(str);
|
||
}
|
||
}
|
||
|
||
return result;
|
||
}
|
||
#region 枚举
|
||
public static IList<SelectListItem> GetItemFromEnum<T>()
|
||
{
|
||
IList<SelectListItem> list = new List<SelectListItem>();
|
||
SelectListItem item;
|
||
string[] names = Enum.GetNames(typeof(T));
|
||
int x = 0;
|
||
foreach (int i in Enum.GetValues(typeof(T)))
|
||
{
|
||
item = new SelectListItem();
|
||
item.Text = names[x];
|
||
item.Value = i.ToString();
|
||
list.Add(item);
|
||
x++;
|
||
}
|
||
return list;
|
||
}
|
||
public static Dictionary<int, string> GetDicFromEnum<T>()
|
||
{
|
||
Dictionary<int, string> dic = new Dictionary<int, string>();
|
||
string[] names = Enum.GetNames(typeof(T));
|
||
int x = 0;
|
||
foreach (int i in Enum.GetValues(typeof(T)))
|
||
{
|
||
dic.Add(i, names[x]);
|
||
x++;
|
||
}
|
||
return dic;
|
||
}
|
||
public static string GetEnumName<T>(int i)
|
||
{
|
||
string name = null;
|
||
string[] names = Enum.GetNames(typeof(T));
|
||
if (i < names.Length)
|
||
{
|
||
name = names[i];
|
||
}
|
||
return name;
|
||
}
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 时间转换
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
/// <returns></returns>
|
||
public static DateTime? GetDateTime(string data)
|
||
{
|
||
DateTime dt;
|
||
if (!string.IsNullOrWhiteSpace(data) && DateTime.TryParse(data, out dt))
|
||
return dt;
|
||
else return null;
|
||
}
|
||
|
||
|
||
public static T JsonDivertToObj<T>(string json)
|
||
{
|
||
JavaScriptSerializer script = new JavaScriptSerializer();
|
||
T dic = script.Deserialize<T>(json);
|
||
return dic;
|
||
}
|
||
|
||
public static string ObjDivertToJson(object obj)
|
||
{
|
||
JavaScriptSerializer script = new JavaScriptSerializer();
|
||
string json = script.Serialize(obj);
|
||
return json;
|
||
}
|
||
|
||
}
|
||
|
||
|
||
}
|