/// <summary> /// 检测验证码,返回ClientId /// </summary> /// <param name="inputCode"></param> /// <param name="encryptCode"></param> /// <returns></returns> public string CheckVerifyCode(string inputCode, string encryptCode) { string str = WuYao.AesDecrypt(encryptCode); if (string.IsNullOrEmpty(str)) { throw new Exception("你还想不想登录了!"); } string[] stra = str.Split('$'); if (stra == null || stra.Length == 0) { throw new Exception("系统有误!"); } if (WuYao.GetMd5(inputCode.ToUpper()) != stra[1]) { throw new Exception("验证码有误!"); } SqlHelper _sql = new SqlHelper(); DataTable dt = _sql.Query("SELECT * FROM tbl_loginverifycode WITH(nolock) WHERE ClientId = @id", new System.Collections.Generic.Dictionary <string, object> { { "@id", stra[0] } }); if (dt == null || dt.Rows.Count == 0) { throw new Exception("验证码已失效!"); } if (DateTime.UtcNow.Ticks > long.Parse(Cast.ConToString(dt.Rows[0]["Ticks"]))) { throw new Exception("验证码已失效!"); } return(stra[0]); }
/// <summary> /// 下载Excel /// </summary> /// <param name="dataTable"></param> /// <returns></returns> public static string DownloadExcel(DataTable dataTable) { if (dataTable == null || dataTable.Rows.Count == 0) { throw new Exception("Data is Null !"); } try { using (MemoryStream ms = new MemoryStream()) { // Create a spreadsheet document by supplying the filepath. // By default, AutoSave = true, Editable = true, and Type = xlsx. using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(ms, SpreadsheetDocumentType.Workbook)) { // Add a WorkbookPart to the document. WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart(); workbookpart.Workbook = new Workbook(); // Add a WorksheetPart to the WorkbookPart. WorksheetPart worksheetPart = workbookpart.AddNewPart <WorksheetPart>(); worksheetPart.Worksheet = new Worksheet(new SheetData()); // Add Sheets to the Workbook. Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild <Sheets>(new Sheets()); // Append a new worksheet and associate it with the workbook. Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "sheet1" }; sheets.Append(sheet); // Get the sheetData cell table. SheetData sheetData = worksheetPart.Worksheet.GetFirstChild <SheetData>(); UInt32Value rowIndex = 1; Row headerTitle = new Row() { RowIndex = rowIndex }; List <string> columnNames = new List <string>(); foreach (DataColumn dtColumn in dataTable.Columns) { Cell cell = new Cell(); cell.DataType = new EnumValue <CellValues>(CellValues.String); cell.CellValue = new CellValue(dtColumn.ColumnName); headerTitle.Append(cell); columnNames.Add(dtColumn.ColumnName); } sheetData.Append(headerTitle); foreach (DataRow dtRow in dataTable.Rows) { rowIndex++; // Add a row to the sheetData. Row row = new Row() { RowIndex = rowIndex }; foreach (string columnname in columnNames) { Cell cell = new Cell(); cell.DataType = new EnumValue <CellValues>(CellValues.String); if (dataTable.Columns[columnname].Caption == Constants.DecryptColoumn) { cell.CellValue = new CellValue(WuYao.AesDecrypt(Cast.ConToString(dtRow[columnname]))); } else { cell.CellValue = new CellValue(Cast.ConToString(dtRow[columnname])); } row.Append(cell); } sheetData.AppendChild(row); } worksheetPart.Worksheet.Save(); } return(Convert.ToBase64String(ms.GetBuffer())); } } catch (Exception ex) { throw ex; } }
/// <summary> /// DataTable转List /// </summary> /// <typeparam name="T">模板</typeparam> /// <param name="dt">DataTable</param> /// <param name="model">数据</param> /// <returns></returns> public static List <T> ToModelList <T>(this DataTable dt) where T : new () { try { if (dt == null || dt.Rows.Count == 0) { return(new List <T>()); } List <T> ts = new List <T>(); Type type = typeof(T); string tempName = string.Empty; if (dt.Columns.IndexOf("Sno") <= -1) { dt.Columns.Add("Sno", typeof(int)); } int index = 1; foreach (DataRow dr in dt.Rows) { dr["Sno"] = index; T t = new T(); // 获得此模型的公共属性 PropertyInfo[] propertys = t.GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { tempName = pi.Name; if (dt.Columns.Contains(tempName)) { if (!pi.CanWrite) { continue; } object value = dr[tempName]; if (value is DBNull) { continue; } if (pi.PropertyType.Name.ToLower() == "string") { if (value.GetType().Name.ToLower() == "guid") { pi.SetValue(t, value.ToString(), null); } else if (value.GetType().Name.ToLower() == "datetime") { pi.SetValue(t, Convert.ToDateTime(value).ToString("yyyy-MM-dd HH:mm:ss"), null); } else { if (dt.Columns[tempName].Caption.ToLower() == tempName.ToLower()) { pi.SetValue(t, Convert.ToString(value), null); } else if (dt.Columns[tempName].Caption == Constants.EncryptColoumn) { pi.SetValue(t, WuYao.AesDecrypt(Convert.ToString(value)), null); } else if (dt.Columns[tempName].Caption == Constants.DecryptColoumn) { pi.SetValue(t, WuYao.AesEncrypt(Convert.ToString(value)), null); } else { pi.SetValue(t, Convert.ToString(value), null); } } } else if (pi.PropertyType.Name.ToLower() == "lookupmodel") { if (dt.Columns.Contains(string.Concat(tempName, "Name"))) { object valuename = dr[string.Concat(tempName, "Name")]; LookUpModel lum = new LookUpModel(); if (valuename != DBNull.Value) { lum.Id = Convert.ToString(value); lum.Name = Convert.ToString(valuename); pi.SetValue(t, lum, null); } else { pi.SetValue(t, lum, null); //throw new Exception(string.Format("The value of column '{0}' is null!", string.Concat(tempName, "Name"))); } } else { throw new Exception(string.Format("The column '{0}' dose not exist!", string.Concat(tempName, "Name"))); } } else if (pi.PropertyType.Name.ToLower() == "int32" || pi.PropertyType.Name.ToLower() == "nullable`1") { pi.SetValue(t, Convert.ToInt32(value), null); } else if (pi.PropertyType.Name.ToLower() == "decimal") { pi.SetValue(t, Convert.ToDecimal(value), null); } else if (pi.PropertyType.Name.ToLower() == "datetime") { pi.SetValue(t, Convert.ToDateTime(value), null); } else if (pi.PropertyType.Name.ToLower() == "boolean") { pi.SetValue(t, Convert.ToBoolean(value), null); } else if (pi.PropertyType.Name.ToLower() == "guid") { pi.SetValue(t, Guid.Parse(value.ToString()), null); } } } ts.Add(t); index++; } return(ts); } catch (Exception ex) { throw new Exception(ex.Message, ex); } }