using Newtonsoft.Json; using Newtonsoft.Json.Converters; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Web; namespace VRS.Handler { public class BaseHandler { protected string GetResult(bool _state) { return JsonConvert.SerializeObject(new Result(_state, _state ? "success" : "fail")); } protected string GetResult(bool _state, object _data) { var iso = new IsoDateTimeConverter(); iso.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; return JsonConvert.SerializeObject(new Result(_state, _data), iso); } protected string GetResult(bool _state, object _data, string _message) { var iso = new IsoDateTimeConverter(); iso.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; return JsonConvert.SerializeObject(new Result(_state, _data, _message), iso); } protected string GetResult(bool _state, string _message) { return JsonConvert.SerializeObject(new Result(_state, _message)); } protected string GetResult(object _data) { var iso = new IsoDateTimeConverter(); iso.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; return JsonConvert.SerializeObject(new Result(_data == null ? false : true, _data), iso); } public static string GetNewId() { var array = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; var NewId = string.Format("{0}{1}{2}{3}{4}{5}", DateTime.Now.ToString("yyyyMMddHHmmss"), array.OrderBy(s => Guid.NewGuid()).First(), array.OrderBy(s => Guid.NewGuid()).First(), array.OrderBy(s => Guid.NewGuid()).First(), array.OrderBy(s => Guid.NewGuid()).First(), array.OrderBy(s => Guid.NewGuid()).First()); return NewId; } public static void write_log(string msg) { DateTime now = DateTime.Now; HttpContext context = HttpContext.Current; if (!Directory.Exists(context.Server.MapPath("/bin/logs/"))) { Directory.CreateDirectory(context.Server.MapPath("/bin/logs/")); } lock (_syncRoot) { using (StreamWriter sw = new StreamWriter(context.Server.MapPath("/bin/logs/" + now.ToString("yyyy-MM-dd") + ".txt"), true)) { sw.Write("=============================================================\r\n" + now + "\r\n" + msg + "\r\n"); sw.Close(); } } } /// /// md5密码加密 /// /// /// public static string GetMD5(string passWord) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] bt = Encoding.Default.GetBytes(passWord);//将待加密字符转为 字节型数组 byte[] resualt = md5.ComputeHash(bt);//将字节数组转为加密的字节数组 string pwds = BitConverter.ToString(resualt).Replace("-", ""); passWord = pwds; return passWord; } static readonly object _syncRoot = new object(); /// /// 内容 /// protected HttpContext baseContext = null; /// /// 跨域配置 /// protected void CrossDomain() { baseContext.Response.AddHeader("Access-Control-Allow-Origin", "*"); baseContext.Response.AddHeader("Access-Control-Allow-Headers", " x-www-form-urlencoded, Content-Type,x-requested-with"); baseContext.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); } } }